CONOPT
Loading...
Searching...
No Matches
square8.f90
Go to the documentation of this file.
1!> @file square8.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! Similar to square7 but with a different right hand side in e3
6!!
7!! \f{eqnarray*}{
8!! &x1 + x2 = 6 \\
9!! &- x2 = 2 \\
10!! &x1 + 2*x2 = 5 \\
11!! &- x2 + x3 + x4 = 1
12!! \f}
13!!
14!! This model is solved as a Square System.
15!!
16!! This model has a pre-triangular part and a redundant constraint.
17!! e2 is solved w.r.t. x2 and e1 and e3 are then the only constraints
18!! involving e1 so one of them is linearly dependent. They are also
19!! inconsistent and the model is therefore globally infeasible.
20!! e4 is post-triangular and is solved w.r.t. either x3 or x4.
21!! Overall, the model should end with 'Infeasible' or Model Status 4.
22!!
23!! When the linear dependence is detected, a traceback should explain it.
24!!
25!!
26!! For more information about the individual callbacks, please have a look at the source code.
27
28#if defined(_WIN32) && !defined(_WIN64)
29#define dec_directives_win32
30#endif
31
32!> Main program. A simple setup and call of CONOPT
33!!
34Program square
35
37 Use conopt
38 Use casedata_num
39 implicit None
40!
41! Declare the user callback routines as Integer, External:
42!
43 Integer, External :: sq_readmatrix ! Mandatory Matrix definition routine defined below
44 Integer, External :: sq_fdeval ! Function and Derivative evaluation routine
45 ! needed a nonlinear model.
46 Integer, External :: std_status ! Standard callback for displaying solution status
47 Integer, External :: std_solution ! Standard callback for displaying solution values
48 Integer, External :: std_message ! Standard callback for managing messages
49 Integer, External :: std_errmsg ! Standard callback for managing error messages
50 Integer, External :: std_triord ! Standard callback for reporting recursive order
51#ifdef dec_directives_win32
52!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Sq_ReadMatrix
53!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Sq_FDEval
54!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
55!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
56!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
57!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
58!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_TriOrd
59#endif
60!
61! Control vector
62!
63 INTEGER, Dimension(:), Pointer :: cntvect
64 INTEGER :: coi_error
65
66 call startup
67!
68! Create and initialize a Control Vector
69!
70 coi_error = coi_create( cntvect )
71!
72! Tell CONOPT about the size of the model by populating the Control Vector:
73!
74 coi_error = max( coi_error, coidef_numvar( cntvect, 4 ) ) ! # variables
75 coi_error = max( coi_error, coidef_numcon( cntvect, 4 ) ) ! # constraints
76 coi_error = max( coi_error, coidef_numnz( cntvect, 8 ) ) ! # nonzeros in the Jacobian
77 coi_error = max( coi_error, coidef_numnlnz( cntvect, 0 ) ) ! # of which are nonlinear
78 coi_error = max( coi_error, coidef_square( cntvect, 1 ) ) ! 1 means Square system
79 coi_error = max( coi_error, coidef_optfile( cntvect, 'square7.opt' ) )
80!
81! Tell CONOPT about the callback routines:
82!
83 coi_error = max( coi_error, coidef_readmatrix( cntvect, sq_readmatrix ) )
84 coi_error = max( coi_error, coidef_fdeval( cntvect, sq_fdeval ) )
85 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
86 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
87 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
88 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
89 coi_error = max( coi_error, coidef_triord( cntvect, std_triord ) )
90
91#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
92 coi_error = max( coi_error, coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_license_text) )
93#endif
94
95 If ( coi_error .ne. 0 ) THEN
96 write(*,*)
97 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
98 write(*,*)
99 call flog( "Skipping Solve due to setup errors", 1 )
100 ENDIF
101!
102! Ask Std_Solution to allocate space for the solution and status
103! vectors and keep this information
104!
105 do_allocate = .true.
106!
107! Start CONOPT:
108!
109 casenum = 1
110 coi_error = coi_solve( cntvect )
111
112 write(*,*)
113 write(*,*) 'End of Square7 - case 1 example. Return code=',coi_error
114
115 If ( coi_error /= 0 ) then
116 call flog( "Case 1: Errors encountered during solution", 1 )
117 elseif ( stacalls == 0 .or. solcalls == 0 ) then
118 call flog( "Case 1: Status or Solution routine was not called", 1 )
119 elseif ( sstat /= 1 .or. mstat /= 4 ) then ! infeasible for dependent rows
120 call flog( "Case 1: Solver or Model status not as expected (1,4)", 1 )
121 elseif ( obj /= 0.d0 ) Then
122 call flog( "Case 1: Objective for square model was not as expected 0.0", 1 )
123 endif
124
125 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
126
127 call flog( "Successful Solve", 0 )
129End Program square
130!
131! ============================================================================
132! Define information about the model:
133!
134
135!> Define information about the model
136!!
137!! @include{doc} readMatrix_params.dox
138Integer Function sq_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
139 colsta, rowno, value, nlflag, n, m, nz, &
140 usrmem )
141#ifdef dec_directives_win32
142!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Sq_ReadMatrix
143#endif
144 Use casedata_num
145 implicit none
146 integer, intent (in) :: n ! number of variables
147 integer, intent (in) :: m ! number of constraints
148 integer, intent (in) :: nz ! number of nonzeros
149 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
150 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
151 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
152 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
153 ! (not defined here)
154 integer, intent (out), dimension(m) :: type ! vector of equation types
155 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
156 ! (not defined here)
157 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
158 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
159 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
160 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
161 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
162 real*8 usrmem(*) ! optional user memory
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! Information about Constraints:
169! Default: Rhs = 0
170! Default: the status information in Esta and the function
171! value in FV are not used.
172! Default: Type: There is no default.
173! 0 = Equality,
174! 1 = Greater than or equal,
175! 2 = Less than or equal,
176! 3 = Non binding.
177!
178! Constraint 1
179! Rhs = 6 and type Equal
180!
181 rhs(1) = 6.d0
182 type(1) = 0
183!
184! Constraint 2
185! Rhs = 2 and type Equality
186!
187 type(2) = 0
188 rhs(2) = 2.d0
189!
190! Constraint 3
191! Rhs = 5 and type Equality
192!
193 rhs(3) = 5.d0
194 type(3) = 0
195!
196! Constraint 4
197! Rhs = 1 and type Equality
198!
199 rhs(4) = 1.d0
200 type(4) = 0
201!
202! Information about the Jacobian. CONOPT expects a columnwise
203! representation in Rowno, Value, Nlflag and Colsta.
204!
205! Colsta = Start of column indices (No Defaults):
206! Rowno = Row indices
207! Value = Value of derivative (by default only linear
208! derivatives are used)
209! Nlflag = 0 for linear and 1 for nonlinear derivative
210! (not needed for completely linear models)
211!
212! Indices
213! Model x1 + x2 = 10
214! - x2 = 2
215! x1 + 2*x2 = 6
216! - x2 + x3 + x4 = 1
217! x(1) x(2) x(3) x(4)
218! 1: 1 3
219! 2: 4
220! 3: 2 5
221! 4: 6 7 8
222!
223 colsta(1) = 1
224 colsta(2) = 3
225 colsta(3) = 7
226 colsta(4) = 8
227 colsta(5) = 9
228 rowno(1) = 1
229 rowno(2) = 3
230 rowno(3) = 1
231 rowno(4) = 2
232 rowno(5) = 3
233 rowno(6) = 4
234 rowno(7) = 4
235 rowno(8) = 4
236!
237! Nonlinearity Structure: All linear -- nlflag is not defined.
238!
239!
240! Value (Linear only)
241! x(1) x(2) x(3) x(4)
242! 1: 1 1
243! 2: -1
244! 3: 1 2
245! 4: -1 1 1
246!
247 value(1) = 1.d0
248 value(2) = 1.d0
249 value(3) = 1.d0
250 value(4) =-1.d0
251 value(5) = 2.d0
252 value(6) =-1.d0
253 value(7) = 1.d0
254 value(8) = 1.d0
255
256 sq_readmatrix = 0 ! Return value means OK
257
258end Function sq_readmatrix
259!
260!==========================================================================
261! Compute nonlinear terms and non-constant Jacobian elements
262!
263
264!> Compute nonlinear terms and non-constant Jacobian elements
265!!
266!! @include{doc} fdeval_params.dox
267Integer Function sq_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
268 n, nz, thread, usrmem )
269#ifdef dec_directives_win32
270!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Sq_FDEval
271#endif
272 implicit none
273 integer, intent (in) :: n ! number of variables
274 integer, intent (in) :: rowno ! number of the row to be evaluated
275 integer, intent (in) :: nz ! number of nonzeros in this row
276 real*8, intent (in), dimension(n) :: x ! vector of current solution values
277 real*8, intent (in out) :: g ! constraint value
278 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
279 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
280 ! in this row. Ffor information only.
281 integer, intent (in) :: mode ! evaluation mode: 1 = function value
282 ! 2 = derivatives, 3 = both
283 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
284 ! as errcnt is incremented
285 integer, intent (in out) :: errcnt ! error counter to be incremented in case
286 ! of function evaluation errors.
287 integer, intent (in) :: thread
288 real*8 usrmem(*) ! optional user memory
289!
290! All rows are declared as linear and should not be called.
291!
292 sq_fdeval = 1
293
294end Function sq_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
real *8 obj
Definition comdecl.f90:16
integer solcalls
Definition comdecl.f90:15
integer sstat
Definition comdecl.f90:18
integer stacalls
Definition comdecl.f90:14
subroutine flog(msg, code)
Definition comdecl.f90:62
logical do_allocate
Definition comdecl.f90:27
integer mstat
Definition comdecl.f90:17
subroutine startup
Definition comdecl.f90:41
integer function sq_fdeval(x, g, jac, rowno, jcnm, mode, ignerr, errcnt, n, nz, thread, usrmem)
Compute nonlinear terms and non-constant Jacobian elements.
Definition square.f90:237
integer function sq_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
Definition square.f90:139
program square
Main program. A simple setup and call of CONOPT.
Definition square.f90:23