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