CONOPT
Loading...
Searching...
No Matches
const01.f90
Go to the documentation of this file.
1!> @file const01.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 + x3*x4 =E= 20
12!! x2.fx = 1; x4.fx = 2;
13!! 2 <= x1 <= 10; x1.l = 5
14!! 2 <= x3 <= 10; x3.l = 5
15!! @endverbatim
16!!
17!! In this model `e1` is the post-triangular objective.
18!! `e2` can be solved w.r.t. `x3` so the constraint is post-triangular
19!!
20!!
21!! For more information about the individual callbacks, please have a look at the source code.
22
23#if defined(_WIN32) && !defined(_WIN64)
24#define dec_directives_win32
25#endif
26
27!> Main program. A simple setup and call of CONOPT
28!!
29Program const01
30
32 Use conopt
33 implicit None
34!
35! Declare the user callback routines as Integer, External:
36!
37 Integer, External :: con_readmatrix ! Mandatory Matrix definition routine defined below
38 Integer, External :: con_fdeval ! Function and Derivative evaluation routine
39 ! needed a nonlinear model.
40 Integer, External :: con_fdinterval ! Function and Derivative evaluation routine
41 ! optional for a nonlinear model.
42 Integer, External :: std_status ! Standard callback for displaying solution status
43 Integer, External :: std_solution ! Standard callback for displaying solution values
44 Integer, External :: std_message ! Standard callback for managing messages
45 Integer, External :: std_errmsg ! Standard callback for managing error messages
46#ifdef dec_directives_win32
47!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Con_ReadMatrix
48!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Con_FDEval
49!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Con_FDInterval
50!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
51!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
52!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
53!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
54#endif
55!
56! Control vector
57!
58 INTEGER, Dimension(:), Pointer :: cntvect
59 INTEGER :: coi_error
60!
61! Create and initialize a Control Vector
62!
63 call startup
64
65 coi_error = coi_create( cntvect )
66!
67! Tell CONOPT about the size of the model by populating the Control Vector:
68!
69 coi_error = max( coi_error, coidef_numvar( cntvect, 4 ) ) ! # variables
70 coi_error = max( coi_error, coidef_numcon( cntvect, 2 ) ) ! # constraints
71 coi_error = max( coi_error, coidef_numnz( cntvect, 6 ) ) ! # nonzeros in the Jacobian
72 coi_error = max( coi_error, coidef_numnlnz( cntvect, 4 ) ) ! # of which are nonlinear
73 coi_error = max( coi_error, coidef_optdir( cntvect, 1 ) ) ! Maximize
74 coi_error = max( coi_error, coidef_objcon( cntvect, 1 ) ) ! Objective is constraint 1
75 coi_error = max( coi_error, coidef_optfile( cntvect, 'const01.opt' ) )
76!
77! Tell CONOPT about the callback routines:
78!
79 coi_error = max( coi_error, coidef_readmatrix( cntvect, con_readmatrix ) )
80 coi_error = max( coi_error, coidef_fdeval( cntvect, con_fdeval ) )
81 coi_error = max( coi_error, coidef_fdinterval( cntvect, con_fdinterval ) )
82 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
83 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
84 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
85 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
86
87#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
88 coi_error = max( coi_error, coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_license_text) )
89#endif
90
91 If ( coi_error .ne. 0 ) THEN
92 write(*,*)
93 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
94 write(*,*)
95 call flog( "Skipping Solve due to setup errors", 1 )
96 ENDIF
97!
98! Save the solution so we can check the duals:
99!
100 do_allocate = .true.
101!
102! Start CONOPT:
103!
104 coi_error = coi_solve( cntvect )
105
106 write(*,*)
107 write(*,*) 'End of const01 example. Return code=',coi_error
108
109 If ( coi_error /= 0 ) then
110 call flog( "Errors encountered during solution", 1 )
111 elseif ( stacalls == 0 .or. solcalls == 0 ) then
112 call flog( "Status or Solution routine was not called", 1 )
113 elseif ( sstat /= 1 .or. mstat /= 1 ) then
114 call flog( "Solver and Model Status was not as expected (1,1)", 1 )
115 elseif ( abs( obj-15.0d0 ) > 0.000001d0 ) then
116 call flog( "Incorrect objective returned", 1 )
117 Else
118 Call checkdual( 'const01', maximize )
119 endif
120
121 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
122
123 call flog( "Successful Solve", 0 )
125End Program const01
126!
127! ============================================================================
128! Define information about the model:
129!
130
131!> Define information about the model
132!!
133!! @include{doc} readMatrix_params.dox
134Integer Function con_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
135 colsta, rowno, value, nlflag, n, m, nz, &
136 usrmem )
137#ifdef dec_directives_win32
138!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Con_ReadMatrix
139#endif
140 implicit none
141 integer, intent (in) :: n ! number of variables
142 integer, intent (in) :: m ! number of constraints
143 integer, intent (in) :: nz ! number of nonzeros
144 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
145 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
146 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
147 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
148 ! (not defined here)
149 integer, intent (out), dimension(m) :: type ! vector of equation types
150 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
151 ! (not defined here)
152 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
153 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
154 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
155 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
156 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
157 real*8 usrmem(*) ! optional user memory
158!
159! Information about Variables:
160! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
161! Default: the status information in Vsta is not used.
162!
163 lower(1) = 2.0d0; curr(1) = 5.0d0; upper(1) = 10.0d0
164 lower(2) = 1.0d0; curr(2) = 1.0d0; upper(2) = 1.0d0
165 lower(3) = 2.0d0; curr(3) = 5.0d0; upper(3) = 10.0d0
166 lower(4) = 2.0d0; curr(4) = 2.0d0; upper(4) = 2.0d0
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!
178 type(1) = 3
179 type(2) = 0
180 rhs(2) = 20.d0
181!
182! Information about the Jacobian. CONOPT expects a columnwise
183! representation in Rowno, Value, Nlflag and Colsta.
184!
185! Colsta = Start of column indices (No Defaults):
186! Rowno = Row indices
187! Value = Value of derivative (by default only linear
188! derivatives are used)
189! Nlflag = 0 for linear and 1 for nonlinear derivative
190! (not needed for completely linear models)
191!
192! Indices
193! x(1) x(2) x(3) x(4)
194! 1: 1 4
195! 2: 2 3 5 6
196!
197 colsta(1) = 1
198 colsta(2) = 3
199 colsta(3) = 4
200 colsta(4) = 6
201 colsta(5) = 7
202 rowno(1) = 1
203 rowno(2) = 2
204 rowno(3) = 2
205 rowno(4) = 1
206 rowno(5) = 2
207 rowno(6) = 2
208!
209! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
210! x(1) x(2) x(3) x(4)
211! 1: L L
212! 2: NL NL NL NL
213!
214 nlflag(1) = 0
215 nlflag(2) = 1
216 nlflag(3) = 1
217 nlflag(4) = 0
218 nlflag(5) = 1
219 nlflag(6) = 1
220!
221! Value (Linear only)
222! x(1) x(2) x(3) x(4)
223! 1: +1 +1
224! 2: NL 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(4)
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) = x(4)
279 jac(4) = x(3)
280 endif
281 con_fdeval = 0
282 Else
283 con_fdeval = 1 ! Should not happen
284 endif
285
286end Function con_fdeval
287
288
289!> Evaluating nonlinear functions and derivatives on an interval. Used in preprocessing
290!!
291!! @include{doc} fdinterval_params.dox
292Integer Function con_fdinterval( XMIN, XMAX, GMIN, GMAX, &
293 JMIN, JMAX, ROWNO, JCNM, &
294 MODE, PINF, N, NJ, USRMEM )
295#ifdef dec_directives_win32
296!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Con_FDInterval
297#endif
298 Implicit None
299 INTEGER, Intent(IN) :: rowno, mode, n, nj
300 INTEGER, Dimension(NJ), Intent(IN) :: jcnm
301 real*8, Dimension(N), Intent(IN) :: xmin, xmax
302 real*8, Intent(IN OUT) :: gmin, gmax
303 real*8, Dimension(N), Intent(IN OUT) :: jmin, jmax
304 real*8, Intent(IN) :: pinf
305 real*8, Intent(IN OUT) :: usrmem(*)
306
307!
308! Row 2: x1*x2+x3*x4 ! with known positive values
309!
310 if ( rowno .eq. 2 ) then
311!
312! Mode = 1 or 3. Function
313!
314 if ( mode .eq. 1 .or. mode .eq. 3 ) then
315 gmin = xmin(1)*xmin(2) + xmin(3)*xmin(4)
316 gmax = xmax(1)*xmax(2) + xmax(3)*xmax(4)
317 endif
318!
319! Mode = 2 or 3: Derivative values:
320!
321 if ( mode .eq. 2 .or. mode .eq. 3 ) then
322 jmin(1) = xmin(2)
323 jmin(2) = xmin(1)
324 jmin(3) = xmin(4)
325 jmin(4) = xmin(3)
326 jmax(1) = xmax(2)
327 jmax(2) = xmax(1)
328 jmax(3) = xmax(4)
329 jmax(4) = xmax(3)
330 endif
332 else
333!
334! There are no other rows:
335!
337 endif
338
339end 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
program const01
Main program. A simple setup and call of CONOPT.
Definition const01.f90:31
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
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