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