CONOPT
Loading...
Searching...
No Matches
evalerror04.f90
Go to the documentation of this file.
1!> @file evalerror04.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! Test function evaluation errors.
6!! Case 04: Derivative value overflow in the initial point.
7!!
8!!
9!! For more information about the individual callbacks, please have a look at the source code.
10
11#if defined(_WIN32) && !defined(_WIN64)
12#define dec_directives_win32
13#endif
14
15!> Main program. A simple setup and call of CONOPT
16!!
17Program evalerror04
18
20 Use conopt
21 implicit None
22!
23! Declare the user callback routines as Integer, External:
24!
25 Integer, External :: eval_readmatrix ! Mandatory Matrix definition routine defined below
26 Integer, External :: eval_fdeval ! Function and Derivative evaluation routine
27 ! needed a nonlinear model.
28 Integer, External :: std_status ! Standard callback for displaying solution status
29 Integer, External :: std_solution ! Standard callback for displaying solution values
30 Integer, External :: std_message ! Standard callback for managing messages
31 Integer, External :: std_errmsg ! Standard callback for managing error messages
32#ifdef dec_directives_win32
33!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Eval_ReadMatrix
34!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Eval_FDEval
35!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
36!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
37!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
38!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
39#endif
40!
41! Control vector
42!
43 INTEGER, Dimension(:), Pointer :: cntvect
44 INTEGER :: coi_error
45!
46! Create and initialize a Control Vector
47!
48 call startup
49
50 coi_error = coi_create( cntvect )
51!
52! Tell CONOPT about the size of the model by populating the Control Vector:
53!
54 coi_error = max( coi_error, coidef_numvar( cntvect, 2 ) ) ! # variables
55 coi_error = max( coi_error, coidef_numcon( cntvect, 1 ) ) ! # constraints
56 coi_error = max( coi_error, coidef_numnz( cntvect, 2 ) ) ! # nonzeros in the Jacobian
57 coi_error = max( coi_error, coidef_numnlnz( cntvect, 1 ) ) ! # of which are nonlinear
58 coi_error = max( coi_error, coidef_optdir( cntvect, 1 ) ) ! Maximize
59 coi_error = max( coi_error, coidef_objvar( cntvect, 1 ) ) ! Objective is variable 1
60 coi_error = max( coi_error, coidef_optfile( cntvect, 'EvalError04.opt' ) )
61!
62! Tell CONOPT about the callback routines:
63!
64 coi_error = max( coi_error, coidef_readmatrix( cntvect, eval_readmatrix ) )
65 coi_error = max( coi_error, coidef_fdeval( cntvect, eval_fdeval ) )
66 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
67 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
68 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
69 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
70
71#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
72 coi_error = max( coi_error, coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_license_text) )
73#endif
74
75 If ( coi_error .ne. 0 ) THEN
76 write(*,*)
77 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
78 write(*,*)
79 call flog( "Skipping Solve due to setup errors", 1 )
80 ENDIF
81!
82! Save the solution so we can check the duals:
83!
84 do_allocate = .true.
85!
86! Start CONOPT:
87!
88 coi_error = coi_solve( cntvect )
89
90 write(*,*)
91 write(*,*) 'End of EvalError04 example. Return code=',coi_error
92
93 If ( coi_error /= 400 ) then
94 call flog( "Excepted COI_Solve to return 400", 1 )
95 endif
96
97 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
98
99 call flog( "Successful Solve", 0 )
100
101End Program evalerror04
102!
103! ============================================================================
104! Define information about the model:
105!
106
107!> Define information about the model
108!!
109!! @include{doc} readMatrix_params.dox
110Integer Function eval_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
111 colsta, rowno, value, nlflag, n, m, nz, &
112 usrmem )
113#ifdef dec_directives_win32
114!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Eval_ReadMatrix
115#endif
116 implicit none
117 integer, intent (in) :: n ! number of variables
118 integer, intent (in) :: m ! number of constraints
119 integer, intent (in) :: nz ! number of nonzeros
120 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
121 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
122 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
123 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
124 ! (not defined here)
125 integer, intent (out), dimension(m) :: type ! vector of equation types
126 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
127 ! (not defined here)
128 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
129 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
130 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
131 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
132 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
133 real*8 usrmem(*) ! optional user memory
134!
135! Information about Variables:
136! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
137! Default: the status information in Vsta is not used.
138!
139 curr(1) = 1.d-45 ! Will give overflow in derivative of log in initial point
140!
141! Information about Constraints:
142! Default: Rhs = 0
143! Default: the status information in Esta and the function
144! value in FV are not used.
145! Default: Type: There is no default.
146! 0 = Equality,
147! 1 = Greater than or equal,
148! 2 = Less than or equal,
149! 3 = Non binding.
150!
151! Constraint 1 (Objective)
152! Rhs = 0 and type Equality
153!
154 type(1) = 0
155!
156! Information about the Jacobian. CONOPT expects a columnwise
157! representation in Rowno, Value, Nlflag and Colsta.
158!
159! Colsta = Start of column indices (No Defaults):
160! Rowno = Row indices
161! Value = Value of derivative (by default only linear
162! derivatives are used)
163! Nlflag = 0 for linear and 1 for nonlinear derivative
164! (not needed for completely linear models)
165!
166! Indices
167! x(1) x(2)
168! 1: 1 2
169!
170 colsta(1) = 1
171 colsta(2) = 2
172 colsta(3) = 3
173 rowno(1) = 1
174 rowno(2) = 1
175!
176! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
177! x(1) x(2)
178! 1: NL L
179!
180 nlflag(1) = 1
181 nlflag(2) = 0
182!
183! Value (Linear only)
184! x(1) x(2)
185! 1: NL -1
186!
187 value(2) = -1.d0
189 eval_readmatrix = 0 ! Return value means OK
190
191end Function eval_readmatrix
192!
193!==========================================================================
194! Compute nonlinear terms and non-constant Jacobian elements
195!
196
197!> Compute nonlinear terms and non-constant Jacobian elements
198!!
199!! @include{doc} fdeval_params.dox
200Integer Function eval_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
201 n, nz, thread, usrmem )
202#ifdef dec_directives_win32
203!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Eval_FDEval
204#endif
205 implicit none
206 integer, intent (in) :: n ! number of variables
207 integer, intent (in) :: rowno ! number of the row to be evaluated
208 integer, intent (in) :: nz ! number of nonzeros in this row
209 real*8, intent (in), dimension(n) :: x ! vector of current solution values
210 real*8, intent (in out) :: g ! constraint value
211 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
212 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
213 ! in this row. Ffor information only.
214 integer, intent (in) :: mode ! evaluation mode: 1 = function value
215 ! 2 = derivatives, 3 = both
216 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
217 ! as errcnt is incremented
218 integer, intent (in out) :: errcnt ! error counter to be incremented in case
219 ! of function evaluation errors.
220 integer, intent (in) :: thread
221 real*8 usrmem(*) ! optional user memory
222!
223! Row 1: x2 = log(x1)
224!
225 eval_fdeval = 0
226 if ( rowno .eq. 1 ) then
227!
228! Mode = 1 or 3. Function value: G = log(x1)
229!
230 if ( mode .eq. 1 .or. mode .eq. 3 ) then
231 if ( x(1) .le. 0.0d0 ) then
232 errcnt = errcnt + 1
233 else
234 g = log(x(1))
235 endif
236 endif
237!
238! Mode = 2 or 3: Derivative values:
239!
240 if ( mode .eq. 2 .or. mode .eq. 3 ) then
241 if ( x(1) .le. 0.0d0 ) then
242 errcnt = errcnt + 1
243 else
244 jac(1) = 1.0d0/x(1)
245 endif
246 endif
247 Else
248!
249! Other rows -- should not happen
250!
251 eval_fdeval = 1
252 endif
253
254end Function eval_fdeval
integer function std_solution(xval, xmar, xbas, xsta, yval, ymar, ybas, ysta, n, m, usrmem)
Definition comdecl.f90:132
integer function std_status(modsta, solsta, iter, objval, usrmem)
Definition comdecl.f90:88
integer function std_message(smsg, dmsg, nmsg, llen, usrmem, msgv)
Definition comdecl.f90:205
integer function std_errmsg(rowno, colno, posno, msglen, usrmem, msg)
Definition comdecl.f90:248
integer function eval_fdeval(x, g, jac, rowno, jcnm, mode, ignerr, errcnt, n, nz, thread, usrmem)
Compute nonlinear terms and non-constant Jacobian elements.
integer function eval_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
program evalerror04
Main program. A simple setup and call of CONOPT.
integer(c_int) function coidef_message(cntvect, coi_message)
define callback routine for handling messages returned during the solution process.
Definition conopt.f90:1265
integer(c_int) function coidef_solution(cntvect, coi_solution)
define callback routine for returning the final solution values.
Definition conopt.f90:1238
integer(c_int) function coidef_status(cntvect, coi_status)
define callback routine for returning the completion status.
Definition conopt.f90:1212
integer(c_int) function coidef_readmatrix(cntvect, coi_readmatrix)
define callback routine for providing the matrix data to CONOPT.
Definition conopt.f90:1111
integer(c_int) function coidef_errmsg(cntvect, coi_errmsg)
define callback routine for returning error messages for row, column or Jacobian elements.
Definition conopt.f90:1291
integer(c_int) function coidef_fdeval(cntvect, coi_fdeval)
define callback routine for performing function and derivative evaluations.
Definition conopt.f90:1135
integer(c_int) function coidef_optfile(cntvect, optfile)
define callback routine for defining an options file.
Definition conopt.f90:928
integer(c_int) function coidef_license(cntvect, licint1, licint2, licint3, licstring)
define the License Information.
Definition conopt.f90:293
integer(c_int) function coidef_numvar(cntvect, numvar)
defines the number of variables in the model.
Definition conopt.f90:97
integer(c_int) function coidef_numcon(cntvect, numcon)
defines the number of constraints in the model.
Definition conopt.f90:121
integer(c_int) function coidef_numnlnz(cntvect, numnlnz)
defines the Number of Nonlinear Nonzeros.
Definition conopt.f90:167
integer(c_int) function coidef_optdir(cntvect, optdir)
defines the Optimization Direction.
Definition conopt.f90:213
integer(c_int) function coidef_numnz(cntvect, numnz)
defines the number of nonzero elements in the Jacobian.
Definition conopt.f90:144
integer(c_int) function coidef_objvar(cntvect, objvar)
defines the Objective Variable.
Definition conopt.f90:257
integer(c_int) function coi_create(cntvect)
initializes CONOPT and creates the control vector.
Definition conopt.f90:1726
integer(c_int) function coi_free(cntvect)
frees the control vector.
Definition conopt.f90:1749
integer(c_int) function coi_solve(cntvect)
method for starting the solving process of CONOPT.
Definition conopt.f90:1625
subroutine flog(msg, code)
Definition comdecl.f90:62
logical do_allocate
Definition comdecl.f90:27
subroutine startup
Definition comdecl.f90:41