CONOPT
Loading...
Searching...
No Matches
square5.f90
Go to the documentation of this file.
1!> @file square5.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 square4, but there are two levels of equations
23!! in the post-triangle.
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, -3.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, 'square5.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 Square5 - 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 Square5 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 if ( error ) call flog( "Case 2: Numerical solution was not as expected.", 1 )
176!
177! Check the status information
178!
179 do i = 1, 4
180 if ( xbasc(i) /= 2 ) error = .true. ! Basic
181 if ( xstat(i) /= 0 ) error = .true. ! Normal
182! if ( Ubasc(i) /= 0 ) error = .true. ! Lower
183 enddo
184 if ( ustat(1) /= 2 .and. ustat(3) /= 2 .and. ustat(4) /= 2 ) error = .true. ! One of them must be infeasible
185! if ( Ubasc(3) /= 1 ) error = .true. ! Upper
186! if ( Ubasc(4) /= 1 ) error = .true. ! Upper
187 if ( error ) call flog( "Case 2: Status information was not as expected.", 1 )
188 endif
189
190 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
191
192 call flog( "Successful Solve", 0 )
193!
194! Free solution memory
195!
196 call finalize
197
198End Program square
199!
200! ============================================================================
201! Define information about the model:
202!
203
204!> Define information about the model
205!!
206!! @include{doc} readMatrix_params.dox
207Integer Function sq_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
208 colsta, rowno, value, nlflag, n, m, nz, &
209 usrmem )
210#ifdef dec_directives_win32
211!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Sq_ReadMatrix
212#endif
213 Use casedata_num
214 implicit none
215 integer, intent (in) :: n ! number of variables
216 integer, intent (in) :: m ! number of constraints
217 integer, intent (in) :: nz ! number of nonzeros
218 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
219 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
220 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
221 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
222 ! (not defined here)
223 integer, intent (out), dimension(m) :: type ! vector of equation types
224 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
225 ! (not defined here)
226 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
227 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
228 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
229 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
230 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
231 real*8 usrmem(*) ! optional user memory
232!
233! Information about Variables:
234! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
235! Default: the status information in Vsta is not used.
236!
237! Information about Constraints:
238! Default: Rhs = 0
239! Default: the status information in Esta and the function
240! value in FV are not used.
241! Default: Type: There is no default.
242! 0 = Equality,
243! 1 = Greater than or equal,
244! 2 = Less than or equal,
245! 3 = Non binding.
246 if ( casenum == 2 ) then
247 lower(3) = 0.d0
248 lower(4) = 0.d0
249 endif
250!
251! Constraint 1
252! Rhs = 10 and type Equal
253!
254 rhs(1) = 10.d0
255 type(1) = 0
256!
257! Constraint 2
258! Rhs = 0 and type Equality
259!
260 type(2) = 0
261!
262! Constraint 3
263! Rhs = 9 and type Equality
264!
265 rhs(3) = 9.d0
266 type(3) = 0
267!
268! Constraint 4
269! Rhs = 1 and type Equality
270!
271 rhs(4) = 1.d0
272 type(4) = 0
273!
274! Information about the Jacobian. CONOPT expects a columnwise
275! representation in Rowno, Value, Nlflag and Colsta.
276!
277! Colsta = Start of column indices (No Defaults):
278! Rowno = Row indices
279! Value = Value of derivative (by default only linear
280! derivatives are used)
281! Nlflag = 0 for linear and 1 for nonlinear derivative
282! (not needed for completely linear models)
283!
284! Indices
285! x(1) x(2) x(3) x(4)
286! 1: 1 4
287! 2: 2 5
288! 3: 3 6 8
289! 4: 7 9 10
290!
291 colsta(1) = 1
292 colsta(2) = 4
293 colsta(3) = 8
294 colsta(4) = 10
295 colsta(5) = 11
296 rowno(1) = 1
297 rowno(2) = 2
298 rowno(3) = 3
299 rowno(4) = 1
300 rowno(5) = 2
301 rowno(6) = 3
302 rowno(7) = 4
303 rowno(8) = 3
304 rowno(9) = 4
305 rowno(10) = 4
306!
307! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
308! x(1) x(2) x(3) x(4)
309! 1: L L
310! 2: NL NL
311! 3: NL NL L
312! 4: NL NL L
313!
314 nlflag(1) = 0
315 nlflag(2) = 1
316 nlflag(3) = 1
317 nlflag(4) = 0
318 nlflag(5) = 1
319 nlflag(6) = 1
320 nlflag(7) = 1
321 nlflag(8) = 0
322 nlflag(9) = 1
323 nlflag(10)= 0
324!
325! Value (Linear only)
326! x(1) x(2) x(3) x(4)
327! 1: 1 1
328! 2: NL NL
329! 3: NL NL 1
330! 4: NL NL 1
331!
332 value(1) = 1.d0
333 value(4) = 1.d0
334 value(8) = 1.d0
335 value(10) = 1.d0
337 sq_readmatrix = 0 ! Return value means OK
338
339end Function sq_readmatrix
340!
341!==========================================================================
342! Compute nonlinear terms and non-constant Jacobian elements
343!
344
345!> Compute nonlinear terms and non-constant Jacobian elements
346!!
347!! @include{doc} fdeval_params.dox
348Integer Function sq_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
349 n, nz, thread, usrmem )
350#ifdef dec_directives_win32
351!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Sq_FDEval
352#endif
353 implicit none
354 integer, intent (in) :: n ! number of variables
355 integer, intent (in) :: rowno ! number of the row to be evaluated
356 integer, intent (in) :: nz ! number of nonzeros in this row
357 real*8, intent (in), dimension(n) :: x ! vector of current solution values
358 real*8, intent (in out) :: g ! constraint value
359 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
360 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
361 ! in this row. Ffor information only.
362 integer, intent (in) :: mode ! evaluation mode: 1 = function value
363 ! 2 = derivatives, 3 = both
364 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
365 ! as errcnt is incremented
366 integer, intent (in out) :: errcnt ! error counter to be incremented in case
367 ! of function evaluation errors.
368 integer, intent (in) :: thread
369 real*8 usrmem(*) ! optional user memory
370!
371! Row 1: Is declared as linear and should not be called.
372!
373 if ( rowno .eq. 1 ) then
374 sq_fdeval = 1
375 return
376!
377! Row 2: x1 + x2 assumed to be nonlinear
378!
379 elseif ( rowno .eq. 2 ) then
380!
381! Mode = 1 or 3: Function value
382!
383 if ( mode .eq. 1 .or. mode .eq. 3 ) then
384 g = x(1) - x(2)
385 endif
386!
387! Mode = 2 or 3: Derivatives
388!
389 if ( mode .eq. 2 .or. mode .eq. 3 ) then
390 jac(1) = 1.d0
391 jac(2) = -1.d0
392 endif
393 elseif ( rowno .eq. 3 ) then
394!
395! Mode = 1 or 3: Function value
396!
397 if ( mode .eq. 1 .or. mode .eq. 3 ) then
398 g = x(1) + x(2)
399 endif
400!
401! Mode = 2 or 3: Derivatives
402!
403 if ( mode .eq. 2 .or. mode .eq. 3 ) then
404 jac(1) = 1.d0
405 jac(2) = 1.d0
406 endif
407 elseif ( rowno .eq. 4 ) then
408!
409! Mode = 1 or 3: Function value
410!
411 if ( mode .eq. 1 .or. mode .eq. 3 ) then
412 g = x(2) + x(3)
413 endif
414!
415! Mode = 2 or 3: Derivatives
416!
417 if ( mode .eq. 2 .or. mode .eq. 3 ) then
418 jac(2) = 1.d0
419 jac(3) = 1.d0
420 endif
421 endif
422 sq_fdeval = 0
423
424end 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