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#if defined(_WIN32) && !defined(_WIN64)
26#define dec_directives_win32
27#endif
28
29!> Main program. A simple setup and call of CONOPT
30!!
31Program const07
32
34 Use conopt
35 implicit None
36!
37! Declare the user callback routines as Integer, External:
38!
39 Integer, External :: con_readmatrix ! Mandatory Matrix definition routine defined below
40 Integer, External :: con_fdeval ! Function and Derivative evaluation routine
41 ! needed a nonlinear model.
42 Integer, External :: con_fdinterval ! Function and Derivative evaluation routine
43 ! optional for a nonlinear model.
44 Integer, External :: std_status ! Standard callback for displaying solution status
45 Integer, External :: std_solution ! Standard callback for displaying solution values
46 Integer, External :: std_message ! Standard callback for managing messages
47 Integer, External :: std_errmsg ! Standard callback for managing error messages
48#ifdef dec_directives_win32
49!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Con_ReadMatrix
50!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Con_FDEval
51!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Con_FDInterval
52!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
53!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
54!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
55!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
56#endif
57!
58! Control vector
59!
60 INTEGER, Dimension(:), Pointer :: cntvect
61 INTEGER :: coi_error
62!
63! Create and initialize a Control Vector
64!
65 call startup
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, 6 ) ) ! # variables
72 coi_error = max( coi_error, coidef_numcon( cntvect, 2 ) ) ! # constraints
73 coi_error = max( coi_error, coidef_numnz( cntvect, 8 ) ) ! # nonzeros in the Jacobian
74 coi_error = max( coi_error, coidef_numnlnz( cntvect, 6 ) ) ! # of which are nonlinear
75 coi_error = max( coi_error, coidef_optdir( cntvect, 1 ) ) ! Maximize
76 coi_error = max( coi_error, coidef_objcon( cntvect, 1 ) ) ! Objective is constraint 1
77 coi_error = max( coi_error, coidef_optfile( cntvect, 'const07.opt' ) )
78!
79! Tell CONOPT about the callback routines:
80!
81 coi_error = max( coi_error, coidef_readmatrix( cntvect, con_readmatrix ) )
82 coi_error = max( coi_error, coidef_fdeval( cntvect, con_fdeval ) )
83 coi_error = max( coi_error, coidef_fdinterval( cntvect, con_fdinterval ) )
84 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
85 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
86 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
87 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
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 const07 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 /= 4 ) then
116 call flog( "Solver and Model Status was not as expected (1,4)", 1 )
117 Else
118 Call checkdual( 'const07', infeasible )
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!
125! Free solution memory
126!
127 call finalize
129End Program const07
130!
131! ============================================================================
132! Define information about the model:
133!
134
135!> Define information about the model
136!!
137!! @include{doc} readMatrix_params.dox
138Integer Function con_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
139 colsta, rowno, value, nlflag, n, m, nz, &
140 usrmem )
141#ifdef dec_directives_win32
142!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Con_ReadMatrix
143#endif
144 implicit none
145 integer, intent (in) :: n ! number of variables
146 integer, intent (in) :: m ! number of constraints
147 integer, intent (in) :: nz ! number of nonzeros
148 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
149 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
150 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
151 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
152 ! (not defined here)
153 integer, intent (out), dimension(m) :: type ! vector of equation types
154 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
155 ! (not defined here)
156 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
157 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
158 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
159 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
160 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
161 real*8 usrmem(*) ! optional user memory
162!
163! Information about Variables:
164! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
165! Default: the status information in Vsta is not used.
166!
167 lower(1) = 2.0d0; curr(1) = 5.0d0; upper(1) = 9.0d0
168 lower(2) = 1.0d0; curr(2) = 1.0d0; upper(2) = 1.0d0
169 lower(3) = 2.0d0; curr(3) = 3.0d0; upper(3) = 4.0d0
170 lower(4) = 2.0d0; curr(4) = 2.0d0; upper(4) = 2.0d0
171 lower(5) = 3.0d0; curr(5) = 3.0d0; upper(5) = 3.0d0
172 lower(6) = 0.0d0; curr(6) = 0.0d0; upper(6) = 1.0d0
173!
174! Information about Constraints:
175! Default: Rhs = 0
176! Default: the status information in Esta and the function
177! value in FV are not used.
178! Default: Type: There is no default.
179! 0 = Equality,
180! 1 = Greater than or equal,
181! 2 = Less than or equal,
182! 3 = Non binding.
183!
184 type(1) = 3
185 type(2) = 0
186 rhs(2) = 22.d0
187!
188! Information about the Jacobian. CONOPT expects a columnwise
189! representation in Rowno, Value, Nlflag and Colsta.
190!
191! Colsta = Start of column indices (No Defaults):
192! Rowno = Row indices
193! Value = Value of derivative (by default only linear
194! derivatives are used)
195! Nlflag = 0 for linear and 1 for nonlinear derivative
196! (not needed for completely linear models)
197!
198! Indices
199! x(1) x(2) x(3) x(4) x(5) x(6)
200! 1: 1 4
201! 2: 2 3 5 6 7 8
202!
203 colsta(1) = 1
204 colsta(2) = 3
205 colsta(3) = 4
206 colsta(4) = 6
207 colsta(5) = 7
208 colsta(6) = 8
209 colsta(7) = 9
210 rowno(1) = 1
211 rowno(2) = 2
212 rowno(3) = 2
213 rowno(4) = 1
214 rowno(5) = 2
215 rowno(6) = 2
216 rowno(7) = 2
217 rowno(8) = 2
218!
219! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
220! x(1) x(2) x(3) x(4) x(5) x(6)
221! 1: L L
222! 2: NL NL NL NL NL NL
223!
224 nlflag(1) = 0
225 nlflag(2) = 1
226 nlflag(3) = 1
227 nlflag(4) = 0
228 nlflag(5) = 1
229 nlflag(6) = 1
230 nlflag(7) = 1
231 nlflag(8) = 1
232!
233! Value (Linear only)
234! x(1) x(2) x(3) x(4) x(5) x(6)
235! 1: +1 +1
236! 2: NL NL NL NL NL NL
237!
238 value(1) = +1.d0
239 value(4) = +1.d0
240
241 con_readmatrix = 0 ! Return value means OK
242
243end Function con_readmatrix
244!
245!==========================================================================
246! Compute nonlinear terms and non-constant Jacobian elements
247!
248
249!> Compute nonlinear terms and non-constant Jacobian elements
250!!
251!! @include{doc} fdeval_params.dox
252Integer Function con_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
253 n, nz, thread, usrmem )
254#ifdef dec_directives_win32
255!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Con_FDEval
256#endif
257 implicit none
258 integer, intent (in) :: n ! number of variables
259 integer, intent (in) :: rowno ! number of the row to be evaluated
260 integer, intent (in) :: nz ! number of nonzeros in this row
261 real*8, intent (in), dimension(n) :: x ! vector of current solution values
262 real*8, intent (in out) :: g ! constraint value
263 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
264 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
265 ! in this row. Ffor information only.
266 integer, intent (in) :: mode ! evaluation mode: 1 = function value
267 ! 2 = derivatives, 3 = both
268 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
269 ! as errcnt is incremented
270 integer, intent (in out) :: errcnt ! error counter to be incremented in case
271 ! of function evaluation errors.
272 integer, intent (in) :: thread
273 real*8 usrmem(*) ! optional user memory
274!
275! Row 1: the objective function is nonlinear
276!
277 if ( rowno .eq. 2 ) then
278!
279! Mode = 1 or 3: Function value
280!
281 if ( mode .eq. 1 .or. mode .eq. 3 ) then
282 g = x(1)*x(2) + x(3)*x(4) + x(5)*x(6)
283 endif
284!
285! Mode = 2 or 3: Derivatives
286!
287 if ( mode .eq. 2 .or. mode .eq. 3 ) then
288 jac(1) = x(2)
289 jac(2) = x(1)
290 jac(3) = x(4)
291 jac(4) = x(3)
292 jac(5) = x(6)
293 jac(6) = x(5)
294 endif
295 con_fdeval = 0
296 Else
297 con_fdeval = 1 ! Should not happen
298 endif
299
300end Function con_fdeval
301
302
303!> Evaluating nonlinear functions and derivatives on an interval. Used in preprocessing
304!!
305!! @include{doc} fdinterval_params.dox
306Integer Function con_fdinterval( XMIN, XMAX, GMIN, GMAX, &
307 JMIN, JMAX, ROWNO, JCNM, &
308 MODE, PINF, N, NJ, USRMEM )
309#ifdef dec_directives_win32
310!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Con_FDInterval
311#endif
312 Implicit None
313 INTEGER, Intent(IN) :: rowno, mode, n, nj
314 INTEGER, Dimension(NJ), Intent(IN) :: jcnm
315 real*8, Dimension(N), Intent(IN) :: xmin, xmax
316 real*8, Intent(IN OUT) :: gmin, gmax
317 real*8, Dimension(N), Intent(IN OUT) :: jmin, jmax
318 real*8, Intent(IN) :: pinf
319 real*8, Intent(IN OUT) :: usrmem(*)
320
321!
322! Row 2: x1*x2+x3*x4 + x5*x6 ! with known positive values
323!
324 if ( rowno .eq. 2 ) then
325!
326! Mode = 1 or 3. Function value
327!
328 if ( mode .eq. 1 .or. mode .eq. 3 ) then
329 gmin = xmin(1)*xmin(2) + xmin(3)*xmin(4) + xmin(5)*xmin(6)
330 gmax = xmax(1)*xmax(2) + xmax(3)*xmax(4) + xmax(5)*xmax(6)
331 endif
332!
333! Mode = 2 or 3: Derivative values:
334!
335 if ( mode .eq. 2 .or. mode .eq. 3 ) then
336 jmin(1) = xmin(2)
337 jmin(2) = xmin(1)
338 jmin(3) = xmin(4)
339 jmin(4) = xmin(3)
340 jmin(5) = xmin(6)
341 jmin(6) = xmin(5)
342 jmax(1) = xmax(2)
343 jmax(2) = xmax(1)
344 jmax(3) = xmax(4)
345 jmax(4) = xmax(3)
346 jmax(5) = xmax(6)
347 jmax(6) = xmax(5)
348 endif
350 else
351!
352! There are no other rows:
353!
355 endif
356
357end Function con_fdinterval
integer function std_solution(xval, xmar, xbas, xsta, yval, ymar, ybas, ysta, n, m, usrmem)
Definition comdecl.f90:170
integer function std_status(modsta, solsta, iter, objval, usrmem)
Definition comdecl.f90:126
subroutine checkdual(case, minmax)
Definition comdecl.f90:432
integer function std_message(smsg, dmsg, nmsg, llen, usrmem, msgv)
Definition comdecl.f90:243
integer function std_errmsg(rowno, colno, posno, msglen, usrmem, msg)
Definition comdecl.f90:286
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:131
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:233
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:283
program const07
Main program. A simple setup and call of CONOPT.
Definition const07.f90:33
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
subroutine finalize
Definition comdecl.f90:79
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