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