CONOPT
Loading...
Searching...
No Matches
tutorial.f90
Go to the documentation of this file.
1!> @file tutorial.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!! @brief a tutorial providing an introduction to the CONOPT API
4!!
5!! For more information about the individual callbacks, please have a look at the source code.
6
7#if defined(_WIN32) && !defined(_WIN64)
8#define dec_directives_win32
9#endif
10
11!> Main program. A simple setup and call of CONOPT
12!!
13Program tutorial
14
16 Use conopt
17 implicit None
18!
19! Declare the user callback routines as Integer, External:
20!
21 Integer, External :: tut_readmatrix ! Mandatory Matrix definition routine defined below
22 Integer, External :: tut_fdeval ! Function and Derivative evaluation routine
23 ! needed a nonlinear model.
24 Integer, External :: std_status ! Standard callback for displaying solution status
25 Integer, External :: std_solution ! Standard callback for displaying solution values
26 Integer, External :: std_message ! Standard callback for managing messages
27 Integer, External :: std_errmsg ! Standard callback for managing error messages
28#ifdef dec_directives_win32
29!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Tut_ReadMatrix
30!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Tut_FDEval
31!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
32!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
33!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
34!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
35#endif
36!
37! Control vector
38!
39 INTEGER, Dimension(:), Pointer :: cntvect
40 INTEGER :: coi_error
41!
42! Create and initialize a Control Vector
43!
44 call startup
45
46 coi_error = coi_create( cntvect )
47!
48! Tell CONOPT about the size of the model by populating the Control Vector:
49!
50 coi_error = max( coi_error, coidef_numvar( cntvect, 4 ) ) ! 4 variables
51 coi_error = max( coi_error, coidef_numcon( cntvect, 3 ) ) ! 3 constraints
52 coi_error = max( coi_error, coidef_numnz( cntvect, 9 ) ) ! 9 nonzeros in the Jacobian
53 coi_error = max( coi_error, coidef_numnlnz( cntvect, 4 ) ) ! 4 of which are nonlinear
54 coi_error = max( coi_error, coidef_optdir( cntvect, 1 ) ) ! Maximize
55 coi_error = max( coi_error, coidef_objcon( cntvect, 1 ) ) ! Objective is constraint 1
56 coi_error = max( coi_error, coidef_optfile( cntvect, 'tutorial.opt' ) )
57!
58! Tell CONOPT about the callback routines:
59!
60 coi_error = max( coi_error, coidef_readmatrix( cntvect, tut_readmatrix ) )
61 coi_error = max( coi_error, coidef_fdeval( cntvect, tut_fdeval ) )
62 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
63 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
64 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
65 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
66
67#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
68 coi_error = max( coi_error, coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_license_text) )
69#endif
70
71 If ( coi_error .ne. 0 ) THEN
72 write(*,*)
73 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
74 write(*,*)
75 call flog( "Skipping Solve due to setup errors", 1 )
76 ENDIF
77!
78! Save the solution so we can check the duals:
79!
80 do_allocate = .true.
81!
82! Start CONOPT:
83!
84 coi_error = coi_solve( cntvect )
85
86 write(*,*)
87 write(*,*) 'End of Tutorial example. Return code=',coi_error
88
89 If ( coi_error /= 0 ) then
90 call flog( "Errors encountered during solution", 1 )
91 elseif ( stacalls == 0 .or. solcalls == 0 ) then
92 call flog( "Status or Solution routine was not called", 1 )
93 elseif ( sstat /= 1 .or. mstat /= 2 ) then
94 call flog( "Solver and Model Status was not as expected (1,2)", 1 )
95 elseif ( abs( obj-0.572943d0 ) > 0.000001d0 ) then
96 call flog( "Incorrect objective returned", 1 )
97 Else
98 Call checkdual( 'Tutorial', maximize )
99 endif
100
101 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
102
103 call flog( "Successful Solve", 0 )
104
105!
106! Free solution memory
107!
108 call finalize
109
110End Program tutorial
111
112!> Define information about the model
113!!
114!! @include{doc} readMatrix_params.dox
115Integer Function tut_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
116 colsta, rowno, value, nlflag, n, m, nz, &
117 usrmem )
118#ifdef dec_directives_win32
119!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Tut_ReadMatrix
120#endif
121 implicit none
122 integer, intent (in) :: n ! number of variables
123 integer, intent (in) :: m ! number of constraints
124 integer, intent (in) :: nz ! number of nonzeros
125 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
126 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
127 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
128 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
129 ! (not defined here)
130 integer, intent (out), dimension(m) :: type ! vector of equation types
131 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
132 ! (not defined here)
133 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
134 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
135 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
136 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
137 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
138 real*8 usrmem(*) ! optional user memory
139!
140! Information about Variables:
141! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
142! Default: the status information in Vsta is not used.
143!
144! Lower bound on L = X(1) = 0.1 and initial value = 0.5:
145!
146 lower(1) = 0.1d0
147 curr(1) = 0.5d0
148!
149! Lower bound on INP = X(2) = 0.1 and initial value = 0.5:
150!
151 lower(2) = 0.1d0
152 curr(2) = 0.5d0
153!
154! Lower bound on OUT = X(3) and P = X(4) are both 0 and the
155! default initial value of 0 is used:
156!
157 lower(3) = 0.d0
158 lower(4) = 0.d0
159!
160! Information about Constraints:
161! Default: Rhs = 0
162! Default: the status information in Esta and the function
163! value in FV are not used.
164! Default: Type: There is no default.
165! 0 = Equality,
166! 1 = Greater than or equal,
167! 2 = Less than or equal,
168! 3 = Non binding.
169!
170! Constraint 1 (Objective)
171! Rhs = -0.1 and type Non binding
172!
173 rhs(1) = -0.1d0
174 type(1) = 3
175!
176! Constraint 2 (Production Function)
177! Rhs = 0 and type Equality
178!
179 type(2) = 0
180!
181! Constraint 3 (Price equation)
182! Rhs = 4.0 and type Equality
183!
184 rhs(3) = 4.d0
185 type(3) = 0
186!
187! Information about the Jacobian. CONOPT expects a columnwise
188! representation in Rowno, Value, Nlflag and Colsta.
189!
190! Colsta = Start of column indices (No Defaults):
191! Rowno = Row indices
192! Value = Value of derivative (by default only linear
193! derivatives are used)
194! Nlflag = 0 for linear and 1 for nonlinear derivative
195! (not needed for completely linear models)
196!
197! Indices
198! x(1) x(2) x(3) x(4)
199! 1: 1 3 5 8
200! 2: 2 4 6
201! 3: 7 9
202!
203 colsta(1) = 1
204 colsta(2) = 3
205 colsta(3) = 5
206 colsta(4) = 8
207 colsta(5) = 10
208 rowno(1) = 1
209 rowno(2) = 2
210 rowno(3) = 1
211 rowno(4) = 2
212 rowno(5) = 1
213 rowno(6) = 2
214 rowno(7) = 3
215 rowno(8) = 1
216 rowno(9) = 3
217!
218! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
219! x(1) x(2) x(3) x(4)
220! 1: L L NL NL
221! 2: NL NL L
222! 3: L L
223!
224 nlflag(1) = 0
225 nlflag(2) = 1
226 nlflag(3) = 0
227 nlflag(4) = 1
228 nlflag(5) = 1
229 nlflag(6) = 0
230 nlflag(7) = 0
231 nlflag(8) = 1
232 nlflag(9) = 0
233!
234! Value (Linear only)
235! x(1) x(2) x(3) x(4)
236! 1: -1 -1 NL NL
237! 2: NL NL -1
238! 3: 1 2
239!
240 value(1) = -1.d0
241 value(3) = -1.d0
242 value(6) = -1.d0
243 value(7) = 1.d0
244 value(9) = 2.d0
245
246 tut_readmatrix = 0 ! Return value means OK
247
248end Function tut_readmatrix
249!> Compute nonlinear terms and non-constant Jacobian elements
250!!
251!! @include{doc} fdeval_params.dox
252Integer Function tut_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
253 n, nz, thread, usrmem )
254#ifdef dec_directives_win32
255!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Tut_FDEval
256#endif
257 implicit none
258 integer, intent (in) :: n ! number of variables
259 integer, intent (in) :: rowno ! number of the row to be evaluated
260 integer, intent (in) :: nz ! number of nonzeros in this row
261 real*8, intent (in), dimension(n) :: x ! vector of current solution values
262 real*8, intent (in out) :: g ! constraint value
263 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
264 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
265 ! in this row. Ffor information only.
266 integer, intent (in) :: mode ! evaluation mode: 1 = function value
267 ! 2 = derivatives, 3 = both
268 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
269 ! as errcnt is incremented
270 integer, intent (in out) :: errcnt ! error counter to be incremented in case
271 ! of function evaluation errors.
272 integer, intent (in) :: thread
273 real*8 usrmem(*) ! optional user memory
274!
275! Declare local copies of the optimization variables. This is
276! just for convenience to make the expressions easier to read.
277!
278 real*8 :: l, inp, out, p
279!
280! Declare parameters and their data values.
281!
282 real*8, parameter :: w = 1.0d0
283 real*8, parameter :: l0 = 0.1d0
284 real*8, parameter :: pinp = 1.0d0
285 real*8, parameter :: al = 0.16d0
286 real*8, parameter :: ak = 2.0d0
287 real*8, parameter :: ainp = 0.16d0
288 real*8, parameter :: rho = 1.0d0
289 real*8, parameter :: k = 4.0d0
290 real*8 :: hold1, hold2, hold3 ! Intermediate results
291!
292! Move the optimization variables from the X vector to a set
293! of local variables with the same names as the variables in
294! the model description. This is not necessary, but it should make
295! the equations easier to recognize.
296!
297 l = x(1)
298 inp = x(2)
299 out = x(3)
300 p = x(4)
301!
302! Row 1: the objective function is nonlinear
303!
304 if ( rowno .eq. 1 ) then
305!
306! Mode = 1 or 3. Function value: G = P * Out
307!
308 if ( mode .eq. 1 .or. mode .eq. 3 ) then
309 g = p * out
310 endif
311!
312! Mode = 2 or 3: Derivative values:
313!
314 if ( mode .eq. 2 .or. mode .eq. 3 ) then
315 jac(3) = p ! derivative w.r.t. Out = X(3)
316 jac(4) = out ! derivative w.r.t. P = X(4)
317 endif
318!
319! Row 2: The production function is nonlinear
320!
321 elseif ( rowno .eq. 2 ) then
322!
323! Compute some common terms
324!
325 hold1 = (al*l**(-rho) + ak*k**(-rho) + ainp*inp**(-rho))
326 hold2 = hold1 ** ( -1.d0/rho )
327!
328! Mode = 1 or 3: Function value
329!
330 if ( mode .eq. 1 .or. mode .eq. 3 ) then
331 g = hold2
332 endif
333!
334! Mode = 2 or 3: Derivatives
335!
336 if ( mode .eq. 2 .or. mode .eq. 3 ) then
337 hold3 = hold2 / hold1
338 jac(1) = hold3 * al * l ** (-rho-1.d0) ! derivative w.r.t. L = X(1)
339 jac(2) = hold3 * ainp * inp ** (-rho-1.d0) ! derivative w.r.t. Inp = X(2)
340 endif
341!
342! Row = 3: The row is linear and will not be called.
343!
344 endif
345 tut_fdeval = 0
346
347end Function tut_fdeval
Main program. A simple setup and call of CONOPT.
Definition tutorial.java:14
integer function std_solution(xval, xmar, xbas, xsta, yval, ymar, ybas, ysta, n, m, usrmem)
Definition comdecl.f90:170
integer function std_status(modsta, solsta, iter, objval, usrmem)
Definition comdecl.f90:126
subroutine checkdual(case, minmax)
Definition comdecl.f90:432
integer function std_message(smsg, dmsg, nmsg, llen, usrmem, msgv)
Definition comdecl.f90:243
integer function std_errmsg(rowno, colno, posno, msglen, usrmem, msg)
Definition comdecl.f90:286
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_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
real *8 obj
Definition comdecl.f90:16
integer solcalls
Definition comdecl.f90:15
integer sstat
Definition comdecl.f90:18
subroutine finalize
Definition comdecl.f90:79
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
integer function tut_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
Definition tutorial.f90:109
integer function tut_fdeval(x, g, jac, rowno, jcnm, mode, ignerr, errcnt, n, nz, thread, usrmem)
Compute nonlinear terms and non-constant Jacobian elements.
Definition tutorial.f90:242