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
124
125 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
126
127 call flog( "Successful Solve", 0 )
129! Free solution memory
130!
131 call finalize
132
133End Program tutorial
134
135!> Define information about the model
136!!
137!! @include{doc} readMatrix_params.dox
138Integer Function tut_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 :: Tut_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 bound on L = X(1) = 0.1 and initial value = 0.5:
168!
169 lower(1) = 0.1d0
170 curr(1) = 0.5d0
171!
172! Lower bound on INP = X(2) = 0.1 and initial value = 0.5:
173!
174 lower(2) = 0.1d0
175 curr(2) = 0.5d0
176!
177! Lower bound on OUT = X(3) and P = X(4) are both 0 and the
178! default initial value of 0 is used:
179!
180 lower(3) = 0.d0
181 lower(4) = 0.d0
182!
183! Information about Constraints:
184! Default: Rhs = 0
185! Default: the status information in Esta and the function
186! value in FV are not used.
187! Default: Type: There is no default.
188! 0 = Equality,
189! 1 = Greater than or equal,
190! 2 = Less than or equal,
191! 3 = Non binding.
192!
193! Constraint 1 (Objective)
194! Rhs = -0.1 and type Non binding
195!
196 rhs(1) = -0.1d0
197 type(1) = 3
198!
199! Constraint 2 (Production Function)
200! Rhs = 0 and type Equality
201!
202 type(2) = 0
203!
204! Constraint 3 (Price equation)
205! Rhs = 4.0 and type Equality
206!
207 rhs(3) = 4.d0
208 type(3) = 0
209!
210! Information about the Jacobian. CONOPT expects a columnwise
211! representation in Rowno, Value, Nlflag and Colsta.
212!
213! Colsta = Start of column indices (No Defaults):
214! Rowno = Row indices
215! Value = Value of derivative (by default only linear
216! derivatives are used)
217! Nlflag = 0 for linear and 1 for nonlinear derivative
218! (not needed for completely linear models)
219!
220! Indices
221! x(1) x(2) x(3) x(4)
222! 1: 1 3 5 8
223! 2: 2 4 6
224! 3: 7 9
225!
226 colsta(1) = 1
227 colsta(2) = 3
228 colsta(3) = 5
229 colsta(4) = 8
230 colsta(5) = 10
231 rowno(1) = 1
232 rowno(2) = 2
233 rowno(3) = 1
234 rowno(4) = 2
235 rowno(5) = 1
236 rowno(6) = 2
237 rowno(7) = 3
238 rowno(8) = 1
239 rowno(9) = 3
240!
241! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
242! x(1) x(2) x(3) x(4)
243! 1: L L NL NL
244! 2: NL NL L
245! 3: L L
246!
247 nlflag(1) = 0
248 nlflag(2) = 1
249 nlflag(3) = 0
250 nlflag(4) = 1
251 nlflag(5) = 1
252 nlflag(6) = 0
253 nlflag(7) = 0
254 nlflag(8) = 1
255 nlflag(9) = 0
256!
257! Value (Linear only)
258! x(1) x(2) x(3) x(4)
259! 1: -1 -1 NL NL
260! 2: NL NL -1
261! 3: 1 2
262!
263 value(1) = -1.d0
264 value(3) = -1.d0
265 value(6) = -1.d0
266 value(7) = 1.d0
267 value(9) = 2.d0
268
269 tut_readmatrix = 0 ! Return value means OK
270
271end Function tut_readmatrix
272
273!> Initialises the function and derivative evaluation. Called each time the point of interest changes
274!!
275!! @include{doc} fdevalini_params.dox
276Integer function tut_fdevalini( x, rowlist, mode, listsize, numthread, ignerr, errcnt, numvar, usrmem )
277#ifdef dec_directives_win32
278!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Tut_FDEvalIni
279#endif
280 Use var
281 implicit none
282 integer, intent (in) :: numvar ! number of variables
283 integer, intent (in) :: listsize ! number of elements in rowlist
284 integer, intent (in) :: numthread ! number of threads for following FDEval loop
285 real*8, intent (in), dimension(numvar) :: x ! vector of current solution values
286 integer, intent (in), dimension(listsize) :: rowlist ! The rows that will be called in this point
287 integer, intent (in) :: mode ! evaluation mode: 1 = function value
288 ! 2 = derivatives, 3 = both
289 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
290 ! as errcnt is incremented
291 integer, intent (in out) :: errcnt ! error counter to be incremented in case
292 ! of function evaluation errors.
293 real*8 usrmem(*) ! optional user memory
294!
295! move the optimization variables from the x vector to the variables
296! in module Var. They will then be used in FDEval
297!
298 l = x(1)
299 inp = x(2)
300 out = x(3)
301 p = x(4)
302!
303! compute some common terms
304!
305 hold1 = (al*l**(-rho) + ak*k**(-rho) + ainp*inp**(-rho))
306 hold2 = hold1 ** ( -1.d0/rho )
307 hold3 = hold2 / hold1
308
309 tut_fdevalini = 0
310 End Function tut_fdevalini
311
312!> Compute nonlinear terms and non-constant Jacobian elements
313!!
314!! @include{doc} fdeval_params.dox
315integer function tut_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
316 numvar, nz, thread, usrmem )
317#ifdef dec_directives_win32
318!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Tut_FDEval
319#endif
320 Use var
321 implicit none
322 integer, intent (in) :: numvar ! number of variables
323 integer, intent (in) :: rowno ! number of the row to be evaluated
324 integer, intent (in) :: nz ! number of nonzeros in this row
325 real*8, intent (in), dimension(numvar) :: x ! vector of current solution values
326 real*8, intent (in out) :: g ! constraint value
327 real*8, intent (in out), dimension(numvar) :: jac ! vector of derivatives for current constraint
328 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
329 ! in this row. ffor information only.
330 integer, intent (in) :: mode ! evaluation mode: 1 = function value
331 ! 2 = derivatives, 3 = both
332 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
333 ! as errcnt is incremented
334 integer, intent (in out) :: errcnt ! error counter to be incremented in case
335 ! of function evaluation errors.
336 integer, intent (in) :: thread
337 real*8 usrmem(*) ! optional user memory
338!
339! row 1: the objective function is nonlinear
340!
341 if ( rowno .eq. 1 ) then
342!
343! mode = 1 or 3. function value: g = p * out
344!
345 if ( mode .eq. 1 .or. mode .eq. 3 ) then
346 g = p * out
347 endif
348!
349! mode = 2 or 3: derivative values:
350!
351 if ( mode .eq. 2 .or. mode .eq. 3 ) then
352 jac(3) = p ! derivative w.r.t. out = x(3)
353 jac(4) = out ! derivative w.r.t. p = x(4)
354 endif
355!
356! row 2: the production function is nonlinear
357!
358 elseif ( rowno .eq. 2 ) then
359!
360! mode = 1 or 3: function value
361!
362 if ( mode .eq. 1 .or. mode .eq. 3 ) then
363 g = hold2
364 endif
365!
366! mode = 2 or 3: derivatives
367!
368 if ( mode .eq. 2 .or. mode .eq. 3 ) then
369 jac(1) = hold3 * al * l ** (-rho-1.d0) ! derivative w.r.t. l = x(1)
370 jac(2) = hold3 * ainp * inp ** (-rho-1.d0) ! derivative w.r.t. inp = x(2)
371 endif
372!
373! row = 3: the row is linear and will not be called.
374!
375 endif
376 tut_fdeval = 0
377
378end 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_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
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
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: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
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.