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