CONOPT
Loading...
Searching...
No Matches
bound02.f90
Go to the documentation of this file.
1!> @file bound02.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! Model in which several simple inequalities are converted into simple
6!! bounds.
7!!
8!! \f{eqnarray*}{
9!! \min &sqr(x3-x1) \\
10!! &x1 + x2 <= 10 \\
11!! &x2 <= 4 \\
12!! &x2 >= 2 \\
13!! &x1 + x2 + x3 = 20 \\
14!! \\
15!! &x1.fx = 2
16!! \f}
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 bound02
24
25 Use proginfo
26 Use coidef
27 implicit None
28!
29! Declare the user callback routines as Integer, External:
30!
31 Integer, External :: bound_readmatrix ! Mandatory Matrix definition routine defined below
32 Integer, External :: bound_fdeval ! Function and Derivative evaluation routine
33 ! needed a nonlinear model.
34 Integer, External :: std_status ! Standard callback for displaying solution status
35 Integer, External :: std_solution ! Standard callback for displaying solution values
36 Integer, External :: std_message ! Standard callback for managing messages
37 Integer, External :: std_errmsg ! Standard callback for managing error messages
38 Integer, External :: std_triord ! Standard callback for triangular order
39#if defined(itl)
40!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Bound_ReadMatrix
41!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Bound_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!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_TriOrd
47#endif
48!
49! Control vector
50!
51 INTEGER :: numcallback
52 INTEGER, Dimension(:), Pointer :: cntvect
53 INTEGER :: coi_error
54
55 call startup
56!
57! Create and initialize a Control Vector
58!
59 numcallback = coidef_size()
60 Allocate( cntvect(numcallback) )
61 coi_error = coidef_inifort( cntvect )
62!
63! Tell CONOPT about the size of the model by populating the Control Vector:
64!
65 coi_error = max( coi_error, coidef_numvar( cntvect, 3 ) ) ! # variables
66 coi_error = max( coi_error, coidef_numcon( cntvect, 5 ) ) ! # constraints
67 coi_error = max( coi_error, coidef_numnz( cntvect, 9 ) ) ! # nonzeros in the Jacobian
68 coi_error = max( coi_error, coidef_numnlnz( cntvect, 2 ) ) ! # of which are nonlinear
69 coi_error = max( coi_error, coidef_optdir( cntvect, -1 ) ) ! Minimize
70 coi_error = max( coi_error, coidef_objcon( cntvect, 5 ) ) ! Objective is constraint 2
71 coi_error = max( coi_error, coidef_optfile( cntvect, 'bound02.opt' ) )
72!
73! Tell CONOPT about the callback routines:
74!
75 coi_error = max( coi_error, coidef_readmatrix( cntvect, bound_readmatrix ) )
76 coi_error = max( coi_error, coidef_fdeval( cntvect, bound_fdeval ) )
77 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
78 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
79 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
80 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
81 coi_error = max( coi_error, coidef_triord( cntvect, std_triord ) )
82
83#if defined(LICENSE_INT_1) && defined(LICENSE_INT_2) && defined(LICENSE_INT_3) && defined(LICENSE_TEXT)
84 coi_error = max( coi_error, coidef_license( cntvect, license_int_1, license_int_2, license_int_3, license_text) )
85#endif
86
87 If ( coi_error .ne. 0 ) THEN
88 write(*,*)
89 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
90 write(*,*)
91 call flog( "Skipping Solve due to setup errors", 1 )
92 ENDIF
93!
94! Save the solution so we can check the duals:
95!
96 do_allocate = .true.
97!
98! Start CONOPT:
99!
100 coi_error = coi_solve( cntvect )
101
102 write(*,*)
103 write(*,*) 'End of Bound02 example. Return code=',coi_error
104
105 If ( coi_error /= 0 ) then
106 call flog( "Errors encountered during solution", 1 )
107 elseif ( stacalls == 0 .or. solcalls == 0 ) then
108 call flog( "Status or Solution routine was not called", 1 )
109 elseif ( sstat /= 1 .or. mstat /= 2 ) then
110 call flog( "Solver and Model Status was not as expected (1,2)", 1 )
111 elseif ( abs( obj-144.0d0 ) > 0.000001d0 ) then
112 call flog( "Incorrect objective returned", 1 )
113 elseif ( abs( xprim(1)+xprim(2)-uprim(1) ) > 1.d-7 ) then
114 call flog( "Incorrect activity in row 1", 1 )
115 elseif ( abs( xprim(2)-uprim(2) ) > 1.d-7 ) then
116 call flog( "Incorrect activity in row 2", 1 )
117 elseif ( abs( xprim(2)-uprim(3) ) > 1.d-7 ) then
118 call flog( "Incorrect activity in row 3", 1 )
119 elseif ( abs( xprim(1)+xprim(2)+xprim(3)-uprim(4) ) > 1.d-7 ) then
120 call flog( "Incorrect activity in row 4", 1 )
121 Else
122 Call checkdual( 'Bound02', minimize )
123 endif
124
125 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
126
127 call flog( "Successful Solve", 0 )
128
129End Program bound02
130!
131! ============================================================================
132! Define information about the model:
133!
134
135!> Define information about the model
136!!
137!! @include{doc} readMatrix_params.dox
138Integer Function bound_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
139 colsta, rowno, value, nlflag, n, m, nz, &
140 usrmem )
141#if defined(itl)
142!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Bound_ReadMatrix
143#endif
144 implicit none
145 integer, intent (in) :: n ! number of variables
146 integer, intent (in) :: m ! number of constraints
147 integer, intent (in) :: nz ! number of nonzeros
148 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
149 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
150 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
151 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
152 ! (not defined here)
153 integer, intent (out), dimension(m) :: type ! vector of equation types
154 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
155 ! (not defined here)
156 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
157 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
158 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
159 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
160 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
161 real*8 usrmem(*) ! optional user memory
162!
163! Information about Variables:
164! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
165! Default: the status information in Vsta is not used.
166!
167! The model uses defaults
168!
169! Information about Constraints:
170! Default: Rhs = 0
171! Default: the status information in Esta and the function
172! value in FV are not used.
173! Default: Type: There is no default.
174! 0 = Equality,
175! 1 = Greater than or equal,
176! 2 = Less than or equal,
177! 3 = Non binding.
178 integer, parameter :: equal = 0, greater = 1, less = 2, nonbnd = 3
179 lower(1) = 2.d0
180 upper(1) = 2.d0
181 curr(1) = 2.d0
182!
183! Constraint 1:
184!
185 rhs(1) = 10.0d0
186 type(1) = less
187!
188! Constraint 2:
189!
190 rhs(2) = 4.d0
191 type(2) = less
192!
193! Constraint 3:
194!
195 rhs(3) = 2.d0
196 type(3) = greater
197!
198! Constraint 4:
199!
200 rhs(4) = 20.d0
201 type(4) = equal
202!
203! Constraint 5:
204!
205 type(5) = nonbnd
206!
207! Information about the Jacobian. We use the standard method with
208! Rowno, Value, Nlflag and Colsta and we do not use Colno.
209!
210! Colsta = Start of column indices (No Defaults):
211! Rowno = Row indices
212! Value = Value of derivative (by default only linear
213! derivatives are used)
214! Nlflag = 0 for linear and 1 for nonlinear derivative
215! (not needed for completely linear models)
216!
217! Indices
218! x(1) x(2) x(3)
219! 1: 1 4
220! 2: 5
221! 3: 6
222! 4: 2 7 8
223! 5: 3 9
224!
225 colsta(1) = 1
226 colsta(2) = 4
227 colsta(3) = 8
228 colsta(4) = 10
229 rowno(1) = 1
230 rowno(2) = 4
231 rowno(3) = 5
232 rowno(4) = 1
233 rowno(5) = 2
234 rowno(6) = 3
235 rowno(7) = 4
236 rowno(8) = 4
237 rowno(9) = 5
238!
239! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
240! x(1) x(2) x(3)
241! 1: L L
242! 2: L
243! 3: L
244! 4: L L L
245! 5 NL NL
246!
247 nlflag(1) = 0
248 nlflag(2) = 0
249 nlflag(3) = 1
250 nlflag(4) = 0
251 nlflag(5) = 0
252 nlflag(6) = 0
253 nlflag(7) = 0
254 nlflag(8) = 0
255 nlflag(9) = 1
256!
257! x1 + x2 <= 10
258! x2 <= 4
259! x2 >= 2
260! x1 + x2 + x3 = 20
261! min sqr(x3-x1)
262! Value (Linear only)
263! x(1) x(2) x(3)
264! 1: 1.0 1.0
265! 2: 1.0
266! 3: 1.0
267! 4: 1.0 1.0 1.0
268! 5: NL NL
269!
270 value(1) = 1.d0
271 value(2) = 1.d0
272 value(4) = 1.d0
273 value(5) = 1.d0
274 value(6) = 1.d0
275 value(7) = 1.d0
276 value(8) = 1.d0
277
278 bound_readmatrix = 0 ! Return value means OK
279
280end Function bound_readmatrix
281!
282!==========================================================================
283! Compute nonlinear terms and non-constant Jacobian elements
284!
285
286!> Compute nonlinear terms and non-constant Jacobian elements
287!!
288!! @include{doc} fdeval_params.dox
289Integer Function bound_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
290 n, nz, thread, usrmem )
291#if defined(itl)
292!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Bound_FDEval
293#endif
294 implicit none
295 integer, intent (in) :: n ! number of variables
296 integer, intent (in) :: rowno ! number of the row to be evaluated
297 integer, intent (in) :: nz ! number of nonzeros in this row
298 real*8, intent (in), dimension(n) :: x ! vector of current solution values
299 real*8, intent (in out) :: g ! constraint value
300 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
301 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
302 ! in this row. Ffor information only.
303 integer, intent (in) :: mode ! evaluation mode: 1 = function value
304 ! 2 = derivatives, 3 = both
305 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
306 ! as errcnt is incremented
307 integer, intent (in out) :: errcnt ! error counter to be incremented in case
308 ! of function evaluation errors.
309 integer, intent (in) :: thread
310 real*8 usrmem(*) ! optional user memory
311!
312! Row 5 is nonlinear: sqr(x3-x1)
313!
314 if ( rowno .eq. 5 ) then
315!
316! Mode = 1 or 3. G = sqr(x1-1)
317!
318 if ( mode .eq. 1 .or. mode .eq. 3 ) then
319 g = (x(3)-x(1))*(x(3)-x(1))
320 endif
321!
322! Mode = 2 or 3: Derivative values:
323!
324 if ( mode .eq. 2 .or. mode .eq. 3 ) then
325 jac(1) = -2.d0*(x(3)-x(1))
326 jac(3) = +2.d0*(x(3)-x(1))
327 endif
328 bound_fdeval = 0
329!
330! The other rows are linear and will not be called
331!
332 else
333 bound_fdeval = 1
334 endif
335
336end Function bound_fdeval
integer function bound_fdeval(x, g, jac, rowno, jcnm, mode, ignerr, errcnt, n, nz, thread, usrmem)
Compute nonlinear terms and non-constant Jacobian elements.
Definition bound01.f90:226
integer function bound_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
Definition bound01.f90:130
program bound02
Main program. A simple setup and call of CONOPT.
Definition bound02.f90:23
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
subroutine checkdual(case, minmax)
Definition comdecl.f90:365
integer function std_message(smsg, dmsg, nmsg, llen, usrmem, msgv)
Definition comdecl.f90:203
integer function std_triord(mode, type, status, irow, icol, inf, value, resid, usrmem)
Definition comdecl.f90:291
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_triord(cntvect, coi_triord)
define callback routine for providing the triangular order information.
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
real *8 obj
Definition comdecl.f90:10
integer solcalls
Definition comdecl.f90:9
integer sstat
Definition comdecl.f90:12
integer, parameter minimize
Definition comdecl.f90:25
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