CONOPT
Loading...
Searching...
No Matches
sqp3.f90
Go to the documentation of this file.
1!> @file sqp3.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! SQP3: Sparse QP model with `2DLagrStr` and `2DLagrVal` routines
6!!
7!!
8!! For more information about the individual callbacks, please have a look at the source code.
9
10#if defined(_WIN32) && !defined(_WIN64)
11#define dec_directives_win32
12#endif
13
14!> Main program. A simple setup and call of CONOPT for a QP model
15!!
16Program qp
17
19 Use conopt
20 Use qp_data_sp
21 implicit None
22!
23! Declare the user callback routines as Integer, External:
24!
25 Integer, External :: qp_readmatrix ! Mandatory Matrix definition routine defined below
26 Integer, External :: qp_fdeval ! Function and Derivative evaluation routine
27 ! needed a nonlinear model.
28 Integer, External :: qp_2dlagrstr ! 2nd derivative routine for structure
29 Integer, External :: qp_2dlagrval ! 2nd derivative routine for values
30 Integer, External :: std_status ! Standard callback for displaying solution status
31 Integer, External :: std_solution ! Standard callback for displaying solution values
32 Integer, External :: std_message ! Standard callback for managing messages
33 Integer, External :: std_errmsg ! Standard callback for managing error messages
34#ifdef dec_directives_win32
35!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: QP_ReadMatrix
36!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: QP_FDEval
37!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: QP_2DLagrStr
38!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: QP_2DLagrVal
39!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
40!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
41!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
42!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
43#endif
44!
45! Control vector
46!
47 INTEGER, Dimension(:), Pointer :: cntvect
48 INTEGER :: coi_error
49!
50! Local variables used to define Q
51!
52 Integer :: i,j,k
53
54 call startup
55!
56! Create and initialize a Control Vector
57!
58 coi_error = coi_create( cntvect )
59!
60! Tell CONOPT about the size of the model by populating the Control Vector:
61! We will create a QP model with one constraint, sum(i, x(i) ) = 1
62! and NN variables. NN and the Q matrix are defined in Module QP_Data_Sp.
63!
64 j = 0
65 do i = 1, nn
66 j = j + 1
67 q(j) = 1.d0
68 qrow(j) = i
69 qcol(j) = i
70 do k = i+1, nn, 3
71 j = j + 1
72 q(j) = q(j-1)*0.25 ! Make sure the matrix is diagonally dominant.
73 qrow(j) = k
74 qcol(j) = i
75 enddo
76 enddo
77 nq = j
78 do i = 1, nn
79 target(i) = 10.d0
80 enddo
81
82 coi_error = max( coi_error, coidef_numvar( cntvect, nn ) ) ! NN variables
83 coi_error = max( coi_error, coidef_numcon( cntvect, 2 ) ) ! 1 constraint + 1 objective
84 coi_error = max( coi_error, coidef_numnz( cntvect, 2*nn ) ) ! 2*NN nonzeros in the Jacobian
85 coi_error = max( coi_error, coidef_numnlnz( cntvect, nn ) ) ! NN of which are nonlinear
86 coi_error = max( coi_error, coidef_numhess( cntvect, nq ) ) ! NQ elements in the Hessian
87 coi_error = max( coi_error, coidef_optdir( cntvect, -1 ) ) ! Minimize
88 coi_error = max( coi_error, coidef_objcon( cntvect, 1 ) ) ! Objective is constraint 1
89 coi_error = max( coi_error, coidef_hessfac( cntvect, 1.d0 ) ) ! Only allow a very sparse Hessian in 2DLagr
90 coi_error = max( coi_error, coidef_optfile( cntvect, 'sqp3.opt' ) )
91!
92! Tell CONOPT about the callback routines:
93!
94 coi_error = max( coi_error, coidef_readmatrix( cntvect, qp_readmatrix ) )
95 coi_error = max( coi_error, coidef_fdeval( cntvect, qp_fdeval ) )
96 coi_error = max( coi_error, coidef_2dlagrstr( cntvect, qp_2dlagrstr ) )
97 coi_error = max( coi_error, coidef_2dlagrval( cntvect, qp_2dlagrval ) )
98 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
99 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
100 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
101 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
102
103#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
104 coi_error = max( coi_error, coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_license_text) )
105#endif
106
107 If ( coi_error .ne. 0 ) THEN
108 write(*,*)
109 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
110 write(*,*)
111 call flog( "Skipping Solve due to setup errors", 1 )
112 ENDIF
113!
114! Start CONOPT:
115!
116 coi_error = coi_solve( cntvect )
117
118 If ( coi_error /= 0 ) then
119 call flog( "Errors encountered during solution", 1 )
120 elseif ( stacalls == 0 .or. solcalls == 0 ) then
121 call flog( "Status or Solution routine was not called", 1 )
122 elseif ( sstat /= 1 .or. mstat /= 2 ) then
123 call flog( "Solver and Model Status was not as expected (1,2)", 1 )
124 endif
125!
126! Repeat but this time allow a dense Hessian
127!
128 coi_error = max( coi_error, coidef_hessfac( cntvect, 0.d0 ) ) ! Allow a dense Hessian in 2DLagr. 0.0 means no limit
129 If ( coi_error .ne. 0 ) THEN
130 write(*,*)
131 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
132 write(*,*)
133 call flog( "Skipping Solve due to setup errors", 1 )
134 ENDIF
135
136 coi_error = coi_solve( cntvect )
137
138 If ( coi_error /= 0 ) then
139 call flog( "Errors encountered during solution", 1 )
140 elseif ( stacalls == 0 .or. solcalls == 0 ) then
141 call flog( "Status or Solution routine was not called", 1 )
142 elseif ( sstat /= 1 .or. mstat /= 2 ) then
143 call flog( "Solver and Model Status was not as expected (1,2)", 1 )
144 endif
145!
146! Repeat once again but this time with a MaxHeap limit that should prevent using the Hessian
147!
148 coi_error = max( coi_error, coidef_maxheap( cntvect, 2.5d0 ) ) ! 2.5 MBytes is not enough for the Hessian
149 If ( coi_error .ne. 0 ) THEN
150 write(*,*)
151 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
152 write(*,*)
153 call flog( "Skipping Solve due to setup errors", 1 )
154 ENDIF
155
156 coi_error = coi_solve( cntvect )
157
158 If ( coi_error /= 0 ) then
159 call flog( "Errors encountered during solution", 1 )
160 elseif ( stacalls == 0 .or. solcalls == 0 ) then
161 call flog( "Status or Solution routine was not called", 1 )
162 elseif ( sstat /= 1 .or. mstat /= 2 ) then
163 call flog( "Solver and Model Status was not as expected (1,2)", 1 )
164 endif
165
166 write(*,*)
167 write(*,*) 'End of SQP example 3. Return code=',coi_error
168
169 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
170
171 call flog( "Successful Solve", 0 )
172
173End Program qp
174!
175! ============================================================================
176! Define information about the model:
177!
178
179!> Define information about the model
180!!
181!! @include{doc} readMatrix_params.dox
182Integer Function qp_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
183 colsta, rowno, value, nlflag, n, m, nz, &
184 usrmem )
185#ifdef dec_directives_win32
186!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: QP_ReadMatrix
187#endif
188 Use qp_data_sp
189 implicit none
190 integer, intent (in) :: n ! number of variables
191 integer, intent (in) :: m ! number of constraints
192 integer, intent (in) :: nz ! number of nonzeros
193 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
194 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
195 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
196 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
197 ! (not defined here)
198 integer, intent (out), dimension(m) :: type ! vector of equation types
199 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
200 ! (not defined here)
201 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
202 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
203 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
204 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
205 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
206 real*8 usrmem(*) ! optional user memory
207 integer :: i, j
208!
209! Information about Variables:
210! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
211! Default: the status information in Vsta is not used.
212!
213! Nondefault: Lower bound = 0 for all variables
214!
215 do i = 1, nn
216 lower(i) = 0.0d0
217 enddo
218!
219! Information about Constraints:
220! Default: Rhs = 0
221! Default: the status information in Esta and the function
222! value in FV are not used.
223! Default: Type: There is no default.
224! 0 = Equality,
225! 1 = Greater than or equal,
226! 2 = Less than or equal,
227! 3 = Non binding.
228!
229! Constraint 1 (Objective)
230! Rhs = 0.0 and type Non binding
231!
232 type(1) = 3
233!
234! Constraint 2 (Sum of all vars = 1)
235! Rhs = 1 and type Equality
236!
237 rhs(2) = 1.d0
238 type(2) = 0
239!
240! Information about the Jacobian. CONOPT expects a columnwise
241! representation in Rowno, Value, Nlflag and Colsta.
242!
243! Colsta = Start of column indices (No Defaults):
244! Rowno = Row indices
245! Value = Value of derivative (by default only linear
246! derivatives are used)
247! Nlflag = 0 for linear and 1 for nonlinear derivative
248! (not needed for completely linear models)
249!
250 j = 1 ! counter for current nonzero
251 do i = 1, nn
252 colsta(i) = j
253 rowno(j) = 1
254 nlflag(j) = 1
255 j = j + 1
256 rowno(j) = 2
257 value(j) = 1.d0
258 nlflag(j) = 0
259 j = j + 1
260 enddo
261 colsta(nn+1) = j
262
263 qp_readmatrix = 0 ! Return value means OK
264
265end Function qp_readmatrix
266!
267!==========================================================================
268! Compute nonlinear terms and non-constant Jacobian elements
269!
270
271!> Compute nonlinear terms and non-constant Jacobian elements
272!!
273!! @include{doc} fdeval_params.dox
274Integer Function qp_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
275 n, nz, thread, usrmem )
276#ifdef dec_directives_win32
277!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: QP_FDEval
278#endif
279 Use qp_data_sp
280 implicit none
281 integer, intent (in) :: n ! number of variables
282 integer, intent (in) :: rowno ! number of the row to be evaluated
283 integer, intent (in) :: nz ! number of nonzeros in this row
284 real*8, intent (in), dimension(n) :: x ! vector of current solution values
285 real*8, intent (in out) :: g ! constraint value
286 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
287 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
288 ! in this row. Ffor information only.
289 integer, intent (in) :: mode ! evaluation mode: 1 = function value
290 ! 2 = derivatives, 3 = both
291 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
292 ! as errcnt is incremented
293 integer, intent (in out) :: errcnt ! error counter to be incremented in case
294 ! of function evaluation errors.
295 integer, intent (in) :: thread
296 real*8 usrmem(*) ! optional user memory
297 Integer :: i,j, k
298 real*8 :: sum
299!
300! Row 1: The objective function is nonlinear
301!
302 if ( rowno .eq. 1 ) then
303!
304! Mode = 1 or 3. Function value: G = x * Q * x / 2
305!
306 if ( mode .eq. 1 .or. mode .eq. 3 ) then
307 sum = 0.d0
308 do k = 1, nq
309 i = qrow(k)
310 j = qcol(k)
311 if ( i == j ) then
312 sum = sum + (x(i)-target(i))*q(k)*(x(j)-target(j)) / 2.d0
313 else
314 sum = sum + (x(i)-target(i))*q(k)*(x(j)-target(j))
315 endif
316 enddo
317 g = sum
318 endif
319!
320! Mode = 2 or 3: Derivative values: Q*x
321!
322 if ( mode .eq. 2 .or. mode .eq. 3 ) then
323 do i = 1, nn
324 jac(i) = 0.0d0
325 enddo
326 do k = 1, nq
327 i = qrow(k)
328 j = qcol(k)
329 if ( i == j ) then
330 jac(i) = jac(i) + q(k) * (x(i)-target(i))
331 else
332 jac(i) = jac(i) + q(k) * (x(j)-target(j))
333 jac(j) = jac(j) + q(k) * (x(i)-target(i))
334 endif
335 enddo
336 endif
337!
338! Row = 2: The row is linear and will not be called.
339!
340 endif
341 qp_fdeval = 0
342
343end Function qp_fdeval
344
345
346!> Specify the structure of the Lagrangian of the Hessian
347!!
348!! @include{doc} 2DLagrStr_params.dox
349Integer Function qp_2dlagrstr( ROWNO, COLNO, NODRV, N, M, NHESS, USRMEM )
350#ifdef dec_directives_win32
351!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: QP_2DLagrStr
352#endif
353 Use qp_data_sp
354 implicit none
355 INTEGER n, m, nhess, nodrv
356 real*8 usrmem(*)
357 INTEGER rowno(nhess), colno(nhess)
358
359 Integer :: j
360
361 Do j = 1, nq
362 rowno(j) = qrow(j)
363 colno(j) = qcol(j)
364 Enddo
365 qp_2dlagrstr = 0
366
367End function qp_2dlagrstr
368
369
370!> Compute the Lagrangian of the Hessian
371!!
372!! @include{doc} 2DLagrVal_params.dox
373Integer Function qp_2dlagrval( X, U, ROWNO, COLNO, VALUE, NODRV, N, M, NHESS, USRMEM )
374#ifdef dec_directives_win32
375!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: QP_2DLagrVal
376#endif
377 Use qp_data_sp
378 implicit none
379 INTEGER n, m, nhess, nodrv
380 real*8 x(n), u(m), value(nhess), usrmem(*)
381 INTEGER rowno(nhess), colno(nhess)
382
383 Integer :: j
384
385 Do j = 1, nq
386 value(j) = q(j)*u(1)
387 Enddo
388 qp_2dlagrval = 0
389
390End function qp_2dlagrval
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_2dlagrstr(cntvect, coi_2dlagrstr)
define callback routine for providing the structure of the second derivatives of the Lagrangian.
Definition conopt.f90:1547
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_2dlagrval(cntvect, coi_2dlagrval)
define callback routine for computing the values of the second derivatives of the Lagrangian.
Definition conopt.f90:1573
integer(c_int) function coidef_license(cntvect, licint1, licint2, licint3, licstring)
define the License Information.
Definition conopt.f90:293
integer(c_int) function coidef_hessfac(cntvect, hessfac)
factor for Hessian density relative to Jacobian density HessFac.
Definition conopt.f90:858
integer(c_int) function coidef_maxheap(cntvect, maxheap)
define Limit on Heap Memory. ""
Definition conopt.f90:898
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_optdir(cntvect, optdir)
defines the Optimization Direction.
Definition conopt.f90:213
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 coidef_numhess(cntvect, numhess)
defines the Number of Hessian Nonzeros.
Definition conopt.f90:188
integer(c_int) function coidef_objcon(cntvect, objcon)
defines the Objective Constraint.
Definition conopt.f90:239
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
integer solcalls
Definition comdecl.f90:15
integer sstat
Definition comdecl.f90:18
integer stacalls
Definition comdecl.f90:14
subroutine flog(msg, code)
Definition comdecl.f90:62
integer mstat
Definition comdecl.f90:17
subroutine startup
Definition comdecl.f90:41
program qp
Main program. A simple setup and call of CONOPT for a QP model.
Definition qp1.f90:31
integer function qp_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
Definition qp1.f90:200
integer function qp_fdeval(x, g, jac, rowno, jcnm, mode, ignerr, errcnt, n, nz, thread, usrmem)
Compute nonlinear terms and non-constant Jacobian elements.
Definition qp1.f90:284
integer function qp_2dlagrval(x, u, rowno, colno, value, nodrv, n, m, nhess, usrmem)
Compute the Lagrangian of the Hessian.
Definition qp3.f90:298
integer function qp_2dlagrstr(rowno, colno, nodrv, n, m, nhess, usrmem)
Specify the structure of the Lagrangian of the Hessian.
Definition qp3.f90:278