quadprog: Quadratic Programming

View source: R/quadprog.R

quadprogR Documentation

Quadratic Programming

Description

Solves quadratic programming problems with linear and box constraints.

Usage

quadprog(C, d, A = NULL, b = NULL,
         Aeq = NULL, beq = NULL, lb = NULL, ub = NULL) 

Arguments

C

symmetric matrix, representing the quadratic term.

d

vector, representing the linear term.

A

matrix, represents the linear constraint coefficients.

b

vector, constant vector in the constraints.

Aeq

matrix, linear equality constraint coefficients.

beq

vector, constant equality constraint vector.

lb

elementwise lower bounds.

ub

elementwise upper bounds.

Details

Finds a minimum for the quadratic programming problem specified as:

min 1/2 x'Cx + d'x

such that the following constraints are satisfied:

A x <= b

Aeq x = beq

lb <= x <= ub

The matrix should be symmetric and positive definite, in which case the solution is unique, indicated when the exit flag is 1.

For more information, see ?solve.QP.

Value

Returns a list with components

xmin

minimum solution, subject to all bounds and constraints.

fval

value of the target expression at the arg minimum.

eflag

exit flag.

Note

This function is wrapping the active set quadratic solver in the quadprog package: quadprog::solve.QP, combined with a more MATLAB-like API interface.

References

Nocedal, J., and St. J. Wright (2006). Numerical Optimization. Second Edition, Springer Series in Operations Research, New York.

See Also

lsqlincon, quadprog::solve.QP

Examples

## Example in ?solve.QP
# Assume we want to minimize: 1/2 x^T x - (0 5 0) %*% x
# under the constraints:      A x <= b
# with b = (8,-2, 0)
# and      ( 4  3  0) 
#      A = (-2 -1  0)
#          ( 0  2,-1)
# and possibly equality constraint  3x1 + 2x2 + x3 = 1
# or upper bound c(1.5, 1.5, 1.5).

C <- diag(1, 3); d <- -c(0, 5, 0)
A <- matrix(c(4,3,0, -2,-1,0, 0,2,-1), 3, 3, byrow=TRUE)
b <- c(8, -2, 0)

quadprog(C, d, A, b)
# $xmin
# [1] 0.4761905 1.0476190 2.0952381
# $fval
# [1] -2.380952
# $eflag
# [1] 1

Aeq <- c(3, 2, 1);  beq <- 1
quadprog(C, d, A, b, Aeq, beq)
# $xmin
# [1]  1.4 -0.8 -1.6
# $fval
# [1] 6.58
# $eflag
# [1] 1

quadprog(C, d, A, b, lb = 0, ub = 1.5)
# $xmin
# [1] 0.625 0.750 1.500
# $fval
# [1] -2.148438
# $eflag
# [1] 1

## Example help(quadprog)
C <- matrix(c(1, -1, -1, 2), 2, 2)
d <- c(-2, -6)
A <- matrix(c(1,1, -1,2, 2,1), 3, 2, byrow=TRUE)
b <- c(2, 2, 3)
lb <- c(0, 0)

quadprog(C, d, A, b, lb=lb)
# $xmin
# [1] 0.6666667 1.3333333
# $fval
# [1] -8.222222
# $eflag
# [1] 1

pracma documentation built on March 19, 2024, 3:05 a.m.