CONOPT
Loading...
Searching...
No Matches
tutoriali.f90
Go to the documentation of this file.
1!> @file tutoriali.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!! @brief This model is a revision of Tutorial in which we have added a
4!! an initialization callback for the First derivative, Tut_FDEvalIni.
5!!
6!! For more information about the individual callbacks, please have a look at the source code.
7
8#if defined(_WIN32) && !defined(_WIN64)
9#define dec_directives_win32
10#endif
11
12Module var
13!
14! Declare copies of the optimization variables. The values are
15! communicated from FdevalIni to Fdeval.
16!
17 real*8 :: l, inp, out, p
18!
19! declare parameters and their data values.
20!
21 real*8, parameter :: w = 1.0d0
22 real*8, parameter :: l0 = 0.1d0
23 real*8, parameter :: pinp = 1.0d0
24 real*8, parameter :: al = 0.16d0
25 real*8, parameter :: ak = 2.0d0
26 real*8, parameter :: ainp = 0.16d0
27 real*8, parameter :: rho = 1.0d0
28 real*8, parameter :: k = 4.0d0
29 real*8 :: hold1, hold2, hold3 ! intermediate results
30End Module var
32!> Main program. A simple setup and call of CONOPT
33!!
34Program tutorial
35
37 Use conopt
38 implicit None
39!
40! Declare the user callback routines as Integer, External:
41!
42 Integer, External :: tut_readmatrix ! Mandatory Matrix definition routine defined below
43 Integer, External :: tut_fdeval ! Function and Derivative evaluation routine
44 ! needed a nonlinear model.
45 Integer, External :: tut_fdevalini ! Function and Derivative initialization routine.
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 :: Tut_ReadMatrix
52!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Tut_FDEval
53!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Tut_FDEvalIni
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, 4 ) ) ! 4 variables
74 coi_error = max( coi_error, coidef_numcon( cntvect, 3 ) ) ! 3 constraints
75 coi_error = max( coi_error, coidef_numnz( cntvect, 9 ) ) ! 9 nonzeros in the Jacobian
76 coi_error = max( coi_error, coidef_numnlnz( cntvect, 4 ) ) ! 4 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, 'tutoriali.opt' ) )
80!
81! Tell CONOPT about the callback routines:
82!
83 coi_error = max( coi_error, coidef_readmatrix( cntvect, tut_readmatrix ) )
84 coi_error = max( coi_error, coidef_fdeval( cntvect, tut_fdeval ) )
85 coi_error = max( coi_error, coidef_fdevalini( cntvect, tut_fdevalini ) )
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 Tutorial 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-0.572943d0 ) > 0.000001d0 ) then
120 call flog( "Incorrect objective returned", 1 )
121 Else
122 Call checkdual( 'Tutorial', maximize )
123 endif
125 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
126
127 call flog( "Successful Solve", 0 )
128
129End Program tutorial
130
131!> Define information about the model
132!!
133!! @include{doc} readMatrix_params.dox
134Integer Function tut_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 :: Tut_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 bound on L = X(1) = 0.1 and initial value = 0.5:
164!
165 lower(1) = 0.1d0
166 curr(1) = 0.5d0
167!
168! Lower bound on INP = X(2) = 0.1 and initial value = 0.5:
169!
170 lower(2) = 0.1d0
171 curr(2) = 0.5d0
172!
173! Lower bound on OUT = X(3) and P = X(4) are both 0 and the
174! default initial value of 0 is used:
175!
176 lower(3) = 0.d0
177 lower(4) = 0.d0
178!
179! Information about Constraints:
180! Default: Rhs = 0
181! Default: the status information in Esta and the function
182! value in FV are not used.
183! Default: Type: There is no default.
184! 0 = Equality,
185! 1 = Greater than or equal,
186! 2 = Less than or equal,
187! 3 = Non binding.
188!
189! Constraint 1 (Objective)
190! Rhs = -0.1 and type Non binding
191!
192 rhs(1) = -0.1d0
193 type(1) = 3
194!
195! Constraint 2 (Production Function)
196! Rhs = 0 and type Equality
197!
198 type(2) = 0
199!
200! Constraint 3 (Price equation)
201! Rhs = 4.0 and type Equality
202!
203 rhs(3) = 4.d0
204 type(3) = 0
205!
206! Information about the Jacobian. CONOPT expects a columnwise
207! representation in Rowno, Value, Nlflag and Colsta.
208!
209! Colsta = Start of column indices (No Defaults):
210! Rowno = Row indices
211! Value = Value of derivative (by default only linear
212! derivatives are used)
213! Nlflag = 0 for linear and 1 for nonlinear derivative
214! (not needed for completely linear models)
215!
216! Indices
217! x(1) x(2) x(3) x(4)
218! 1: 1 3 5 8
219! 2: 2 4 6
220! 3: 7 9
221!
222 colsta(1) = 1
223 colsta(2) = 3
224 colsta(3) = 5
225 colsta(4) = 8
226 colsta(5) = 10
227 rowno(1) = 1
228 rowno(2) = 2
229 rowno(3) = 1
230 rowno(4) = 2
231 rowno(5) = 1
232 rowno(6) = 2
233 rowno(7) = 3
234 rowno(8) = 1
235 rowno(9) = 3
236!
237! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
238! x(1) x(2) x(3) x(4)
239! 1: L L NL NL
240! 2: NL NL L
241! 3: L L
242!
243 nlflag(1) = 0
244 nlflag(2) = 1
245 nlflag(3) = 0
246 nlflag(4) = 1
247 nlflag(5) = 1
248 nlflag(6) = 0
249 nlflag(7) = 0
250 nlflag(8) = 1
251 nlflag(9) = 0
252!
253! Value (Linear only)
254! x(1) x(2) x(3) x(4)
255! 1: -1 -1 NL NL
256! 2: NL NL -1
257! 3: 1 2
258!
259 value(1) = -1.d0
260 value(3) = -1.d0
261 value(6) = -1.d0
262 value(7) = 1.d0
263 value(9) = 2.d0
264
265 tut_readmatrix = 0 ! Return value means OK
266
267end Function tut_readmatrix
268
269!> Initialises the function and derivative evaluation. Called each time the point of interest changes
270!!
271!! @include{doc} fdevalini_params.dox
272Integer function tut_fdevalini( x, rowlist, mode, listsize, numthread, ignerr, errcnt, numvar, usrmem )
273#ifdef dec_directives_win32
274!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Tut_FDEvalIni
275#endif
276 Use var
277 implicit none
278 integer, intent (in) :: numvar ! number of variables
279 integer, intent (in) :: listsize ! number of elements in rowlist
280 integer, intent (in) :: numthread ! number of threads for following FDEval loop
281 real*8, intent (in), dimension(numvar) :: x ! vector of current solution values
282 integer, intent (in), dimension(listsize) :: rowlist ! The rows that will be called in this point
283 integer, intent (in) :: mode ! evaluation mode: 1 = function value
284 ! 2 = derivatives, 3 = both
285 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
286 ! as errcnt is incremented
287 integer, intent (in out) :: errcnt ! error counter to be incremented in case
288 ! of function evaluation errors.
289 real*8 usrmem(*) ! optional user memory
290!
291! move the optimization variables from the x vector to the variables
292! in module Var. They will then be used in FDEval
293!
294 l = x(1)
295 inp = x(2)
296 out = x(3)
297 p = x(4)
298!
299! compute some common terms
300!
301 hold1 = (al*l**(-rho) + ak*k**(-rho) + ainp*inp**(-rho))
302 hold2 = hold1 ** ( -1.d0/rho )
303 hold3 = hold2 / hold1
304
305 tut_fdevalini = 0
306 End Function tut_fdevalini
307
308!> Compute nonlinear terms and non-constant Jacobian elements
309!!
310!! @include{doc} fdeval_params.dox
311integer function tut_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
312 numvar, nz, thread, usrmem )
313#ifdef dec_directives_win32
314!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Tut_FDEval
315#endif
316 Use var
317 implicit none
318 integer, intent (in) :: numvar ! number of variables
319 integer, intent (in) :: rowno ! number of the row to be evaluated
320 integer, intent (in) :: nz ! number of nonzeros in this row
321 real*8, intent (in), dimension(numvar) :: x ! vector of current solution values
322 real*8, intent (in out) :: g ! constraint value
323 real*8, intent (in out), dimension(numvar) :: jac ! vector of derivatives for current constraint
324 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
325 ! in this row. ffor information only.
326 integer, intent (in) :: mode ! evaluation mode: 1 = function value
327 ! 2 = derivatives, 3 = both
328 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
329 ! as errcnt is incremented
330 integer, intent (in out) :: errcnt ! error counter to be incremented in case
331 ! of function evaluation errors.
332 integer, intent (in) :: thread
333 real*8 usrmem(*) ! optional user memory
334!
335! row 1: the objective function is nonlinear
336!
337 if ( rowno .eq. 1 ) then
338!
339! mode = 1 or 3. function value: g = p * out
340!
341 if ( mode .eq. 1 .or. mode .eq. 3 ) then
342 g = p * out
343 endif
344!
345! mode = 2 or 3: derivative values:
346!
347 if ( mode .eq. 2 .or. mode .eq. 3 ) then
348 jac(3) = p ! derivative w.r.t. out = x(3)
349 jac(4) = out ! derivative w.r.t. p = x(4)
350 endif
351!
352! row 2: the production function is nonlinear
353!
354 elseif ( rowno .eq. 2 ) then
355!
356! mode = 1 or 3: function value
357!
358 if ( mode .eq. 1 .or. mode .eq. 3 ) then
359 g = hold2
360 endif
361!
362! mode = 2 or 3: derivatives
363!
364 if ( mode .eq. 2 .or. mode .eq. 3 ) then
365 jac(1) = hold3 * al * l ** (-rho-1.d0) ! derivative w.r.t. l = x(1)
366 jac(2) = hold3 * ainp * inp ** (-rho-1.d0) ! derivative w.r.t. inp = x(2)
367 endif
368!
369! row = 3: the row is linear and will not be called.
370!
371 endif
372 tut_fdeval = 0
373
374end 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: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(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_fdevalini(cntvect, coi_fdevalini)
define callback routine to perform initialization tasks for the function and derivative evaluation.
Definition conopt.f90:1161
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
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
real *8 p
Definition tutoriali.f90:19
real *8, parameter al
Definition tutoriali.f90:26
real *8 inp
Definition tutoriali.f90:19
real *8, parameter ak
Definition tutoriali.f90:27
real *8, parameter ainp
Definition tutoriali.f90:28
real *8, parameter w
Definition tutoriali.f90:23
real *8 l
Definition tutoriali.f90:19
real *8, parameter pinp
Definition tutoriali.f90:25
real *8, parameter l0
Definition tutoriali.f90:24
real *8, parameter rho
Definition tutoriali.f90:29
real *8 out
Definition tutoriali.f90:19
real *8, parameter k
Definition tutoriali.f90:30
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:104
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:237
integer function tut_fdevalini(x, rowlist, mode, listsize, numthread, ignerr, errcnt, numvar, usrmem)
Initialises the function and derivative evaluation. Called each time the point of interest changes.