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
147End Program largerhs
148!
149! ============================================================================
150! Define information about the model:
151!
152
153!> Define information about the model
154!!
155!! @include{doc} readMatrix_params.dox
156Integer Function rhs_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
157 colsta, rowno, value, nlflag, n, m, nz, &
158 usrmem )
159#ifdef dec_directives_win32
160!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Rhs_ReadMatrix
161#endif
162 implicit none
163 integer, intent (in) :: n ! number of variables
164 integer, intent (in) :: m ! number of constraints
165 integer, intent (in) :: nz ! number of nonzeros
166 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
167 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
168 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
169 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
170 ! (not defined here)
171 integer, intent (out), dimension(m) :: type ! vector of equation types
172 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
173 ! (not defined here)
174 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
175 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
176 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
177 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
178 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
179 real*8 usrmem(*) ! optional user memory
180!
181! Information about Variables:
182! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
183! Default: the status information in Vsta is not used.
184!
185! The model uses defaults
186!
187! Information about Constraints:
188! Default: Rhs = 0
189! Default: the status information in Esta and the function
190! value in FV are not used.
191! Default: Type: There is no default.
192! 0 = Equality,
193! 1 = Greater than or equal,
194! 2 = Less than or equal,
195! 3 = Non binding.
196!
197! Constraint 1: e1
198! Rhs = 2.0 and type Equality
199!
200 rhs(1) = 2.0d16 ! Special value
201 type(1) = 0
202!
203! Constraint 2: e2
204! Rhs = 6.0 and type Equality
205!
206 rhs(2) = 6.0d0
207 type(2) = 0
208!
209! Constraint 3: e3
210! Rhs = 1.0 and type Less than or equal
211!
212 rhs(3) = 1.0d0
213 type(3) = 2
214!
215! Constraint 4: e4
216! Rhs = 1.0 and type Less than or equal
217!
218 rhs(4) = 1.0d16 ! Special value
219 type(4) = 2
220!
221! Constraint 5: e5
222! Rhs = 1.0 and type Less than or equal
223!
224 rhs(5) = 1.0d0
225 type(5) = 2
226!
227! Non-default Bounds
228!
229 lower(3) = 2.0d0
230 upper(3) = 2.0d0
231 lower(4) = 0.0d0
232 lower(5) = 0.0d0
233 lower(6) = 0.0d0
234!
235! Information about the Jacobian. CONOPT expects a columnwise
236! representation in Rowno, Value, Nlflag and Colsta.
237!
238! Colsta = Start of column indices (No Defaults):
239! Rowno = Row indices
240! Value = Value of derivative (by default only linear
241! derivatives are used)
242! Nlflag = 0 for linear and 1 for nonlinear derivative
243! (not needed for completely linear models)
244!
245! Indices
246! x(1) x(2) x(3) x(4) x(5) x(6)
247! 1: 1 3
248! 2: 4 6
249! 3: 7
250! 4: 5 8
251! 5: 2 9 10
252!
253 colsta(1) = 1
254 colsta(2) = 3
255 colsta(3) = 6
256 colsta(4) = 7
257 colsta(5) = 9
258 colsta(6) = 10
259 colsta(7) = 11
260 rowno(1) = 1
261 rowno(2) = 5
262 rowno(3) = 1
263 rowno(4) = 2
264 rowno(5) = 4
265 rowno(6) = 2
266 rowno(7) = 3
267 rowno(8) = 4
268 rowno(9) = 5
269 rowno(10) = 5
270!
271! Nonlinearity Structure: Model is linear
272!
273!
274! Value (Linear only)
275! x(1) x(2) x(3) x(4) x(5) x(6)
276! 1: 1.0 1.0
277! 2: 4.0 1.0
278! 3: 1.0
279! 4: 1.0 1.0
280! 5: 1.0 1.0 1.0
281!
282 value(1) = 1.d0
283 value(2) = 1.d0
284 value(3) = 1.d0
285 value(4) = 4.d0
286 value(5) = 1.d0
287 value(6) = 1.d0
288 value(7) = 1.d0
289 value(8) = 1.d0
290 value(9) = 1.d0
291 value(10) = 1.d0
292
293 rhs_readmatrix = 0 ! Return value means OK
294
295end Function rhs_readmatrix
296!
297!==========================================================================
298! Compute nonlinear terms and non-constant Jacobian elements
299!
300
301!> Compute nonlinear terms and non-constant Jacobian elements
302!!
303!! @include{doc} fdeval_params.dox
304Integer Function rhs_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
305 n, nz, thread, usrmem )
306#ifdef dec_directives_win32
307!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Rhs_FDEval
308#endif
309 implicit none
310 integer, intent (in) :: n ! number of variables
311 integer, intent (in) :: rowno ! number of the row to be evaluated
312 integer, intent (in) :: nz ! number of nonzeros in this row
313 real*8, intent (in), dimension(n) :: x ! vector of current solution values
314 real*8, intent (in out) :: g ! constraint value
315 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
316 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
317 ! in this row. Ffor information only.
318 integer, intent (in) :: mode ! evaluation mode: 1 = function value
319 ! 2 = derivatives, 3 = both
320 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
321 ! as errcnt is incremented
322 integer, intent (in out) :: errcnt ! error counter to be incremented in case
323 ! of function evaluation errors.
324 integer, intent (in) :: thread
325 real*8 usrmem(*) ! optional user memory
326!
327! The model is linear and FDEval should not be called.
328!
329 rhs_fdeval = 1
330
331end Function rhs_fdeval
332
333
334!> Sets runtime options
335!!
336!! @include{doc} option_params.dox
337Integer Function rhs_option( ncall, rval, ival, lval, usrmem, name )
338#ifdef dec_directives_win32
339!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Rhs_Option
340#endif
341 integer ncall, ival, lval
342 character(Len=*) :: name
343 real*8 rval
344 real*8 usrmem(*) ! optional user memory
345
346 Select case (ncall)
347 case (1)
348 name = 'Lim_Variable'
349 rval = 1.d20
350 case default
351 name = ' '
352 end Select
353 rhs_option = 0
354
355end Function rhs_option
integer function std_solution(xval, xmar, xbas, xsta, yval, ymar, ybas, ysta, n, m, usrmem)
Definition comdecl.f90:132
integer function std_status(modsta, solsta, iter, objval, usrmem)
Definition comdecl.f90:88
integer function std_message(smsg, dmsg, nmsg, llen, usrmem, msgv)
Definition comdecl.f90:205
integer function std_triord(mode, type, status, irow, icol, inf, value, resid, usrmem)
Definition comdecl.f90:289
integer function std_errmsg(rowno, colno, posno, msglen, usrmem, msg)
Definition comdecl.f90:248
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:148
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:292
integer function rhs_option(ncall, rval, ival, lval, usrmem, name)
Sets runtime options.
Definition largerhs.f90:321
program largerhs
Main program. A simple setup and call of CONOPT.
Definition largerhs.f90:46
subroutine flog(msg, code)
Definition comdecl.f90:62
subroutine startup
Definition comdecl.f90:41