CONOPT
Loading...
Searching...
No Matches
square6.f90
Go to the documentation of this file.
1!> @file square6.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 square5, but the last equation is binding
17!! upper instead of lower.
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, 7.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, 'square6.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 Square6 - 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 Square6 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! Model x1 + x2 = 10 Check the activity levels based on primal values.
169! x1 - x2 = 0
170! x1 + x2 + x3 = 9
171! - x2 + x3 + x4 = 1
172 if ( abs(xprim(1)+xprim(2)-uprim(1)) > 1.d-7 ) error = .true.
173 if ( abs(xprim(1)-xprim(2)-uprim(2)) > 1.d-7 ) error = .true.
174 if ( abs(xprim(1)+xprim(2)+xprim(3)-uprim(3)) > 1.d-7 ) error = .true.
175 if ( abs(-xprim(2)+xprim(3)+xprim(3)-uprim(4)) > 1.d-7 ) error = .true.
176 if ( error ) call flog( "Case 2: Numerical solution was not as expected.", 1 )
177!
178! Check the status information
179!
180 do i = 1, 4
181 if ( xbasc(i) /= 2 ) error = .true. ! Basic
182 if ( xstat(i) /= 0 ) error = .true. ! Normal
183! if ( Ubasc(i) /= 0 ) error = .true. ! Lower
184 enddo
185 if ( ustat(1) /= 2 .and. ustat(3) /= 2 .and. ustat(4) /= 2 ) error = .true. ! One of them must be infeasible
186! if ( Ubasc(3) /= 1 ) error = .true. ! Upper
187! if ( Ubasc(4) /= 0 ) error = .true. ! Lower
188 if ( error ) call flog( "Case 2: Status information was not as expected.", 1 )
189 endif
190
191 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
192
193 call flog( "Successful Solve", 0 )
194
195End Program square
196!
197! ============================================================================
198! Define information about the model:
199!
200
201!> Define information about the model
202!!
203!! @include{doc} readMatrix_params.dox
204Integer Function sq_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
205 colsta, rowno, value, nlflag, n, m, nz, &
206 usrmem )
207#if defined(itl)
208!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Sq_ReadMatrix
209#endif
210 Use casedata_num
211 implicit none
212 integer, intent (in) :: n ! number of variables
213 integer, intent (in) :: m ! number of constraints
214 integer, intent (in) :: nz ! number of nonzeros
215 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
216 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
217 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
218 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
219 ! (not defined here)
220 integer, intent (out), dimension(m) :: type ! vector of equation types
221 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
222 ! (not defined here)
223 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
224 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
225 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
226 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
227 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
228 real*8 usrmem(*) ! optional user memory
229!
230! Information about Variables:
231! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
232! Default: the status information in Vsta is not used.
233!
234! Information about Constraints:
235! Default: Rhs = 0
236! Default: the status information in Esta and the function
237! value in FV are not used.
238! Default: Type: There is no default.
239! 0 = Equality,
240! 1 = Greater than or equal,
241! 2 = Less than or equal,
242! 3 = Non binding.
243 if ( casenum == 2 ) then
244 lower(3) = 0.d0
245 upper(4) = 0.d0
246 endif
247!
248! Constraint 1
249! Rhs = 10 and type Equal
250!
251 rhs(1) = 10.d0
252 type(1) = 0
253!
254! Constraint 2
255! Rhs = 0 and type Equality
256!
257 type(2) = 0
258!
259! Constraint 3
260! Rhs = 9 and type Equality
261!
262 rhs(3) = 9.d0
263 type(3) = 0
264!
265! Constraint 4
266! Rhs = 1 and type Equality
267!
268 rhs(4) = 1.d0
269 type(4) = 0
270!
271! Information about the Jacobian. We use the standard method with
272! Rowno, Value, Nlflag and Colsta and we do not use Colno.
273!
274! Colsta = Start of column indices (No Defaults):
275! Rowno = Row indices
276! Value = Value of derivative (by default only linear
277! derivatives are used)
278! Nlflag = 0 for linear and 1 for nonlinear derivative
279! (not needed for completely linear models)
280!
281! Indices
282! x(1) x(2) x(3) x(4)
283! 1: 1 4
284! 2: 2 5
285! 3: 3 6 8
286! 4: 7 9 10
287!
288 colsta(1) = 1
289 colsta(2) = 4
290 colsta(3) = 8
291 colsta(4) = 10
292 colsta(5) = 11
293 rowno(1) = 1
294 rowno(2) = 2
295 rowno(3) = 3
296 rowno(4) = 1
297 rowno(5) = 2
298 rowno(6) = 3
299 rowno(7) = 4
300 rowno(8) = 3
301 rowno(9) = 4
302 rowno(10) = 4
303!
304! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
305! x(1) x(2) x(3) x(4)
306! 1: L L
307! 2: NL NL
308! 3: NL NL L
309! 4: NL NL L
310!
311 nlflag(1) = 0
312 nlflag(2) = 1
313 nlflag(3) = 1
314 nlflag(4) = 0
315 nlflag(5) = 1
316 nlflag(6) = 1
317 nlflag(7) = 1
318 nlflag(8) = 0
319 nlflag(9) = 1
320 nlflag(10)= 0
321!
322! Value (Linear only)
323! x(1) x(2) x(3) x(4)
324! 1: 1 1
325! 2: NL NL
326! 3: NL NL 1
327! 4: NL NL 1
328!
329 value(1) = 1.d0
330 value(4) = 1.d0
331 value(8) = 1.d0
332 value(10) = 1.d0
333
334 sq_readmatrix = 0 ! Return value means OK
335
336end Function sq_readmatrix
337!
338!==========================================================================
339! Compute nonlinear terms and non-constant Jacobian elements
340!
341
342!> Compute nonlinear terms and non-constant Jacobian elements
343!!
344!! @include{doc} fdeval_params.dox
345Integer Function sq_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
346 n, nz, thread, usrmem )
347#if defined(itl)
348!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Sq_FDEval
349#endif
350 implicit none
351 integer, intent (in) :: n ! number of variables
352 integer, intent (in) :: rowno ! number of the row to be evaluated
353 integer, intent (in) :: nz ! number of nonzeros in this row
354 real*8, intent (in), dimension(n) :: x ! vector of current solution values
355 real*8, intent (in out) :: g ! constraint value
356 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
357 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
358 ! in this row. Ffor information only.
359 integer, intent (in) :: mode ! evaluation mode: 1 = function value
360 ! 2 = derivatives, 3 = both
361 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
362 ! as errcnt is incremented
363 integer, intent (in out) :: errcnt ! error counter to be incremented in case
364 ! of function evaluation errors.
365 integer, intent (in) :: thread
366 real*8 usrmem(*) ! optional user memory
367!
368! Row 1: Is declared as linear and should not be called.
369!
370 if ( rowno .eq. 1 ) then
371 sq_fdeval = 1
372 return
373!
374! Row 2: x1 + x2 assumed to be nonlinear
375!
376 elseif ( rowno .eq. 2 ) then
377!
378! Mode = 1 or 3: Function value
379!
380 if ( mode .eq. 1 .or. mode .eq. 3 ) then
381 g = x(1) - x(2)
382 endif
383!
384! Mode = 2 or 3: Derivatives
385!
386 if ( mode .eq. 2 .or. mode .eq. 3 ) then
387 jac(1) = 1.d0
388 jac(2) = -1.d0
389 endif
390 elseif ( rowno .eq. 3 ) then
391!
392! Mode = 1 or 3: Function value
393!
394 if ( mode .eq. 1 .or. mode .eq. 3 ) then
395 g = x(1) + x(2)
396 endif
397!
398! Mode = 2 or 3: Derivatives
399!
400 if ( mode .eq. 2 .or. mode .eq. 3 ) then
401 jac(1) = 1.d0
402 jac(2) = 1.d0
403 endif
404 elseif ( rowno .eq. 4 ) then
405!
406! Mode = 1 or 3: Function value
407!
408 if ( mode .eq. 1 .or. mode .eq. 3 ) then
409 g = -x(2) + x(3)
410 endif
411!
412! Mode = 2 or 3: Derivatives
413!
414 if ( mode .eq. 2 .or. mode .eq. 3 ) then
415 jac(2) = -1.d0
416 jac(3) = 1.d0
417 endif
418 endif
419 sq_fdeval = 0
420
421end 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