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