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!
186! Free solution memory
187!
188 call finalize
189
190End Program tria03
191!
192! ============================================================================
193! Define information about the model:
194!
195
196!> Define information about the model
197!!
198!! @include{doc} readMatrix_params.dox
199Integer Function tria_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
200 colsta, rowno, value, nlflag, n, m, nz, &
201 usrmem )
202#ifdef dec_directives_win32
203!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Tria_ReadMatrix
204#endif
205 implicit none
206 integer, intent (in) :: n ! number of variables
207 integer, intent (in) :: m ! number of constraints
208 integer, intent (in) :: nz ! number of nonzeros
209 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
210 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
211 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
212 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
213 ! (not defined here)
214 integer, intent (out), dimension(m) :: type ! vector of equation types
215 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
216 ! (not defined here)
217 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
218 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
219 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
220 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
221 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
222 real*8 usrmem(*) ! optional user memory
223!
224! Information about Variables:
225! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
226! Default: the status information in Vsta is not used.
227!
228! The model uses defaults, except for x2 where the starting value is
229! default in first call, +10 in the second, and -10 in the third
230!
231 integer, save :: ncall = 0
232 if ( ncall == 0 ) then
233 ncall = 1
234 else if ( ncall == 1 ) then
235 curr(2) = +10.d0; ncall = 2
236 else if ( ncall == 2 ) then
237 curr(2) = -10.d0; ncall = 3
238 else
239 tria_readmatrix = 1; return
240 endif
241!
242! Information about Constraints:
243! Default: Rhs = 0
244! Default: the status information in Esta and the function
245! value in FV are not used.
246! Default: Type: There is no default.
247! 0 = Equality,
248! 1 = Greater than or equal,
249! 2 = Less than or equal,
250! 3 = Non binding.
251!
252! Constraint 1: e1
253! Rhs = 5.0 and type Equality
254!
255 rhs(1) = 5.0d0
256 type(1) = 0
257!
258! Constraint 2: e2
259! Rhs = 4.0 and type Equality
260!
261 rhs(2) = 4.0d0
262 type(2) = 0
263!
264! Constraint 3: e3
265! Rhs = 0.0 and type Equality
266!
267 type(3) = 0
268!
269! Information about the Jacobian. CONOPT expects a columnwise
270! representation in Rowno, Value, Nlflag and Colsta.
271!
272! Colsta = Start of column indices (No Defaults):
273! Rowno = Row indices
274! Value = Value of derivative (by default only linear
275! derivatives are used)
276! Nlflag = 0 for linear and 1 for nonlinear derivative
277! (not needed for completely linear models)
278!
279! Indices
280! x(1) x(2) x(3) x(4)
281! 1: 1 3
282! 2: 4
283! 3: 2 5 6 7
284!
285 colsta(1) = 1
286 colsta(2) = 3
287 colsta(3) = 6
288 colsta(4) = 7
289 colsta(5) = 8
290 rowno(1) = 1
291 rowno(2) = 3
292 rowno(3) = 1
293 rowno(4) = 2
294 rowno(5) = 3
295 rowno(6) = 3
296 rowno(7) = 3
297!
298! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
299! x(1) x(2) x(3) x(4)
300! 1: L NL
301! 2: NL
302! 3: L L L NL
303!
304 nlflag(1) = 0
305 nlflag(2) = 0
306 nlflag(3) = 1
307 nlflag(4) = 1
308 nlflag(5) = 0
309 nlflag(6) = 0
310 nlflag(7) = 1
311!
312! Value (Linear only)
313! x(1) x(2) x(3) x(4)
314! 1: 1.0 NL
315! 2: 5.0
316! 3: -1.0 -1.0 1.0 NL
317!
318 value(1) = 1.d0
319 value(2) = -1.d0
320 value(4) = 5.d0
321 value(5) = -1.d0
322 value(6) = 1.d0
324 tria_readmatrix = 0 ! Return value means OK
325
326end Function tria_readmatrix
327!
328!==========================================================================
329! Compute nonlinear terms and non-constant Jacobian elements
330!
331
332!> Compute nonlinear terms and non-constant Jacobian elements
333!!
334!! @include{doc} fdeval_params.dox
335Integer Function tria_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
336 n, nz, thread, usrmem )
337#ifdef dec_directives_win32
338!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Tria_FDEval
339#endif
340 implicit none
341 integer, intent (in) :: n ! number of variables
342 integer, intent (in) :: rowno ! number of the row to be evaluated
343 integer, intent (in) :: nz ! number of nonzeros in this row
344 real*8, intent (in), dimension(n) :: x ! vector of current solution values
345 real*8, intent (in out) :: g ! constraint value
346 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
347 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
348 ! in this row. Ffor information only.
349 integer, intent (in) :: mode ! evaluation mode: 1 = function value
350 ! 2 = derivatives, 3 = both
351 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
352 ! as errcnt is incremented
353 integer, intent (in out) :: errcnt ! error counter to be incremented in case
354 ! of function evaluation errors.
355 integer, intent (in) :: thread
356 real*8 usrmem(*) ! optional user memory
357!
358! Row 1: e1 .. x1 + power(x2,3) =e= 5;
359!
360 if ( rowno .eq. 1 ) then
361!
362! Mode = 1 or 3. G = power(x2,3)
363!
364 if ( mode .eq. 1 .or. mode .eq. 3 ) then
365 g = x(2)*x(2)*x(2)
366 endif
367!
368! Mode = 2 or 3: Derivative values:
369!
370 if ( mode .eq. 2 .or. mode .eq. 3 ) then
371 jac(2) = 3.d0*x(2)*x(2)
372 endif
373 tria_fdeval = 0
374!
375! Row 2: e2 .. sqr(x2) =e= 4;
376!
377 elseif ( rowno .eq. 2 ) then
378!
379! Mode = 1 or 3. G = sqr(x2)
380!
381 if ( mode .eq. 1 .or. mode .eq. 3 ) then
382 g = x(2)*x(2)
383 endif
384!
385! Mode = 2 or 3: Derivative values:
386!
387 if ( mode .eq. 2 .or. mode .eq. 3 ) then
388 jac(2) = 2.d0*x(2)
389 endif
390 tria_fdeval = 0
391!
392! Row 2: e3 .. x3 =e= x2 + x1 + sqr(x4);
393!
394 elseif ( rowno .eq. 3 ) then
395!
396! Mode = 1 or 3. G = -sqr(x4)
397!
398 if ( mode .eq. 1 .or. mode .eq. 3 ) then
399 g = -x(4)*x(4)
400 endif
401!
402! Mode = 2 or 3: Derivative values:
403!
404 if ( mode .eq. 2 .or. mode .eq. 3 ) then
405 jac(4) = -2.d0*x(4)
406 endif
407 tria_fdeval = 0
408 Else
409!
410! Illegal row number
411!
412 tria_fdeval = 1
413 endif
414
415end Function tria_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
subroutine checkdual(case, minmax)
Definition comdecl.f90:432
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(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
subroutine finalize
Definition comdecl.f90:79
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:257
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:140
program tria03
Main program. A simple setup and call of CONOPT.
Definition tria03.f90:59