I am looking for a R solver to minimize a function f(x) under the constraint Ax <= B and x >= 0
where A is a matrix, and x and B are vectors.
Do you know one please ?
Thank you !
I am looking for a R solver to minimize a function f(x) under the constraint Ax <= B and x >= 0
where A is a matrix, and x and B are vectors.
Do you know one please ?
Thank you !
Since your program is on of quadratic objective function but linear constraints, it's convex. Hence, you shouldn't need to resort to advanced non-linear libraries.
Have a look at the quadprog package. From its documentation, the first example should work as a good template for your
## First example from documentation of `quadprog`:
##
## Assume we want to minimize: -(0 5 0) %*% b + 1/2 b^T b
## under the constraints: A^T b >= b0
## with b0 = (-8,2,0)^T
## and
##
##
## we can use solve.QP as follows:
##
Dmat <- matrix(0,3,3)
diag(Dmat) <- 1
dvec <- c(0,5,0)
Amat <- matrix(c(-4,-3,0,2,1,0,0,-2,1),3,3)
bvec <- c(-8,2,0)
solve.QP(Dmat,dvec,Amat,bvec=bvec)
In this case, the comments describe a program where the b
vector contains the decision variables. The constraints are on the form Ax >= b
(A^Tb >= b0), whereas you have constraints of the form Ax <= b
; the latter is easily transformed into the former with E = -A and f = -b, yielding Ex >= f
. Note that in this example the coefficient matrix Dmat
for the quadratic term is the unit matrix, whereas in your case, you'll have to set Dmat
(and dvec
) according to your objective function.
To clarify (even further), from the documentation for the quadprog
program:
This routine implements the dual method of Goldfarb and Idnani (1982, 1983) for solving quadratic programming problems of the form min(−dT b + 1/2bT Db) with the constraints AT b >= b0.
Hence, express your objective function (here using x instead of b for decision variable vector)
f(x) = -d^T x + 1/2 x^T D x
where, d: coefficient *vector* for linear part of obj. function
D: coefficient *matrix* for quadratic
x: decision variable *vector*
and where ^T denotes the vector transpose.
In example above, d and D are denoted dvec and Dmat, respectively.