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