CONOPT
Loading...
Searching...
No Matches
const15.f90
Go to the documentation of this file.
1!> @file const15.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! Model with derivatives that become constant after other variables are fixed.
6!!
7!! This is a CONOPT implementation of the GAMS model:
8!!
9!! @verbatim
10!! e1: max x1+x3
11!! e2: x1*x2 + sqr(x3) =l= -5
12!! x2.fx = 1;
13!! -5 <= x1 <= 10; x1.l = 3
14!! -10 <= x3 <= 10; x3.l = 3
15!! @endverbatim
16!!
17!! The model is similar to const14 but the right hand side is changed to -5. The feasible space
18!! has only one point, (x1,x3) = (-5,0), but it is not defined by sharp constraints.
19!! e1 is post-triangular.
20!! The expected solution is: x1 = -5 as a soft upper bound and x3 = 0 giving objective = -5.
21!! Since the marginal on e2 is infinite we will probably get a solution with x3 slightly positive.
22!! The solution should be labeled locally optimal.
23!!
24!!
25!! For more information about the individual callbacks, please have a look at the source code.
26
27#if defined(_WIN32) && !defined(_WIN64)
28#define dec_directives_win32
29#endif
30
31!> Main program. A simple setup and call of CONOPT
32!!
33Program const15
34
36 Use conopt
37 implicit None
38!
39! Declare the user callback routines as Integer, External:
40!
41 Integer, External :: con_readmatrix ! Mandatory Matrix definition routine defined below
42 Integer, External :: con_fdeval ! Function and Derivative evaluation routine
43 ! needed a nonlinear model.
44 Integer, External :: con_fdinterval ! Function and Derivative evaluation routine
45 ! optional for a nonlinear model.
46 Integer, External :: std_status ! Standard callback for displaying solution status
47 Integer, External :: std_solution ! Standard callback for displaying solution values
48 Integer, External :: std_message ! Standard callback for managing messages
49 Integer, External :: std_errmsg ! Standard callback for managing error messages
50#ifdef dec_directives_win32
51!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Con_ReadMatrix
52!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Con_FDEval
53!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Con_FDInterval
54!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
55!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
56!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
57!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
58#endif
59!
60! Control vector
61!
62 INTEGER, Dimension(:), Pointer :: cntvect
63 INTEGER :: coi_error
64!
65! Create and initialize a Control Vector
66!
67 call startup
68
69 coi_error = coi_create( cntvect )
70!
71! Tell CONOPT about the size of the model by populating the Control Vector:
72!
73 coi_error = max( coi_error, coidef_numvar( cntvect, 3 ) ) ! # variables
74 coi_error = max( coi_error, coidef_numcon( cntvect, 2 ) ) ! # constraints
75 coi_error = max( coi_error, coidef_numnz( cntvect, 5 ) ) ! # nonzeros in the Jacobian
76 coi_error = max( coi_error, coidef_numnlnz( cntvect, 3 ) ) ! # of which are nonlinear
77 coi_error = max( coi_error, coidef_optdir( cntvect, 1 ) ) ! Maximize
78 coi_error = max( coi_error, coidef_objcon( cntvect, 1 ) ) ! Objective is constraint 1
79 coi_error = max( coi_error, coidef_optfile( cntvect, 'const15.opt' ) )
80!
81! Tell CONOPT about the callback routines:
82!
83 coi_error = max( coi_error, coidef_readmatrix( cntvect, con_readmatrix ) )
84 coi_error = max( coi_error, coidef_fdeval( cntvect, con_fdeval ) )
85 coi_error = max( coi_error, coidef_fdinterval( cntvect, con_fdinterval ) )
86 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
87 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
88 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
89 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
90
91#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
92 coi_error = max( coi_error, coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_license_text) )
93#endif
94
95 If ( coi_error .ne. 0 ) THEN
96 write(*,*)
97 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
98 write(*,*)
99 call flog( "Skipping Solve due to setup errors", 1 )
100 ENDIF
101!
102! Save the solution so we can check the duals:
103!
104 do_allocate = .true.
105!
106! Start CONOPT:
107!
108 coi_error = coi_solve( cntvect )
109
110 write(*,*)
111 write(*,*) 'End of const15 example. Return code=',coi_error
112
113 If ( coi_error /= 0 ) then
114 call flog( "Errors encountered during solution", 1 )
115 elseif ( stacalls == 0 .or. solcalls == 0 ) then
116 call flog( "Status or Solution routine was not called", 1 )
117 elseif ( sstat /= 1 .or. mstat /= 2 ) then
118 call flog( "Solver and Model Status was not as expected (1,2)", 1 )
119 elseif ( abs( obj-(-5.0d0 ) ) > 0.001d0 ) then
120 call flog( "Incorrect objective returned", 1 )
121 Else
122 Call checkdual( 'const15', maximize )
123 endif
124
125 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
126
127 call flog( "Successful Solve", 0 )
129End Program const15
130!
131! ============================================================================
132! Define information about the model:
133!
134
135!> Define information about the model
136!!
137!! @include{doc} readMatrix_params.dox
138Integer Function con_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
139 colsta, rowno, value, nlflag, n, m, nz, &
140 usrmem )
141#ifdef dec_directives_win32
142!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Con_ReadMatrix
143#endif
144 implicit none
145 integer, intent (in) :: n ! number of variables
146 integer, intent (in) :: m ! number of constraints
147 integer, intent (in) :: nz ! number of nonzeros
148 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
149 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
150 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
151 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
152 ! (not defined here)
153 integer, intent (out), dimension(m) :: type ! vector of equation types
154 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
155 ! (not defined here)
156 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
157 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
158 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
159 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
160 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
161 real*8 usrmem(*) ! optional user memory
162!
163! Information about Variables:
164! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
165! Default: the status information in Vsta is not used.
166!
167 lower(1) = -5.0d0; curr(1) = 3.0d0; upper(1) = 10.0d0
168 lower(2) = 1.0d0; curr(2) = 1.0d0; upper(2) = 1.0d0
169 lower(3) = -10.0d0; curr(3) = 3.0d0; upper(3) = 10.0d0
170!
171! Information about Constraints:
172! Default: Rhs = 0
173! Default: the status information in Esta and the function
174! value in FV are not used.
175! Default: Type: There is no default.
176! 0 = Equality,
177! 1 = Greater than or equal,
178! 2 = Less than or equal,
179! 3 = Non binding.
180!
181 type(1) = 3
182 type(2) = 2
183 rhs(2) = -5.0d0
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) x(2) x(3)
197! 1: 1 4
198! 2: 2 3 5
199!
200 colsta(1) = 1
201 colsta(2) = 3
202 colsta(3) = 4
203 colsta(4) = 6
204 rowno(1) = 1
205 rowno(2) = 2
206 rowno(3) = 2
207 rowno(4) = 1
208 rowno(5) = 2
209!
210! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
211! x(1) x(2) x(3)
212! 1: L L
213! 2: NL NL NL
214!
215 nlflag(1) = 0
216 nlflag(2) = 1
217 nlflag(3) = 1
218 nlflag(4) = 0
219 nlflag(5) = 1
220!
221! Value (Linear only)
222! x(1) x(2) x(3)
223! 1: +1 +1
224! 2: NL NL NL
225!
226 value(1) = +1.d0
227 value(4) = +1.d0
228
229 con_readmatrix = 0 ! Return value means OK
230
231end Function con_readmatrix
232!
233!==========================================================================
234! Compute nonlinear terms and non-constant Jacobian elements
235!
236
237!> Compute nonlinear terms and non-constant Jacobian elements
238!!
239!! @include{doc} fdeval_params.dox
240Integer Function con_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
241 n, nz, thread, usrmem )
242#ifdef dec_directives_win32
243!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Con_FDEval
244#endif
245 implicit none
246 integer, intent (in) :: n ! number of variables
247 integer, intent (in) :: rowno ! number of the row to be evaluated
248 integer, intent (in) :: nz ! number of nonzeros in this row
249 real*8, intent (in), dimension(n) :: x ! vector of current solution values
250 real*8, intent (in out) :: g ! constraint value
251 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
252 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
253 ! in this row. Ffor information only.
254 integer, intent (in) :: mode ! evaluation mode: 1 = function value
255 ! 2 = derivatives, 3 = both
256 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
257 ! as errcnt is incremented
258 integer, intent (in out) :: errcnt ! error counter to be incremented in case
259 ! of function evaluation errors.
260 integer, intent (in) :: thread
261 real*8 usrmem(*) ! optional user memory
262!
263! Row 1: the objective function is nonlinear
264!
265 if ( rowno .eq. 2 ) then
266!
267! Mode = 1 or 3: Function value
268!
269 if ( mode .eq. 1 .or. mode .eq. 3 ) then
270 g = x(1)*x(2) + x(3)*x(3)
271 endif
272!
273! Mode = 2 or 3: Derivatives
274!
275 if ( mode .eq. 2 .or. mode .eq. 3 ) then
276 jac(1) = x(2)
277 jac(2) = x(1)
278 jac(3) = 2.0*x(3)
279 endif
280 con_fdeval = 0
281 Else
282 con_fdeval = 1 ! Should not happen
283 endif
284! e1: max x1+x3
285! e2: x1*x2 + sqr(x3) =l= 10
286
287end Function con_fdeval
288
289
290!> Evaluating nonlinear functions and derivatives on an interval. Used in preprocessing
291!!
292!! @include{doc} fdinterval_params.dox
293Integer Function con_fdinterval( XMIN, XMAX, GMIN, GMAX, &
294 JMIN, JMAX, ROWNO, JCNM, &
295 MODE, PINF, N, NJ, USRMEM )
296#ifdef dec_directives_win32
297!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Con_FDInterval
298#endif
299 Implicit None
300 INTEGER, Intent(IN) :: rowno, mode, n, nj
301 INTEGER, Dimension(NJ), Intent(IN) :: jcnm
302 real*8, Dimension(N), Intent(IN) :: xmin, xmax
303 real*8, Intent(IN OUT) :: gmin, gmax
304 real*8, Dimension(N), Intent(IN OUT) :: jmin, jmax
305 real*8, Intent(IN) :: pinf
306 real*8, Intent(IN OUT) :: usrmem(*)
307
308 real*8 :: bnd3
309!
310! Row 2: x1*x2+sqr(x3)
311!
312 if ( rowno .eq. 2 ) then
313!
314! Mode = 1 or 3. Function value
315!
316 if ( mode .eq. 1 .or. mode .eq. 3 ) then
317 bnd3 = min( max( xmin(3), 0.0d0 ), xmax(3) )
318 gmin = xmin(1)*xmin(2) + bnd3*bnd3
319 gmax = xmax(1)*xmax(2) + max(xmin(3)*xmin(3),xmax(3)*xmax(3))
320 endif
321!
322! Mode = 2 or 3: Derivative values:
323!
324 if ( mode .eq. 2 .or. mode .eq. 3 ) then
325 jmin(1) = xmin(2)
326 jmin(2) = xmin(1)
327 jmin(3) = 2.0d0*xmin(3)
328 jmax(1) = xmax(2)
329 jmax(2) = xmax(1)
330 jmax(3) = 2.0d0*xmax(3)
331 endif
333 else
334!
335! There are no other rows:
336!
338 endif
339
340end Function con_fdinterval
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_errmsg(rowno, colno, posno, msglen, usrmem, msg)
Definition comdecl.f90:248
integer function con_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
Definition const01.f90:127
integer function con_fdeval(x, g, jac, rowno, jcnm, mode, ignerr, errcnt, n, nz, thread, usrmem)
Compute nonlinear terms and non-constant Jacobian elements.
Definition const01.f90:229
integer function con_fdinterval(xmin, xmax, gmin, gmax, jmin, jmax, rowno, jcnm, mode, pinf, n, nj, usrmem)
Evaluating nonlinear functions and derivatives on an interval. Used in preprocessing.
Definition const01.f90:279
program const15
Main program. A simple setup and call of CONOPT.
Definition const15.f90:35
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_fdinterval(cntvect, coi_fdinterval)
define callback routine for performing function and derivative evaluations on intervals.
Definition conopt.f90:1396
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
#define nj
Definition mp_trans.c:46
real *8 obj
Definition comdecl.f90:16
integer solcalls
Definition comdecl.f90:15
integer sstat
Definition comdecl.f90:18
integer stacalls
Definition comdecl.f90:14
subroutine flog(msg, code)
Definition comdecl.f90:62
logical do_allocate
Definition comdecl.f90:27
integer, parameter maximize
Definition comdecl.f90:31
integer mstat
Definition comdecl.f90:17
subroutine startup
Definition comdecl.f90:41