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