CONOPT
Loading...
Searching...
No Matches
mono08.f90
Go to the documentation of this file.
1!> @file mono08.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! Monotone function to bound conversion example 08
6!!
7!! This is a CONOPT implementation of the GAMS model:
8!!
9!! @verbatim
10!! variable x1
11!! equation e1;
12!!
13!! e1 .. sqr(x1) =E= 4;
14!!
15!! x1.lo = 0.0; x.l = 0.0;
16!! * Comment: The slope at the initial value is zero, but the function
17!! * is monotone within the feasible region and x must therefore be
18!! * increased.
19!! model mono / all /;
20!! solve mono using nlp maximizing x1;
21!! @endverbatim
22!!
23!!
24!!
25!! For more information about the individual callbacks, please have a look at the source code.
26
27!> Main program. A simple setup and call of CONOPT
28!!
29Program mono08
30
31 Use proginfo
32 Use coidef
33 implicit None
34!
35! Declare the user callback routines as Integer, External:
36!
37 Integer, External :: mono_readmatrix ! Mandatory Matrix definition routine defined below
38 Integer, External :: mono_fdeval ! Function and Derivative evaluation routine
39 ! needed a nonlinear model.
40 Integer, External :: mono_fdinterval ! Function and Derivative evaluation routine
41 ! needed a nonlinear model.
42 Integer, External :: std_status ! Standard callback for displaying solution status
43 Integer, External :: std_solution ! Standard callback for displaying solution values
44 Integer, External :: std_message ! Standard callback for managing messages
45 Integer, External :: std_errmsg ! Standard callback for managing error messages
46 Integer, External :: std_triord ! Standard callback for Monongular order
47#if defined(itl)
48!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Mono_ReadMatrix
49!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Mono_FDEval
50!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Mono_FDInterval
51!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
52!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
53!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
54!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
55!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_TriOrd
56#endif
57!
58! Control vector
59!
60 INTEGER :: numcallback
61 INTEGER, Dimension(:), Pointer :: cntvect
62 INTEGER :: coi_error
63
64 call startup
65!
66! Create and initialize a Control Vector
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, 1 ) ) ! # variables
75 coi_error = max( coi_error, coidef_numcon( cntvect, 1 ) ) ! # constraints
76 coi_error = max( coi_error, coidef_numnz( cntvect, 1 ) ) ! # nonzeros in the Jacobian
77 coi_error = max( coi_error, coidef_numnlnz( cntvect, 1 ) ) ! # of which are nonlinear
78 coi_error = max( coi_error, coidef_optdir( cntvect, +1 ) ) ! Maximize
79 coi_error = max( coi_error, coidef_objvar( cntvect, 1 ) ) ! Objective is variable 3
80 coi_error = max( coi_error, coidef_optfile( cntvect, 'Mono08.opt' ) )
81!
82! Tell CONOPT about the callback routines:
83!
84 coi_error = max( coi_error, coidef_readmatrix( cntvect, mono_readmatrix ) )
85 coi_error = max( coi_error, coidef_fdeval( cntvect, mono_fdeval ) )
86 coi_error = max( coi_error, coidef_fdinterval( cntvect, mono_fdinterval ) )
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 coi_error = max( coi_error, coidef_triord( cntvect, std_triord ) )
92
93#if defined(LICENSE_INT_1) && defined(LICENSE_INT_2) && defined(LICENSE_INT_3) && defined(LICENSE_TEXT)
94 coi_error = max( coi_error, coidef_license( cntvect, license_int_1, license_int_2, license_int_3, license_text) )
95#endif
96
97 If ( coi_error .ne. 0 ) THEN
98 write(*,*)
99 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
100 write(*,*)
101 call flog( "Skipping Solve due to setup errors", 1 )
102 ENDIF
103!
104! Save the solution so we can check the duals:
105!
106 do_allocate = .true.
107!
108! Start CONOPT:
109!
110 coi_error = coi_solve( cntvect )
111
112 write(*,*)
113 write(*,*) 'End of Mono08 example. Return code=',coi_error
114
115 If ( coi_error /= 0 ) then
116 call flog( "Errors encountered during solution", 1 )
117 elseif ( stacalls == 0 .or. solcalls == 0 ) then
118 call flog( "Status or Solution routine was not called", 1 )
119 elseif ( sstat /= 1 .or. mstat /= 1 ) then
120 call flog( "Solver and Model Status was not as expected (1,1)", 1 )
121 elseif ( abs( obj-2.0d0 ) > 0.000001d0 ) then
122 call flog( "Incorrect objective returned", 1 )
123 Else
124 Call checkdual( 'Mono08', maximize )
125 endif
126
127 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
128
129 call flog( "Successful Solve", 0 )
130
131End Program mono08
132!
133! ============================================================================
134! Define information about the model:
135!
136
137!> Define information about the model
138!!
139!! @include{doc} readMatrix_params.dox
140Integer Function mono_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
141 colsta, rowno, value, nlflag, n, m, nz, &
142 usrmem )
143#if defined(itl)
144!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Mono_ReadMatrix
145#endif
146 implicit none
147 integer, intent (in) :: n ! number of variables
148 integer, intent (in) :: m ! number of constraints
149 integer, intent (in) :: nz ! number of nonzeros
150 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
151 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
152 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
153 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
154 ! (not defined here)
155 integer, intent (out), dimension(m) :: type ! vector of equation types
156 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
157 ! (not defined here)
158 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
159 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
160 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
161 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
162 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
163 real*8 usrmem(*) ! optional user memory
164!
165! Information about Variables:
166! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
167! Default: the status information in Vsta is not used.
168!
169! The model uses defaults
170!
171! Information about Constraints:
172! Default: Rhs = 0
173! Default: the status information in Esta and the function
174! value in FV are not used.
175! Default: Type: There is no default.
176! 0 = Equality,
177! 1 = Greater than or equal,
178! 2 = Less than or equal,
179! 3 = Non binding.
180!
181! Constraint 1: e1
182! Rhs = 10.0 and type Equality
183!
184 rhs(1) = 4.0d0
185 type(1) = 0
186!
187 lower(1) = 0.0d0
188 curr(1) = 0.0d0
189!
190! Information about the Jacobian. We use the standard method with
191! Rowno, Value, Nlflag and Colsta and we do not use Colno.
192!
193! Colsta = Start of column indices (No Defaults):
194! Rowno = Row indices
195! Value = Value of derivative (by default only linear
196! derivatives are used)
197! Nlflag = 0 for linear and 1 for nonlinear derivative
198! (not needed for completely linear models)
199!
200! Indices
201! x(1)
202! 1: 1
203!
204 colsta(1) = 1
205 colsta(2) = 2
206 rowno(1) = 1
207!
208! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
209! x(1)
210! 1: NL
211!
212 nlflag(1) = 1
213!
214! Value (Linear only)
215! x(1)
216! 1: NL
217!
218 mono_readmatrix = 0 ! Return value means OK
219
220end Function mono_readmatrix
221!
222!==========================================================================
223! Compute nonlinear terms and non-constant Jacobian elements
224!
225
226!> Compute nonlinear terms and non-constant Jacobian elements
227!!
228!! @include{doc} fdeval_params.dox
229Integer Function mono_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
230 n, nz, thread, usrmem )
231#if defined(itl)
232!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Mono_FDEval
233#endif
234 implicit none
235 integer, intent (in) :: n ! number of variables
236 integer, intent (in) :: rowno ! number of the row to be evaluated
237 integer, intent (in) :: nz ! number of nonzeros in this row
238 real*8, intent (in), dimension(n) :: x ! vector of current solution values
239 real*8, intent (in out) :: g ! constraint value
240 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
241 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
242 ! in this row. Ffor information only.
243 integer, intent (in) :: mode ! evaluation mode: 1 = function value
244 ! 2 = derivatives, 3 = both
245 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
246 ! as errcnt is incremented
247 integer, intent (in out) :: errcnt ! error counter to be incremented in case
248 ! of function evaluation errors.
249 integer, intent (in) :: thread
250 real*8 usrmem(*) ! optional user memory
251!
252! Row 1: e1
253!
254 if ( rowno .eq. 1 ) then
255!
256! Mode = 1 or 3. G = log(x1)
257!
258 if ( mode .eq. 1 .or. mode .eq. 3 ) then
259 g = x(1)*x(1)
260 endif
261!
262! Mode = 2 or 3: Derivative values:
263!
264 if ( mode .eq. 2 .or. mode .eq. 3 ) then
265 jac(1) = 2.d0*x(1)
266 endif
267 mono_fdeval = 0
268 else
269!
270! There are no other rows:
271!
272 mono_fdeval = 1
273 endif
274
275end Function mono_fdeval
276
277
278!> Evaluating nonlinear functions and derivatives on an interval. Used in preprocessing
279!!
280!! @include{doc} fdinterval_params.dox
281Integer Function mono_fdinterval( XMIN, XMAX, GMIN, GMAX, &
282 JMIN, JMAX, ROWNO, JCNM, &
283 MODE, PINF, N, NJ, USRMEM )
284#if defined(itl)
285!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Mono_FDInterval
286#endif
287 Implicit None
288 INTEGER, Intent(IN) :: rowno, mode, n, nj
289 INTEGER, Dimension(NJ), Intent(IN) :: jcnm
290 real*8, Dimension(N), Intent(IN) :: xmin, xmax
291 real*8, Intent(IN OUT) :: gmin, gmax
292 real*8, Dimension(N), Intent(IN OUT) :: jmin, jmax
293 real*8, Intent(IN) :: pinf
294 real*8, Intent(IN OUT) :: usrmem(*)
295
296!
297! Row 1: e1
298!
299 write(10,*) 'Enter Mono_FDInterval. Row=',rowno,' Mode=',mode
300 write(10,*) 'Xmin=',xmin
301 write(10,*) 'Xmax=',xmax
302 if ( rowno .eq. 1 ) then
303!
304! Mode = 1 or 3. G = log(x1)
305!
306 if ( mode .eq. 1 .or. mode .eq. 3 ) then
307 If ( xmin(1) >= 0.0d0 ) then
308 gmin = xmin(1)*xmin(1)
309 gmax = xmax(1)*xmax(1)
310 else if ( xmax(1) <= 0.0d0 ) then
311 gmin = xmax(1)*xmax(1)
312 gmax = xmin(1)*xmin(1)
313 else
314 gmin = 0.0d0
315 gmax = max(xmax(1)*xmax(1),xmin(1)*xmin(1))
316 endif
317 write(10,*) 'Gmin=',gmin,' Gmax=',gmax
318 endif
319!
320! Mode = 2 or 3: Derivative values:
321!
322 if ( mode .eq. 2 .or. mode .eq. 3 ) then
323 jmin(1) = 2.0d0*xmin(1)
324 jmax(1) = 2.0d0*xmax(1)
325 write(10,*) 'Jmin=',jmin
326 write(10,*) 'Jmax=',jmax
327 endif
329 else
330!
331! There are no other rows:
332!
334 endif
335
336end 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 mono08
Main program. A simple setup and call of CONOPT.
Definition mono08.f90:29
#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