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