Search code examples
rlpsolveinequality

How to implement != (not equal) in lpSolve r


Since lpSolve does not allow to use != for the constraint directions, what is an alternative way to get the same result? I would like to maximize x1 + x2 with constraints: x1 <= 5 and x2 != 5 and keep using lpSolve R package.

I've tried using a combination of > < in order to replicate the same behaviour of !=, however I do not obtain the result I expected.

f.obj<-c(1,1)
f.con<-matrix(c(1,0,0,1),nrow=2,ncol=2,byrow=TRUE)
f.dir<-c("<=","!=")
f.rhs<-c(5,5)
lp("max",f.obj,f.con,f.dir,f.rhs)$solution

Since lpSolve does not support !=, I get the error message:

Error in lp("max",f.obj,f.con,f.dir,f.rhs): Unknown constraint direction found

EDIT

I would like to maximize x1 + x2 with constraints: x1 <= 5 and x2 < 10 and x2 != 9. So the solution would be 5 and 8.


Solution

  • You can't do that, even in theory, since the resulting constraint set is not closed. It is like trying to minimize x^2 over the set x > 0. For any proposed solution x0 in that set the solution x0/2 is better so there is no optimum.

    I would just use x <= 5 as your constraint and if the constraint is not active (i.e. it turns out that x < 5) then you have found the solution; otherwise, there is no solution. If there is no solution you can try x <= 5 - eps for an arbitrarily chosen eps.

    ADDED:

    If what you intended was that the variables x1 and x2 are integer then

    x < 10 and x != 9
    

    is equivalent to

    x <= 8
    

    Note that lp has the all.int argument which defaults to FALSE.

    ADDED 2:

    If you just want to find multiple feasible solutions then if opt is the value of the objective from the first solution rerun adding the constraint (assuming a maximization problem):

    objective <= opt - eps
    

    where eps is an arbitrary small constant.

    Also note that if the vectors x1 and x2 are two optimal solutions to an LP then since the constraint set is necessarily convex any convex combination of those solutions is also feasible and because the objective is linear all of those convex combinations must also be optimal so if there is more than one optimum then there are an infinite number of such optimal solutions so you can't simply enumerate them.

    ADDED 3.

    The feasible set of a linear program form a simplex (i.e. a polytope) and at least one vertex must be at the optimal value if such optimal value exists. If there are more than one vertex with the same optimal value then the points on the line connecting them are all optimal values as well. Although there are an infinite number of optimal values in that case there are only a finite number of vertices so you could enumerate them using the vertexenum package. Then evaluate the objective at each one. If there is one vertex whose objective value is greater than all others then that is the optimum. If there are multiple then we know that those plus all convex combinations of those are optimal. This might work if your problem is not too large.