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