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