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