CONOPT
Loading...
Searching...
No Matches
const15.f90
Go to the documentation of this file.
1!> @file const15.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! Model with derivatives that become constant after other variables are fixed.
6!!
7!! This is a CONOPT implementation of the GAMS model:
8!!
9!! @verbatim
10!! e1: max x1+x3
11!! e2: x1*x2 + sqr(x3) =l= -5
12!! x2.fx = 1;
13!! -5 <= x1 <= 10; x1.l = 3
14!! -10 <= x3 <= 10; x3.l = 3
15!! @endverbatim
16!!
17!! The model is similar to const14 but the right hand side is changed to -5. The feasible space
18!! has only one point, (x1,x3) = (-5,0), but it is not defined by sharp constraints.
19!! e1 is post-triangular.
20!! The expected solution is: x1 = -5 as a soft upper bound and x3 = 0 giving objective = -5.
21!! Since the marginal on e2 is infinite we will probably get a solution with x3 slightly positive.
22!! The solution should be labeled locally optimal.
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 const15
30
31 Use proginfo
32 Use coidef
33 implicit None
34!
35! Declare the user callback routines as Integer, External:
36!
37 Integer, External :: con_readmatrix ! Mandatory Matrix definition routine defined below
38 Integer, External :: con_fdeval ! Function and Derivative evaluation routine
39 ! needed a nonlinear model.
40 Integer, External :: con_fdinterval ! Function and Derivative evaluation routine
41 ! optional for 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#if defined(itl)
47!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Con_ReadMatrix
48!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Con_FDEval
49!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Con_FDInterval
50!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
51!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
52!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
53!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
54#endif
55!
56! Control vector
57!
58 INTEGER, Dimension(:), Pointer :: cntvect
59 INTEGER :: coi_error
60!
61! Create and initialize a Control Vector
62!
63 call startup
64
65 coi_error = coi_createfort( cntvect )
66!
67! Tell CONOPT about the size of the model by populating the Control Vector:
68!
69 coi_error = max( coi_error, coidef_numvar( cntvect, 3 ) ) ! # variables
70 coi_error = max( coi_error, coidef_numcon( cntvect, 2 ) ) ! # constraints
71 coi_error = max( coi_error, coidef_numnz( cntvect, 5 ) ) ! # nonzeros in the Jacobian
72 coi_error = max( coi_error, coidef_numnlnz( cntvect, 3 ) ) ! # of which are nonlinear
73 coi_error = max( coi_error, coidef_optdir( cntvect, 1 ) ) ! Maximize
74 coi_error = max( coi_error, coidef_objcon( cntvect, 1 ) ) ! Objective is constraint 1
75 coi_error = max( coi_error, coidef_optfile( cntvect, 'const15.opt' ) )
76!
77! Tell CONOPT about the callback routines:
78!
79 coi_error = max( coi_error, coidef_readmatrix( cntvect, con_readmatrix ) )
80 coi_error = max( coi_error, coidef_fdeval( cntvect, con_fdeval ) )
81 coi_error = max( coi_error, coidef_fdinterval( cntvect, con_fdinterval ) )
82 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
83 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
84 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
85 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
86
87#if defined(LICENSE_INT_1) && defined(LICENSE_INT_2) && defined(LICENSE_INT_3) && defined(LICENSE_TEXT)
88 coi_error = max( coi_error, coidef_license( cntvect, license_int_1, license_int_2, license_int_3, license_text) )
89#endif
90
91 If ( coi_error .ne. 0 ) THEN
92 write(*,*)
93 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
94 write(*,*)
95 call flog( "Skipping Solve due to setup errors", 1 )
96 ENDIF
97!
98! Save the solution so we can check the duals:
99!
100 do_allocate = .true.
101!
102! Start CONOPT:
103!
104 coi_error = coi_solve( cntvect )
105
106 write(*,*)
107 write(*,*) 'End of const15 example. Return code=',coi_error
108
109 If ( coi_error /= 0 ) then
110 call flog( "Errors encountered during solution", 1 )
111 elseif ( stacalls == 0 .or. solcalls == 0 ) then
112 call flog( "Status or Solution routine was not called", 1 )
113 elseif ( sstat /= 1 .or. mstat /= 2 ) then
114 call flog( "Solver and Model Status was not as expected (1,2)", 1 )
115 elseif ( abs( obj-(-5.0d0 ) ) > 0.001d0 ) then
116 call flog( "Incorrect objective returned", 1 )
117 Else
118 Call checkdual( 'const15', maximize )
119 endif
120
121 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
122
123 call flog( "Successful Solve", 0 )
124
125End Program const15
126!
127! ============================================================================
128! Define information about the model:
129!
130
131!> Define information about the model
132!!
133!! @include{doc} readMatrix_params.dox
134Integer Function con_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
135 colsta, rowno, value, nlflag, n, m, nz, &
136 usrmem )
137#if defined(itl)
138!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Con_ReadMatrix
139#endif
140 implicit none
141 integer, intent (in) :: n ! number of variables
142 integer, intent (in) :: m ! number of constraints
143 integer, intent (in) :: nz ! number of nonzeros
144 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
145 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
146 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
147 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
148 ! (not defined here)
149 integer, intent (out), dimension(m) :: type ! vector of equation types
150 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
151 ! (not defined here)
152 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
153 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
154 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
155 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
156 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
157 real*8 usrmem(*) ! optional user memory
158!
159! Information about Variables:
160! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
161! Default: the status information in Vsta is not used.
162!
163 lower(1) = -5.0d0; curr(1) = 3.0d0; upper(1) = 10.0d0
164 lower(2) = 1.0d0; curr(2) = 1.0d0; upper(2) = 1.0d0
165 lower(3) = -10.0d0; curr(3) = 3.0d0; upper(3) = 10.0d0
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 type(1) = 3
178 type(2) = 2
179 rhs(2) = -5.0d0
180!
181! Information about the Jacobian. We use the standard method with
182! Rowno, Value, Nlflag and Colsta and we do not use Colno.
183!
184! Colsta = Start of column indices (No Defaults):
185! Rowno = Row indices
186! Value = Value of derivative (by default only linear
187! derivatives are used)
188! Nlflag = 0 for linear and 1 for nonlinear derivative
189! (not needed for completely linear models)
190!
191! Indices
192! x(1) x(2) x(3)
193! 1: 1 4
194! 2: 2 3 5
195!
196 colsta(1) = 1
197 colsta(2) = 3
198 colsta(3) = 4
199 colsta(4) = 6
200 rowno(1) = 1
201 rowno(2) = 2
202 rowno(3) = 2
203 rowno(4) = 1
204 rowno(5) = 2
205!
206! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
207! x(1) x(2) x(3)
208! 1: L L
209! 2: NL NL NL
210!
211 nlflag(1) = 0
212 nlflag(2) = 1
213 nlflag(3) = 1
214 nlflag(4) = 0
215 nlflag(5) = 1
216!
217! Value (Linear only)
218! x(1) x(2) x(3)
219! 1: +1 +1
220! 2: NL NL NL
221!
222 value(1) = +1.d0
223 value(4) = +1.d0
224
225 con_readmatrix = 0 ! Return value means OK
226
227end Function con_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 con_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 :: Con_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! Row 1: the objective function is nonlinear
260!
261 if ( rowno .eq. 2 ) then
262!
263! Mode = 1 or 3: Function value
264!
265 if ( mode .eq. 1 .or. mode .eq. 3 ) then
266 g = x(1)*x(2) + x(3)*x(3)
267 endif
268!
269! Mode = 2 or 3: Derivatives
270!
271 if ( mode .eq. 2 .or. mode .eq. 3 ) then
272 jac(1) = x(2)
273 jac(2) = x(1)
274 jac(3) = 2.0*x(3)
275 endif
276 con_fdeval = 0
277 Else
278 con_fdeval = 1 ! Should not happen
279 endif
280! e1: max x1+x3
281! e2: x1*x2 + sqr(x3) =l= 10
282
283end Function con_fdeval
284
285
286!> Evaluating nonlinear functions and derivatives on an interval. Used in preprocessing
287!!
288!! @include{doc} fdinterval_params.dox
289Integer Function con_fdinterval( XMIN, XMAX, GMIN, GMAX, &
290 JMIN, JMAX, ROWNO, JCNM, &
291 MODE, PINF, N, NJ, USRMEM )
292#if defined(itl)
293!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Con_FDInterval
294#endif
295 Implicit None
296 INTEGER, Intent(IN) :: rowno, mode, n, nj
297 INTEGER, Dimension(NJ), Intent(IN) :: jcnm
298 real*8, Dimension(N), Intent(IN) :: xmin, xmax
299 real*8, Intent(IN OUT) :: gmin, gmax
300 real*8, Dimension(N), Intent(IN OUT) :: jmin, jmax
301 real*8, Intent(IN) :: pinf
302 real*8, Intent(IN OUT) :: usrmem(*)
303
304 real*8 :: bnd3
305!
306! Row 2: x1*x2+sqr(x3)
307!
308 if ( rowno .eq. 2 ) then
309!
310! Mode = 1 or 3. Function value
311!
312 if ( mode .eq. 1 .or. mode .eq. 3 ) then
313 bnd3 = min( max( xmin(3), 0.0d0 ), xmax(3) )
314 gmin = xmin(1)*xmin(2) + bnd3*bnd3
315 gmax = xmax(1)*xmax(2) + max(xmin(3)*xmin(3),xmax(3)*xmax(3))
316 endif
317!
318! Mode = 2 or 3: Derivative values:
319!
320 if ( mode .eq. 2 .or. mode .eq. 3 ) then
321 jmin(1) = xmin(2)
322 jmin(2) = xmin(1)
323 jmin(3) = 2.0d0*xmin(3)
324 jmax(1) = xmax(2)
325 jmax(2) = xmax(1)
326 jmax(3) = 2.0d0*xmax(3)
327 endif
329 else
330!
331! There are no other rows:
332!
334 endif
335
336end Function con_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_errmsg(rowno, colno, posno, msglen, usrmem, msg)
Definition comdecl.f90:248
integer function con_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
Definition const01.f90:133
integer function con_fdeval(x, g, jac, rowno, jcnm, mode, ignerr, errcnt, n, nz, thread, usrmem)
Compute nonlinear terms and non-constant Jacobian elements.
Definition const01.f90:238
integer function con_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 const01.f90:291
program const15
Main program. A simple setup and call of CONOPT.
Definition const15.f90:29
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_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_objcon(cntvect, objcon)
defines the Objective Constraint.
Definition coistart.f90:629
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 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