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