CONOPT
Loading...
Searching...
No Matches
mono04.f90
Go to the documentation of this file.
1!> @file mono04.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! Monotone function to bound conversion example 04
6!!
7!! This is a CONOPT implementation of the GAMS model:
8!!
9!! @verbatim
10!! variable x1
11!! equation e1;
12!!
13!! e1 .. log(x1) =L= 2;
14!!
15!! x1.lo = 0.01;
16!! x1.up = 5;
17!! model mono / all /;
18!! solve mono using nlp maximizing x1;
19!! @endverbatim
20!!
21!!
22!!
23!! For more information about the individual callbacks, please have a look at the source code.
24
25!> Main program. A simple setup and call of CONOPT
26!!
27Program mono04
28
29 Use proginfo
30 Use coidef
31 implicit None
32!
33! Declare the user callback routines as Integer, External:
34!
35 Integer, External :: mono_readmatrix ! Mandatory Matrix definition routine defined below
36 Integer, External :: mono_fdeval ! Function and Derivative evaluation routine
37 ! needed a nonlinear model.
38 Integer, External :: mono_fdinterval ! Function and Derivative evaluation routine
39 ! needed a nonlinear model.
40 Integer, External :: std_status ! Standard callback for displaying solution status
41 Integer, External :: std_solution ! Standard callback for displaying solution values
42 Integer, External :: std_message ! Standard callback for managing messages
43 Integer, External :: std_errmsg ! Standard callback for managing error messages
44 Integer, External :: std_triord ! Standard callback for Monongular order
45#if defined(itl)
46!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Mono_ReadMatrix
47!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Mono_FDEval
48!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Mono_FDInterval
49!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
50!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
51!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
52!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
53!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_TriOrd
54#endif
55!
56! Control vector
57!
58 INTEGER :: numcallback
59 INTEGER, Dimension(:), Pointer :: cntvect
60 INTEGER :: coi_error
61
62 call startup
63!
64! Create and initialize a Control Vector
65!
66 numcallback = coidef_size()
67 Allocate( cntvect(numcallback) )
68 coi_error = coidef_inifort( cntvect )
69!
70! Tell CONOPT about the size of the model by populating the Control Vector:
71!
72 coi_error = max( coi_error, coidef_numvar( cntvect, 1 ) ) ! # variables
73 coi_error = max( coi_error, coidef_numcon( cntvect, 1 ) ) ! # constraints
74 coi_error = max( coi_error, coidef_numnz( cntvect, 1 ) ) ! # nonzeros in the Jacobian
75 coi_error = max( coi_error, coidef_numnlnz( cntvect, 1 ) ) ! # of which are nonlinear
76 coi_error = max( coi_error, coidef_optdir( cntvect, +1 ) ) ! Maximize
77 coi_error = max( coi_error, coidef_objvar( cntvect, 1 ) ) ! Objective is variable 3
78 coi_error = max( coi_error, coidef_optfile( cntvect, 'Mono04.opt' ) )
79!
80! Tell CONOPT about the callback routines:
81!
82 coi_error = max( coi_error, coidef_readmatrix( cntvect, mono_readmatrix ) )
83 coi_error = max( coi_error, coidef_fdeval( cntvect, mono_fdeval ) )
84 coi_error = max( coi_error, coidef_fdinterval( cntvect, mono_fdinterval ) )
85 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
86 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
87 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
88 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
89 coi_error = max( coi_error, coidef_triord( cntvect, std_triord ) )
90
91#if defined(LICENSE_INT_1) && defined(LICENSE_INT_2) && defined(LICENSE_INT_3) && defined(LICENSE_TEXT)
92 coi_error = max( coi_error, coidef_license( cntvect, license_int_1, license_int_2, license_int_3, 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 Mono04 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 /= 1 ) then
118 call flog( "Solver and Model Status was not as expected (1,1)", 1 )
119 elseif ( abs( obj-5.0d0 ) > 0.000001d0 ) then
120 call flog( "Incorrect objective returned", 1 )
121 Else
122 Call checkdual( 'Mono04', 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 )
128
129End Program mono04
130!
131! ============================================================================
132! Define information about the model:
133!
134
135!> Define information about the model
136!!
137!! @include{doc} readMatrix_params.dox
138Integer Function mono_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
139 colsta, rowno, value, nlflag, n, m, nz, &
140 usrmem )
141#if defined(itl)
142!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Mono_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! The model uses defaults
168!
169! Information about Constraints:
170! Default: Rhs = 0
171! Default: the status information in Esta and the function
172! value in FV are not used.
173! Default: Type: There is no default.
174! 0 = Equality,
175! 1 = Greater than or equal,
176! 2 = Less than or equal,
177! 3 = Non binding.
178!
179! Constraint 1: e1
180! Rhs = 10.0 and type Less than or Equal
181!
182 rhs(1) = 2.0d0
183 type(1) = 2
184!
185 lower(1) = 0.01d0
186 curr(1) = 0.01d0
187 upper(1) = 5.00d0
188!
189! Information about the Jacobian. We use the standard method with
190! Rowno, Value, Nlflag and Colsta and we do not use Colno.
191!
192! Colsta = Start of column indices (No Defaults):
193! Rowno = Row indices
194! Value = Value of derivative (by default only linear
195! derivatives are used)
196! Nlflag = 0 for linear and 1 for nonlinear derivative
197! (not needed for completely linear models)
198!
199! Indices
200! x(1)
201! 1: 1
202!
203 colsta(1) = 1
204 colsta(2) = 2
205 rowno(1) = 1
206!
207! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
208! x(1)
209! 1: NL
210!
211 nlflag(1) = 1
212!
213! Value (Linear only)
214! x(1)
215! 1: NL
216!
217 mono_readmatrix = 0 ! Return value means OK
218
219end Function mono_readmatrix
220!
221!==========================================================================
222! Compute nonlinear terms and non-constant Jacobian elements
223!
224
225!> Compute nonlinear terms and non-constant Jacobian elements
226!!
227!! @include{doc} fdeval_params.dox
228Integer Function mono_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
229 n, nz, thread, usrmem )
230#if defined(itl)
231!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Mono_FDEval
232#endif
233 implicit none
234 integer, intent (in) :: n ! number of variables
235 integer, intent (in) :: rowno ! number of the row to be evaluated
236 integer, intent (in) :: nz ! number of nonzeros in this row
237 real*8, intent (in), dimension(n) :: x ! vector of current solution values
238 real*8, intent (in out) :: g ! constraint value
239 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
240 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
241 ! in this row. Ffor information only.
242 integer, intent (in) :: mode ! evaluation mode: 1 = function value
243 ! 2 = derivatives, 3 = both
244 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
245 ! as errcnt is incremented
246 integer, intent (in out) :: errcnt ! error counter to be incremented in case
247 ! of function evaluation errors.
248 integer, intent (in) :: thread
249 real*8 usrmem(*) ! optional user memory
250!
251! Row 1: e1
252!
253 if ( rowno .eq. 1 ) then
254!
255! Mode = 1 or 3. G = log(x1)
256!
257 if ( mode .eq. 1 .or. mode .eq. 3 ) then
258 g = log(x(1))
259 endif
260!
261! Mode = 2 or 3: Derivative values:
262!
263 if ( mode .eq. 2 .or. mode .eq. 3 ) then
264 jac(1) = 1.d0/x(1)
265 endif
266 mono_fdeval = 0
267 else
268!
269! There are no other rows:
270!
271 mono_fdeval = 1
272 endif
273
274end Function mono_fdeval
275
276
277!> Evaluating nonlinear functions and derivatives on an interval. Used in preprocessing
278!!
279!! @include{doc} fdinterval_params.dox
280Integer Function mono_fdinterval( XMIN, XMAX, GMIN, GMAX, &
281 JMIN, JMAX, ROWNO, JCNM, &
282 MODE, PINF, N, NJ, USRMEM )
283#if defined(itl)
284!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Mono_FDInterval
285#endif
286 Implicit None
287 INTEGER, Intent(IN) :: rowno, mode, n, nj
288 INTEGER, Dimension(NJ), Intent(IN) :: jcnm
289 real*8, Dimension(N), Intent(IN) :: xmin, xmax
290 real*8, Intent(IN OUT) :: gmin, gmax
291 real*8, Dimension(N), Intent(IN OUT) :: jmin, jmax
292 real*8, Intent(IN) :: pinf
293 real*8, Intent(IN OUT) :: usrmem(*)
294
295!
296! Row 1: e1
297!
298 write(10,*) 'Enter Mono_FDInterval. Row=',rowno,' Mode=',mode
299 write(10,*) 'Xmin=',xmin
300 write(10,*) 'Xmax=',xmax
301 if ( rowno .eq. 1 ) then
302!
303! Mode = 1 or 3. G = log(x1)
304!
305 if ( mode .eq. 1 .or. mode .eq. 3 ) then
306 If ( xmin(1) <= 0.0d0 ) then
307 gmin = -pinf
308 else
309 gmin = log(xmin(1))
310 endif
311 If ( xmax(1) <= 0.0d0 ) then
312 gmax = -pinf
313 else
314 gmax = log(xmax(1))
315 endif
316 write(10,*) 'Gmin=',gmin,' Gmax=',gmax
317 endif
318!
319! Mode = 2 or 3: Derivative values:
320!
321 if ( mode .eq. 2 .or. mode .eq. 3 ) then
322 If ( xmin(1) <= 0.0d0 ) then
323 jmin(1) = -pinf
324 jmax(1) = +pinf
325 else
326 jmin(1) = 1.0d0/xmax(1)
327 jmax(1) = 1.0d0/xmin(1)
328 endif
329 write(10,*) 'Jmin=',jmin
330 write(10,*) 'Jmax=',jmax
331 endif
333 else
334!
335! There are no other rows:
336!
338 endif
339
340end Function mono_fdinterval
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_triord(mode, type, status, irow, icol, inf, value, resid, usrmem)
Definition comdecl.f90:291
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_optfile(cntvect, optfile)
define callback routine for defining an options file.
integer function coidef_triord(cntvect, coi_triord)
define callback routine for providing the triangular order information.
integer function coidef_fdinterval(cntvect, coi_fdinterval)
define callback routine for performing function and derivative evaluations on intervals.
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_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_objvar(cntvect, objvar)
defines the Objective Variable.
Definition coistart.f90:586
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
integer function mono_fdinterval(xmin, xmax, gmin, gmax, jmin, jmax, rowno, jcnm, mode, pinf, n, nj, usrmem)
Evaluating nonlinear functions and derivatives on an interval. Used in preprocessing.
Definition mono01.f90:281
integer function mono_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
Definition mono01.f90:140
integer function mono_fdeval(x, g, jac, rowno, jcnm, mode, ignerr, errcnt, n, nz, thread, usrmem)
Compute nonlinear terms and non-constant Jacobian elements.
Definition mono01.f90:228
program mono04
Main program. A simple setup and call of CONOPT.
Definition mono04.f90:27
#define nj
Definition mp_trans.c:46
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