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