-1

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 !

Nik
  • 1
  • 1
  • Yes thanks, B is also a vector, I corrected it – Nik Dec 11 '15 at 10:35
  • have a look to `constrOptim` – Colonel Beauvel Dec 11 '15 at 10:41
  • is `f` a linear function? ;-) – jogo Dec 11 '15 at 10:43
  • f is a quadratic function :) – Nik Dec 11 '15 at 10:46
  • So it is quadratic optimization with linear constraints. https://cran.r-project.org/web/views/Optimization.html – jogo Dec 11 '15 at 10:50
  • I already checked and i found ‘nloptr’ but it allows only the inequalty constraints, hin(x) >= 0 I don't know how to provide the constraint Ax < b – Nik Dec 11 '15 at 11:00
  • Only linear terms in x: Ax <= B <==> Ax - B <= 0 <==> (-Ax - B) >= 0 <==> B - Ax => 0. Hence, for hin: hin <- function(x) B - Ax ("pseudosyntax"). – dfrib Dec 11 '15 at 12:24
  • @Nik: If you are satisfied with the answer to your question, please mark it as answered. Otherwise, please give feedback or refine your question to see if someone else can answer it. – dfrib Dec 17 '15 at 03:09

1 Answers1

1

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 quadprogprogram:

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.

dfrib
  • 70,367
  • 12
  • 127
  • 192
  • I saw that function also but i didn't understand. Does it mean that I have to express my f(x) according to Dmat and dvec such that f(x) = t(dvec).Dmat.dvec ? – Nik Dec 11 '15 at 13:35
  • (I'll use x here as decision variable vector). You express your objective function combination of linear form and quadratic form (https://en.wikipedia.org/wiki/Quadratic_form), i.e., f(x) = dvec * x + x^T * Dmat * x / 2. I'll edit into the answer to clarify. – dfrib Dec 11 '15 at 13:47