1

I'm interested in solving the following linear programming problem.

In this toy example, the second constraint tells me that x1 <= -1, that is, x1 must be negative, so the minimum value of x1 should be negative. Using lpSolveAPI, I coded up this toy example.

library(lpSolveAPI)
my.lp <- make.lp(nrow = 2, ncol = 2)
set.column(my.lp, 1, c(1, 2))
set.column(my.lp, 2, c(3, 0))
set.objfn(my.lp, c(1, 0))
set.constr.type(my.lp, rep("<=", 2))
set.rhs(my.lp, c(-4, -2))
set.bounds(my.lp, lower = c(-Inf, -Inf), upper = c(Inf, Inf))
> my.lp
Model name: 
            C1    C2        
Minimize     1     0        
R1           1     3  <=  -4
R2           2     0  <=  -2
Kind       Std   Std        
Type      Real  Real        
Upper      Inf   Inf        
Lower     -Inf  -Inf 

However, solving this linear programming problem in R gives me

> solve(my.lp)
[1] 3
> get.variables(my.lp)
[1]   3.694738e-57 -2.681562e+154
> get.objective(my.lp)
[1] 1e+30

get.objective(my.lp) returns a value of 1e+30 for x1, which clearly does not satisfy the second constraint. I specifically used set.bounds so that x1, x2 can take any value on the real line, but I still did not get a negative number. Where did things go wrong?

Adrian
  • 9,229
  • 24
  • 74
  • 132
  • 1
    The problem you describe is unconstrained. `x1` could be infinitely negative. So you are probably seeing junk results from the solver's effort. I'm not sure how you check the solver status in `r`, but that should provide some info. – AirSquid Jan 29 '21 at 21:02
  • I see. Would it be possible to have a solver return `-Inf` in this case instead? I'm open to using a different package/function other than `lpSolveAPI`. Thanks. – Adrian Jan 29 '21 at 21:06
  • I don't know of any solvers that can intuit a good response to an unconstrained problem, but perhaps somebody has a comment on that. Depending on the solver and the interface, you can get a handful of messages back on the status of the solve effort. – AirSquid Jan 29 '21 at 21:11
  • The solver stops before actually taking the step. You may get better results by replacing -Inf and Inf bounds by a large number like 999999. Or just look at the status code. 3 means: "the model is unbounded" (see the documentation) – Erwin Kalvelagen Jan 29 '21 at 21:33
  • The 3 output from `solve` tells you it is unbounded. See `?solve.lpExtPrtr` for the codes. – G. Grothendieck Jan 29 '21 at 22:01

1 Answers1

1
library(CVXR)

x1 <- Variable(1)
x2 <- Variable(1)

# Problem definition
objective <- Minimize(x1)
constraints <- list(x1 + 3*x2 <= -4, 2*x1 + 0*x2  <= -2)
prob <- Problem(objective, constraints)

# Problem solution
sol <- solve(prob)

sol$value
# [1] -Inf

sol$status
# [1] "unbounded"
Jakub.Novotny
  • 2,912
  • 2
  • 6
  • 21