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#if defined(_WIN32) && !defined(_WIN64)
29#define dec_directives_win32
30#endif
31
32!> Main program. A simple setup and call of CONOPT
33!!
34Program overlap02
35
37 Use conopt
38 implicit None
39!
40! Declare the user callback routines as Integer, External:
41!
42 Integer, External :: overlap_readmatrix ! Mandatory Matrix definition routine defined below
43 Integer, External :: overlap_fdeval ! Function and Derivative evaluation routine
44 ! needed a nonlinear model.
45 Integer, External :: overlap_fdinterval ! Function and Derivative evaluation routine
46 ! needed a nonlinear model.
47 Integer, External :: std_status ! Standard callback for displaying solution status
48 Integer, External :: std_solution ! Standard callback for displaying solution values
49 Integer, External :: std_message ! Standard callback for managing messages
50 Integer, External :: std_errmsg ! Standard callback for managing error messages
51 Integer, External :: std_triord ! Standard callback for Overlapngular order
52#ifdef dec_directives_win32
53!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Overlap_ReadMatrix
54!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Overlap_FDEval
55!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Overlap_FDInterval
56!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
57!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
58!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
59!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
60!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_TriOrd
61#endif
62!
63! Control vector
64!
65 INTEGER, Dimension(:), Pointer :: cntvect
66 INTEGER :: coi_error
67
68 call startup
69!
70! Create and initialize a Control Vector
71!
72 coi_error = coi_create( cntvect )
73!
74! Tell CONOPT about the size of the model by populating the Control Vector:
75!
76 coi_error = max( coi_error, coidef_numvar( cntvect, 1 ) ) ! # variables
77 coi_error = max( coi_error, coidef_numcon( cntvect, 2 ) ) ! # constraints
78 coi_error = max( coi_error, coidef_numnz( cntvect, 2 ) ) ! # nonzeros in the Jacobian
79 coi_error = max( coi_error, coidef_numnlnz( cntvect, 2 ) ) ! # of which are nonlinear
80 coi_error = max( coi_error, coidef_optdir( cntvect, +1 ) ) ! Maximize
81 coi_error = max( coi_error, coidef_objvar( cntvect, 1 ) ) ! Objective is variable 1
82 coi_error = max( coi_error, coidef_optfile( cntvect, 'Overlap02.opt' ) )
83!
84! Tell CONOPT about the callback routines:
85!
86 coi_error = max( coi_error, coidef_readmatrix( cntvect, overlap_readmatrix ) )
87 coi_error = max( coi_error, coidef_fdeval( cntvect, overlap_fdeval ) )
88 coi_error = max( coi_error, coidef_fdinterval( cntvect, overlap_fdinterval ) )
89 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
90 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
91 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
92 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
93 coi_error = max( coi_error, coidef_triord( cntvect, std_triord ) )
94
95#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
96 coi_error = max( coi_error, coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_license_text) )
97#endif
98
99 If ( coi_error .ne. 0 ) THEN
100 write(*,*)
101 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
102 write(*,*)
103 call flog( "Skipping Solve due to setup errors", 1 )
104 ENDIF
105!
106! Save the solution so we can check the duals:
107!
108 do_allocate = .true.
109!
110! Start CONOPT:
111!
112 coi_error = coi_solve( cntvect )
113
114 write(*,*)
115 write(*,*) 'End of Overlap02 example. Return code=',coi_error
116
117 If ( coi_error /= 0 ) then
118 call flog( "Errors encountered during solution", 1 )
119 elseif ( stacalls == 0 .or. solcalls == 0 ) then
120 call flog( "Status or Solution routine was not called", 1 )
121 elseif ( sstat /= 1 .or. mstat /= 1 ) then
122 call flog( "Solver and Model Status was not as expected (1,1)", 1 )
123 elseif ( abs( obj-1.0d0 ) > 0.000001d0 ) then
124 call flog( "Incorrect objective returned", 1 )
125 Else
126 Call checkdual( 'Overlap02', maximize )
127 endif
128
129 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
130
131 call flog( "Successful Solve", 0 )
132!
133! Free solution memory
134!
136
137End Program overlap02
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 overlap_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
147 colsta, rowno, value, nlflag, n, m, nz, &
148 usrmem )
149#ifdef dec_directives_win32
150!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Overlap_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 = 0.0 and type Equality
189!
190 rhs(1) = 1.0d0
191 type(1) = 1
192!
193! Constraint 1: e2
194! Rhs = 1.0 and type Equality
195!
196 rhs(2) = 1.0d0
197 type(2) = 2
198!
199!
200! Information about the Jacobian. CONOPT expects a columnwise
201! representation in Rowno, Value, Nlflag and Colsta.
202!
203! Colsta = Start of column indices (No Defaults):
204! Rowno = Row indices
205! Value = Value of derivative (by default only linear
206! derivatives are used)
207! Nlflag = 0 for linear and 1 for nonlinear derivative
208! (not needed for completely linear models)
209!
210! Indices
211! x(1)
212! 1: 1
213! 2: 2
214!
215 colsta(1) = 1
216 colsta(2) = 3
217 rowno(1) = 1
218 rowno(2) = 2
219!
220! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
221! x(1)
222! 1: NL
223!
224 nlflag(1) = 1
225 nlflag(2) = 1
226!
227! Value (Linear only)
228! x(1)
229! 1: NL
230!
231 overlap_readmatrix = 0 ! Return value means OK
232
233end Function overlap_readmatrix
234!
235!==========================================================================
236! Compute nonlinear terms and non-constant Jacobian elements
237!
238
239!> Compute nonlinear terms and non-constant Jacobian elements
240!!
241!! @include{doc} fdeval_params.dox
242Integer Function overlap_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
243 n, nz, thread, usrmem )
244#ifdef dec_directives_win32
245!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Overlap_FDEval
246#endif
247 implicit none
248 integer, intent (in) :: n ! number of variables
249 integer, intent (in) :: rowno ! number of the row to be evaluated
250 integer, intent (in) :: nz ! number of nonzeros in this row
251 real*8, intent (in), dimension(n) :: x ! vector of current solution values
252 real*8, intent (in out) :: g ! constraint value
253 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
254 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
255 ! in this row. Ffor information only.
256 integer, intent (in) :: mode ! evaluation mode: 1 = function value
257 ! 2 = derivatives, 3 = both
258 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
259 ! as errcnt is incremented
260 integer, intent (in out) :: errcnt ! error counter to be incremented in case
261 ! of function evaluation errors.
262 integer, intent (in) :: thread
263 real*8 usrmem(*) ! optional user memory
264!
265! Row 1: e1
266!
267 if ( rowno .eq. 1 ) then
268!
269! Mode = 1 or 3. G = exp(x1)
270!
271 if ( mode .eq. 1 .or. mode .eq. 3 ) then
272 g = exp(x(1))
273 endif
274!
275! Mode = 2 or 3: Derivative values:
276!
277 if ( mode .eq. 2 .or. mode .eq. 3 ) then
278 jac(1) = exp(x(1))
279 endif
281 else if ( rowno .eq. 2 ) then
282!
283! Mode = 1 or 3. G = sqr(x1)
284!
285 if ( mode .eq. 1 .or. mode .eq. 3 ) then
286 g = x(1)*x(1)
287 endif
288!
289! Mode = 2 or 3: Derivative values:
290!
291 if ( mode .eq. 2 .or. mode .eq. 3 ) then
292 jac(1) = 2.d0*x(1)
293 endif
295 else
296!
297! There are no other rows:
298!
300 endif
301
302end Function overlap_fdeval
303
304
305!> Evaluating nonlinear functions and derivatives on an interval. Used in preprocessing
306!!
307!! @include{doc} fdinterval_params.dox
308Integer Function overlap_fdinterval( XMIN, XMAX, GMIN, GMAX, &
309 JMIN, JMAX, ROWNO, JCNM, &
310 MODE, PINF, N, NJ, USRMEM )
311#ifdef dec_directives_win32
312!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Overlap_FDInterval
313#endif
314 Implicit None
315 INTEGER, Intent(IN) :: rowno, mode, n, nj
316 INTEGER, Dimension(NJ), Intent(IN) :: jcnm
317 real*8, Dimension(N), Intent(IN) :: xmin, xmax
318 real*8, Intent(IN OUT) :: gmin, gmax
319 real*8, Dimension(N), Intent(IN OUT) :: jmin, jmax
320 real*8, Intent(IN) :: pinf
321 real*8, Intent(IN OUT) :: usrmem(*)
322
323!
324! Row 1: e1
325!
326 write(10,*) 'Enter Overlap_FDInterval. Row=',rowno,' Mode=',mode
327 write(10,*) 'Xmin=',xmin
328 write(10,*) 'Xmax=',xmax
329 if ( rowno .eq. 1 ) then
330!
331! Mode = 1 or 3. G = exp(x1)
332!
333 if ( mode .eq. 1 .or. mode .eq. 3 ) then
334 gmin = exp(xmin(1))
335 if ( xmax(1) > log(pinf) ) Then
336 gmax = pinf
337 else
338 gmax = exp(xmax(1))
339 endif
340 write(10,*) 'Gmin=',gmin,' Gmax=',gmax
341 endif
342!
343! Mode = 2 or 3: Derivative values:
344!
345 if ( mode .eq. 2 .or. mode .eq. 3 ) then
346 jmin(1) = exp(xmin(1))
347 if ( xmax(1) > log(pinf) ) Then
348 jmax(1) = pinf
349 else
350 jmax(1) = exp(xmax(1))
351 endif
352 write(10,*) 'Jmin=',jmin
353 write(10,*) 'Jmax=',jmax
354 endif
356 Else if ( rowno .eq. 2 ) then
357!
358! Mode = 1 or 3. G = sqr(x1)
359!
360 if ( mode .eq. 1 .or. mode .eq. 3 ) then
361 if ( xmin(1) >= 0.0d0 ) Then
362 gmin = xmin(1)*xmin(1)
363 gmax = min( xmax(1)*xmax(1), pinf )
364 Else if ( xmax(1) <= 0.0d0 ) Then
365 gmin = xmax(1)*xmax(1)
366 gmax = min( xmin(1)*xmin(1), pinf )
367 else
368 gmin = 0.0d0
369 gmax = min( max( xmax(1)*xmax(1), xmin(1)*xmin(1) ), pinf )
370 endif
371 write(10,*) 'Gmin=',gmin,' Gmax=',gmax
372 endif
373!
374! Mode = 2 or 3: Derivative values:
375!
376 if ( mode .eq. 2 .or. mode .eq. 3 ) then
377 jmin(1) = max( min( 2.d0*xmin(1), pinf ), -pinf )
378 jmax(1) = max( min( 2.d0*xmax(1), pinf ), -pinf )
379 write(10,*) 'Jmin=',jmin
380 write(10,*) 'Jmax=',jmax
381 endif
383 else
384!
385! There are no other rows:
386!
388 endif
389
390end Function overlap_fdinterval
integer function std_solution(xval, xmar, xbas, xsta, yval, ymar, ybas, ysta, n, m, usrmem)
Definition comdecl.f90:170
integer function std_status(modsta, solsta, iter, objval, usrmem)
Definition comdecl.f90:126
subroutine checkdual(case, minmax)
Definition comdecl.f90:432
integer function std_message(smsg, dmsg, nmsg, llen, usrmem, msgv)
Definition comdecl.f90:243
integer function std_triord(mode, type, status, irow, icol, inf, value, resid, usrmem)
Definition comdecl.f90:327
integer function std_errmsg(rowno, colno, posno, msglen, usrmem, msg)
Definition comdecl.f90:286
integer(c_int) function coidef_message(cntvect, coi_message)
define callback routine for handling messages returned during the solution process.
Definition conopt.f90:1265
integer(c_int) function coidef_solution(cntvect, coi_solution)
define callback routine for returning the final solution values.
Definition conopt.f90:1238
integer(c_int) function coidef_status(cntvect, coi_status)
define callback routine for returning the completion status.
Definition conopt.f90:1212
integer(c_int) function coidef_readmatrix(cntvect, coi_readmatrix)
define callback routine for providing the matrix data to CONOPT.
Definition conopt.f90:1111
integer(c_int) function coidef_errmsg(cntvect, coi_errmsg)
define callback routine for returning error messages for row, column or Jacobian elements.
Definition conopt.f90:1291
integer(c_int) function coidef_fdeval(cntvect, coi_fdeval)
define callback routine for performing function and derivative evaluations.
Definition conopt.f90:1135
integer(c_int) function coidef_optfile(cntvect, optfile)
define callback routine for defining an options file.
Definition conopt.f90:928
integer(c_int) function coidef_fdinterval(cntvect, coi_fdinterval)
define callback routine for performing function and derivative evaluations on intervals.
Definition conopt.f90:1396
integer(c_int) function coidef_triord(cntvect, coi_triord)
define callback routine for providing the triangular order information.
Definition conopt.f90:1371
integer(c_int) function coidef_license(cntvect, licint1, licint2, licint3, licstring)
define the License Information.
Definition conopt.f90:293
integer(c_int) function coidef_numvar(cntvect, numvar)
defines the number of variables in the model.
Definition conopt.f90:97
integer(c_int) function coidef_numcon(cntvect, numcon)
defines the number of constraints in the model.
Definition conopt.f90:121
integer(c_int) function coidef_numnlnz(cntvect, numnlnz)
defines the Number of Nonlinear Nonzeros.
Definition conopt.f90:167
integer(c_int) function coidef_optdir(cntvect, optdir)
defines the Optimization Direction.
Definition conopt.f90:213
integer(c_int) function coidef_numnz(cntvect, numnz)
defines the number of nonzero elements in the Jacobian.
Definition conopt.f90:144
integer(c_int) function coidef_objvar(cntvect, objvar)
defines the Objective Variable.
Definition conopt.f90:257
integer(c_int) function coi_create(cntvect)
initializes CONOPT and creates the control vector.
Definition conopt.f90:1726
integer(c_int) function coi_free(cntvect)
frees the control vector.
Definition conopt.f90:1749
integer(c_int) function coi_solve(cntvect)
method for starting the solving process of CONOPT.
Definition conopt.f90:1625
#define nj
Definition mp_trans.c:46
real *8 obj
Definition comdecl.f90:16
integer solcalls
Definition comdecl.f90:15
integer sstat
Definition comdecl.f90:18
subroutine finalize
Definition comdecl.f90:79
integer stacalls
Definition comdecl.f90:14
subroutine flog(msg, code)
Definition comdecl.f90:62
logical do_allocate
Definition comdecl.f90:27
integer, parameter maximize
Definition comdecl.f90:31
integer mstat
Definition comdecl.f90:17
subroutine startup
Definition comdecl.f90:41
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:36
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.