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 )
126!
127! Free solution memory
128!
129 call finalize
131End Program undef01
132!
133! ============================================================================
134! Define information about the model:
135!
136
137!> Define information about the model
138!!
139!! @include{doc} readMatrix_params.dox
140Integer Function tria_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
141 colsta, rowno, value, nlflag, n, m, nz, &
142 usrmem )
143#ifdef dec_directives_win32
144!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Tria_ReadMatrix
145#endif
146 Use casedata_num
147 implicit none
148 integer, intent (in) :: n ! number of variables
149 integer, intent (in) :: m ! number of constraints
150 integer, intent (in) :: nz ! number of nonzeros
151 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
152 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
153 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
154 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
155 ! (not defined here)
156 integer, intent (out), dimension(m) :: type ! vector of equation types
157 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
158 ! (not defined here)
159 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
160 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
161 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
162 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
163 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
164 real*8 usrmem(*) ! optional user memory
165
166 integer :: i
167!
168! Information about Variables:
169! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
170! Default: the status information in Vsta is not used.
171!
172 lower(8) = 8.0d0 ! In most cases variable x8
173 upper(8) = 8.0d0 ! is fixed at 8.
174 curr(8) = 8.d0
175 lower(5) = 5.0d0 ! In most cases variable x5
176 upper(5) = 5.0d0 ! is fixed at 5.
177 curr(5) = 5.d0
178 If ( casenum == 2 ) Then
179 lower(8) = lower(1) ! In case 2 variable x8 is free
180 upper(8) = upper(1)
181 curr(8) = 0.d0
182 Endif
183 If ( casenum == 3 ) Then
184 lower(5) = lower(1) ! In case 3 variable x5 is free
185 upper(5) = upper(1)
186 curr(5) = 0.d0
187 lower(8) = lower(1) ! In case 3 variable x8 is free
188 upper(8) = upper(1)
189 curr(8) = 0.d0
190 lower(2) = 2.d0 ! In case 3 variable x2 is fixed at 2
191 upper(2) = 2.d0
192 lower(6) = 6.d0 ! In case 3 variable x6 is fixed at 6
193 upper(6) = 6.d0
194 Endif
195 if ( casenum == 4 ) Then
196 lower(3) = 4.d0 ! In case 3 x3 has a lower bound of 4
197 curr(3) = 4.d0
198 Endif
199 if ( casenum == 5 ) Then
200 lower(3) = 4.d0 ! In case 4 x3 is fixed at 4
201 upper(3) = 4.d0
202 curr(3) = 4.d0
203 Endif
204!
205! Information about Constraints:
206! Default: Rhs = 0
207! Default: the status information in Esta and the function
208! value in FV are not used.
209! Default: Type: There is no default.
210! 0 = Equality,
211! 1 = Greater than or equal,
212! 2 = Less than or equal,
213! 3 = Non binding.
214!
215 rhs(1) = 9.0d0
216 rhs(2) = 24.0d0
217 rhs(3) = 10.0d0
218 rhs(4) = 7.0d0
219 rhs(5) = 17.0d0
220 rhs(6) = 10.0d0
221 rhs(7) = 15.0d0
222 do i = 1, 6
223 type(i) = 0 ! Equality
224 enddo
225 type(7) = 2 ! Less than or equal
226!
227! Information about the Jacobian. CONOPT expects a columnwise
228! representation in Rowno, Value, Nlflag and Colsta.
229!
230! Colsta = Start of column indices (No Defaults):
231! Rowno = Row indices
232! Value = Value of derivative (by default only linear
233! derivatives are used)
234! Nlflag = 0 for linear and 1 for nonlinear derivative
235! (not needed for completely linear models)
236!
237! e1.. x2 + x7 =e= 9;
238! e2.. x1 + x3 + x5 + x7 + x8 =e= 24;
239! e3.. x1 + x4 + x5 =e= 10;
240! e4.. x2 + x5 =e= 7;
241! e5.. x4 + x5 + x8 =e= 17;
242! e6.. x1 + x3 + x6 =e= 10;
243! e7.. x1 + x4 + x7 =l= 15;
244!
245 colsta(1) = 1
246 colsta(2) = 5
247 colsta(3) = 7
248 colsta(4) = 9
249 colsta(5) = 12
250 colsta(6) = 16
251 colsta(7) = 17
252 colsta(8) = 20
253 colsta(9) = 22
254 rowno(1) = 2
255 rowno(2) = 3
256 rowno(3) = 6
257 rowno(4) = 7
258 rowno(5) = 1
259 rowno(6) = 4
260 rowno(7) = 2
261 rowno(8) = 6
262 rowno(9) = 3
263 rowno(10) = 5
264 rowno(11) = 7
265 rowno(12) = 2
266 rowno(13) = 3
267 rowno(14) = 4
268 rowno(15) = 5
269 rowno(16) = 6
270 rowno(17) = 1
271 rowno(18) = 2
272 rowno(19) = 7
273 rowno(20) = 2
274 rowno(21) = 5
275!
276! Nonlinearity Structure: The model is linear and nlflag is not needed.
277!
278! Value (Linear only): All Jacobian elements are 1
279!
280 do i = 1, 18 ! The last 3 elements are not defined. Should give an error.
281 value(i) = 1.0d0
282 enddo
283
284 tria_readmatrix = 0 ! Return value means OK
285
286end Function tria_readmatrix
287!
288!==========================================================================
289! Compute nonlinear terms and non-constant Jacobian elements
290!
291
292!> Compute nonlinear terms and non-constant Jacobian elements
293!!
294!! @include{doc} fdeval_params.dox
295Integer Function tria_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
296 n, nz, thread, usrmem )
297#ifdef dec_directives_win32
298!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Tria_FDEval
299#endif
300 implicit none
301 integer, intent (in) :: n ! number of variables
302 integer, intent (in) :: rowno ! number of the row to be evaluated
303 integer, intent (in) :: nz ! number of nonzeros in this row
304 real*8, intent (in), dimension(n) :: x ! vector of current solution values
305 real*8, intent (in out) :: g ! constraint value
306 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
307 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
308 ! in this row. Ffor information only.
309 integer, intent (in) :: mode ! evaluation mode: 1 = function value
310 ! 2 = derivatives, 3 = both
311 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
312 ! as errcnt is incremented
313 integer, intent (in out) :: errcnt ! error counter to be incremented in case
314 ! of function evaluation errors.
315 integer, intent (in) :: thread
316 real*8 usrmem(*) ! optional user memory
317
318!
319! The model is linear and FDEval should never be called.
320!
321 tria_fdeval = 1
322
323end Function tria_fdeval
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_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 finalize
Definition comdecl.f90:79
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:257
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:140
program undef01
Main program. A simple setup and call of CONOPT.
Definition undef01.f90:41