CONOPT
Loading...
Searching...
No Matches
tria03.f90
Go to the documentation of this file.
1!> @file tria03.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! Triangular Demo model 03
6!!
7!! This is a CONOPT implementation of the GAMS model:
8!!
9!! @verbatim
10!! variable x1, x2, x3, x4 ;
11!! equation e1, e2, e3 ;
12!!
13!! e1 .. x1 + power(x2,3) =e= 5;
14!!
15!! e2 .. sqr(x2) =e= 4;
16!!
17!! e3 .. x3 =e= x2 + x1 + sqr(x4);
18!!
19!! model tria03 / all /;
20!! @endverbatim
21!!
22!! The preprocessor will not solve this model given the initial values.
23!! Initially, equation e2 should be solved with respect to variable x2,
24!! but the derivative in the initial point is 0 and the nonlinear term
25!! is not monotone so CONOPT cannot determine how to change x2.
26!! Therefore, the preprocessor is interrupted without solving e2.
27!! However, CONOPT itself should be able to solve the model using
28!! the new negative curvature method.
29!!
30!! @verbatim
31!! solve tria03 using nlp minimizing x3;
32!! @endverbatim
33!!
34!! We try again, this time with a positive initial value for x2 = +10:
35!! x2.l = 10;
36!!
37!! @verbatim
38!! solve tria03 using nlp minimizing x3;
39!! @endverbatim
40!!
41!! And we try with a negative initial value for x2 = -10:
42!! x2.l = -10;
43!!
44!! @verbatim
45!! solve tria03 using nlp minimizing x3;
46!! @endverbatim
47!!
48!!
49!! For more information about the individual callbacks, please have a look at the source code.
50
51#if defined(_WIN32) && !defined(_WIN64)
52#define dec_directives_win32
53#endif
54
55!> Main program. A simple setup and call of CONOPT
56!!
57Program tria03
58
60 Use conopt
61 implicit None
62!
63! Declare the user callback routines as Integer, External:
64!
65 Integer, External :: tria_readmatrix ! Mandatory Matrix definition routine defined below
66 Integer, External :: tria_fdeval ! Function and Derivative evaluation routine
67 ! needed a nonlinear model.
68 Integer, External :: std_status ! Standard callback for displaying solution status
69 Integer, External :: std_solution ! Standard callback for displaying solution values
70 Integer, External :: std_message ! Standard callback for managing messages
71 Integer, External :: std_errmsg ! Standard callback for managing error messages
72#ifdef dec_directives_win32
73!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Tria_ReadMatrix
74!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Tria_FDEval
75!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
76!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
77!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
78!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
79#endif
80!
81! Control vector
82!
83 INTEGER, Dimension(:), Pointer :: cntvect
84 INTEGER :: coi_error
85!
86! Create and initialize a Control Vector
87!
88 call startup
89
90 coi_error = coi_create( cntvect )
91!
92! Tell CONOPT about the size of the model by populating the Control Vector:
93!
94 coi_error = max( coi_error, coidef_numvar( cntvect, 4 ) ) ! 3 variables
95 coi_error = max( coi_error, coidef_numcon( cntvect, 3 ) ) ! 3 constraints
96 coi_error = max( coi_error, coidef_numnz( cntvect, 7 ) ) ! 6 nonzeros in the Jacobian
97 coi_error = max( coi_error, coidef_numnlnz( cntvect, 3 ) ) ! 3 of which are nonlinear
98 coi_error = max( coi_error, coidef_optdir( cntvect, -1 ) ) ! Minimize
99 coi_error = max( coi_error, coidef_objvar( cntvect, 3 ) ) ! Objective is variable 3
100 coi_error = max( coi_error, coidef_optfile( cntvect, 'tria03.opt' ) )
101!
102! Tell CONOPT about the callback routines:
103!
104 coi_error = max( coi_error, coidef_readmatrix( cntvect, tria_readmatrix ) )
105 coi_error = max( coi_error, coidef_fdeval( cntvect, tria_fdeval ) )
106 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
107 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
108 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
109 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
110
111#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
112 coi_error = max( coi_error, coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_license_text) )
113#endif
114
115 If ( coi_error .ne. 0 ) THEN
116 write(*,*)
117 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
118 write(*,*)
119 call flog( "Skipping Solve due to setup errors", 1 )
120 ENDIF
121!
122! Save the solution so we can check the duals:
123!
124 do_allocate = .true.
125!
126! Start CONOPT:
127!
128 coi_error = coi_solve( cntvect )
129
130 write(*,*)
131 write(*,*) 'End of First Call. Return code=',coi_error
132
133 If ( coi_error /= 0 ) then
134 call flog( "Errors encountered during solution", 1 )
135 elseif ( stacalls == 0 .or. solcalls == 0 ) then
136 call flog( "Status or Solution routine was not called", 1 )
137 elseif ( sstat /= 1 .or. mstat /= 2 ) then
138 call flog( "Solver and Model Status was not as expected (1,2)", 1 )
139! No Objective test
140 Else if ( mstat == 2 ) then
141 Call checkdual( 'Tria03', minimize )
142 Else if ( mstat == 5 ) then
143 Call checkdual( 'Tria03', infeasible )
144 endif
145
146 stacalls = 0; solcalls = 0
147 coi_error = coi_solve( cntvect )
148
149 write(*,*)
150 write(*,*) 'End of Second Call. Return code=',coi_error
151
152 If ( coi_error /= 0 ) then
153 call flog( "Errors encountered during solution", 1 )
154 elseif ( stacalls == 0 .or. solcalls == 0 ) then
155 call flog( "Status or Solution routine was not called", 1 )
156 elseif ( sstat /= 1 .or. mstat /= 2 ) then
157 call flog( "Solver and Model Status was not as expected (1,2)", 1 )
158 elseif ( abs( obj-(-1.0d0) ) > 0.000001d0 ) then
159 call flog( "Incorrect objective returned", 1 )
160 Else
161 Call checkdual( 'Tria03', minimize )
162 endif
163
164 stacalls = 0; solcalls = 0
165 coi_error = coi_solve( cntvect )
166
167 write(*,*)
168 write(*,*) 'End of Third Call. Return code=',coi_error
169
170 If ( coi_error /= 0 ) then
171 call flog( "Errors encountered during solution", 1 )
172 elseif ( stacalls == 0 .or. solcalls == 0 ) then
173 call flog( "Status or Solution routine was not called", 1 )
174 elseif ( sstat /= 1 .or. mstat /= 2 ) then
175 call flog( "Solver and Model Status was not as expected (1,2)", 1 )
176 elseif ( abs( obj-11.0d0 ) > 0.000001d0 ) then
177 call flog( "Incorrect objective returned", 1 )
178 Else
179 Call checkdual( 'Tria03', minimize )
180 endif
181
182 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
183
184 call flog( "Successful Solve", 0 )
185
186End Program tria03
187!
188! ============================================================================
189! Define information about the model:
190!
191
192!> Define information about the model
193!!
194!! @include{doc} readMatrix_params.dox
195Integer Function tria_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
196 colsta, rowno, value, nlflag, n, m, nz, &
197 usrmem )
198#ifdef dec_directives_win32
199!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Tria_ReadMatrix
200#endif
201 implicit none
202 integer, intent (in) :: n ! number of variables
203 integer, intent (in) :: m ! number of constraints
204 integer, intent (in) :: nz ! number of nonzeros
205 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
206 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
207 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
208 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
209 ! (not defined here)
210 integer, intent (out), dimension(m) :: type ! vector of equation types
211 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
212 ! (not defined here)
213 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
214 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
215 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
216 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
217 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
218 real*8 usrmem(*) ! optional user memory
219!
220! Information about Variables:
221! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
222! Default: the status information in Vsta is not used.
223!
224! The model uses defaults, except for x2 where the starting value is
225! default in first call, +10 in the second, and -10 in the third
226!
227 integer, save :: ncall = 0
228 if ( ncall == 0 ) then
229 ncall = 1
230 else if ( ncall == 1 ) then
231 curr(2) = +10.d0; ncall = 2
232 else if ( ncall == 2 ) then
233 curr(2) = -10.d0; ncall = 3
234 else
235 tria_readmatrix = 1; return
236 endif
237!
238! Information about Constraints:
239! Default: Rhs = 0
240! Default: the status information in Esta and the function
241! value in FV are not used.
242! Default: Type: There is no default.
243! 0 = Equality,
244! 1 = Greater than or equal,
245! 2 = Less than or equal,
246! 3 = Non binding.
247!
248! Constraint 1: e1
249! Rhs = 5.0 and type Equality
250!
251 rhs(1) = 5.0d0
252 type(1) = 0
253!
254! Constraint 2: e2
255! Rhs = 4.0 and type Equality
256!
257 rhs(2) = 4.0d0
258 type(2) = 0
259!
260! Constraint 3: e3
261! Rhs = 0.0 and type Equality
262!
263 type(3) = 0
264!
265! Information about the Jacobian. CONOPT expects a columnwise
266! representation in Rowno, Value, Nlflag and Colsta.
267!
268! Colsta = Start of column indices (No Defaults):
269! Rowno = Row indices
270! Value = Value of derivative (by default only linear
271! derivatives are used)
272! Nlflag = 0 for linear and 1 for nonlinear derivative
273! (not needed for completely linear models)
274!
275! Indices
276! x(1) x(2) x(3) x(4)
277! 1: 1 3
278! 2: 4
279! 3: 2 5 6 7
280!
281 colsta(1) = 1
282 colsta(2) = 3
283 colsta(3) = 6
284 colsta(4) = 7
285 colsta(5) = 8
286 rowno(1) = 1
287 rowno(2) = 3
288 rowno(3) = 1
289 rowno(4) = 2
290 rowno(5) = 3
291 rowno(6) = 3
292 rowno(7) = 3
293!
294! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
295! x(1) x(2) x(3) x(4)
296! 1: L NL
297! 2: NL
298! 3: L L L NL
299!
300 nlflag(1) = 0
301 nlflag(2) = 0
302 nlflag(3) = 1
303 nlflag(4) = 1
304 nlflag(5) = 0
305 nlflag(6) = 0
306 nlflag(7) = 1
307!
308! Value (Linear only)
309! x(1) x(2) x(3) x(4)
310! 1: 1.0 NL
311! 2: 5.0
312! 3: -1.0 -1.0 1.0 NL
313!
314 value(1) = 1.d0
315 value(2) = -1.d0
316 value(4) = 5.d0
317 value(5) = -1.d0
318 value(6) = 1.d0
320 tria_readmatrix = 0 ! Return value means OK
321
322end Function tria_readmatrix
323!
324!==========================================================================
325! Compute nonlinear terms and non-constant Jacobian elements
326!
327
328!> Compute nonlinear terms and non-constant Jacobian elements
329!!
330!! @include{doc} fdeval_params.dox
331Integer Function tria_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
332 n, nz, thread, usrmem )
333#ifdef dec_directives_win32
334!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Tria_FDEval
335#endif
336 implicit none
337 integer, intent (in) :: n ! number of variables
338 integer, intent (in) :: rowno ! number of the row to be evaluated
339 integer, intent (in) :: nz ! number of nonzeros in this row
340 real*8, intent (in), dimension(n) :: x ! vector of current solution values
341 real*8, intent (in out) :: g ! constraint value
342 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
343 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
344 ! in this row. Ffor information only.
345 integer, intent (in) :: mode ! evaluation mode: 1 = function value
346 ! 2 = derivatives, 3 = both
347 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
348 ! as errcnt is incremented
349 integer, intent (in out) :: errcnt ! error counter to be incremented in case
350 ! of function evaluation errors.
351 integer, intent (in) :: thread
352 real*8 usrmem(*) ! optional user memory
353!
354! Row 1: e1 .. x1 + power(x2,3) =e= 5;
355!
356 if ( rowno .eq. 1 ) then
357!
358! Mode = 1 or 3. G = power(x2,3)
359!
360 if ( mode .eq. 1 .or. mode .eq. 3 ) then
361 g = x(2)*x(2)*x(2)
362 endif
363!
364! Mode = 2 or 3: Derivative values:
365!
366 if ( mode .eq. 2 .or. mode .eq. 3 ) then
367 jac(2) = 3.d0*x(2)*x(2)
368 endif
369 tria_fdeval = 0
370!
371! Row 2: e2 .. sqr(x2) =e= 4;
372!
373 elseif ( rowno .eq. 2 ) then
374!
375! Mode = 1 or 3. G = sqr(x2)
376!
377 if ( mode .eq. 1 .or. mode .eq. 3 ) then
378 g = x(2)*x(2)
379 endif
380!
381! Mode = 2 or 3: Derivative values:
382!
383 if ( mode .eq. 2 .or. mode .eq. 3 ) then
384 jac(2) = 2.d0*x(2)
385 endif
386 tria_fdeval = 0
387!
388! Row 2: e3 .. x3 =e= x2 + x1 + sqr(x4);
389!
390 elseif ( rowno .eq. 3 ) then
391!
392! Mode = 1 or 3. G = -sqr(x4)
393!
394 if ( mode .eq. 1 .or. mode .eq. 3 ) then
395 g = -x(4)*x(4)
396 endif
397!
398! Mode = 2 or 3: Derivative values:
399!
400 if ( mode .eq. 2 .or. mode .eq. 3 ) then
401 jac(4) = -2.d0*x(4)
402 endif
403 tria_fdeval = 0
404 Else
405!
406! Illegal row number
407!
408 tria_fdeval = 1
409 endif
410
411end Function tria_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
subroutine checkdual(case, minmax)
Definition comdecl.f90:394
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(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
real *8 obj
Definition comdecl.f90:16
integer solcalls
Definition comdecl.f90:15
integer sstat
Definition comdecl.f90:18
integer, parameter infeasible
Definition comdecl.f90:31
integer, parameter minimize
Definition comdecl.f90:31
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
integer function tria_fdeval(x, g, jac, rowno, jcnm, mode, ignerr, errcnt, n, nz, thread, usrmem)
Compute nonlinear terms and non-constant Jacobian elements.
Definition tria01.f90:253
integer function tria_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
Definition tria01.f90:136
program tria03
Main program. A simple setup and call of CONOPT.
Definition tria03.f90:59