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