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