CONOPT
Loading...
Searching...
No Matches
square4.f90
Go to the documentation of this file.
1!> @file square4.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!! A square model where we pretend that the last two constraints are completely
5!! nonlinear except for the last term.
6!!
7!! \f{eqnarray*}{
8!! &x1 + x2 = 10 \\
9!! &x1 - x2 = 0 \\
10!! &x1 + x2 + x3 = 9
11!! \f}
12!!
13!! In a second case we add a lower bound on x3 of 0 so the model becomes
14!! infeasible.
15!! The model is similar to square2, but the slack variable in equation 3
16!! has been made explicit as a structural variable
17!!
18!!
19!! For more information about the individual callbacks, please have a look at the source code.
20
21#if defined(_WIN32) && !defined(_WIN64)
22#define dec_directives_win32
23#endif
24
25!> Main program. A simple setup and call of CONOPT
26!!
27Program square
28
30 Use conopt
31 Use casedata_num
32 implicit None
33!
34! Declare the user callback routines as Integer, External:
35!
36 Integer, External :: sq_readmatrix ! Mandatory Matrix definition routine defined below
37 Integer, External :: sq_fdeval ! Function and Derivative evaluation routine
38 ! needed a nonlinear model.
39 Integer, External :: std_status ! Standard callback for displaying solution status
40 Integer, External :: std_solution ! Standard callback for displaying solution values
41 Integer, External :: std_message ! Standard callback for managing messages
42 Integer, External :: std_errmsg ! Standard callback for managing error messages
43#ifdef dec_directives_win32
44!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Sq_ReadMatrix
45!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Sq_FDEval
46!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
47!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
48!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
49!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
50#endif
51!
52! Control vector
53!
54 INTEGER, Dimension(:), Pointer :: cntvect
55 INTEGER :: coi_error
56
57 Integer :: i
58 Logical :: error
59
60 real*8, dimension(3) :: xsol1 = (/ 5.d0, 5.d0, -1.d0 /)
61 real*8, dimension(3) :: usol1 = (/ 10.d0, 0.d0, 9.d0 /)
62
63 call startup
64!
65! Create and initialize a Control Vector
66!
67 coi_error = coi_create( cntvect )
68!
69! Tell CONOPT about the size of the model by populating the Control Vector:
70!
71 coi_error = max( coi_error, coidef_numvar( cntvect, 3 ) ) ! 3 variables
72 coi_error = max( coi_error, coidef_numcon( cntvect, 3 ) ) ! 3 constraints
73 coi_error = max( coi_error, coidef_numnz( cntvect, 7 ) ) ! 6 nonzeros in the Jacobian
74 coi_error = max( coi_error, coidef_numnlnz( cntvect, 4 ) ) ! 4 of which are nonlinear
75 coi_error = max( coi_error, coidef_square( cntvect, 1 ) ) ! 1 means Square system
76 coi_error = max( coi_error, coidef_optfile( cntvect, 'square4.opt' ) )
77!
78! Tell CONOPT about the callback routines:
79!
80 coi_error = max( coi_error, coidef_readmatrix( cntvect, sq_readmatrix ) )
81 coi_error = max( coi_error, coidef_fdeval( cntvect, sq_fdeval ) )
82 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
83 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
84 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
85 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
86
87#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
88 coi_error = max( coi_error, coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_license_text) )
89#endif
90
91 If ( coi_error .ne. 0 ) THEN
92 write(*,*)
93 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
94 write(*,*)
95 call flog( "Skipping Solve due to setup errors", 1 )
96 ENDIF
97!
98! Allocate space for the solution and status vectors.
99!
100 do_allocate = .true.
101!
102! Start CONOPT:
103!
104 casenum = 1
105 coi_error = coi_solve( cntvect )
106
107 write(*,*)
108 write(*,*) 'End of Square4 - case 1 example. Return code=',coi_error
109
110 If ( coi_error /= 0 ) then
111 call flog( "Case 1: Errors encountered during solution", 1 )
112 elseif ( stacalls == 0 .or. solcalls == 0 ) then
113 call flog( "Case 1: Status or Solution routine was not called", 1 )
114 elseif ( sstat /= 1 .or. mstat < 15 .or. mstat > 16 ) then
115 call flog( "Case 1: Solver or Model status not as expected (1,15) or (1,16)", 1 )
116 elseif ( obj /= 0.d0 ) Then
117 call flog( "Case 1: Objective for square model was not as expected 0.0", 1 )
118 else
119!
120! Check the primal and dual solution itself
121!
122 error = .false.
123 do i = 1, 3
124 if ( abs(xprim(i)-xsol1(i)) > 1.d-7 ) error = .true.
125 if ( abs(xdual(i)) > 1.d-7 ) error = .true.
126 if ( abs(udual(i)) > 1.d-7 ) error = .true.
127 if ( abs(uprim(i)- usol1(i)) > 1.e-7 ) error = .true.
128 enddo
129 if ( error ) call flog( "Case 1: Numerical solution was not as expected.", 1 )
130!
131! Check the status information
132!
133 do i = 1, 3
134 if ( xbasc(i) /= 2 ) error = .true. ! Basic
135 if ( xstat(i) /= 0 ) error = .true. ! Normal
136! if ( Ubasc(i) /= 0 ) error = .true. ! Lower
137 if ( ustat(i) /= 0 ) error = .true. ! Normal
138 enddo
139 if ( error ) call flog( "Case 1: Status information was not as expected.", 1 )
140 endif
141!
142! Start CONOPT with second case where the right hand side in equation 3 is 11:
143!
144 casenum = 2
145 coi_error = coi_solve( cntvect )
146
147 write(*,*)
148 write(*,*) 'End of Square4 example case 2. Return code=',coi_error
149
150 If ( coi_error /= 0 ) then
151 call flog( "Case 2: Errors encountered during solution", 1 )
152 elseif ( stacalls == 0 .or. solcalls == 0 ) then
153 call flog( "Case 2: Status or Solution routine was not called", 1 )
154 elseif ( sstat /= 1 .or. mstat < 4 .or. mstat > 5 ) then
155 call flog( "Case 2: Solver or Model status not as expected (1,4) or (1,5)", 1 )
156 elseif ( obj /= 0.d0 ) Then
157 call flog( "Case 2: Objective for square model was not as expected 0.0", 1 )
158 else
159!
160! Check the primal and dual solution itself
161!
162 error = .false.
163 do i = 1, 3
164 if ( abs(xdual(i)) > 1.d-7 ) error = .true.
165 if ( abs(udual(i)) > 1.d-7 ) error = .true.
166 enddo
167 if ( error ) call flog( "Case 2: Numerical solution was not as expected.", 1 )
168!
169! Check the status information
170!
171 do i = 1, 3
172 if ( xbasc(i) /= 2 ) error = .true. ! Basic
173 if ( xstat(i) /= 0 ) error = .true. ! Normal
174! if ( Ubasc(i) /= 0 ) error = .true. ! Lower
175 enddo
176 if ( ustat(1) /= 2 .and. ustat(3) /= 2 ) error = .true. ! One of them must be infeasible
177! if ( Ubasc(3) /= 1 ) error = .true. ! Upper
178 if ( error ) call flog( "Case 2: Status information was not as expected.", 1 )
179 endif
180
181 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
182
183 call flog( "Successful Solve", 0 )
184
185End Program square
186!
187! ============================================================================
188! Define information about the model:
189!
190
191!> Define information about the model
192!!
193!! @include{doc} readMatrix_params.dox
194Integer Function sq_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
195 colsta, rowno, value, nlflag, n, m, nz, &
196 usrmem )
197#ifdef dec_directives_win32
198!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Sq_ReadMatrix
199#endif
200 Use casedata_num
201 implicit none
202 integer, intent (in) :: n ! number of variables
203 integer, intent (in) :: m ! number of constraints
204 integer, intent (in) :: nz ! number of nonzeros
205 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
206 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
207 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
208 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
209 ! (not defined here)
210 integer, intent (out), dimension(m) :: type ! vector of equation types
211 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
212 ! (not defined here)
213 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
214 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
215 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
216 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
217 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
218 real*8 usrmem(*) ! optional user memory
219!
220! Information about Variables:
221! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
222! Default: the status information in Vsta is not used.
223!
224! Information about Constraints:
225! Default: Rhs = 0
226! Default: the status information in Esta and the function
227! value in FV are not used.
228! Default: Type: There is no default.
229! 0 = Equality,
230! 1 = Greater than or equal,
231! 2 = Less than or equal,
232! 3 = Non binding.
233 if ( casenum == 2 ) then
234 lower(3) = 0.d0
235 endif
236!
237! Constraint 1
238! Rhs = 10 and type Equal
239!
240 rhs(1) = 10.d0
241 type(1) = 0
242!
243! Constraint 2
244! Rhs = 0 and type Equality
245!
246 type(2) = 0
247!
248! Constraint 3
249! Rhs = 9 and type Equality
250!
251 rhs(3) = 9.d0
252 type(3) = 0
253!
254! Information about the Jacobian. CONOPT expects a columnwise
255! representation in Rowno, Value, Nlflag and Colsta.
256!
257! Colsta = Start of column indices (No Defaults):
258! Rowno = Row indices
259! Value = Value of derivative (by default only linear
260! derivatives are used)
261! Nlflag = 0 for linear and 1 for nonlinear derivative
262! (not needed for completely linear models)
263!
264! Indices
265! x(1) x(2) x(3)
266! 1: 1 4
267! 2: 2 5
268! 3: 3 6 7
269!
270 colsta(1) = 1
271 colsta(2) = 4
272 colsta(3) = 7
273 colsta(4) = 8
274 rowno(1) = 1
275 rowno(2) = 2
276 rowno(3) = 3
277 rowno(4) = 1
278 rowno(5) = 2
279 rowno(6) = 3
280 rowno(7) = 3
281!
282! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
283! x(1) x(2) x(3)
284! 1: L L
285! 2: NL NL
286! 3: NL NL L
287!
288 nlflag(1) = 0
289 nlflag(2) = 1
290 nlflag(3) = 1
291 nlflag(4) = 0
292 nlflag(5) = 1
293 nlflag(6) = 1
294 nlflag(7) = 0
295!
296! Value (Linear only)
297! x(1) x(2) x(3) x(4)
298! 1: 1 1
299! 2: NL NL
300! 3: NL NL
301!
302 value(1) = 1.d0
303 value(4) = 1.d0
304 value(7) = 1.d0
306 sq_readmatrix = 0 ! Return value means OK
307
308end Function sq_readmatrix
309!
310!==========================================================================
311! Compute nonlinear terms and non-constant Jacobian elements
312!
313
314!> Compute nonlinear terms and non-constant Jacobian elements
315!!
316!! @include{doc} fdeval_params.dox
317Integer Function sq_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
318 n, nz, thread, usrmem )
319#ifdef dec_directives_win32
320!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Sq_FDEval
321#endif
322 implicit none
323 integer, intent (in) :: n ! number of variables
324 integer, intent (in) :: rowno ! number of the row to be evaluated
325 integer, intent (in) :: nz ! number of nonzeros in this row
326 real*8, intent (in), dimension(n) :: x ! vector of current solution values
327 real*8, intent (in out) :: g ! constraint value
328 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
329 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
330 ! in this row. Ffor information only.
331 integer, intent (in) :: mode ! evaluation mode: 1 = function value
332 ! 2 = derivatives, 3 = both
333 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
334 ! as errcnt is incremented
335 integer, intent (in out) :: errcnt ! error counter to be incremented in case
336 ! of function evaluation errors.
337 integer, intent (in) :: thread
338 real*8 usrmem(*) ! optional user memory
339!
340! Row 1: Is declared as linear and should not be called.
341!
342 if ( rowno .eq. 1 ) then
343 sq_fdeval = 1
344 return
345!
346! Row 2: x1 + x2 assumed to be nonlinear
347!
348 elseif ( rowno .eq. 2 ) then
349!
350! Mode = 1 or 3: Function value
351!
352 if ( mode .eq. 1 .or. mode .eq. 3 ) then
353 g = x(1) - x(2)
354 endif
355!
356! Mode = 2 or 3: Derivatives
357!
358 if ( mode .eq. 2 .or. mode .eq. 3 ) then
359 jac(1) = 1.d0
360 jac(2) = -1.d0
361 endif
362 elseif ( rowno .eq. 3 ) then
363!
364! Mode = 1 or 3: Function value
365!
366 if ( mode .eq. 1 .or. mode .eq. 3 ) then
367 g = x(1) + x(2)
368 endif
369!
370! Mode = 2 or 3: Derivatives
371!
372 if ( mode .eq. 2 .or. mode .eq. 3 ) then
373 jac(1) = 1.d0
374 jac(2) = 1.d0
375 endif
376 endif
377 sq_fdeval = 0
378
379end 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_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_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
real *8, dimension(:), pointer udual
Definition comdecl.f90:24
real *8, dimension(:), pointer xdual
Definition comdecl.f90:23
integer, dimension(:), pointer xstat
Definition comdecl.f90:25
integer, dimension(:), pointer xbasc
Definition comdecl.f90:25
integer, dimension(:), pointer ustat
Definition comdecl.f90:26
integer stacalls
Definition comdecl.f90:14
subroutine flog(msg, code)
Definition comdecl.f90:62
logical do_allocate
Definition comdecl.f90:27
real *8, dimension(:), pointer xprim
Definition comdecl.f90:23
real *8, dimension(:), pointer uprim
Definition comdecl.f90:24
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