CONOPT
Loading...
Searching...
No Matches
square2.f90
Go to the documentation of this file.
1!> @file square2.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!! A square model where we pretend that the second constraint is completely nonlinear.
5!!
6!! \f[
7!! x1 + x2 = 10
8!! \f]
9!! \f[
10!! x1 - x2 = 0
11!! \f]
12!! \f[
13!! x1 + x2 \leq 9
14!! \f]
15!! In a second case we change the Rhs in the inequality to 11 so the
16!! model becomes feasible.
17!!
18!!
19!! For more information about the individual callbacks, please have a look at the source code.
20
21#if defined(_WIN32) && !defined(_WIN64)
22#define dec_directives_win32
23#endif
24
25!> Main program. A simple setup and call of CONOPT
26!!
27Program square
28
30 Use conopt
31 Use casedata_num
32 implicit None
33!
34! Declare the user callback routines as Integer, External:
35!
36 Integer, External :: sq_readmatrix ! Mandatory Matrix definition routine defined below
37 Integer, External :: sq_fdeval ! Function and Derivative evaluation routine
38 ! needed a nonlinear model.
39 Integer, External :: std_status ! Standard callback for displaying solution status
40 Integer, External :: std_solution ! Standard callback for displaying solution values
41 Integer, External :: std_message ! Standard callback for managing messages
42 Integer, External :: std_errmsg ! Standard callback for managing error messages
43#ifdef dec_directives_win32
44!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Sq_ReadMatrix
45!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Sq_FDEval
46!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
47!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
48!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
49!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
50#endif
51!
52! Control vector
53!
54 INTEGER, Dimension(:), Pointer :: cntvect
55 INTEGER :: coi_error
56
57 Integer :: i
58 Logical :: error
59
60 call startup
61!
62! Create and initialize a Control Vector
63!
64 coi_error = coi_create( cntvect )
65!
66! Tell CONOPT about the size of the model by populating the Control Vector:
67!
68 coi_error = max( coi_error, coidef_numvar( cntvect, 2 ) ) ! 2 variables
69 coi_error = max( coi_error, coidef_numcon( cntvect, 3 ) ) ! 3 constraints
70 coi_error = max( coi_error, coidef_numnz( cntvect, 6 ) ) ! 6 nonzeros in the Jacobian
71 coi_error = max( coi_error, coidef_numnlnz( cntvect, 4 ) ) ! 4 of which are nonlinear
72 coi_error = max( coi_error, coidef_square( cntvect, 1 ) ) ! 1 means Square system
73 coi_error = max( coi_error, coidef_optfile( cntvect, 'square2.opt' ) )
74!
75! Tell CONOPT about the callback routines:
76!
77 coi_error = max( coi_error, coidef_readmatrix( cntvect, sq_readmatrix ) )
78 coi_error = max( coi_error, coidef_fdeval( cntvect, sq_fdeval ) )
79 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
80 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
81 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
82 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
83
84#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
85 coi_error = max( coi_error, coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_license_text) )
86#endif
87
88 If ( coi_error .ne. 0 ) THEN
89 write(*,*)
90 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
91 write(*,*)
92 call flog( "Skipping Solve due to setup errors", 1 )
93 ENDIF
94!
95! Allocate space for the solution and status vectors.
96!
97 do_allocate = .true.
98!
99! Start CONOPT:
100!
101 casenum = 1
102 coi_error = coi_solve( cntvect )
103
104 write(*,*)
105 write(*,*) 'End of Square2 - case 1 example. Return code=',coi_error
106
107 If ( coi_error /= 0 ) then
108 call flog( "Case 1: Errors encountered during solution", 1 )
109 elseif ( stacalls == 0 .or. solcalls == 0 ) then
110 call flog( "Case 1: Status or Solution routine was not called", 1 )
111 elseif ( sstat /= 1 .or. mstat < 4 .or. mstat > 5 ) then
112 call flog( "Case 1: Solver or Model status not as expected (1,4) or (1,5)", 1 )
113 elseif ( obj /= 0.d0 ) Then
114 call flog( "Case 1: Objective for square model was not as expected 0.0", 1 )
115 else
116!
117! Check the primal and dual solution itself
118!
119 error = .false.
120 do i = 1, 2
121! if ( abs(Xprim(i)-5.0d0) > 1.d-7 ) error = .true. ! could be something else
122 if ( abs(xdual(i)) > 1.d-7 ) error = .true.
123 if ( abs(udual(i)) > 1.d-7 ) error = .true.
124 enddo
125 if ( abs(xprim(1)+xprim(2)-uprim(1)) > 1.e-7 ) error = .true. ! could be something else
126 if ( abs(xprim(1)-xprim(2)-uprim(2)) > 1.e-7 ) error = .true.
127 if ( abs(xprim(1)+xprim(2)-uprim(3)) > 1.e-7 ) error = .true.
128 if ( error ) call flog( "Case 1: Numerical solution was not as expected.", 1 )
129!
130! Check the status information
131!
132 do i = 1, 2
133 if ( xbasc(i) /= 2 ) error = .true. ! Basic
134 if ( xstat(i) /= 0 ) error = .true. ! Normal
135! if ( Ubasc(i) /= 0 ) error = .true. ! Lower
136! if ( UStat(i) /= 0 ) error = .true. ! Normal
137 enddo
138 if ( ustat(2) /= 0 ) error = .true. ! Normal
139 if ( ustat(3) /= 2 .and. ustat(1) /= 2 ) error = .true. ! Either Equation 1 or 3 must be infeasible
140! if ( Ubasc(3) /= 1 ) error = .true. ! Upper
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 Square2 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 < 15 .or. mstat > 16 ) then
157 call flog( "Case 2: Solver or Model status not as expected (1,15) or (1,16)", 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, 2
166 if ( abs(xprim(i)-5.0d0) > 1.d-7 ) error = .true.
167 if ( abs(xdual(i)) > 1.d-7 ) error = .true.
168 if ( abs(udual(i)) > 1.d-7 ) error = .true.
169 enddo
170 if ( abs(uprim(1)-10.d0) > 1.e-7 ) error = .true. ! could be something else
171 if ( abs(uprim(2)- 0.d0) > 1.e-7 ) error = .true.
172 if ( abs(uprim(3)-10.d0) > 1.e-7 ) error = .true.
173 if ( error ) call flog( "Case 2: Numerical solution was not as expected.", 1 )
174!
175! Check the status information
176!
177 do i = 1, 2
178 if ( xbasc(i) /= 2 ) error = .true. ! Basic
179 if ( xstat(i) /= 0 ) error = .true. ! Normal
180! if ( Ubasc(i) /= 0 ) error = .true. ! Lower
181 if ( ustat(i) /= 0 ) error = .true. ! Normal
182 enddo
183! if ( Ubasc(3) /= 2 ) error = .true. ! Basic
184 if ( ustat(3) /= 0 ) error = .true. ! Normal
185 if ( error ) call flog( "Case 2: Status information was not as expected.", 1 )
186 endif
187
188 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
189
190 call flog( "Successful Solve", 0 )
191!
192! Free solution memory
193!
194 call finalize
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!
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 Less than (and Rhs = 11 in case 2)
258!
259 if ( casenum == 1 ) then
260 rhs(3) = 9.d0
261 else if ( casenum == 2 ) then
262 rhs(3) = 11.d0
263 endif
264 type(3) = 2
265!
266! Information about the Jacobian. CONOPT expects a columnwise
267! representation in Rowno, Value, Nlflag and Colsta.
268!
269! Colsta = Start of column indices (No Defaults):
270! Rowno = Row indices
271! Value = Value of derivative (by default only linear
272! derivatives are used)
273! Nlflag = 0 for linear and 1 for nonlinear derivative
274! (not needed for completely linear models)
275!
276! Indices
277! x(1) x(2)
278! 1: 1 4
279! 2: 2 5
280! 3: 3 6
281!
282 colsta(1) = 1
283 colsta(2) = 4
284 colsta(3) = 7
285 rowno(1) = 1
286 rowno(2) = 2
287 rowno(3) = 3
288 rowno(4) = 1
289 rowno(5) = 2
290 rowno(6) = 3
291!
292! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
293! x(1) x(2)
294! 1: L L
295! 2: NL NL
296! 3: NL NL
297!
298 nlflag(1) = 0
299 nlflag(2) = 1
300 nlflag(3) = 1
301 nlflag(4) = 0
302 nlflag(5) = 1
303 nlflag(6) = 1
304!
305! Value (Linear only)
306! x(1) x(2) x(3) x(4)
307! 1: 1 1
308! 2: NL NL
309! 3: NL NL
310!
311 value(1) = 1.d0
312 value(4) = 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