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