I would like to further constrain the system below with the following additional constraint, which makes use of the absolute value operator:
abs(x1)+abs(x2)+abs(x3) <= 10
Is there a feasible way to implement these additional absolute value constraints in R?
System of equations:
maximize: x1 + 9x2 + x3;
subject to:
x1 + 2x2 + 3x3 <= 9
3x1 + 2x2 + 2x3 <= 15
R Code:
require(lpSolve)
# objective function, constants, constraints
obj = c(1,9,1)
con = matrix(c(1,2,3,3,2,2), nrow=2, byrow=TRUE)
rel = c("<=", "<=")
rhs = c(9,15)
Solution:
my.lp = lp("max", obj, con, rel, rhs)
my.lp$objval
my.lp$solution
Obviously this is a simple example to illustrate the problem I pulled after searching online. It seems there is an approach in lp_solve
itself, as evidenced here in the lp_solve
online help guide. However, I would prefer to keep the problem framed in R if possible.
To model |x|
in an LP, you typically create two new variables, x^-
and x^+
. Constrain them both to be nonnegative:
x^-, x^+ >= 0
Then each time you have x
in your model, replace it with x^+ - x^-
, and each time you have |x|
, replace it with x^+ + x^-
. Note that this only works if you're using a simplex-based LP solver rather than an interior-point method. lp_solve uses simplex.
The reason why this works is: Suppose x = 10
in the optimal solution. Then the solver would set x^+ = 10
and x^- = 0
to obtain
x = x^+ - x^- = 10
|x| = x^+ + x^- = 10.
And if x = -10
in the optimal solution, then x^+ = 0
, x^- = 10
, and
x = x^+ - x^- = -10
|x| = x^+ + x^- = 10.
(The solver won't choose, say, x^+ = 50
and x^- = 40
to get x = 10
because the simplex method always chooses extreme-point solutions.)
If you do this trick separately with each of the three abs
's in your model, it should work.