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