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
9
10Module var
11!
12! Declare copies of the optimization variables. The values are
13! communicated from FdevalIni to Fdeval.
14!
15 real*8 :: l, inp, out, p
16!
17! declare parameters and their data values.
18!
19 real*8, parameter :: w = 1.0d0
20 real*8, parameter :: l0 = 0.1d0
21 real*8, parameter :: pinp = 1.0d0
22 real*8, parameter :: al = 0.16d0
23 real*8, parameter :: ak = 2.0d0
24 real*8, parameter :: ainp = 0.16d0
25 real*8, parameter :: rho = 1.0d0
26 real*8, parameter :: k = 4.0d0
27 real*8 :: hold1, hold2, hold3 ! intermediate results
28End Module var
29
30!> Main program. A simple setup and call of CONOPT
31!!
32Program tutorial
33
34 Use proginfo
35 Use coidef
36 implicit None
37!
38! Declare the user callback routines as Integer, External:
39!
40 Integer, External :: tut_readmatrix ! Mandatory Matrix definition routine defined below
41 Integer, External :: tut_fdeval ! Function and Derivative evaluation routine
42 ! needed a nonlinear model.
43 Integer, External :: tut_fdevalini ! Function and Derivative initialization routine.
44 Integer, External :: std_status ! Standard callback for displaying solution status
45 Integer, External :: std_solution ! Standard callback for displaying solution values
46 Integer, External :: std_message ! Standard callback for managing messages
47 Integer, External :: std_errmsg ! Standard callback for managing error messages
48#if defined(itl)
49!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Tut_ReadMatrix
50!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Tut_FDEval
51!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Tut_FDEvalIni
52!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
53!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
54!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
55!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
56#endif
57!
58! Control vector
59!
60 INTEGER :: numcallback
61 INTEGER, Dimension(:), Pointer :: cntvect
62 INTEGER :: coi_error
63!
64! Create and initialize a Control Vector
65!
66 call startup
67
68 numcallback = coidef_size()
69 Allocate( cntvect(numcallback) )
70 coi_error = coidef_inifort( cntvect )
71!
72! Tell CONOPT about the size of the model by populating the Control Vector:
73!
74 coi_error = max( coi_error, coidef_numvar( cntvect, 4 ) ) ! 4 variables
75 coi_error = max( coi_error, coidef_numcon( cntvect, 3 ) ) ! 3 constraints
76 coi_error = max( coi_error, coidef_numnz( cntvect, 9 ) ) ! 9 nonzeros in the Jacobian
77 coi_error = max( coi_error, coidef_numnlnz( cntvect, 4 ) ) ! 4 of which are nonlinear
78 coi_error = max( coi_error, coidef_optdir( cntvect, 1 ) ) ! Maximize
79 coi_error = max( coi_error, coidef_objcon( cntvect, 1 ) ) ! Objective is constraint 1
80 coi_error = max( coi_error, coidef_optfile( cntvect, 'tutoriali.opt' ) )
81!
82! Tell CONOPT about the callback routines:
83!
84 coi_error = max( coi_error, coidef_readmatrix( cntvect, tut_readmatrix ) )
85 coi_error = max( coi_error, coidef_fdeval( cntvect, tut_fdeval ) )
86 coi_error = max( coi_error, coidef_fdevalini( cntvect, tut_fdevalini ) )
87 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
88 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
89 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
90 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
91
92#if defined(LICENSE_INT_1) && defined(LICENSE_INT_2) && defined(LICENSE_INT_3) && defined(LICENSE_TEXT)
93 coi_error = max( coi_error, coidef_license( cntvect, license_int_1, license_int_2, license_int_3, license_text) )
94#endif
95
96 If ( coi_error .ne. 0 ) THEN
97 write(*,*)
98 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
99 write(*,*)
100 call flog( "Skipping Solve due to setup errors", 1 )
101 ENDIF
102!
103! Save the solution so we can check the duals:
104!
105 do_allocate = .true.
106!
107! Start CONOPT:
108!
109 coi_error = coi_solve( cntvect )
110
111 write(*,*)
112 write(*,*) 'End of Tutorial example. Return code=',coi_error
113
114 If ( coi_error /= 0 ) then
115 call flog( "Errors encountered during solution", 1 )
116 elseif ( stacalls == 0 .or. solcalls == 0 ) then
117 call flog( "Status or Solution routine was not called", 1 )
118 elseif ( sstat /= 1 .or. mstat /= 2 ) then
119 call flog( "Solver and Model Status was not as expected (1,2)", 1 )
120 elseif ( abs( obj-0.572943d0 ) > 0.000001d0 ) then
121 call flog( "Incorrect objective returned", 1 )
122 Else
123 Call checkdual( 'Tutorial', maximize )
124 endif
125
126 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
127
128 call flog( "Successful Solve", 0 )
129
130End Program tutorial
131
132!> Define information about the model
133!!
134!! @include{doc} readMatrix_params.dox
135Integer Function tut_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
136 colsta, rowno, value, nlflag, n, m, nz, &
137 usrmem )
138#if defined(itl)
139!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Tut_ReadMatrix
140#endif
141 implicit none
142 integer, intent (in) :: n ! number of variables
143 integer, intent (in) :: m ! number of constraints
144 integer, intent (in) :: nz ! number of nonzeros
145 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
146 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
147 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
148 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
149 ! (not defined here)
150 integer, intent (out), dimension(m) :: type ! vector of equation types
151 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
152 ! (not defined here)
153 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
154 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
155 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
156 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
157 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
158 real*8 usrmem(*) ! optional user memory
159!
160! Information about Variables:
161! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
162! Default: the status information in Vsta is not used.
163!
164! Lower bound on L = X(1) = 0.1 and initial value = 0.5:
165!
166 lower(1) = 0.1d0
167 curr(1) = 0.5d0
168!
169! Lower bound on INP = X(2) = 0.1 and initial value = 0.5:
170!
171 lower(2) = 0.1d0
172 curr(2) = 0.5d0
173!
174! Lower bound on OUT = X(3) and P = X(4) are both 0 and the
175! default initial value of 0 is used:
176!
177 lower(3) = 0.d0
178 lower(4) = 0.d0
179!
180! Information about Constraints:
181! Default: Rhs = 0
182! Default: the status information in Esta and the function
183! value in FV are not used.
184! Default: Type: There is no default.
185! 0 = Equality,
186! 1 = Greater than or equal,
187! 2 = Less than or equal,
188! 3 = Non binding.
189!
190! Constraint 1 (Objective)
191! Rhs = -0.1 and type Non binding
192!
193 rhs(1) = -0.1d0
194 type(1) = 3
195!
196! Constraint 2 (Production Function)
197! Rhs = 0 and type Equality
198!
199 type(2) = 0
200!
201! Constraint 3 (Price equation)
202! Rhs = 4.0 and type Equality
203!
204 rhs(3) = 4.d0
205 type(3) = 0
206!
207! Information about the Jacobian. We use the standard method with
208! Rowno, Value, Nlflag and Colsta and we do not use Colno.
209!
210! Colsta = Start of column indices (No Defaults):
211! Rowno = Row indices
212! Value = Value of derivative (by default only linear
213! derivatives are used)
214! Nlflag = 0 for linear and 1 for nonlinear derivative
215! (not needed for completely linear models)
216!
217! Indices
218! x(1) x(2) x(3) x(4)
219! 1: 1 3 5 8
220! 2: 2 4 6
221! 3: 7 9
222!
223 colsta(1) = 1
224 colsta(2) = 3
225 colsta(3) = 5
226 colsta(4) = 8
227 colsta(5) = 10
228 rowno(1) = 1
229 rowno(2) = 2
230 rowno(3) = 1
231 rowno(4) = 2
232 rowno(5) = 1
233 rowno(6) = 2
234 rowno(7) = 3
235 rowno(8) = 1
236 rowno(9) = 3
237!
238! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
239! x(1) x(2) x(3) x(4)
240! 1: L L NL NL
241! 2: NL NL L
242! 3: L L
243!
244 nlflag(1) = 0
245 nlflag(2) = 1
246 nlflag(3) = 0
247 nlflag(4) = 1
248 nlflag(5) = 1
249 nlflag(6) = 0
250 nlflag(7) = 0
251 nlflag(8) = 1
252 nlflag(9) = 0
253!
254! Value (Linear only)
255! x(1) x(2) x(3) x(4)
256! 1: -1 -1 NL NL
257! 2: NL NL -1
258! 3: 1 2
259!
260 value(1) = -1.d0
261 value(3) = -1.d0
262 value(6) = -1.d0
263 value(7) = 1.d0
264 value(9) = 2.d0
265
266 tut_readmatrix = 0 ! Return value means OK
267
268end Function tut_readmatrix
269
270!> Initialises the function and derivative evaluation. Called each time the point of interest changes
271!!
272!! @include{doc} fdevalini_params.dox
273Integer function tut_fdevalini( x, rowlist, mode, listsize, numthread, ignerr, errcnt, numvar, usrmem )
274#if defined(itl)
275!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Tut_FDEvalIni
276#endif
277 Use var
278 implicit none
279 integer, intent (in) :: numvar ! number of variables
280 integer, intent (in) :: listsize ! number of elements in rowlist
281 integer, intent (in) :: numthread ! number of threads for following FDEval loop
282 real*8, intent (in), dimension(numvar) :: x ! vector of current solution values
283 integer, intent (in), dimension(listsize) :: rowlist ! The rows that will be called in this point
284 integer, intent (in) :: mode ! evaluation mode: 1 = function value
285 ! 2 = derivatives, 3 = both
286 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
287 ! as errcnt is incremented
288 integer, intent (in out) :: errcnt ! error counter to be incremented in case
289 ! of function evaluation errors.
290 real*8 usrmem(*) ! optional user memory
291!
292! move the optimization variables from the x vector to the variables
293! in module Var. They will then be used in FDEval
294!
295 l = x(1)
296 inp = x(2)
297 out = x(3)
298 p = x(4)
299!
300! compute some common terms
301!
302 hold1 = (al*l**(-rho) + ak*k**(-rho) + ainp*inp**(-rho))
303 hold2 = hold1 ** ( -1.d0/rho )
304 hold3 = hold2 / hold1
305
306 tut_fdevalini = 0
307 End Function tut_fdevalini
308
309!> Compute nonlinear terms and non-constant Jacobian elements
310!!
311!! @include{doc} fdeval_params.dox
312integer function tut_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
313 numvar, nz, thread, usrmem )
314#if defined(itl)
315!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Tut_FDEval
316#endif
317 Use var
318 implicit none
319 integer, intent (in) :: numvar ! number of variables
320 integer, intent (in) :: rowno ! number of the row to be evaluated
321 integer, intent (in) :: nz ! number of nonzeros in this row
322 real*8, intent (in), dimension(numvar) :: x ! vector of current solution values
323 real*8, intent (in out) :: g ! constraint value
324 real*8, intent (in out), dimension(numvar) :: jac ! vector of derivatives for current constraint
325 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
326 ! in this row. ffor information only.
327 integer, intent (in) :: mode ! evaluation mode: 1 = function value
328 ! 2 = derivatives, 3 = both
329 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
330 ! as errcnt is incremented
331 integer, intent (in out) :: errcnt ! error counter to be incremented in case
332 ! of function evaluation errors.
333 integer, intent (in) :: thread
334 real*8 usrmem(*) ! optional user memory
335!
336! row 1: the objective function is nonlinear
337!
338 if ( rowno .eq. 1 ) then
339!
340! mode = 1 or 3. function value: g = p * out
341!
342 if ( mode .eq. 1 .or. mode .eq. 3 ) then
343 g = p * out
344 endif
345!
346! mode = 2 or 3: derivative values:
347!
348 if ( mode .eq. 2 .or. mode .eq. 3 ) then
349 jac(3) = p ! derivative w.r.t. out = x(3)
350 jac(4) = out ! derivative w.r.t. p = x(4)
351 endif
352!
353! row 2: the production function is nonlinear
354!
355 elseif ( rowno .eq. 2 ) then
356!
357! mode = 1 or 3: function value
358!
359 if ( mode .eq. 1 .or. mode .eq. 3 ) then
360 g = hold2
361 endif
362!
363! mode = 2 or 3: derivatives
364!
365 if ( mode .eq. 2 .or. mode .eq. 3 ) then
366 jac(1) = hold3 * al * l ** (-rho-1.d0) ! derivative w.r.t. l = x(1)
367 jac(2) = hold3 * ainp * inp ** (-rho-1.d0) ! derivative w.r.t. inp = x(2)
368 endif
369!
370! row = 3: the row is linear and will not be called.
371!
372 endif
373 tut_fdeval = 0
374
375end 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:128
integer function std_status(modsta, solsta, iter, objval, usrmem)
Definition comdecl.f90:82
subroutine checkdual(case, minmax)
Definition comdecl.f90:365
integer function std_message(smsg, dmsg, nmsg, llen, usrmem, msgv)
Definition comdecl.f90:203
integer function std_errmsg(rowno, colno, posno, msglen, usrmem, msg)
Definition comdecl.f90:248
integer function coidef_fdeval(cntvect, coi_fdeval)
define callback routine for performing function and derivative evaluations.
integer function coidef_errmsg(cntvect, coi_errmsg)
define callback routine for returning error messages for row, column or Jacobian elements.
integer function coidef_message(cntvect, coi_message)
define callback routine for handling messages returned during the solution process.
integer function coidef_readmatrix(cntvect, coi_readmatrix)
define callback routine for providing the matrix data to CONOPT.
integer function coidef_status(cntvect, coi_status)
define callback routine for returning the completion status.
integer function coidef_solution(cntvect, coi_solution)
define callback routine for returning the final solution values.
integer function coidef_fdevalini(cntvect, coi_fdevalini)
define callback routine to perform initialization tasks for the function and derivative evaluation.
integer function coidef_optfile(cntvect, optfile)
define callback routine for defining an options file.
integer function coidef_license(cntvect, licint1, licint2, licint3, licstring)
define the License Information.
Definition coistart.f90:680
integer function coidef_numvar(cntvect, numvar)
defines the number of variables in the model.
Definition coistart.f90:358
integer function coidef_objcon(cntvect, objcon)
defines the Objective Constraint.
Definition coistart.f90:629
integer function coidef_numnz(cntvect, numnz)
defines the number of nonzero elements in the Jacobian.
Definition coistart.f90:437
integer function coidef_optdir(cntvect, optdir)
defines the Optimization Direction.
Definition coistart.f90:552
integer function coidef_numnlnz(cntvect, numnlnz)
defines the Number of Nonlinear Nonzeros.
Definition coistart.f90:476
integer function coidef_numcon(cntvect, numcon)
defines the number of constraints in the model.
Definition coistart.f90:398
integer function coidef_size()
returns the size the Control Vector must have, measured in standard Integer units.
Definition coistart.f90:176
integer function coidef_inifort(cntvect)
initialisation method for Fortran applications.
Definition coistart.f90:314
integer function coi_solve(cntvect)
method for starting the solving process of CONOPT.
Definition coistart.f90:14
real *8 obj
Definition comdecl.f90:10
integer solcalls
Definition comdecl.f90:9
integer sstat
Definition comdecl.f90:12
integer stacalls
Definition comdecl.f90:8
subroutine flog(msg, code)
Definition comdecl.f90:56
logical do_allocate
Definition comdecl.f90:21
integer, parameter maximize
Definition comdecl.f90:25
integer mstat
Definition comdecl.f90:11
subroutine startup
Definition comdecl.f90:35
real *8 p
Definition tutoriali.f90:15
real *8, parameter al
Definition tutoriali.f90:22
real *8 inp
Definition tutoriali.f90:15
real *8, parameter ak
Definition tutoriali.f90:23
real *8, parameter ainp
Definition tutoriali.f90:24
real *8, parameter w
Definition tutoriali.f90:19
real *8 l
Definition tutoriali.f90:15
real *8, parameter pinp
Definition tutoriali.f90:21
real *8, parameter l0
Definition tutoriali.f90:20
real *8, parameter rho
Definition tutoriali.f90:25
real *8 out
Definition tutoriali.f90:15
real *8, parameter k
Definition tutoriali.f90:26
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:245
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.