CONOPT
Loading...
Searching...
No Matches
undef01.f90
Go to the documentation of this file.
1!> @file undef01.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! Test with some undefined Jacobian elements.
6!!
7!! This is a CONOPT implementation of the GAMS model:
8!!
9!! @verbatim
10!! variable x1, x2, x3, x4, x5, x6, x7, x8 ;
11!! equation e1, e2, e3, e4, e5, e6, e7 ;
12!!
13!! e1.. x2 + x7 =e= 9;
14!! e2.. x1 + x3 + x5 + x7 + x8 =e= 24;
15!! e3.. x1 + x4 + x5 =e= 10;
16!! e4.. x2 + x5 =e= 7;
17!! e5.. x4 + x5 + x8 =e= 17;
18!! e6.. x1 + x3 + x6 =e= 10;
19!! e7.. x1 + x4 + x7 =l= 15;
20!! x5.fx = 5; x8.fx = 8;
21!!
22!! model undef01 / all /;
23!! solve undef01 using cns;
24!! @endverbatim
25!!
26!! and the solution is xi = i for all i.
27!!
28!! The last 3 Jacobian elements are not defined.
29!!
30!!
31!! For more information about the individual callbacks, please have a look at the source code.
32
33!> Main program. A simple setup and call of CONOPT
34!!
35Program undef01
36
37 Use proginfo
38 Use coidef
39 Use casedata_num
40 implicit None
41!
42! Declare the user callback routines as Integer, External:
43!
44 Integer, External :: tria_readmatrix ! Mandatory Matrix definition routine defined below
45 Integer, External :: tria_fdeval ! Function and Derivative evaluation routine
46 ! needed a nonlinear model.
47 Integer, External :: std_status ! Standard callback for displaying solution status
48 Integer, External :: std_solution ! Standard callback for displaying solution values
49 Integer, External :: std_message ! Standard callback for managing messages
50 Integer, External :: std_errmsg ! Standard callback for managing error messages
51 Integer, External :: std_triord ! Standard callback for triangular order
52#if defined(itl)
53!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Tria_ReadMatrix
54!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Tria_FDEval
55!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
56!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
57!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
58!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
59!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_TriOrd
60#endif
61!
62! Control vector
63!
64 INTEGER :: numcallback
65 INTEGER, Dimension(:), Pointer :: cntvect
66 INTEGER :: coi_error
67
68 Integer, parameter :: nvar = 8, neq = 7
69
70 call startup
71!
72! Create and initialize a Control Vector
73!
74 numcallback = coidef_size()
75 Allocate( cntvect(numcallback) )
76 coi_error = coidef_inifort( cntvect )
77!
78! Tell CONOPT about the size of the model by populating the Control Vector:
79!
80 coi_error = max( coi_error, coidef_numvar( cntvect, nvar ) ) ! # variables
81 coi_error = max( coi_error, coidef_numcon( cntvect, neq ) ) ! # constraints
82 coi_error = max( coi_error, coidef_numnz( cntvect, 21 ) ) ! # nonzeros in the Jacobian
83 coi_error = max( coi_error, coidef_numnlnz( cntvect, 0 ) ) ! # of which are nonlinear
84 coi_error = max( coi_error, coidef_square( cntvect, 1 ) ) ! 1 means the model is square
85 coi_error = max( coi_error, coidef_optfile( cntvect, 'undef01.opt' ) )
86!
87! Tell CONOPT about the callback routines:
88!
89 coi_error = max( coi_error, coidef_readmatrix( cntvect, tria_readmatrix ) )
90 coi_error = max( coi_error, coidef_fdeval( cntvect, tria_fdeval ) )
91 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
92 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
93 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
94 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
95 coi_error = max( coi_error, coidef_triord( cntvect, std_triord ) )
96!
97! Allocate space for the solution values so we can check them.
98!
99 do_allocate = .true.
100
101#if defined(LICENSE_INT_1) && defined(LICENSE_INT_2) && defined(LICENSE_INT_3) && defined(LICENSE_TEXT)
102 coi_error = max( coi_error, coidef_license( cntvect, license_int_1, license_int_2, license_int_3, license_text) )
103#endif
104
105 If ( coi_error .ne. 0 ) THEN
106 write(*,*)
107 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
108 write(*,*)
109 call flog( "Skipping Solve due to setup errors", 1 )
110 ENDIF
111!
112! Start CONOPT:
113!
114 casenum = 1;
115 coi_error = coi_solve( cntvect )
116 If ( coi_error /= 400 ) then
117 call flog( "COI_Solve did not return 400 as expected.", 1 )
118 endif
119 write(*,*)
120 write(*,*) 'Successful return for undef01.'
121
122 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
123
124 call flog( "Successful Solve", 0 )
125
126End Program undef01
127!
128! ============================================================================
129! Define information about the model:
130!
131
132!> Define information about the model
133!!
134!! @include{doc} readMatrix_params.dox
135Integer Function tria_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
136 colsta, rowno, value, nlflag, n, m, nz, &
137 usrmem )
138#if defined(itl)
139!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Tria_ReadMatrix
140#endif
141 Use casedata_num
142 implicit none
143 integer, intent (in) :: n ! number of variables
144 integer, intent (in) :: m ! number of constraints
145 integer, intent (in) :: nz ! number of nonzeros
146 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
147 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
148 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
149 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
150 ! (not defined here)
151 integer, intent (out), dimension(m) :: type ! vector of equation types
152 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
153 ! (not defined here)
154 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
155 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
156 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
157 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
158 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
159 real*8 usrmem(*) ! optional user memory
160
161 integer :: i
162!
163! Information about Variables:
164! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
165! Default: the status information in Vsta is not used.
166!
167 lower(8) = 8.0d0 ! In most cases variable x8
168 upper(8) = 8.0d0 ! is fixed at 8.
169 curr(8) = 8.d0
170 lower(5) = 5.0d0 ! In most cases variable x5
171 upper(5) = 5.0d0 ! is fixed at 5.
172 curr(5) = 5.d0
173 If ( casenum == 2 ) Then
174 lower(8) = lower(1) ! In case 2 variable x8 is free
175 upper(8) = upper(1)
176 curr(8) = 0.d0
177 Endif
178 If ( casenum == 3 ) Then
179 lower(5) = lower(1) ! In case 3 variable x5 is free
180 upper(5) = upper(1)
181 curr(5) = 0.d0
182 lower(8) = lower(1) ! In case 3 variable x8 is free
183 upper(8) = upper(1)
184 curr(8) = 0.d0
185 lower(2) = 2.d0 ! In case 3 variable x2 is fixed at 2
186 upper(2) = 2.d0
187 lower(6) = 6.d0 ! In case 3 variable x6 is fixed at 6
188 upper(6) = 6.d0
189 Endif
190 if ( casenum == 4 ) Then
191 lower(3) = 4.d0 ! In case 3 x3 has a lower bound of 4
192 curr(3) = 4.d0
193 Endif
194 if ( casenum == 5 ) Then
195 lower(3) = 4.d0 ! In case 4 x3 is fixed at 4
196 upper(3) = 4.d0
197 curr(3) = 4.d0
198 Endif
199!
200! Information about Constraints:
201! Default: Rhs = 0
202! Default: the status information in Esta and the function
203! value in FV are not used.
204! Default: Type: There is no default.
205! 0 = Equality,
206! 1 = Greater than or equal,
207! 2 = Less than or equal,
208! 3 = Non binding.
209!
210 rhs(1) = 9.0d0
211 rhs(2) = 24.0d0
212 rhs(3) = 10.0d0
213 rhs(4) = 7.0d0
214 rhs(5) = 17.0d0
215 rhs(6) = 10.0d0
216 rhs(7) = 15.0d0
217 do i = 1, 6
218 type(i) = 0 ! Equality
219 enddo
220 type(7) = 2 ! Less than or equal
221!
222! Information about the Jacobian. We use the standard method with
223! Rowno, Value, Nlflag and Colsta and we do not use Colno.
224!
225! Colsta = Start of column indices (No Defaults):
226! Rowno = Row indices
227! Value = Value of derivative (by default only linear
228! derivatives are used)
229! Nlflag = 0 for linear and 1 for nonlinear derivative
230! (not needed for completely linear models)
231!
232! e1.. x2 + x7 =e= 9;
233! e2.. x1 + x3 + x5 + x7 + x8 =e= 24;
234! e3.. x1 + x4 + x5 =e= 10;
235! e4.. x2 + x5 =e= 7;
236! e5.. x4 + x5 + x8 =e= 17;
237! e6.. x1 + x3 + x6 =e= 10;
238! e7.. x1 + x4 + x7 =l= 15;
239!
240 colsta(1) = 1
241 colsta(2) = 5
242 colsta(3) = 7
243 colsta(4) = 9
244 colsta(5) = 12
245 colsta(6) = 16
246 colsta(7) = 17
247 colsta(8) = 20
248 colsta(9) = 22
249 rowno(1) = 2
250 rowno(2) = 3
251 rowno(3) = 6
252 rowno(4) = 7
253 rowno(5) = 1
254 rowno(6) = 4
255 rowno(7) = 2
256 rowno(8) = 6
257 rowno(9) = 3
258 rowno(10) = 5
259 rowno(11) = 7
260 rowno(12) = 2
261 rowno(13) = 3
262 rowno(14) = 4
263 rowno(15) = 5
264 rowno(16) = 6
265 rowno(17) = 1
266 rowno(18) = 2
267 rowno(19) = 7
268 rowno(20) = 2
269 rowno(21) = 5
270!
271! Nonlinearity Structure: The model is linear and nlflag is not needed.
272!
273! Value (Linear only): All Jacobian elements are 1
274!
275 do i = 1, 18 ! The last 3 elements are not defined. Should give an error.
276 value(i) = 1.0d0
277 enddo
278
279 tria_readmatrix = 0 ! Return value means OK
280
281end Function tria_readmatrix
282!
283!==========================================================================
284! Compute nonlinear terms and non-constant Jacobian elements
285!
286
287!> Compute nonlinear terms and non-constant Jacobian elements
288!!
289!! @include{doc} fdeval_params.dox
290Integer Function tria_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
291 n, nz, thread, usrmem )
292#if defined(itl)
293!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Tria_FDEval
294#endif
295 implicit none
296 integer, intent (in) :: n ! number of variables
297 integer, intent (in) :: rowno ! number of the row to be evaluated
298 integer, intent (in) :: nz ! number of nonzeros in this row
299 real*8, intent (in), dimension(n) :: x ! vector of current solution values
300 real*8, intent (in out) :: g ! constraint value
301 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
302 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
303 ! in this row. Ffor information only.
304 integer, intent (in) :: mode ! evaluation mode: 1 = function value
305 ! 2 = derivatives, 3 = both
306 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
307 ! as errcnt is incremented
308 integer, intent (in out) :: errcnt ! error counter to be incremented in case
309 ! of function evaluation errors.
310 integer, intent (in) :: thread
311 real*8 usrmem(*) ! optional user memory
312
313!
314! The model is linear and FDEval should never be called.
315!
316 tria_fdeval = 1
317
318end Function tria_fdeval
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_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_square(cntvect, square)
square models.
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_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_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
subroutine flog(msg, code)
Definition comdecl.f90:56
logical do_allocate
Definition comdecl.f90:21
subroutine startup
Definition comdecl.f90:35
integer function tria_fdeval(x, g, jac, rowno, jcnm, mode, ignerr, errcnt, n, nz, thread, usrmem)
Compute nonlinear terms and non-constant Jacobian elements.
Definition tria01.f90:265
integer function tria_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
Definition tria01.f90:145
program undef01
Main program. A simple setup and call of CONOPT.
Definition undef01.f90:35