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