CONOPT
Loading...
Searching...
No Matches
largerhs.f90
Go to the documentation of this file.
1!> @file largerhs.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! Model with large right Hand Side
6!!
7!! The model is a copy of force01 with adjusted right hand sides.
8!! The model does not make sense -- it is only intended to test that
9!! large right hand sides are flagged as errors.
10!! Note that the large right hand side in e4 really means that the
11!! constraint is non-binding. However, we still exclude it as an
12!! error
13!!
14!! This is a CONOPT implementation of the GAMS model:
15!!
16!! @verbatim
17!! variable x1, x2, x3, x4, x5, x6;
18!! equation e1, e2, e3, e4, e5;
19!!
20!! positive variable x4, x5;
21!! x3.fx = 2;
22!! x6.lo = 0;
23!!
24!! e1 .. x1 + x2 =E= 2.e16;
25!! e2 .. 4*x2 + x3 =E= 6;
26!! e3 .. x4 =L= 1;
27!! e4 .. x2 + x4 =L= 1.e16;
28!! e5 .. x1 + x5 + x6 =L= 1;
29!!
30!! model m / all /;
31!! solve m using nlp minimizing x6;
32!! solve m using nlp maximizing x6;
33!! @endverbatim
34!!
35!!
36!! For more information about the individual callbacks, please have a look at the source code.
37
38!> Main program. A simple setup and call of CONOPT
39!!
40Program largerhs
41
42 Use proginfo
43 Use coidef
44 implicit None
45!
46! Declare the user callback routines as Integer, External:
47!
48 Integer, External :: rhs_readmatrix ! Mandatory Matrix definition routine defined below
49 Integer, External :: rhs_fdeval ! Function and Derivative evaluation routine
50 ! needed a nonlinear model.
51 Integer, External :: std_status ! Standard callback for displaying solution status
52 Integer, External :: std_solution ! Standard callback for displaying solution values
53 Integer, External :: std_message ! Standard callback for managing messages
54 Integer, External :: std_errmsg ! Standard callback for managing error messages
55 Integer, External :: std_triord ! Standard callback for Triangular order
56 Integer, External :: rhs_option ! Option defining callback routine
57#if defined(itl)
58!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Rhs_ReadMatrix
59!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Rhs_FDEval
60!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
61!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
62!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
63!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
64!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_TriOrd
65!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Rhs_Option
66#endif
67!
68! Control vector
69!
70 INTEGER :: numcallback
71 INTEGER, Dimension(:), Pointer :: cntvect
72 INTEGER :: coi_error
73
74 call startup
75!
76! Create and initialize a Control Vector
77!
78 numcallback = coidef_size()
79 Allocate( cntvect(numcallback) )
80 coi_error = coidef_inifort( cntvect )
81!
82! Tell CONOPT about the size of the model by populating the Control Vector:
83!
84 coi_error = max( coi_error, coidef_numvar( cntvect, 6 ) ) ! # variables
85 coi_error = max( coi_error, coidef_numcon( cntvect, 5 ) ) ! # constraints
86 coi_error = max( coi_error, coidef_numnz( cntvect, 10 ) ) ! # nonzeros in the Jacobian
87 coi_error = max( coi_error, coidef_numnlnz( cntvect, 0 ) ) ! # of which are nonlinear
88 coi_error = max( coi_error, coidef_optdir( cntvect, -1 ) ) ! Minimize
89 coi_error = max( coi_error, coidef_objvar( cntvect, 6 ) ) ! Objective variable #
90 coi_error = max( coi_error, coidef_optfile( cntvect, 'largerhs.opt' ) )
91!
92! Tell CONOPT about the callback routines:
93!
94 coi_error = max( coi_error, coidef_readmatrix( cntvect, rhs_readmatrix ) )
95 coi_error = max( coi_error, coidef_fdeval( cntvect, rhs_fdeval ) )
96 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
97 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
98 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
99 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
100 coi_error = max( coi_error, coidef_triord( cntvect, std_triord ) )
101
102#if defined(LICENSE_INT_1) && defined(LICENSE_INT_2) && defined(LICENSE_INT_3) && defined(LICENSE_TEXT)
103 coi_error = max( coi_error, coidef_license( cntvect, license_int_1, license_int_2, license_int_3, license_text) )
104#endif
105
106 If ( coi_error .ne. 0 ) THEN
107 write(*,*)
108 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
109 write(*,*)
110 call flog( "Skipping Solve due to setup errors", 1 )
111 ENDIF
112!
113! Start CONOPT:
114!
115 coi_error = coi_solve( cntvect )
116
117 write(*,*)
118 write(*,*) 'End of LargeRhs example. Return code=',coi_error
119!
120! The large right hand side should result in a setup error and a nonzero
121! return code in COI_Error
122!
123 If ( coi_error == 0 ) then
124 call flog( "Expected errors were not found during solution", 1 )
125 endif
126!
127! Now repeat with an option that changes the upper bound
128!
129 coi_error = 0
130 coi_error = max( coi_error, coidef_option( cntvect, rhs_option ) )
131 If ( coi_error .ne. 0 ) THEN
132 write(*,*)
133 write(*,*) '**** Fatal Error while loading CONOPT Callback routines for 2nd solve.'
134 write(*,*)
135 call flog( "Skipping 2nd Solve due to setup errors", 1 )
136 ENDIF
137!
138! Start CONOPT:
139!
140 coi_error = coi_solve( cntvect )
141
142 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
143
144 call flog( "Successful Solve", 0 )
145
146End Program largerhs
147!
148! ============================================================================
149! Define information about the model:
150!
151
152!> Define information about the model
153!!
154!! @include{doc} readMatrix_params.dox
155Integer Function rhs_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
156 colsta, rowno, value, nlflag, n, m, nz, &
157 usrmem )
158#if defined(itl)
159!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Rhs_ReadMatrix
160#endif
161 implicit none
162 integer, intent (in) :: n ! number of variables
163 integer, intent (in) :: m ! number of constraints
164 integer, intent (in) :: nz ! number of nonzeros
165 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
166 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
167 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
168 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
169 ! (not defined here)
170 integer, intent (out), dimension(m) :: type ! vector of equation types
171 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
172 ! (not defined here)
173 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
174 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
175 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
176 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
177 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
178 real*8 usrmem(*) ! optional user memory
179!
180! Information about Variables:
181! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
182! Default: the status information in Vsta is not used.
183!
184! The model uses defaults
185!
186! Information about Constraints:
187! Default: Rhs = 0
188! Default: the status information in Esta and the function
189! value in FV are not used.
190! Default: Type: There is no default.
191! 0 = Equality,
192! 1 = Greater than or equal,
193! 2 = Less than or equal,
194! 3 = Non binding.
195!
196! Constraint 1: e1
197! Rhs = 2.0 and type Equality
198!
199 rhs(1) = 2.0d16 ! Special value
200 type(1) = 0
201!
202! Constraint 2: e2
203! Rhs = 6.0 and type Equality
204!
205 rhs(2) = 6.0d0
206 type(2) = 0
207!
208! Constraint 3: e3
209! Rhs = 1.0 and type Less than or equal
210!
211 rhs(3) = 1.0d0
212 type(3) = 2
213!
214! Constraint 4: e4
215! Rhs = 1.0 and type Less than or equal
216!
217 rhs(4) = 1.0d16 ! Special value
218 type(4) = 2
219!
220! Constraint 5: e5
221! Rhs = 1.0 and type Less than or equal
222!
223 rhs(5) = 1.0d0
224 type(5) = 2
225!
226! Non-default Bounds
227!
228 lower(3) = 2.0d0
229 upper(3) = 2.0d0
230 lower(4) = 0.0d0
231 lower(5) = 0.0d0
232 lower(6) = 0.0d0
233!
234! Information about the Jacobian. We use the standard method with
235! Rowno, Value, Nlflag and Colsta and we do not use Colno.
236!
237! Colsta = Start of column indices (No Defaults):
238! Rowno = Row indices
239! Value = Value of derivative (by default only linear
240! derivatives are used)
241! Nlflag = 0 for linear and 1 for nonlinear derivative
242! (not needed for completely linear models)
243!
244! Indices
245! x(1) x(2) x(3) x(4) x(5) x(6)
246! 1: 1 3
247! 2: 4 6
248! 3: 7
249! 4: 5 8
250! 5: 2 9 10
251!
252 colsta(1) = 1
253 colsta(2) = 3
254 colsta(3) = 6
255 colsta(4) = 7
256 colsta(5) = 9
257 colsta(6) = 10
258 colsta(7) = 11
259 rowno(1) = 1
260 rowno(2) = 5
261 rowno(3) = 1
262 rowno(4) = 2
263 rowno(5) = 4
264 rowno(6) = 2
265 rowno(7) = 3
266 rowno(8) = 4
267 rowno(9) = 5
268 rowno(10) = 5
269!
270! Nonlinearity Structure: Model is linear
271!
272!
273! Value (Linear only)
274! x(1) x(2) x(3) x(4) x(5) x(6)
275! 1: 1.0 1.0
276! 2: 4.0 1.0
277! 3: 1.0
278! 4: 1.0 1.0
279! 5: 1.0 1.0 1.0
280!
281 value(1) = 1.d0
282 value(2) = 1.d0
283 value(3) = 1.d0
284 value(4) = 4.d0
285 value(5) = 1.d0
286 value(6) = 1.d0
287 value(7) = 1.d0
288 value(8) = 1.d0
289 value(9) = 1.d0
290 value(10) = 1.d0
291
292 rhs_readmatrix = 0 ! Return value means OK
293
294end Function rhs_readmatrix
295!
296!==========================================================================
297! Compute nonlinear terms and non-constant Jacobian elements
298!
299
300!> Compute nonlinear terms and non-constant Jacobian elements
301!!
302!! @include{doc} fdeval_params.dox
303Integer Function rhs_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
304 n, nz, thread, usrmem )
305#if defined(itl)
306!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Rhs_FDEval
307#endif
308 implicit none
309 integer, intent (in) :: n ! number of variables
310 integer, intent (in) :: rowno ! number of the row to be evaluated
311 integer, intent (in) :: nz ! number of nonzeros in this row
312 real*8, intent (in), dimension(n) :: x ! vector of current solution values
313 real*8, intent (in out) :: g ! constraint value
314 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
315 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
316 ! in this row. Ffor information only.
317 integer, intent (in) :: mode ! evaluation mode: 1 = function value
318 ! 2 = derivatives, 3 = both
319 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
320 ! as errcnt is incremented
321 integer, intent (in out) :: errcnt ! error counter to be incremented in case
322 ! of function evaluation errors.
323 integer, intent (in) :: thread
324 real*8 usrmem(*) ! optional user memory
325!
326! The model is linear and FDEval should not be called.
327!
328 rhs_fdeval = 1
329
330end Function rhs_fdeval
331
332
333!> Sets runtime options
334!!
335!! @include{doc} option_params.dox
336Integer Function rhs_option( ncall, rval, ival, lval, usrmem, name )
337#if defined(itl)
338!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Rhs_Option
339#endif
340 integer ncall, ival, lval
341 character(Len=*) :: name
342 real*8 rval
343 real*8 usrmem(*) ! optional user memory
344
345 Select case (ncall)
346 case (1)
347 name = 'Lim_Variable'
348 rval = 1.d20
349 case default
350 name = ' '
351 end Select
352 rhs_option = 0
353
354end Function rhs_option
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
integer function std_message(smsg, dmsg, nmsg, llen, usrmem, msgv)
Definition comdecl.f90:203
integer function std_triord(mode, type, status, irow, icol, inf, value, resid, usrmem)
Definition comdecl.f90:291
integer function std_errmsg(rowno, colno, posno, msglen, usrmem, msg)
Definition comdecl.f90:248
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_option(cntvect, coi_option)
define callback routine for defining runtime options.
integer function coidef_optfile(cntvect, optfile)
define callback routine for defining an options file.
integer function coidef_triord(cntvect, coi_triord)
define callback routine for providing the triangular order information.
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_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 coidef_objvar(cntvect, objvar)
defines the Objective Variable.
Definition coistart.f90:586
integer function coidef_size()
returns the size the Control Vector must have, measured in standard Integer units.
Definition coistart.f90:176
integer function coidef_inifort(cntvect)
initialisation method for Fortran applications.
Definition coistart.f90:314
integer function coi_solve(cntvect)
method for starting the solving process of CONOPT.
Definition coistart.f90:14
integer function rhs_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
Definition largerhs.f90:158
integer function rhs_fdeval(x, g, jac, rowno, jcnm, mode, ignerr, errcnt, n, nz, thread, usrmem)
Compute nonlinear terms and non-constant Jacobian elements.
Definition largerhs.f90:305
integer function rhs_option(ncall, rval, ival, lval, usrmem, name)
Sets runtime options.
Definition largerhs.f90:337
program largerhs
Main program. A simple setup and call of CONOPT.
Definition largerhs.f90:40
subroutine flog(msg, code)
Definition comdecl.f90:56
subroutine startup
Definition comdecl.f90:35