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