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!
257! Free solution memory
258!
259 call finalize
260
261End Program cns12
262!
263! ============================================================================
264! Define information about the model:
265!
266
267!> Define information about the model
268!!
269!! @include{doc} readMatrix_params.dox
270Integer Function cns12_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
271 colsta, rowno, value, nlflag, n, m, nz, &
272 usrmem )
273#ifdef dec_directives_win32
274!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Cns12_ReadMatrix
275#endif
276 Use casedata_num
277 implicit none
278 integer, intent (in) :: n ! number of variables
279 integer, intent (in) :: m ! number of constraints
280 integer, intent (in) :: nz ! number of nonzeros
281 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
282 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
283 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
284 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
285 ! (not defined here)
286 integer, intent (out), dimension(m) :: type ! vector of equation types
287 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
288 ! (not defined here)
289 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
290 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
291 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
292 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
293 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
294 real*8 usrmem(*) ! optional user memory
295!
296! Information about Variables:
297! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
298! Default: the status information in Vsta is not used.
299!
300 if ( casenum == 1 ) then
301 curr(1) = 1.0d0
302 curr(2) = 1.0d0
303 curr(3) = 1.0d0
304 endif
305!
306! Information about Constraints:
307! Default: Rhs = 0
308! Default: the status information in Esta and the function
309! value in FV are not used.
310! Default: Type: There is no default.
311! 0 = Equality,
312! 1 = Greater than or equal,
313! 2 = Less than or equal,
314! 3 = Non binding.
315!
316 type(1) = 0
317 rhs(1) = scale
318 type(2) = 0
319 type(3) = 0
320!
321! Information about the Jacobian. CONOPT expects a columnwise
322! representation in Rowno, Value, Nlflag and Colsta.
323!
324! Colsta = Start of column indices (No Defaults):
325! Rowno = Row indices
326! Value = Value of derivative (by default only linear
327! derivatives are used)
328! Nlflag = 0 for linear and 1 for nonlinear derivative
329! (not needed for completely linear models)
330!
331! Indices
332! x(1) x(2) x(3)
333! 1: 1 2
334! 2: 3 5
335! 3: 4 6
336!
337 colsta(1) = 1
338 colsta(2) = 2
339 colsta(3) = 5
340 colsta(4) = 7
341 rowno(1) = 1
342 rowno(2) = 1
343 rowno(3) = 2
344 rowno(4) = 3
345 rowno(5) = 2
346 rowno(6) = 3
347!
348! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
349! x(1) x(2) x(3)
350! 1: NL NL
351! 2: L L
352! 3: L L
353!
354 nlflag(1) = 1
355 nlflag(2) = 1
356 nlflag(3) = 0
357 nlflag(4) = 0
358 nlflag(5) = 0
359 nlflag(6) = 0
360!
361! Value (Linear only)
362! x(1) x(2) x(3)
363! 1: NL NL
364! 2: 1 1
365! 3: 1 -1
366!
367 value(3) = 1.0d0
368 value(4) = 1.0d0
369 value(5) = 1.0d0
370 value(6) = -1.0d0
371
372 cns12_readmatrix = 0 ! Return value means OK
373
374end Function cns12_readmatrix
375!
376!==========================================================================
377! Compute nonlinear terms and non-constant Jacobian elements
378!
379
380!> Compute nonlinear terms and non-constant Jacobian elements
381!!
382!! @include{doc} fdeval_params.dox
383Integer Function cns12_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
384 n, nz, thread, usrmem )
385#ifdef dec_directives_win32
386!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Cns12_FDEval
387#endif
388 use casedata_num
389 implicit none
390 integer, intent (in) :: n ! number of variables
391 integer, intent (in) :: rowno ! number of the row to be evaluated
392 integer, intent (in) :: nz ! number of nonzeros in this row
393 real*8, intent (in), dimension(n) :: x ! vector of current solution values
394 real*8, intent (in out) :: g ! constraint value
395 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
396 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
397 ! in this row. Ffor information only.
398 integer, intent (in) :: mode ! evaluation mode: 1 = function value
399 ! 2 = derivatives, 3 = both
400 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
401 ! as errcnt is incremented
402 integer, intent (in out) :: errcnt ! error counter to be incremented in case
403 ! of function evaluation errors.
404 integer, intent (in) :: thread
405 real*8 usrmem(*) ! optional user memory
406!
407! Row 1: the only nonlinear row: x*x
408!
409 if ( rowno .eq. 1 ) then
410!
411! Mode = 1 or 3. Function value: G = P * Out
412!
413 if ( mode .eq. 1 .or. mode .eq. 3 ) then
414 g = scale*x(1)*x(2)
415 endif
416!
417! Mode = 2 or 3: Derivative values:
418!
419 if ( mode .eq. 2 .or. mode .eq. 3 ) then
420 jac(1) = scale*x(2)
421 jac(2) = scale*x(1)
422 endif
423 endif
424 cns12_fdeval = 0
425
426end 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:324
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:215
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
integer function std_message(smsg, dmsg, nmsg, llen, usrmem, msgv)
Definition comdecl.f90:243
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_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
subroutine finalize
Definition comdecl.f90:79
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