CONOPT
Loading...
Searching...
No Matches
bound03.f90
Go to the documentation of this file.
1!> @file bound03.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! Model in which several simple inequalities are converted into simple
6!! bounds. The implied bounds are infeasible
7!!
8!! \f{eqnarray*}{
9!! \min &sqr(x1-x2) \\
10!! &x2 <= 4 \\
11!! &x2 >= 3 \\
12!! &x1 >= 1 \\
13!! &2*x2 <= 5
14!! \f}
15!!
16!! For more information about the individual callbacks, please have a look at the source code.
17
18#if defined(_WIN32) && !defined(_WIN64)
19#define dec_directives_win32
20#endif
21
22!> Main program. A simple setup and call of CONOPT
23!!
24Program bound03
25
27 Use conopt
28 implicit None
29!
30! Declare the user callback routines as Integer, External:
31!
32 Integer, External :: bound_readmatrix ! Mandatory Matrix definition routine defined below
33 Integer, External :: bound_fdeval ! Function and Derivative evaluation routine
34 ! needed a nonlinear model.
35 Integer, External :: std_status ! Standard callback for displaying solution status
36 Integer, External :: std_solution ! Standard callback for displaying solution values
37 Integer, External :: std_message ! Standard callback for managing messages
38 Integer, External :: std_errmsg ! Standard callback for managing error messages
39 Integer, External :: std_triord ! Standard callback for triangular order
40#ifdef dec_directives_win32
41!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Bound_ReadMatrix
42!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Bound_FDEval
43!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
44!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
45!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
46!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
47!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_TriOrd
48#endif
49!
50! Control vector
51!
52 INTEGER, Dimension(:), Pointer :: cntvect
53 INTEGER :: coi_error
54
55 call startup
56!
57! Create and initialize a Control Vector
58!
59 coi_error = coi_create( 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, 2 ) ) ! # variables
64 coi_error = max( coi_error, coidef_numcon( cntvect, 5 ) ) ! # constraints
65 coi_error = max( coi_error, coidef_numnz( cntvect, 6 ) ) ! # nonzeros in the Jacobian
66 coi_error = max( coi_error, coidef_numnlnz( cntvect, 2 ) ) ! # of which are nonlinear
67 coi_error = max( coi_error, coidef_optdir( cntvect, -1 ) ) ! Minimize
68 coi_error = max( coi_error, coidef_objcon( cntvect, 5 ) ) ! Objective is constraint #
69 coi_error = max( coi_error, coidef_optfile( cntvect, 'bound03.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(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
82 coi_error = max( coi_error, coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_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 Bound03 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( 'Bound03', 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 )
127End Program bound03
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#ifdef dec_directives_win32
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) = 4.0d0
181 type(1) = less
182!
183! Constraint 2:
184!
185 rhs(2) = 3.d0
186 type(2) = greater
187!
188! Constraint 3:
189!
190 rhs(3) = 1.d0
191 type(3) = greater
192!
193! Constraint 4:
194!
195 rhs(4) = 5.d0
196 type(4) = less
197!
198! Constraint 5:
199!
200 type(5) = nonbnd
201!
202! Information about the Jacobian. CONOPT expects a columnwise
203! representation in Rowno, Value, Nlflag and Colsta.
204!
205! Colsta = Start of column indices (No Defaults):
206! Rowno = Row indices
207! Value = Value of derivative (by default only linear
208! derivatives are used)
209! Nlflag = 0 for linear and 1 for nonlinear derivative
210! (not needed for completely linear models)
211!
212! Indices
213! x2 <= 4
214! x2 >= 3
215! x1 >= 1
216! 2*x2 <= 5
217! min sqr(x1-x2)
218! x(1) x(2)
219! 1: 3
220! 2: 4
221! 3: 1
222! 4: 5
223! 5: 2 6
224!
225 colsta(1) = 1
226 colsta(2) = 3
227 colsta(3) = 7
228 rowno(1) = 3
229 rowno(2) = 5
230 rowno(3) = 1
231 rowno(4) = 2
232 rowno(5) = 4
233 rowno(6) = 5
234!
235! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
236! x(1) x(2)
237! 1: L
238! 2: L
239! 3: L
240! 4: L
241! 5: NL NL
242!
243 nlflag(1) = 0
244 nlflag(2) = 1
245 nlflag(3) = 0
246 nlflag(4) = 0
247 nlflag(5) = 0
248 nlflag(6) = 1
249!
250! x2 <= 4
251! x2 >= 3
252! x1 >= 1
253! 2*x2 <= 5
254! min sqr(x1-x2)
255! Value (Linear only)
256! x(1) x(2)
257! 5: NL NL
258!
259 value(1) = 1.d0
260 value(3) = 1.d0
261 value(4) = 1.d0
262 value(5) = 2.d0
263
264 bound_readmatrix = 0 ! Return value means OK
265
266end Function bound_readmatrix
267!
268!==========================================================================
269! Compute nonlinear terms and non-constant Jacobian elements
270!
271
272!> Compute nonlinear terms and non-constant Jacobian elements
273!!
274!! @include{doc} fdeval_params.dox
275Integer Function bound_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
276 n, nz, thread, usrmem )
277#ifdef dec_directives_win32
278!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Bound_FDEval
279#endif
280 implicit none
281 integer, intent (in) :: n ! number of variables
282 integer, intent (in) :: rowno ! number of the row to be evaluated
283 integer, intent (in) :: nz ! number of nonzeros in this row
284 real*8, intent (in), dimension(n) :: x ! vector of current solution values
285 real*8, intent (in out) :: g ! constraint value
286 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
287 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
288 ! in this row. Ffor information only.
289 integer, intent (in) :: mode ! evaluation mode: 1 = function value
290 ! 2 = derivatives, 3 = both
291 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
292 ! as errcnt is incremented
293 integer, intent (in out) :: errcnt ! error counter to be incremented in case
294 ! of function evaluation errors.
295 integer, intent (in) :: thread
296 real*8 usrmem(*) ! optional user memory
297!
298! Row 5 is nonlinear: sqr(x2-x1)
299!
300 if ( rowno .eq. 5 ) then
301!
302! Mode = 1 or 3. G = sqr(x1-1)
303!
304 if ( mode .eq. 1 .or. mode .eq. 3 ) then
305 g = (x(2)-x(1))*(x(2)-x(1))
306 endif
307!
308! Mode = 2 or 3: Derivative values:
309!
310 if ( mode .eq. 2 .or. mode .eq. 3 ) then
311 jac(1) = -2.d0*(x(2)-x(1))
312 jac(2) = +2.d0*(x(2)-x(1))
313 endif
314 bound_fdeval = 0
315!
316! The other rows are linear and will not be called
317!
318 else
319 bound_fdeval = 1
320 endif
321
322end 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 bound03
Main program. A simple setup and call of CONOPT.
Definition bound03.f90:26
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
integer solcalls
Definition comdecl.f90:15
integer sstat
Definition comdecl.f90:18
integer, parameter infeasible
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