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