CONOPT
Loading...
Searching...
No Matches
const01.f90
Go to the documentation of this file.
1!> @file const01.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 =E= 20
12!! x2.fx = 1; x4.fx = 2;
13!! 2 <= x1 <= 10; x1.l = 5
14!! 2 <= x3 <= 10; x3.l = 5
15!! @endverbatim
16!!
17!! In this model `e1` is the post-triangular objective.
18!! `e2` can be solved w.r.t. `x3` so the constraint is post-triangular
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 const01
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, 4 ) ) ! # variables
66 coi_error = max( coi_error, coidef_numcon( cntvect, 2 ) ) ! # constraints
67 coi_error = max( coi_error, coidef_numnz( cntvect, 6 ) ) ! # nonzeros in the Jacobian
68 coi_error = max( coi_error, coidef_numnlnz( cntvect, 4 ) ) ! # 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, 'const01.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 const01 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 /= 1 ) then
110 call flog( "Solver and Model Status was not as expected (1,1)", 1 )
111 elseif ( abs( obj-15.0d0 ) > 0.000001d0 ) then
112 call flog( "Incorrect objective returned", 1 )
113 Else
114 Call checkdual( 'const01', maximize )
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 const01
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) = 10.0d0
160 lower(2) = 1.0d0; curr(2) = 1.0d0; upper(2) = 1.0d0
161 lower(3) = 2.0d0; curr(3) = 5.0d0; upper(3) = 10.0d0
162 lower(4) = 2.0d0; curr(4) = 2.0d0; upper(4) = 2.0d0
163!
164! Information about Constraints:
165! Default: Rhs = 0
166! Default: the status information in Esta and the function
167! value in FV are not used.
168! Default: Type: There is no default.
169! 0 = Equality,
170! 1 = Greater than or equal,
171! 2 = Less than or equal,
172! 3 = Non binding.
173!
174 type(1) = 3
175 type(2) = 0
176 rhs(2) = 20.d0
177!
178! Information about the Jacobian. We use the standard method with
179! Rowno, Value, Nlflag and Colsta and we do not use Colno.
180!
181! Colsta = Start of column indices (No Defaults):
182! Rowno = Row indices
183! Value = Value of derivative (by default only linear
184! derivatives are used)
185! Nlflag = 0 for linear and 1 for nonlinear derivative
186! (not needed for completely linear models)
187!
188! Indices
189! x(1) x(2) x(3) x(4)
190! 1: 1 4
191! 2: 2 3 5 6
192!
193 colsta(1) = 1
194 colsta(2) = 3
195 colsta(3) = 4
196 colsta(4) = 6
197 colsta(5) = 7
198 rowno(1) = 1
199 rowno(2) = 2
200 rowno(3) = 2
201 rowno(4) = 1
202 rowno(5) = 2
203 rowno(6) = 2
204!
205! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
206! x(1) x(2) x(3) x(4)
207! 1: L L
208! 2: NL NL NL NL
209!
210 nlflag(1) = 0
211 nlflag(2) = 1
212 nlflag(3) = 1
213 nlflag(4) = 0
214 nlflag(5) = 1
215 nlflag(6) = 1
216!
217! Value (Linear only)
218! x(1) x(2) x(3) x(4)
219! 1: +1 +1
220! 2: NL NL NL NL
221!
222 value(1) = +1.d0
223 value(4) = +1.d0
224
225 con_readmatrix = 0 ! Return value means OK
226
227end Function con_readmatrix
228!
229!==========================================================================
230! Compute nonlinear terms and non-constant Jacobian elements
231!
232
233!> Compute nonlinear terms and non-constant Jacobian elements
234!!
235!! @include{doc} fdeval_params.dox
236Integer Function con_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
237 n, nz, thread, usrmem )
238#if defined(itl)
239!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Con_FDEval
240#endif
241 implicit none
242 integer, intent (in) :: n ! number of variables
243 integer, intent (in) :: rowno ! number of the row to be evaluated
244 integer, intent (in) :: nz ! number of nonzeros in this row
245 real*8, intent (in), dimension(n) :: x ! vector of current solution values
246 real*8, intent (in out) :: g ! constraint value
247 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
248 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
249 ! in this row. Ffor information only.
250 integer, intent (in) :: mode ! evaluation mode: 1 = function value
251 ! 2 = derivatives, 3 = both
252 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
253 ! as errcnt is incremented
254 integer, intent (in out) :: errcnt ! error counter to be incremented in case
255 ! of function evaluation errors.
256 integer, intent (in) :: thread
257 real*8 usrmem(*) ! optional user memory
258!
259! Row 1: the objective function is nonlinear
260!
261 if ( rowno .eq. 2 ) then
262!
263! Mode = 1 or 3: Function value
264!
265 if ( mode .eq. 1 .or. mode .eq. 3 ) then
266 g = x(1)*x(2) + x(3)*x(4)
267 endif
268!
269! Mode = 2 or 3: Derivatives
270!
271 if ( mode .eq. 2 .or. mode .eq. 3 ) then
272 jac(1) = x(2)
273 jac(2) = x(1)
274 jac(3) = x(4)
275 jac(4) = x(3)
276 endif
277 con_fdeval = 0
278 Else
279 con_fdeval = 1 ! Should not happen
280 endif
281
282end Function con_fdeval
283
284
285!> Evaluating nonlinear functions and derivatives on an interval. Used in preprocessing
286!!
287!! @include{doc} fdinterval_params.dox
288Integer Function con_fdinterval( XMIN, XMAX, GMIN, GMAX, &
289 JMIN, JMAX, ROWNO, JCNM, &
290 MODE, PINF, N, NJ, USRMEM )
291#if defined(itl)
292!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Con_FDInterval
293#endif
294 Implicit None
295 INTEGER, Intent(IN) :: rowno, mode, n, nj
296 INTEGER, Dimension(NJ), Intent(IN) :: jcnm
297 real*8, Dimension(N), Intent(IN) :: xmin, xmax
298 real*8, Intent(IN OUT) :: gmin, gmax
299 real*8, Dimension(N), Intent(IN OUT) :: jmin, jmax
300 real*8, Intent(IN) :: pinf
301 real*8, Intent(IN OUT) :: usrmem(*)
302
303!
304! Row 2: x1*x2+x3*x4 ! with known positive values
305!
306 if ( rowno .eq. 2 ) then
307!
308! Mode = 1 or 3. Function
309!
310 if ( mode .eq. 1 .or. mode .eq. 3 ) then
311 gmin = xmin(1)*xmin(2) + xmin(3)*xmin(4)
312 gmax = xmax(1)*xmax(2) + xmax(3)*xmax(4)
313 endif
314!
315! Mode = 2 or 3: Derivative values:
316!
317 if ( mode .eq. 2 .or. mode .eq. 3 ) then
318 jmin(1) = xmin(2)
319 jmin(2) = xmin(1)
320 jmin(3) = xmin(4)
321 jmin(4) = xmin(3)
322 jmax(1) = xmax(2)
323 jmax(2) = xmax(1)
324 jmax(3) = xmax(4)
325 jmax(4) = xmax(3)
326 endif
328 else
329!
330! There are no other rows:
331!
333 endif
334
335end 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
program const01
Main program. A simple setup and call of CONOPT.
Definition const01.f90:25
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
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
real *8 obj
Definition comdecl.f90:10
integer solcalls
Definition comdecl.f90:9
integer sstat
Definition comdecl.f90:12
integer stacalls
Definition comdecl.f90:8
subroutine flog(msg, code)
Definition comdecl.f90:56
logical do_allocate
Definition comdecl.f90:21
integer, parameter maximize
Definition comdecl.f90:25
integer mstat
Definition comdecl.f90:11
subroutine startup
Definition comdecl.f90:35