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