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