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!> Main program. A simple setup and call of CONOPT
19!!
20Program bound03
21
22 Use proginfo
23 Use coidef
24 implicit None
25!
26! Declare the user callback routines as Integer, External:
27!
28 Integer, External :: bound_readmatrix ! Mandatory Matrix definition routine defined below
29 Integer, External :: bound_fdeval ! Function and Derivative evaluation routine
30 ! needed a nonlinear model.
31 Integer, External :: std_status ! Standard callback for displaying solution status
32 Integer, External :: std_solution ! Standard callback for displaying solution values
33 Integer, External :: std_message ! Standard callback for managing messages
34 Integer, External :: std_errmsg ! Standard callback for managing error messages
35 Integer, External :: std_triord ! Standard callback for triangular order
36#if defined(itl)
37!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Bound_ReadMatrix
38!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Bound_FDEval
39!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
40!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
41!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
42!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
43!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_TriOrd
44#endif
45!
46! Control vector
47!
48 INTEGER :: numcallback
49 INTEGER, Dimension(:), Pointer :: cntvect
50 INTEGER :: coi_error
51
52 call startup
53!
54! Create and initialize a Control Vector
55!
56 numcallback = coidef_size()
57 Allocate( cntvect(numcallback) )
58 coi_error = coidef_inifort( 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, 2 ) ) ! # variables
63 coi_error = max( coi_error, coidef_numcon( cntvect, 5 ) ) ! # constraints
64 coi_error = max( coi_error, coidef_numnz( cntvect, 6 ) ) ! # nonzeros in the Jacobian
65 coi_error = max( coi_error, coidef_numnlnz( cntvect, 2 ) ) ! # of which are nonlinear
66 coi_error = max( coi_error, coidef_optdir( cntvect, -1 ) ) ! Minimize
67 coi_error = max( coi_error, coidef_objcon( cntvect, 5 ) ) ! Objective is constraint #
68 coi_error = max( coi_error, coidef_optfile( cntvect, 'bound03.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(LICENSE_INT_1) && defined(LICENSE_INT_2) && defined(LICENSE_INT_3) && defined(LICENSE_TEXT)
81 coi_error = max( coi_error, coidef_license( cntvect, license_int_1, license_int_2, license_int_3, 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 Bound03 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 /= 4 ) then ! Inconsistent bounds give global infeasibility
107 call flog( "Solver and Model Status was not as expected (1,4)", 1 )
108 elseif ( abs( xprim(2)-uprim(1) ) > 1.d-7 ) then
109 call flog( "Incorrect activity in row 1", 1 )
110 elseif ( abs( xprim(2)-uprim(2) ) > 1.d-7 ) then
111 call flog( "Incorrect activity in row 2", 1 )
112 elseif ( abs( xprim(1)-uprim(3) ) > 1.d-7 ) then
113 call flog( "Incorrect activity in row 3", 1 )
114 elseif ( abs( 2*xprim(2)-uprim(4) ) > 1.d-7 ) then
115 call flog( "Incorrect activity in row 4", 1 )
116 elseif ( abs( (xprim(1)-xprim(2))**2-uprim(5) ) > 1.d-7 ) then
117 call flog( "Incorrect activity in row 5", 1 )
118 Else
119 Call checkdual( 'Bound03', infeasible )
120 endif
121
122 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
123
124 call flog( "Successful Solve", 0 )
125
126End Program bound03
127!
128! ============================================================================
129! Define information about the model:
130!
131
132!> Define information about the model
133!!
134!! @include{doc} readMatrix_params.dox
135Integer Function bound_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
136 colsta, rowno, value, nlflag, n, m, nz, &
137 usrmem )
138#if defined(itl)
139!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Bound_ReadMatrix
140#endif
141 implicit none
142 integer, intent (in) :: n ! number of variables
143 integer, intent (in) :: m ! number of constraints
144 integer, intent (in) :: nz ! number of nonzeros
145 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
146 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
147 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
148 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
149 ! (not defined here)
150 integer, intent (out), dimension(m) :: type ! vector of equation types
151 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
152 ! (not defined here)
153 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
154 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
155 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
156 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
157 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
158 real*8 usrmem(*) ! optional user memory
159!
160! Information about Variables:
161! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
162! Default: the status information in Vsta is not used.
163!
164! The model uses defaults
165!
166! Information about Constraints:
167! Default: Rhs = 0
168! Default: the status information in Esta and the function
169! value in FV are not used.
170! Default: Type: There is no default.
171! 0 = Equality,
172! 1 = Greater than or equal,
173! 2 = Less than or equal,
174! 3 = Non binding.
175 integer, parameter :: equal = 0, greater = 1, less = 2, nonbnd = 3
176!
177! Constraint 1:
178!
179 rhs(1) = 4.0d0
180 type(1) = less
181!
182! Constraint 2:
183!
184 rhs(2) = 3.d0
185 type(2) = greater
186!
187! Constraint 3:
188!
189 rhs(3) = 1.d0
190 type(3) = greater
191!
192! Constraint 4:
193!
194 rhs(4) = 5.d0
195 type(4) = less
196!
197! Constraint 5:
198!
199 type(5) = nonbnd
200!
201! Information about the Jacobian. We use the standard method with
202! Rowno, Value, Nlflag and Colsta and we do not use Colno.
203!
204! Colsta = Start of column indices (No Defaults):
205! Rowno = Row indices
206! Value = Value of derivative (by default only linear
207! derivatives are used)
208! Nlflag = 0 for linear and 1 for nonlinear derivative
209! (not needed for completely linear models)
210!
211! Indices
212! x2 <= 4
213! x2 >= 3
214! x1 >= 1
215! 2*x2 <= 5
216! min sqr(x1-x2)
217! x(1) x(2)
218! 1: 3
219! 2: 4
220! 3: 1
221! 4: 5
222! 5: 2 6
223!
224 colsta(1) = 1
225 colsta(2) = 3
226 colsta(3) = 7
227 rowno(1) = 3
228 rowno(2) = 5
229 rowno(3) = 1
230 rowno(4) = 2
231 rowno(5) = 4
232 rowno(6) = 5
233!
234! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
235! x(1) x(2)
236! 1: L
237! 2: L
238! 3: L
239! 4: L
240! 5: NL NL
241!
242 nlflag(1) = 0
243 nlflag(2) = 1
244 nlflag(3) = 0
245 nlflag(4) = 0
246 nlflag(5) = 0
247 nlflag(6) = 1
248!
249! x2 <= 4
250! x2 >= 3
251! x1 >= 1
252! 2*x2 <= 5
253! min sqr(x1-x2)
254! Value (Linear only)
255! x(1) x(2)
256! 5: NL NL
257!
258 value(1) = 1.d0
259 value(3) = 1.d0
260 value(4) = 1.d0
261 value(5) = 2.d0
262
263 bound_readmatrix = 0 ! Return value means OK
264
265end Function bound_readmatrix
266!
267!==========================================================================
268! Compute nonlinear terms and non-constant Jacobian elements
269!
270
271!> Compute nonlinear terms and non-constant Jacobian elements
272!!
273!! @include{doc} fdeval_params.dox
274Integer Function bound_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
275 n, nz, thread, usrmem )
276#if defined(itl)
277!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Bound_FDEval
278#endif
279 implicit none
280 integer, intent (in) :: n ! number of variables
281 integer, intent (in) :: rowno ! number of the row to be evaluated
282 integer, intent (in) :: nz ! number of nonzeros in this row
283 real*8, intent (in), dimension(n) :: x ! vector of current solution values
284 real*8, intent (in out) :: g ! constraint value
285 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
286 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
287 ! in this row. Ffor information only.
288 integer, intent (in) :: mode ! evaluation mode: 1 = function value
289 ! 2 = derivatives, 3 = both
290 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
291 ! as errcnt is incremented
292 integer, intent (in out) :: errcnt ! error counter to be incremented in case
293 ! of function evaluation errors.
294 integer, intent (in) :: thread
295 real*8 usrmem(*) ! optional user memory
296!
297! Row 5 is nonlinear: sqr(x2-x1)
298!
299 if ( rowno .eq. 5 ) then
300!
301! Mode = 1 or 3. G = sqr(x1-1)
302!
303 if ( mode .eq. 1 .or. mode .eq. 3 ) then
304 g = (x(2)-x(1))*(x(2)-x(1))
305 endif
306!
307! Mode = 2 or 3: Derivative values:
308!
309 if ( mode .eq. 2 .or. mode .eq. 3 ) then
310 jac(1) = -2.d0*(x(2)-x(1))
311 jac(2) = +2.d0*(x(2)-x(1))
312 endif
313 bound_fdeval = 0
314!
315! The other rows are linear and will not be called
316!
317 else
318 bound_fdeval = 1
319 endif
320
321end 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 bound03
Main program. A simple setup and call of CONOPT.
Definition bound03.f90:20
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
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