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