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