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