CONOPT
Loading...
Searching...
No Matches
pen02.f90
Go to the documentation of this file.
1!> @file pen02.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! Test model pen02.gms -- a model with penalty pairs.
6!!
7!! Positive variables \f$x1, x2, x3, p, n\f$
8!!
9!! \f[
10!! \min x1 + p + n
11!! \f]
12!! \f[
13!! x1 + x2 + x3 = 6
14!! \f]
15!! \f[
16!! x1^2 + x2^2 + x3^2 = 14
17!! \f]
18!! \f[
19!! x1^3 + x2^3 + x3^3 = 36
20!! \f]
21!! \f[
22!! x1 + x2^2 + x3^3 + p - n = 0
23!! \f]
24!!
25!! Initial values in the first case x1 = x2 = x3 = 0.5;
26!! With these values the first three constraints are parallel and we
27!! will very likely end in a locally infeasible solution.
28!!
29!! Initial values in the next cases x1 = 0.2, x2 = 0.4, and x3 = 0.6;
30!! In case 2 we stop after 1 iteration which means we are in Phase 0
31!! in the internal model.
32!! We test that the solution is relative to the large model.
33!!
34!! In case 3 we stop after 4 iteration which means we are in Phase 1
35!! in the internal model. We should have duals.
36!! We test that the solution is relative to the large model.
37!!
38!! In case 4 we solve the model to optimality.
39!!
40!!
41!! For more information about the individual callbacks, please have a look at the source code.
42
43#if defined(_WIN32) && !defined(_WIN64)
44#define dec_directives_win32
45#endif
46
47!> Main program. A simple setup and call of CONOPT
48!!
49Program pen02
50
52 Use conopt
53 Use casedata_num
54 implicit None
55!
56! Declare the user callback routines as Integer, External:
57!
58 Integer, External :: pen02_readmatrix ! Mandatory Matrix definition routine defined below
59 Integer, External :: pen02_fdeval ! Function and Derivative evaluation routine
60 Integer, External :: std_status ! Standard callback for displaying solution status
61 Integer, External :: std_solution ! Standard callback for displaying solution values
62 Integer, External :: std_message ! Standard callback for managing messages
63 Integer, External :: std_errmsg ! Standard callback for managing error messages
64#ifdef dec_directives_win32
65!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Pen02_ReadMatrix
66!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Pen02_FDEval
67!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
68!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
69!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
70!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
71#endif
72!
73! Control vector
74!
75 INTEGER, Dimension(:), Pointer :: cntvect
76 INTEGER :: coi_error
77!
78! Create and initialize a Control Vector
79!
80 call startup
81
82 coi_error = coi_create( cntvect )
83!
84! Tell CONOPT about the size of the model by populating the Control Vector:
85!
86 coi_error = max( coi_error, coidef_numvar( cntvect, 5 ) ) ! # variables
87 coi_error = max( coi_error, coidef_numcon( cntvect, 5 ) ) ! # constraints
88 coi_error = max( coi_error, coidef_numnz( cntvect,17 ) ) ! # nonzeros in the Jacobian
89 coi_error = max( coi_error, coidef_numnlnz( cntvect, 8 ) ) ! # of which are nonlinear
90 coi_error = max( coi_error, coidef_optdir( cntvect, -1 ) ) ! Minimize
91 coi_error = max( coi_error, coidef_objcon( cntvect, 5 ) ) ! Objective is expression 5
92 coi_error = max( coi_error, coidef_optfile( cntvect, 'pen02.opt' ) )
93!
94! Tell CONOPT about the callback routines:
95!
96 coi_error = max( coi_error, coidef_readmatrix( cntvect, pen02_readmatrix ) )
97 coi_error = max( coi_error, coidef_fdeval( cntvect, pen02_fdeval ) )
98 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
99 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
100 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
101 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
102
103#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
104 coi_error = max( coi_error, coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_license_text) )
105#endif
106
107 If ( coi_error .ne. 0 ) THEN
108 write(*,*)
109 write(*,*) '**** Fatal Error while loading CONOPT4 Callback routines.'
110 write(*,*)
111 call flog( "Skipping Solve due to setup errors", 1 )
112 ENDIF
113!
114! Save the solution so we can check the duals:
115!
116 do_allocate = .true.
117!
118! Start CONOPT:
119!
120 casenum = 1
121 coi_error = coi_solve( cntvect )
122 If ( coi_error /= 0 ) then
123 call flog( "Case 1: Errors encountered during solution", 1 )
124 elseif ( stacalls == 0 .or. solcalls == 0 ) then
125 call flog( "Case 1: Status or Solution routine was not called", 1 )
126 elseif ( sstat /= 1 ) then
127 call flog( "Case 1: Solver Status was not 1 as expected.", 1 )
128 endif
129!
130! Solve the second case -- max 1 iteration:
131!
132 stacalls = 0; solcalls = 0;
133 casenum = 2
134 coi_error = max( coi_error, coidef_itlim( cntvect, 1 ) )
135 coi_error = coi_solve( cntvect )
136 If ( coi_error /= 0 ) then
137 call flog( "Case 2: Errors encountered during solution", 1 )
138 elseif ( stacalls == 0 .or. solcalls == 0 ) then
139 call flog( "Case 2: Status or Solution routine was not called", 1 )
140 elseif ( sstat /= 2 ) then
141 call flog( "Case 2: Solver Status was not 2 as expected.", 1 )
142 endif
143!
144! The third -- max 4 iterations:
145!
146 stacalls = 0; solcalls = 0;
147 casenum = 3
148 coi_error = max( coi_error, coidef_itlim( cntvect, 4 ) )
149 coi_error = coi_solve( cntvect )
150 If ( coi_error /= 0 ) then
151 call flog( "Case 3: Errors encountered during solution", 1 )
152 elseif ( stacalls == 0 .or. solcalls == 0 ) then
153 call flog( "Case 3: Status or Solution routine was not called", 1 )
154 elseif ( sstat /= 2 ) then
155 call flog( "Case 3: Solver Status was not 2 as expected.", 1 )
156 endif
157!
158! And the fourth -- no practical iteration limit:
159!
160 stacalls = 0; solcalls = 0;
161 casenum = 4
162 coi_error = max( coi_error, coidef_itlim( cntvect, 10000 ) )
163 coi_error = coi_solve( cntvect )
164 If ( coi_error /= 0 ) then
165 call flog( "Case 4: Errors encountered during solution", 1 )
166 elseif ( stacalls == 0 .or. solcalls == 0 ) then
167 call flog( "Case 4: Status or Solution routine was not called", 1 )
168 elseif ( sstat /= 1 ) then
169 call flog( "Case 4: Solver Status was 1 not as expected.", 1 )
170 endif
171
172 write(*,*)
173 write(*,*) 'End of pen02 example. Return code=',coi_error
174
175 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
176
177 call flog( "Successful Solve", 0 )
178!
179! Free solution memory
180!
181 call finalize
182
183End Program pen02
184!
185! ============================================================================
186! Define information about the model:
187!
188
189!> Define information about the model
190!!
191!! @include{doc} readMatrix_params.dox
192Integer Function pen02_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
193 colsta, rowno, value, nlflag, n, m, nz, &
194 usrmem )
195#ifdef dec_directives_win32
196!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Pen02_ReadMatrix
197#endif
198 use casedata_num
199 implicit none
200 integer, intent (in) :: n ! number of variables
201 integer, intent (in) :: m ! number of constraints
202 integer, intent (in) :: nz ! number of nonzeros
203 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
204 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
205 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
206 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
207 ! (not defined here)
208 integer, intent (out), dimension(m) :: type ! vector of equation types
209 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
210 ! (not defined here)
211 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
212 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
213 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
214 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
215 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
216 real*8 usrmem(*) ! optional user memory
217!
218! Information about Variables:
219! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
220! Default: the status information in Vsta is not used.
221!
222 lower(1) = 0.0d0
223 lower(2) = 0.0d0
224 lower(3) = 0.0d0
225 lower(4) = 0.0d0
226 lower(5) = 0.0d0
227 if ( casenum == 1 ) Then
228 curr(1) = 0.5d0
229 curr(2) = 0.5d0
230 curr(3) = 0.5d0
231 Else
232 curr(1) = 0.2d0
233 curr(2) = 0.4d0
234 curr(3) = 0.6d0
235 Endif
236!
237! Information about Constraints:
238! Default: Rhs = 0
239! Default: the status information in Esta and the function
240! value in FV are not used.
241! Default: Type: There is no default.
242! 0 = Equality,
243! 1 = Greater than or equal,
244! 2 = Less than or equal,
245! 3 = Non binding.
246!
247 type(1) = 0
248 type(2) = 0
249 type(3) = 0
250 type(4) = 0
251 type(5) = 3
252 rhs(1) = 6.d0
253 rhs(2) = 14.d0
254 rhs(3) = 36.d0
255!
256! Information about the Jacobian. CONOPT expects a columnwise
257! representation in Rowno, Value, Nlflag and Colsta.
258!
259! Colsta = Start of column indices (No Defaults):
260! Rowno = Row indices
261! Value = Value of derivative (by default only linear
262! derivatives are used)
263! Nlflag = 0 for linear and 1 for nonlinear derivative
264! (not needed for completely linear models)
265!
266! Indices
267! x1 x2 x3 x4 x5
268! 1: 1 6 10
269! 2: 2 7 11
270! 3: 3 8 12
271! 4: 4 9 13 14 16
272! 5: 5 15 17
273!
274 colsta(1) = 1
275 colsta(2) = 6
276 colsta(3) =10
277 colsta(4) =14
278 colsta(5) =16
279 colsta(6) =18
280 rowno(1) = 1
281 rowno(2) = 2
282 rowno(3) = 3
283 rowno(4) = 4
284 rowno(5) = 5
285 rowno(6) = 1
286 rowno(7) = 2
287 rowno(8) = 3
288 rowno(9) = 4
289 rowno(10) = 1
290 rowno(11) = 2
291 rowno(12) = 3
292 rowno(13) = 4
293 rowno(14) = 4
294 rowno(15) = 5
295 rowno(16) = 4
296 rowno(17) = 5
297!
298! Nonlinearity Structure:
299!
300! x1 x2 x3 x4 x5
301! 1: L L L
302! 2: NL NL NL
303! 3: NL NL NL
304! 4: L NL NL L L
305! 5: L L L
306!
307 nlflag(1) = 0
308 nlflag(2) = 1
309 nlflag(3) = 1
310 nlflag(4) = 0
311 nlflag(5) = 0
312 nlflag(6) = 0
313 nlflag(7) = 1
314 nlflag(8) = 1
315 nlflag(9) = 1
316 nlflag(10) = 0
317 nlflag(11) = 1
318 nlflag(12) = 1
319 nlflag(13) = 1
320 nlflag(14) = 0
321 nlflag(15) = 0
322 nlflag(16) = 0
323 nlflag(17) = 0
324!
325! Value (Linear only)
326! x1 x2 x3 x4 x5
327! 1: 1 1 1
328! 2: NL NL NL
329! 3: NL NL NL
330! 4: 1 NL NL 1 -1
331! 5: 1 1 1
332!
333 value(1) = 1.d0
334 value(4) = 1.d0
335 value(5) = 1.d0
336 value(6) = 1.d0
337 value(10) = 1.d0
338 value(14) = 1.d0
339 value(15) = 1.d0
340 value(16) = -1.d0
341 value(17) = 1.d0
343 pen02_readmatrix = 0 ! Return value means OK
344
345end Function pen02_readmatrix
346!
347!==========================================================================
348! Compute nonlinear terms and non-constant Jacobian elements
349!
350
351!> Compute nonlinear terms and non-constant Jacobian elements
352!!
353!! @include{doc} fdeval_params.dox
354Integer Function pen02_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
355 n, nz, thread, usrmem )
356#ifdef dec_directives_win32
357!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Pen02_FDEval
358#endif
359 implicit none
360 integer, intent (in) :: n ! number of variables
361 integer, intent (in) :: rowno ! number of the row to be evaluated
362 integer, intent (in) :: nz ! number of nonzeros in this row
363 real*8, intent (in), dimension(n) :: x ! vector of current solution values
364 real*8, intent (in out) :: g ! constraint value
365 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
366 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
367 ! in this row. Ffor information only.
368 integer, intent (in) :: mode ! evaluation mode: 1 = function value
369 ! 2 = derivatives, 3 = both
370 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
371 ! as errcnt is incremented
372 integer, intent (in out) :: errcnt ! error counter to be incremented in case
373 ! of function evaluation errors.
374 integer, intent (in) :: thread
375 real*8 usrmem(*) ! optional user memory
376
377 pen02_fdeval = 0
378 if ( rowno .eq. 1 ) then
379!
380! Row 1: Linear
381!
382 pen02_fdeval = 1 ! This is an error
383 elseif ( rowno .eq. 2 ) then
384!
385! Row 2: x1**2 + x2**2 + x3**2
386!
387! Mode = 1 or 3: Function value
388!
389 if ( mode .eq. 1 .or. mode .eq. 3 ) then
390 g = x(1)**2 + x(2)**2 + x(3)**2
391 endif
392!
393! Mode = 2 or 3: Derivatives
394!
395 if ( mode .eq. 2 .or. mode .eq. 3 ) then
396 jac(1) = 2.d0*x(1)
397 jac(2) = 2.d0*x(2)
398 jac(3) = 2.d0*x(3)
399 endif
400 elseif ( rowno .eq. 3 ) then
401!
402! Row 3: x1**3 + x2**3 + x3**3
403!
404! Mode = 1 or 3: Function value
405!
406 if ( mode .eq. 1 .or. mode .eq. 3 ) then
407 g = x(1)**3 + x(2)**3 + x(3)**3
408 endif
409!
410! Mode = 2 or 3: Derivatives
411!
412 if ( mode .eq. 2 .or. mode .eq. 3 ) then
413 jac(1) = 3.d0*x(1)**2
414 jac(2) = 3.d0*x(2)**2
415 jac(3) = 3.d0*x(3)**2
416 endif
417 elseif ( rowno .eq. 4 ) then
418!
419! Row 4: x1 + x2**2 + x3**3 where first term is linear
420!
421! Mode = 1 or 3: Function value
422!
423 if ( mode .eq. 1 .or. mode .eq. 3 ) then
424 g = x(2)**2 + x(3)**3
425 endif
426!
427! Mode = 2 or 3: Derivatives
428!
429 if ( mode .eq. 2 .or. mode .eq. 3 ) then
430 jac(2) = 2.d0*x(2)
431 jac(3) = 3.d0*x(3)**2
432 endif
433 elseif ( rowno .eq. 4 ) then
434!
435! Row = 5: The row is linear and will not be called.
436!
437 pen02_fdeval = 1 ! This is an error
438 endif
439
440end Function pen02_fdeval
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_itlim(cntvect, itlim)
define the Iteration Limit.
Definition conopt.f90:323
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_objcon(cntvect, objcon)
defines the Objective Constraint.
Definition conopt.f90:239
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 stacalls
Definition comdecl.f90:14
subroutine flog(msg, code)
Definition comdecl.f90:62
logical do_allocate
Definition comdecl.f90:27
subroutine startup
Definition comdecl.f90:41
integer function pen02_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
Definition pen02.f90:186
program pen02
Main program. A simple setup and call of CONOPT.
Definition pen02.f90:51
integer function pen02_fdeval(x, g, jac, rowno, jcnm, mode, ignerr, errcnt, n, nz, thread, usrmem)
Compute nonlinear terms and non-constant Jacobian elements.
Definition pen02.f90:344