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