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[
9!! \min \quad \sqrt{x1-1}
10!! \f]
11!! \f[
12!! x1 \geq 5
13!! \f]
14!!
15!! For more information about the individual callbacks, please have a look at the source code.
16
17#if defined(_WIN32) && !defined(_WIN64)
18#define dec_directives_win32
19#endif
20
21!> Main program. A simple setup and call of CONOPT
22!!
23Program bound01
24
26 Use conopt
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#ifdef dec_directives_win32
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, Dimension(:), Pointer :: cntvect
52 INTEGER :: coi_error
53
54 call startup
55!
56! Create and initialize a Control Vector
57!
58 coi_error = coi_create( cntvect )
59!
60! Tell CONOPT about the size of the model by populating the Control Vector:
61!
62 coi_error = max( coi_error, coidef_numvar( cntvect, 1 ) ) ! # variables
63 coi_error = max( coi_error, coidef_numcon( cntvect, 2 ) ) ! # constraints
64 coi_error = max( coi_error, coidef_numnz( cntvect, 2 ) ) ! # nonzeros in the Jacobian
65 coi_error = max( coi_error, coidef_numnlnz( cntvect, 1 ) ) ! # of which are nonlinear
66 coi_error = max( coi_error, coidef_optdir( cntvect, -1 ) ) ! Minimize
67 coi_error = max( coi_error, coidef_objcon( cntvect, 2 ) ) ! Objective is constraint 2
68 coi_error = max( coi_error, coidef_optfile( cntvect, 'bound01.opt' ) )
69!
70! Tell CONOPT about the callback routines:
71!
72 coi_error = max( coi_error, coidef_readmatrix( cntvect, bound_readmatrix ) )
73 coi_error = max( coi_error, coidef_fdeval( cntvect, bound_fdeval ) )
74 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
75 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
76 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
77 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
78 coi_error = max( coi_error, coidef_triord( cntvect, std_triord ) )
79
80#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
81 coi_error = max( coi_error, coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_license_text) )
82#endif
83
84 If ( coi_error .ne. 0 ) THEN
85 write(*,*)
86 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
87 write(*,*)
88 call flog( "Skipping Solve due to setup errors", 1 )
89 ENDIF
90!
91! Save the solution so we can check the duals:
92!
93 do_allocate = .true.
94!
95! Start CONOPT:
96!
97 coi_error = coi_solve( cntvect )
98
99 write(*,*)
100 write(*,*) 'End of Bound01 example. Return code=',coi_error
101
102 If ( coi_error /= 0 ) then
103 call flog( "Errors encountered during solution", 1 )
104 elseif ( stacalls == 0 .or. solcalls == 0 ) then
105 call flog( "Status or Solution routine was not called", 1 )
106 elseif ( sstat /= 1 .or. mstat /= 2 ) then
107 call flog( "Solver and Model Status was not as expected (1,2)", 1 )
108 elseif ( abs( obj-16.0d0 ) > 0.000001d0 ) then
109 call flog( "Incorrect objective returned", 1 )
110 elseif ( abs( xprim(1)-uprim(1) ) > 1.d-7 ) then
111 call flog( "Incorrect activity in row 1", 1 )
112 Else
113 Call checkdual( 'Bound01', minimize )
114 endif
115
116 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
117
118 call flog( "Successful Solve", 0 )
119!
120! Free solution memory
121!
122 call finalize
124End Program bound01
125!
126! ============================================================================
127! Define information about the model:
128!
129
130!> Define information about the model
131!!
132!! @include{doc} readMatrix_params.dox
133Integer Function bound_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
134 colsta, rowno, value, nlflag, n, m, nz, &
135 usrmem )
136#ifdef dec_directives_win32
137!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Bound_ReadMatrix
138#endif
139 implicit none
140 integer, intent (in) :: n ! number of variables
141 integer, intent (in) :: m ! number of constraints
142 integer, intent (in) :: nz ! number of nonzeros
143 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
144 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
145 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
146 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
147 ! (not defined here)
148 integer, intent (out), dimension(m) :: type ! vector of equation types
149 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
150 ! (not defined here)
151 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
152 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
153 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
154 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
155 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
156 real*8 usrmem(*) ! optional user memory
157!
158! Information about Variables:
159! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
160! Default: the status information in Vsta is not used.
161!
162! The model uses defaults
163!
164! Information about Constraints:
165! Default: Rhs = 0
166! Default: the status information in Esta and the function
167! value in FV are not used.
168! Default: Type: There is no default.
169! 0 = Equality,
170! 1 = Greater than or equal,
171! 2 = Less than or equal,
172! 3 = Non binding.
173!
174! Constraint 1: e1
175! Rhs = 5.0 and type Greater than
176!
177 rhs(1) = 5.0d0
178 type(1) = 1
179!
180! Constraint 2: e2
181! Rhs = 4.0 and type Equality
182!
183 type(2) = 3
184!
185! Information about the Jacobian. CONOPT expects a columnwise
186! representation in Rowno, Value, Nlflag and Colsta.
187!
188! Colsta = Start of column indices (No Defaults):
189! Rowno = Row indices
190! Value = Value of derivative (by default only linear
191! derivatives are used)
192! Nlflag = 0 for linear and 1 for nonlinear derivative
193! (not needed for completely linear models)
194!
195! Indices
196! x(1)
197! 1: 1
198! 2: 2
199!
200 colsta(1) = 1
201 colsta(2) = 3
202 rowno(1) = 1
203 rowno(2) = 2
204!
205! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
206! x(1)
207! 1: L
208! 2: NL
209!
210 nlflag(1) = 0
211 nlflag(2) = 1
212!
213! Value (Linear only)
214! x(1)
215! 1: 1.0
216!
217 value(1) = 1.d0
218
219 bound_readmatrix = 0 ! Return value means OK
220
221end Function bound_readmatrix
222!
223!==========================================================================
224! Compute nonlinear terms and non-constant Jacobian elements
225!
226
227!> Compute nonlinear terms and non-constant Jacobian elements
228!!
229!! @include{doc} fdeval_params.dox
230Integer Function bound_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
231 n, nz, thread, usrmem )
232#ifdef dec_directives_win32
233!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Bound_FDEval
234#endif
235 implicit none
236 integer, intent (in) :: n ! number of variables
237 integer, intent (in) :: rowno ! number of the row to be evaluated
238 integer, intent (in) :: nz ! number of nonzeros in this row
239 real*8, intent (in), dimension(n) :: x ! vector of current solution values
240 real*8, intent (in out) :: g ! constraint value
241 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
242 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
243 ! in this row. Ffor information only.
244 integer, intent (in) :: mode ! evaluation mode: 1 = function value
245 ! 2 = derivatives, 3 = both
246 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
247 ! as errcnt is incremented
248 integer, intent (in out) :: errcnt ! error counter to be incremented in case
249 ! of function evaluation errors.
250 integer, intent (in) :: thread !
251 real*8 usrmem(*) ! optional user memory
252!
253! Row 1: e1
254!
255 if ( rowno .eq. 2 ) then
256!
257! Mode = 1 or 3. G = sqr(x1-1)
258!
259 if ( mode .eq. 1 .or. mode .eq. 3 ) then
260 g = (x(1)-1.d0)*(x(1)-1.d0)
261 endif
262!
263! Mode = 2 or 3: Derivative values:
264!
265 if ( mode .eq. 2 .or. mode .eq. 3 ) then
266 jac(1) = 2.d0*(x(1)-1.d0)
267 endif
268 bound_fdeval = 0
269!
270! Row 2 and 3: The rows are linear and will not be called
271!
272 else
273 bound_fdeval = 1
274 endif
275
276end 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:219
program bound01
Main program. A simple setup and call of CONOPT.
Definition bound01.f90:25
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:126
integer function std_solution(xval, xmar, xbas, xsta, yval, ymar, ybas, ysta, n, m, usrmem)
Definition comdecl.f90:170
integer function std_status(modsta, solsta, iter, objval, usrmem)
Definition comdecl.f90:126
subroutine checkdual(case, minmax)
Definition comdecl.f90:432
integer function std_message(smsg, dmsg, nmsg, llen, usrmem, msgv)
Definition comdecl.f90:243
integer function std_triord(mode, type, status, irow, icol, inf, value, resid, usrmem)
Definition comdecl.f90:327
integer function std_errmsg(rowno, colno, posno, msglen, usrmem, msg)
Definition comdecl.f90:286
integer(c_int) function coidef_message(cntvect, coi_message)
define callback routine for handling messages returned during the solution process.
Definition conopt.f90:1265
integer(c_int) function coidef_solution(cntvect, coi_solution)
define callback routine for returning the final solution values.
Definition conopt.f90:1238
integer(c_int) function coidef_status(cntvect, coi_status)
define callback routine for returning the completion status.
Definition conopt.f90:1212
integer(c_int) function coidef_readmatrix(cntvect, coi_readmatrix)
define callback routine for providing the matrix data to CONOPT.
Definition conopt.f90:1111
integer(c_int) function coidef_errmsg(cntvect, coi_errmsg)
define callback routine for returning error messages for row, column or Jacobian elements.
Definition conopt.f90:1291
integer(c_int) function coidef_fdeval(cntvect, coi_fdeval)
define callback routine for performing function and derivative evaluations.
Definition conopt.f90:1135
integer(c_int) function coidef_optfile(cntvect, optfile)
define callback routine for defining an options file.
Definition conopt.f90:928
integer(c_int) function coidef_triord(cntvect, coi_triord)
define callback routine for providing the triangular order information.
Definition conopt.f90:1371
integer(c_int) function coidef_license(cntvect, licint1, licint2, licint3, licstring)
define the License Information.
Definition conopt.f90:293
integer(c_int) function coidef_numvar(cntvect, numvar)
defines the number of variables in the model.
Definition conopt.f90:97
integer(c_int) function coidef_numcon(cntvect, numcon)
defines the number of constraints in the model.
Definition conopt.f90:121
integer(c_int) function coidef_numnlnz(cntvect, numnlnz)
defines the Number of Nonlinear Nonzeros.
Definition conopt.f90:167
integer(c_int) function coidef_optdir(cntvect, optdir)
defines the Optimization Direction.
Definition conopt.f90:213
integer(c_int) function coidef_numnz(cntvect, numnz)
defines the number of nonzero elements in the Jacobian.
Definition conopt.f90:144
integer(c_int) function coidef_objcon(cntvect, objcon)
defines the Objective Constraint.
Definition conopt.f90:239
integer(c_int) function coi_create(cntvect)
initializes CONOPT and creates the control vector.
Definition conopt.f90:1726
integer(c_int) function coi_free(cntvect)
frees the control vector.
Definition conopt.f90:1749
integer(c_int) function coi_solve(cntvect)
method for starting the solving process of CONOPT.
Definition conopt.f90:1625
real *8 obj
Definition comdecl.f90:16
integer solcalls
Definition comdecl.f90:15
integer sstat
Definition comdecl.f90:18
subroutine finalize
Definition comdecl.f90:79
integer, parameter minimize
Definition comdecl.f90:31
integer stacalls
Definition comdecl.f90:14
subroutine flog(msg, code)
Definition comdecl.f90:62
logical do_allocate
Definition comdecl.f90:27
real *8, dimension(:), pointer xprim
Definition comdecl.f90:23
real *8, dimension(:), pointer uprim
Definition comdecl.f90:24
integer mstat
Definition comdecl.f90:17
subroutine startup
Definition comdecl.f90:41