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