Statistics - R Optimization With Equality and Inequality Constraints - Stack Overflow
Statistics - R Optimization With Equality and Inequality Constraints - Stack Overflow
Statistics - R Optimization With Equality and Inequality Constraints - Stack Overflow
I am trying to find the local minimum of a function, and the parameters have a fixed sum. For example,
Fx = 10 - 5x1 + 2x2 - x3
x1 + x2 + x3 = 15
(x1,x2,x3) >= 0
Where the sum of x1, x2, and x3 have a known value, and they are all greater than zero. In R, it would look something like this,
I also tried to use inequalities with constrOptim to force the sum to be fixed. I still think this may be a plausible work around, but I was unable to
make it work. This is a simplified example of the real problem, but any help would be very appreciated.
2 Answers
On this occasion optim will not work obviously because you have equality constraints.
constrOptim will not work either for the same reason (I tried converting the equality to two
inequalities i.e. greater and less than 15 but this didn't work with constrOptim ).
However, there is a package dedicated to this kind of problem and that is Rsolnp .
#specify the equality function. The number 15 (to which the function is equal)
#is specified as an additional argument
equal <- function(x) {
x[1] + x[2] + x[3]
}
Output:
> solnp(c(5,5,5),
+ opt_func,
+ eqfun=equal,
+ eqB=15,
+ LB=c(0,0,0),
+ UB=c(100,100,100))
https://stackoverflow.com/questions/30494083/r-optimization-with-equality-and-inequality-constraints 1/3
12/5/2017 statistics - R optimization with equality and inequality constraints - Stack Overflow
[1] 0
$values
[1] -10 -65 -65
$lagrange
[,1]
[1,] -5
$hessian
[,1] [,2] [,3]
[1,] 121313076 121313076 121313076
[2,] 121313076 121313076 121313076
[3,] 121313076 121313076 121313076
$ineqx0
NULL
$nfuneval
[1] 126
$outer.iter
[1] 2
$elapsed
Time difference of 0.1770101 secs
$vscale
[1] 6.5e+01 1.0e-08 1.0e+00 1.0e+00 1.0e+00
$pars
[1] 1.500000e+01 5.745236e-09 2.069192e-08
which means that the first parameter is 15 and the rest zero and zero. This is indeed the global
minimum in your function since the x2 is adding to the function and 5 * x1 has a much greater
(negative) influence than x3 on the outcome. The choice of 15, 0, 0 is the solution and the
global minimum to the function according to the constraints.
This is actually a linear programming problem, so a natural approach would be to use a linear
programming solver such as the lpSolve package. You need to provide an objective function
and a constraint matrix and the solver will do the rest:
library(lpSolve)
mod <- lp("min", c(-5, 2, -1), matrix(c(1, 1, 1), nrow=1), "=", 15)
Then you can access the optimal solution and the objective value (adding the constant term
10, which is not provided to the solver):
mod$solution
# [1] 15 0 0
mod$objval + 10
# [1] -65
A linear programming solver should be a good deal quicker than a general nonlinear
optimization solver and shouldn't have trouble returning the exact optimal solution (instead of a
nearby point that may be subject to rounding errors).
1 Nice one! When the OP says: "This is a simplified example of the real problem" it makes me think that the
actual problem might be nonlinear. So, just to be sure I suggested a nonlinear method (which works anyway
even if it is slower). Providing the gradient (simple for this case) makes it even faster if speed is an issue.
Anyway, I don't mean bad, it is really good that you added this answer, definitely helpful. – LyzandeR May
29 '15 at 8:54
https://stackoverflow.com/questions/30494083/r-optimization-with-equality-and-inequality-constraints 2/3
12/5/2017 statistics - R optimization with equality and inequality constraints - Stack Overflow
https://stackoverflow.com/questions/30494083/r-optimization-with-equality-and-inequality-constraints 3/3