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 )
139!
140! Free solution memory
141!
142 call finalize
144End Program triabad11
145!
146! ============================================================================
147! Define information about the model:
148!
149
150!> Define information about the model
151!!
152!! @include{doc} readMatrix_params.dox
153Integer Function tria_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
154 colsta, rowno, value, nlflag, n, m, nz, &
155 usrmem )
156#ifdef dec_directives_win32
157!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Tria_ReadMatrix
158#endif
159 implicit none
160 integer, intent (in) :: n ! number of variables
161 integer, intent (in) :: m ! number of constraints
162 integer, intent (in) :: nz ! number of nonzeros
163 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
164 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
165 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
166 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
167 ! (not defined here)
168 integer, intent (out), dimension(m) :: type ! vector of equation types
169 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
170 ! (not defined here)
171 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
172 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
173 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
174 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
175 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
176 real*8 usrmem(*) ! optional user memory
177!
178! Information about Variables:
179! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
180! Default: the status information in Vsta is not used.
181!
182! The model uses initial values for x1 and x2
183!
184 curr(1) = 0.999d0
185 curr(2) = 0.01d0
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 = 1.0 and type Equality
199!
200 rhs(1) = 1.0d0
201 type(1) = 0
202!
203! Constraint 2: e2
204! Rhs = 1.0 and type Equality
205!
206 rhs(2) = 1.0d0
207 type(2) = 0
208!
209! Constraint 3: e3
210! Rhs = 2.0 and type Equality
211!
212 rhs(3) = 2.0d0
213 type(3) = 0
214!
215! Constraint 4: e4
216! Rhs = 0.0 and type Equality
217!
218 type(4) = 0
219!
220! Information about the Jacobian. CONOPT expects a columnwise
221! representation in Rowno, Value, Nlflag and Colsta.
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)
232! 1: 1
233! 2: 4
234! 3: 2 5
235! 4: 3 6 7
236!
237 colsta(1) = 1
238 colsta(2) = 4
239 colsta(3) = 7
240 colsta(4) = 8
241 rowno(1) = 1
242 rowno(2) = 3
243 rowno(3) = 4
244 rowno(4) = 2
245 rowno(5) = 3
246 rowno(6) = 4
247 rowno(7) = 4
248!
249! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
250! x(1) x(2) x(3)
251! 1: NL
252! 2: NL
253! 3: L L
254! 4: L L L
255!
256 nlflag(1) = 1
257 nlflag(2) = 0
258 nlflag(3) = 0
259 nlflag(4) = 1
260 nlflag(5) = 0
261 nlflag(6) = 0
262 nlflag(7) = 0
263!
264! Value (Linear only)
265! x(1) x(2) x(3)
266! 1: NL
267! 2: -1.0 1.0
268!
269 value(2) = 1.d0
270 value(3) = -1.d0
271 value(5) = 1.d0
272 value(6) = -1.d0
273 value(7) = 1.d0
274
275 tria_readmatrix = 0 ! Return value means OK
276
277end Function tria_readmatrix
278!
279!==========================================================================
280! Compute nonlinear terms and non-constant Jacobian elements
281!
282
283!> Compute nonlinear terms and non-constant Jacobian elements
284!!
285!! @include{doc} fdeval_params.dox
286Integer Function tria_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
287 n, nz, thread, usrmem )
288#ifdef dec_directives_win32
289!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Tria_FDEval
290#endif
291 implicit none
292 integer, intent (in) :: n ! number of variables
293 integer, intent (in) :: rowno ! number of the row to be evaluated
294 integer, intent (in) :: nz ! number of nonzeros in this row
295 real*8, intent (in), dimension(n) :: x ! vector of current solution values
296 real*8, intent (in out) :: g ! constraint value
297 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
298 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
299 ! in this row. Ffor information only.
300 integer, intent (in) :: mode ! evaluation mode: 1 = function value
301 ! 2 = derivatives, 3 = both
302 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
303 ! as errcnt is incremented
304 integer, intent (in out) :: errcnt ! error counter to be incremented in case
305 ! of function evaluation errors.
306 integer, intent (in) :: thread
307 real*8 usrmem(*) ! optional user memory
308!
309! Row 1: e1 .. sqr(x1) =E= 1;
310!
311 if ( rowno == 1 ) then
312!
313! Mode = 1 or 3. G = sqr(x1)
314!
315 if ( mode == 1 .or. mode == 3 ) then
316 g = x(1)*x(1)
317 endif
318!
319! Mode = 2 or 3: Derivative values:
320!
321 if ( mode .eq. 2 .or. mode .eq. 3 ) then
322 jac(1) = 2.d0*x(1)
323 endif
324 tria_fdeval = 0
325 else if ( rowno == 2 ) then
326!
327! e2 .. power(x2,3) =E= 1;
328!
329 if ( mode == 1 .or. mode == 3 ) then
330 g = x(2)*x(2)*x(2)
331 endif
332 if ( mode .eq. 2 .or. mode .eq. 3 ) then
333 jac(2) = 3.d0*x(2)*x(2)
334 endif
335 tria_fdeval = 0
336 else
337 tria_fdeval = 1
338 endif
339
340end Function tria_fdeval
341
integer function std_solution(xval, xmar, xbas, xsta, yval, ymar, ybas, ysta, n, m, usrmem)
Definition comdecl.f90:170
integer function std_status(modsta, solsta, iter, objval, usrmem)
Definition comdecl.f90:126
subroutine checkdual(case, minmax)
Definition comdecl.f90:432
integer function std_message(smsg, dmsg, nmsg, llen, usrmem, msgv)
Definition comdecl.f90:243
integer function std_triord(mode, type, status, irow, icol, inf, value, resid, usrmem)
Definition comdecl.f90:327
integer function std_errmsg(rowno, colno, posno, msglen, usrmem, msg)
Definition comdecl.f90:286
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
subroutine finalize
Definition comdecl.f90:79
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:257
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:140
program triabad11
Main program. A simple setup and call of CONOPT.
Definition triabad11.f90:47