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