quadprog.RdSolves quadratic programming problems with linear and box constraints.
quadprog(C, d, A = NULL, b = NULL,
Aeq = NULL, beq = NULL, lb = NULL, ub = NULL)symmetric matrix, representing the quadratic term.
vector, representing the linear term.
matrix, represents the linear constraint coefficients.
vector, constant vector in the constraints.
matrix, linear equality constraint coefficients.
vector, constant equality constraint vector.
elementwise lower bounds.
elementwise upper bounds.
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.
Returns a list with components
minimum solution, subject to all bounds and constraints.
value of the target expression at the arg minimum.
exit flag.
Nocedal, J., and St. J. Wright (2006). Numerical Optimization. Second Edition, Springer Series in Operations Research, New York.
This function is wrapping the active set quadratic solver in the
quadprog package: quadprog::solve.QP, combined with
a more MATLAB-like API interface.
## 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
#>
# $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
#>
# $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
#>
# $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
#>
# $xmin
# [1] 0.6666667 1.3333333
# $fval
# [1] -8.222222
# $eflag
# [1] 1