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