CONOPT
Loading...
Searching...
No Matches
overlap01.f90
Go to the documentation of this file.
1!> @file overlap01.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! Example 01 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 .. log(x1) =E= 0;
15!! e2 .. sqr(x1) =E= 1;
16!!
17!! x1.lo = 0.01;
18!! model Overlap / all /;
19!! solve Overlap using nlp maximizing x1;
20!! @endverbatim
21!!
22!!
23!!
24!! For more information about the individual callbacks, please have a look at the source code.
25
26#if defined(_WIN32) && !defined(_WIN64)
27#define dec_directives_win32
28#endif
29
30!> Main program. A simple setup and call of CONOPT
31!!
32Program overlap01
33
35 Use conopt
36 implicit None
37!
38! Declare the user callback routines as Integer, External:
39!
40 Integer, External :: overlap_readmatrix ! Mandatory Matrix definition routine defined below
41 Integer, External :: overlap_fdeval ! 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#ifdef dec_directives_win32
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 :: 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, Dimension(:), Pointer :: cntvect
61 INTEGER :: coi_error
62
63 call startup
64!
65! Create and initialize a Control Vector
66!
67 coi_error = coi_create( cntvect )
68!
69! Tell CONOPT about the size of the model by populating the Control Vector:
70!
71 coi_error = max( coi_error, coidef_numvar( cntvect, 1 ) ) ! # variables
72 coi_error = max( coi_error, coidef_numcon( cntvect, 2 ) ) ! # constraints
73 coi_error = max( coi_error, coidef_numnz( cntvect, 2 ) ) ! # nonzeros in the Jacobian
74 coi_error = max( coi_error, coidef_numnlnz( cntvect, 2 ) ) ! # of which are nonlinear
75 coi_error = max( coi_error, coidef_optdir( cntvect, +1 ) ) ! Maximize
76 coi_error = max( coi_error, coidef_objvar( cntvect, 1 ) ) ! Objective is variable 1
77 coi_error = max( coi_error, coidef_optfile( cntvect, 'Overlap01.opt' ) )
78!
79! Tell CONOPT about the callback routines:
80!
81 coi_error = max( coi_error, coidef_readmatrix( cntvect, overlap_readmatrix ) )
82 coi_error = max( coi_error, coidef_fdeval( cntvect, overlap_fdeval ) )
83 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
84 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
85 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
86 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
87 coi_error = max( coi_error, coidef_triord( cntvect, std_triord ) )
88
89#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
90 coi_error = max( coi_error, coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_license_text) )
91#endif
92
93 If ( coi_error .ne. 0 ) THEN
94 write(*,*)
95 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
96 write(*,*)
97 call flog( "Skipping Solve due to setup errors", 1 )
98 ENDIF
99!
100! Save the solution so we can check the duals:
101!
102 do_allocate = .true.
103!
104! Start CONOPT:
105!
106 coi_error = coi_solve( cntvect )
107
108 write(*,*)
109 write(*,*) 'End of Overlap01 example. Return code=',coi_error
110
111 If ( coi_error /= 0 ) then
112 call flog( "Errors encountered during solution", 1 )
113 elseif ( stacalls == 0 .or. solcalls == 0 ) then
114 call flog( "Status or Solution routine was not called", 1 )
115 elseif ( sstat /= 1 .or. ( mstat /= 1 .and. mstat /= 2 ) ) then
116 call flog( "Solver and Model Status was not as expected (1,2) or (1,1)", 1 )
117 elseif ( abs( obj-1.0d0 ) > 0.000001d0 ) then
118 call flog( "Incorrect objective returned", 1 )
119 Else
120 Call checkdual( 'Overlap01', maximize )
121 endif
122
123 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
124
125 call flog( "Successful Solve", 0 )
127End Program overlap01
128!
129! ============================================================================
130! Define information about the model:
131!
132
133!> Define information about the model
134!!
135!! @include{doc} readMatrix_params.dox
136Integer Function overlap_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
137 colsta, rowno, value, nlflag, n, m, nz, &
138 usrmem )
139#ifdef dec_directives_win32
140!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Overlap_ReadMatrix
141#endif
142 implicit none
143 integer, intent (in) :: n ! number of variables
144 integer, intent (in) :: m ! number of constraints
145 integer, intent (in) :: nz ! number of nonzeros
146 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
147 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
148 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
149 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
150 ! (not defined here)
151 integer, intent (out), dimension(m) :: type ! vector of equation types
152 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
153 ! (not defined here)
154 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
155 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
156 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
157 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
158 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
159 real*8 usrmem(*) ! optional user memory
160!
161! Information about Variables:
162! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
163! Default: the status information in Vsta is not used.
164!
165! The model uses defaults
166!
167! Information about Constraints:
168! Default: Rhs = 0
169! Default: the status information in Esta and the function
170! value in FV are not used.
171! Default: Type: There is no default.
172! 0 = Equality,
173! 1 = Greater than or equal,
174! 2 = Less than or equal,
175! 3 = Non binding.
176!
177! Constraint 1: e1
178! Rhs = 0.0 and type Equality
179!
180 rhs(1) = 0.0d0
181 type(1) = 0
182!
183! Constraint 1: e2
184! Rhs = 1.0 and type Equality
185!
186 rhs(2) = 1.0d0
187 type(2) = 0
188!
189 lower(1) = 0.01d0
190 curr(1) = 0.01d0
191!
192! Information about the Jacobian. CONOPT expects a columnwise
193! representation in Rowno, Value, Nlflag and Colsta.
194!
195! Colsta = Start of column indices (No Defaults):
196! Rowno = Row indices
197! Value = Value of derivative (by default only linear
198! derivatives are used)
199! Nlflag = 0 for linear and 1 for nonlinear derivative
200! (not needed for completely linear models)
201!
202! Indices
203! x(1)
204! 1: 1
205! 2: 2
206!
207 colsta(1) = 1
208 colsta(2) = 3
209 rowno(1) = 1
210 rowno(2) = 2
211!
212! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
213! x(1)
214! 1: NL
215!
216 nlflag(1) = 1
217 nlflag(2) = 1
218!
219! Value (Linear only)
220! x(1)
221! 1: NL
222!
223 overlap_readmatrix = 0 ! Return value means OK
224
225end Function overlap_readmatrix
226!
227!==========================================================================
228! Compute nonlinear terms and non-constant Jacobian elements
229!
230
231!> Compute nonlinear terms and non-constant Jacobian elements
232!!
233!! @include{doc} fdeval_params.dox
234Integer Function overlap_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
235 n, nz, thread, usrmem )
236#ifdef dec_directives_win32
237!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Overlap_FDEval
238#endif
239 implicit none
240 integer, intent (in) :: n ! number of variables
241 integer, intent (in) :: rowno ! number of the row to be evaluated
242 integer, intent (in) :: nz ! number of nonzeros in this row
243 real*8, intent (in), dimension(n) :: x ! vector of current solution values
244 real*8, intent (in out) :: g ! constraint value
245 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
246 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
247 ! in this row. Ffor information only.
248 integer, intent (in) :: mode ! evaluation mode: 1 = function value
249 ! 2 = derivatives, 3 = both
250 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
251 ! as errcnt is incremented
252 integer, intent (in out) :: errcnt ! error counter to be incremented in case
253 ! of function evaluation errors.
254 integer, intent (in) :: thread
255 real*8 usrmem(*) ! optional user memory
256!
257! Row 1: e1
258!
259 if ( rowno .eq. 1 ) then
260!
261! Mode = 1 or 3. G = log(x1)
262!
263 if ( mode .eq. 1 .or. mode .eq. 3 ) then
264 g = log(x(1))
265 endif
266!
267! Mode = 2 or 3: Derivative values:
268!
269 if ( mode .eq. 2 .or. mode .eq. 3 ) then
270 jac(1) = 1.d0/x(1)
271 endif
273 else if ( rowno .eq. 2 ) then
274!
275! Mode = 1 or 3. G = sqr(x1)
276!
277 if ( mode .eq. 1 .or. mode .eq. 3 ) then
278 g = x(1)*x(1)
279 endif
280!
281! Mode = 2 or 3: Derivative values:
282!
283 if ( mode .eq. 2 .or. mode .eq. 3 ) then
284 jac(1) = 2.d0*x(1)
285 endif
287 else
288!
289! There are no other rows:
290!
292 endif
293
294end Function overlap_fdeval
integer function std_solution(xval, xmar, xbas, xsta, yval, ymar, ybas, ysta, n, m, usrmem)
Definition comdecl.f90:132
integer function std_status(modsta, solsta, iter, objval, usrmem)
Definition comdecl.f90:88
subroutine checkdual(case, minmax)
Definition comdecl.f90:394
integer function std_message(smsg, dmsg, nmsg, llen, usrmem, msgv)
Definition comdecl.f90:205
integer function std_triord(mode, type, status, irow, icol, inf, value, resid, usrmem)
Definition comdecl.f90:289
integer function std_errmsg(rowno, colno, posno, msglen, usrmem, msg)
Definition comdecl.f90:248
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_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
real *8 obj
Definition comdecl.f90:16
integer solcalls
Definition comdecl.f90:15
integer sstat
Definition comdecl.f90:18
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.
program overlap01
Main program. A simple setup and call of CONOPT.
Definition overlap01.f90:34
integer function overlap_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.