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