lpSolve icon indicating copy to clipboard operation
lpSolve copied to clipboard

Wrong answer for a simple binary LP

Open VincentTam opened this issue 4 years ago • 1 comments

Originally from user882349's question on Math.SE. I copied the content here hoping that this bug can be fixed.

Sample use case

Problem

Consider the binary LP.

max A + B + C + D
s.t. A + 2B + 3C + 4D = 10
where A, B, C, D ∈ {0,1}

Steps

Run the following code on an online R runtime: https://rdrr.io/cran/lpSolve/

library(lpSolve)
f.obj <- c(1,1,1,1)
f.con <- c(1,2,3,4)
f.dir <- c("=")
f.rhs <- c(10)
lp("max", f.obj, f.con, f.dir, f.rhs, binary.vec = 1:4, all.bin=TRUE)$solution

Expected result

A = B = C = D = 1

Computed result

The package outputs [1] 0 0 1 0, meaning A = B = D = 0, C = 1.

Screenshot

image

VincentTam avatar Feb 04 '21 09:02 VincentTam

Looks like you have to specify const.mat as a matrix.

library(lpSolve)
f.obj <- c(1,1,1,1)
f.con <- c(1,2,3,4)
f.dir <- c("=")
f.rhs <- c(10)


opt = lp(
  direction = "max",
  objective.in = f.obj,
  const.mat = f.con |> matrix(nrow = 1),
  const.dir = f.dir,
  const.rhs = f.rhs,
  all.bin=TRUE
)

opt$solution

travis-leith avatar Aug 16 '21 21:08 travis-leith