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