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