roptimizationlinear-programminglpsolve

R: How to solve the following linear programming problem


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?


Solution

  • 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"