# Try out NAG Library functions

Explore NAG maths and stats routines with interactive demos
Function ID
E04NQF
Name
nagf_opt_qpconvex2_sparse_solve
Description
Linear programming (LP) or convex quadratic programming (QP), sparse, active-set method, recommended
Keywords
service routine | LP, linear programming | QP, quadratic programming | active-set method | sparse convex QP prolem
This example minimizes the quadratic function $f\left(x\right)={c}^{\mathrm{T}}x+\frac{1}{2}{x}^{\mathrm{T}}Hx$, where
 $c=-200.0,-2000.0,-2000.0,-2000.0,-2000.0,400.0,400.0T$
 $H=2000000020000000220000022000000020000000220000022$
subject to the bounds
 $000≤x1≤0200000≤x2≤2500400≤x3≤0800100≤x4≤0700000≤x5≤1500000≤x6≤1500000≤x7≤1500$
and to the linear constraints
 $x1+x2+x3+x4+x5+x6+x7=20000.15x1+0.04x2+0.02x3+0.04x4+0.02x5+0.01x6+0.03x7≤600.03x1+0.05x2+0.08x3+0.02x4+0.06x5+0.01x6≤1000.02x1+0.04x2+0.01x3+0.02x4+0.02x5≤400.02x1+0.03x2+0.01x5≤301500≤0.70x1+0.75x2+0.80x3+0.75x4+0.80x5+0.97x6250≤0.02x1+0.06x2+0.08x3+0.12x4+0.02x5+0.01x6+0.97x7≤300$
!   E04NQF Example Program Text
!   Mark 26.1 Release. NAG Copyright 2016.

Module e04nqfe_mod

!     E04NQF Example Program Module:
!            Parameters and User-defined Routines

!     .. Use Statements ..
Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
Implicit None
!     .. Accessibility Statements ..
Private
Public                           :: qphx
!     .. Parameters ..
Integer, Parameter, Public       :: lencw = 600, leniw = 600,            &
lenrw = 600, nin = 5, nout = 6
Contains
Subroutine qphx(ncolh,x,hx,nstate,cuser,iuser,ruser)
!       Routine to compute H*x. (In this version of QPHX, the Hessian
!       matrix H is not referenced explicitly.)

!       .. Scalar Arguments ..
Integer, Intent (In)           :: ncolh, nstate
!       .. Array Arguments ..
Real (Kind=nag_wp), Intent (Out) :: hx(ncolh)
Real (Kind=nag_wp), Intent (Inout) :: ruser(*)
Real (Kind=nag_wp), Intent (In) :: x(ncolh)
Integer, Intent (Inout)        :: iuser(*)
Character (8), Intent (Inout)  :: cuser(*)
!       .. Executable Statements ..
hx(1) = 2.0E0_nag_wp*x(1)
hx(2) = 2.0E0_nag_wp*x(2)
hx(3) = 2.0E0_nag_wp*(x(3)+x(4))
hx(4) = hx(3)
hx(5) = 2.0E0_nag_wp*x(5)
hx(6) = 2.0E0_nag_wp*(x(6)+x(7))
hx(7) = hx(6)

Return

End Subroutine qphx
End Module e04nqfe_mod
Program e04nqfe

!     E04NQF Example Main Program

!     .. Use Statements ..
Use e04nqfe_mod, Only: lencw, leniw, lenrw, nin, nout, qphx
Use nag_library, Only: e04npf, e04nqf, e04ntf, nag_wp
!     .. Implicit None Statement ..
Implicit None
!     .. Local Scalars ..
Real (Kind=nag_wp)               :: obj, objadd, sinf
Integer                          :: i, icol, ifail, iobj, jcol, lenc, m, &
n, ncolh, ne, ninf, nname, ns
Logical                          :: verbose_output
Character (8)                    :: prob
Character (1)                    :: start
!     .. Local Arrays ..
Real (Kind=nag_wp), Allocatable  :: acol(:), bl(:), bu(:), c(:), pi(:),  &
rc(:), x(:)
Real (Kind=nag_wp)               :: ruser(1), rw(lenrw)
Integer, Allocatable             :: helast(:), hs(:), inda(:), loca(:)
Integer                          :: iuser(1), iw(leniw)
Character (8)                    :: cuser(1), cw(lencw)
Character (8), Allocatable       :: names(:)
!     .. Intrinsic Procedures ..
Intrinsic                        :: max
!     .. Executable Statements ..
Write (nout,*) 'E04NQF Example Program Results'

!     Skip heading in data file.

Read (nin,*) ne, iobj, ncolh, start, nname
Allocate (inda(ne),loca(n+1),helast(n+m),hs(n+m),acol(ne),bl(n+m),       &
bu(n+m),x(n+m),pi(m),rc(n+m),names(nname))

!     Read the matrix ACOL from data file. Set up LOCA.

jcol = 1
loca(jcol) = 1

Do i = 1, ne

!       Element ( INDA( I ), ICOL ) is stored in ACOL( I ).

If (icol<jcol) Then

!         Elements not ordered by increasing column index.

Write (nout,99998) 'Element in column', icol,                        &
' found after element in column', jcol, '. Problem', ' abandoned.'
Go To 100
Else If (icol==jcol+1) Then

!         Index in ACOL of the start of the ICOL-th column equals I.

loca(icol) = i
jcol = icol
Else If (icol>jcol+1) Then

!         Index in ACOL of the start of the ICOL-th column equals I,
!         but columns JCOL+1,JCOL+2,...,ICOL-1 are empty. Set the
!         corresponding elements of LOCA to I.

loca((jcol+1):icol) = i
jcol = icol
End If

End Do

loca(n+1) = ne + 1

!     Columns N,N-1,...,ICOL+1 are empty. Set the corresponding
!     elements of LOCA accordingly.

Do i = n, icol + 1, -1
loca(i) = loca(i+1)
End Do

If (start=='C') Then
Else If (start=='W') Then
End If

Write (nout,99999) n, m

!     Call E04NPF to initialize E04NQF.

ifail = 0
Call e04npf(cw,lencw,iw,leniw,rw,lenrw,ifail)

!     Set this to .True. to cause e04nqf to produce intermediate
!     progress output
verbose_output = .False.

If (verbose_output) Then
!       By default e04nqf does not print monitoring
!       information. Set the print file unit or the summary
!       file unit to get information.
ifail = 0
Call e04ntf('Print file',nout,cw,iw,rw,ifail)
End If

!     We have no explicit objective vector so set LENC = 0; the
!     objective vector is stored in row IOBJ of ACOL.

lenc = 0
Allocate (c(max(1,lenc)))

prob = ' '

!     Do not allow any elastic variables (i.e. they cannot be
!     infeasible). If we'd set optional argument "Elastic mode" to 0,
!     we wouldn't need to set the individual elements of array HELAST.

helast(1:(n+m)) = 0

!     Solve the QP problem.

ifail = 0
inda,loca,bl,bu,c,names,helast,hs,x,pi,rc,ns,ninf,sinf,obj,cw,lencw,   &
iw,leniw,rw,lenrw,cuser,iuser,ruser,ifail)

Write (nout,*)
Write (nout,99997) obj
Write (nout,99996) x(1:n)

100   Continue

99999 Format (1X,/,1X,'QP problem contains ',I3,' variables and ',I3,          &
' linear constraints')
99998 Format (1X,A,I5,A,I5,A,A)
99997 Format (1X,'Final objective value = ',1P,E11.3)
99996 Format (1X,'Optimal X = ',7F9.2)

End Program e04nqfe

The NAG Library
The world’s largest collection of robust, documented, tested and maintained numerical algorithms.