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