CONOPT
Loading...
Searching...
No Matches
square2.f90
Go to the documentation of this file.
1!> @file square2.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!! A square model where we pretend that the second constraint is completely nonlinear.
5!!
6!! \f{eqnarray*}{
7!! &x1 + x2 = 10 \\
8!! &x1 - x2 = 0 \\
9!! &x1 + x2 \leq 9
10!! \f}
11!!
12!! In a second case we change the Rhs in the inequality to 11 so the
13!! model becomes feasible.
14!!
15!!
16!! For more information about the individual callbacks, please have a look at the source code.
17
18!> Main program. A simple setup and call of CONOPT
19!!
20Program square
21
22 Use proginfo
23 Use coidef
24 Use casedata_num
25 implicit None
26!
27! Declare the user callback routines as Integer, External:
28!
29 Integer, External :: sq_readmatrix ! Mandatory Matrix definition routine defined below
30 Integer, External :: sq_fdeval ! Function and Derivative evaluation routine
31 ! needed a nonlinear model.
32 Integer, External :: std_status ! Standard callback for displaying solution status
33 Integer, External :: std_solution ! Standard callback for displaying solution values
34 Integer, External :: std_message ! Standard callback for managing messages
35 Integer, External :: std_errmsg ! Standard callback for managing error messages
36#if defined(itl)
37!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Sq_ReadMatrix
38!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Sq_FDEval
39!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
40!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
41!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
42!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
43#endif
44!
45! Control vector
46!
47 INTEGER :: numcallback
48 INTEGER, Dimension(:), Pointer :: cntvect
49 INTEGER :: coi_error
50
51 Integer :: i
52 Logical :: error
53
54 call startup
55!
56! Create and initialize a Control Vector
57!
58 numcallback = coidef_size()
59 Allocate( cntvect(numcallback) )
60 coi_error = coidef_inifort( cntvect )
61!
62! Tell CONOPT about the size of the model by populating the Control Vector:
63!
64 coi_error = max( coi_error, coidef_numvar( cntvect, 2 ) ) ! 2 variables
65 coi_error = max( coi_error, coidef_numcon( cntvect, 3 ) ) ! 3 constraints
66 coi_error = max( coi_error, coidef_numnz( cntvect, 6 ) ) ! 6 nonzeros in the Jacobian
67 coi_error = max( coi_error, coidef_numnlnz( cntvect, 4 ) ) ! 4 of which are nonlinear
68 coi_error = max( coi_error, coidef_square( cntvect, 1 ) ) ! 1 means Square system
69 coi_error = max( coi_error, coidef_optfile( cntvect, 'square2.opt' ) )
70!
71! Tell CONOPT about the callback routines:
72!
73 coi_error = max( coi_error, coidef_readmatrix( cntvect, sq_readmatrix ) )
74 coi_error = max( coi_error, coidef_fdeval( cntvect, sq_fdeval ) )
75 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
76 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
77 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
78 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
79
80#if defined(LICENSE_INT_1) && defined(LICENSE_INT_2) && defined(LICENSE_INT_3) && defined(LICENSE_TEXT)
81 coi_error = max( coi_error, coidef_license( cntvect, license_int_1, license_int_2, license_int_3, license_text) )
82#endif
83
84 If ( coi_error .ne. 0 ) THEN
85 write(*,*)
86 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
87 write(*,*)
88 call flog( "Skipping Solve due to setup errors", 1 )
89 ENDIF
90!
91! Allocate space for the solution and status vectors.
92!
93 do_allocate = .true.
94!
95! Start CONOPT:
96!
97 casenum = 1
98 coi_error = coi_solve( cntvect )
99
100 write(*,*)
101 write(*,*) 'End of Square2 - case 1 example. Return code=',coi_error
102
103 If ( coi_error /= 0 ) then
104 call flog( "Case 1: Errors encountered during solution", 1 )
105 elseif ( stacalls == 0 .or. solcalls == 0 ) then
106 call flog( "Case 1: Status or Solution routine was not called", 1 )
107 elseif ( sstat /= 1 .or. mstat < 4 .or. mstat > 5 ) then
108 call flog( "Case 1: Solver or Model status not as expected (1,4) or (1,5)", 1 )
109 elseif ( obj /= 0.d0 ) Then
110 call flog( "Case 1: Objective for square model was not as expected 0.0", 1 )
111 else
112!
113! Check the primal and dual solution itself
114!
115 error = .false.
116 do i = 1, 2
117! if ( abs(Xprim(i)-5.0d0) > 1.d-7 ) error = .true. ! could be something else
118 if ( abs(xdual(i)) > 1.d-7 ) error = .true.
119 if ( abs(udual(i)) > 1.d-7 ) error = .true.
120 enddo
121 if ( abs(xprim(1)+xprim(2)-uprim(1)) > 1.e-7 ) error = .true. ! could be something else
122 if ( abs(xprim(1)-xprim(2)-uprim(2)) > 1.e-7 ) error = .true.
123 if ( abs(xprim(1)+xprim(2)-uprim(3)) > 1.e-7 ) error = .true.
124 if ( error ) call flog( "Case 1: Numerical solution was not as expected.", 1 )
125!
126! Check the status information
127!
128 do i = 1, 2
129 if ( xbasc(i) /= 2 ) error = .true. ! Basic
130 if ( xstat(i) /= 0 ) error = .true. ! Normal
131! if ( Ubasc(i) /= 0 ) error = .true. ! Lower
132! if ( UStat(i) /= 0 ) error = .true. ! Normal
133 enddo
134 if ( ustat(2) /= 0 ) error = .true. ! Normal
135 if ( ustat(3) /= 2 .and. ustat(1) /= 2 ) error = .true. ! Either Equation 1 or 3 must be infeasible
136! if ( Ubasc(3) /= 1 ) error = .true. ! Upper
137 if ( error ) call flog( "Case 1: Status information was not as expected.", 1 )
138 endif
139!
140! Start CONOPT with second case where the right hand side in equation 3 is 11:
141!
142 casenum = 2
143 coi_error = coi_solve( cntvect )
144
145 write(*,*)
146 write(*,*) 'End of Square2 example case 2. Return code=',coi_error
147
148 If ( coi_error /= 0 ) then
149 call flog( "Case 2: Errors encountered during solution", 1 )
150 elseif ( stacalls == 0 .or. solcalls == 0 ) then
151 call flog( "Case 2: Status or Solution routine was not called", 1 )
152 elseif ( sstat /= 1 .or. mstat < 15 .or. mstat > 16 ) then
153 call flog( "Case 2: Solver or Model status not as expected (1,15) or (1,16)", 1 )
154 elseif ( obj /= 0.d0 ) Then
155 call flog( "Case 2: Objective for square model was not as expected 0.0", 1 )
156 else
157!
158! Check the primal and dual solution itself
159!
160 error = .false.
161 do i = 1, 2
162 if ( abs(xprim(i)-5.0d0) > 1.d-7 ) error = .true.
163 if ( abs(xdual(i)) > 1.d-7 ) error = .true.
164 if ( abs(udual(i)) > 1.d-7 ) error = .true.
165 enddo
166 if ( abs(uprim(1)-10.d0) > 1.e-7 ) error = .true. ! could be something else
167 if ( abs(uprim(2)- 0.d0) > 1.e-7 ) error = .true.
168 if ( abs(uprim(3)-10.d0) > 1.e-7 ) error = .true.
169 if ( error ) call flog( "Case 2: Numerical solution was not as expected.", 1 )
170!
171! Check the status information
172!
173 do i = 1, 2
174 if ( xbasc(i) /= 2 ) error = .true. ! Basic
175 if ( xstat(i) /= 0 ) error = .true. ! Normal
176! if ( Ubasc(i) /= 0 ) error = .true. ! Lower
177 if ( ustat(i) /= 0 ) error = .true. ! Normal
178 enddo
179! if ( Ubasc(3) /= 2 ) error = .true. ! Basic
180 if ( ustat(3) /= 0 ) error = .true. ! Normal
181 if ( error ) call flog( "Case 2: Status information was not as expected.", 1 )
182 endif
183
184 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
185
186 call flog( "Successful Solve", 0 )
187
188End Program square
189!
190! ============================================================================
191! Define information about the model:
192!
193
194!> Define information about the model
195!!
196!! @include{doc} readMatrix_params.dox
197Integer Function sq_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
198 colsta, rowno, value, nlflag, n, m, nz, &
199 usrmem )
200#if defined(itl)
201!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Sq_ReadMatrix
202#endif
203 Use casedata_num
204 implicit none
205 integer, intent (in) :: n ! number of variables
206 integer, intent (in) :: m ! number of constraints
207 integer, intent (in) :: nz ! number of nonzeros
208 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
209 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
210 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
211 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
212 ! (not defined here)
213 integer, intent (out), dimension(m) :: type ! vector of equation types
214 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
215 ! (not defined here)
216 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
217 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
218 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
219 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
220 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
221 real*8 usrmem(*) ! optional user memory
222!
223! Information about Variables:
224! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
225! Default: the status information in Vsta is not used.
226!
227! Information about Constraints:
228! Default: Rhs = 0
229! Default: the status information in Esta and the function
230! value in FV are not used.
231! Default: Type: There is no default.
232! 0 = Equality,
233! 1 = Greater than or equal,
234! 2 = Less than or equal,
235! 3 = Non binding.
236!
237! Constraint 1
238! Rhs = 10 and type Equal
239!
240 rhs(1) = 10.d0
241 type(1) = 0
242!
243! Constraint 2
244! Rhs = 0 and type Equality
245!
246 type(2) = 0
247!
248! Constraint 3
249! Rhs = 9 and type Less than (and Rhs = 11 in case 2)
250!
251 if ( casenum == 1 ) then
252 rhs(3) = 9.d0
253 else if ( casenum == 2 ) then
254 rhs(3) = 11.d0
255 endif
256 type(3) = 2
257!
258! Information about the Jacobian. We use the standard method with
259! Rowno, Value, Nlflag and Colsta and we do not use Colno.
260!
261! Colsta = Start of column indices (No Defaults):
262! Rowno = Row indices
263! Value = Value of derivative (by default only linear
264! derivatives are used)
265! Nlflag = 0 for linear and 1 for nonlinear derivative
266! (not needed for completely linear models)
267!
268! Indices
269! x(1) x(2)
270! 1: 1 4
271! 2: 2 5
272! 3: 3 6
273!
274 colsta(1) = 1
275 colsta(2) = 4
276 colsta(3) = 7
277 rowno(1) = 1
278 rowno(2) = 2
279 rowno(3) = 3
280 rowno(4) = 1
281 rowno(5) = 2
282 rowno(6) = 3
283!
284! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
285! x(1) x(2)
286! 1: L L
287! 2: NL NL
288! 3: NL NL
289!
290 nlflag(1) = 0
291 nlflag(2) = 1
292 nlflag(3) = 1
293 nlflag(4) = 0
294 nlflag(5) = 1
295 nlflag(6) = 1
296!
297! Value (Linear only)
298! x(1) x(2) x(3) x(4)
299! 1: 1 1
300! 2: NL NL
301! 3: NL NL
302!
303 value(1) = 1.d0
304 value(4) = 1.d0
305
306 sq_readmatrix = 0 ! Return value means OK
307
308end Function sq_readmatrix
309!
310!==========================================================================
311! Compute nonlinear terms and non-constant Jacobian elements
312!
313
314!> Compute nonlinear terms and non-constant Jacobian elements
315!!
316!! @include{doc} fdeval_params.dox
317Integer Function sq_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
318 n, nz, thread, usrmem )
319#if defined(itl)
320!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Sq_FDEval
321#endif
322 implicit none
323 integer, intent (in) :: n ! number of variables
324 integer, intent (in) :: rowno ! number of the row to be evaluated
325 integer, intent (in) :: nz ! number of nonzeros in this row
326 real*8, intent (in), dimension(n) :: x ! vector of current solution values
327 real*8, intent (in out) :: g ! constraint value
328 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
329 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
330 ! in this row. Ffor information only.
331 integer, intent (in) :: mode ! evaluation mode: 1 = function value
332 ! 2 = derivatives, 3 = both
333 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
334 ! as errcnt is incremented
335 integer, intent (in out) :: errcnt ! error counter to be incremented in case
336 ! of function evaluation errors.
337 integer, intent (in) :: thread
338 real*8 usrmem(*) ! optional user memory
339!
340! Row 1: Is declared as linear and should not be called.
341!
342 if ( rowno .eq. 1 ) then
343 sq_fdeval = 1
344 return
345!
346! Row 2: x1 + x2 assumed to be nonlinear
347!
348 elseif ( rowno .eq. 2 ) then
349!
350! Mode = 1 or 3: Function value
351!
352 if ( mode .eq. 1 .or. mode .eq. 3 ) then
353 g = x(1) - x(2)
354 endif
355!
356! Mode = 2 or 3: Derivatives
357!
358 if ( mode .eq. 2 .or. mode .eq. 3 ) then
359 jac(1) = 1.d0
360 jac(2) = -1.d0
361 endif
362 elseif ( rowno .eq. 3 ) then
363!
364! Mode = 1 or 3: Function value
365!
366 if ( mode .eq. 1 .or. mode .eq. 3 ) then
367 g = x(1) + x(2)
368 endif
369!
370! Mode = 2 or 3: Derivatives
371!
372 if ( mode .eq. 2 .or. mode .eq. 3 ) then
373 jac(1) = 1.d0
374 jac(2) = 1.d0
375 endif
376 endif
377 sq_fdeval = 0
378
379end Function sq_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_square(cntvect, square)
square models.
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_numnz(cntvect, numnz)
defines the number of nonzero elements in the Jacobian.
Definition coistart.f90:437
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
real *8 obj
Definition comdecl.f90:10
integer solcalls
Definition comdecl.f90:9
integer sstat
Definition comdecl.f90:12
real *8, dimension(:), pointer udual
Definition comdecl.f90:18
real *8, dimension(:), pointer xdual
Definition comdecl.f90:17
integer, dimension(:), pointer xstat
Definition comdecl.f90:19
integer, dimension(:), pointer xbasc
Definition comdecl.f90:19
integer, dimension(:), pointer ustat
Definition comdecl.f90:20
integer stacalls
Definition comdecl.f90:8
subroutine flog(msg, code)
Definition comdecl.f90:56
logical do_allocate
Definition comdecl.f90:21
real *8, dimension(:), pointer xprim
Definition comdecl.f90:17
real *8, dimension(:), pointer uprim
Definition comdecl.f90:18
integer mstat
Definition comdecl.f90:11
subroutine startup
Definition comdecl.f90:35
integer function sq_fdeval(x, g, jac, rowno, jcnm, mode, ignerr, errcnt, n, nz, thread, usrmem)
Compute nonlinear terms and non-constant Jacobian elements.
Definition square.f90:248
integer function sq_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
Definition square.f90:147
program square
Main program. A simple setup and call of CONOPT.
Definition square.f90:17