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