Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong answer for a simple binary LP #7

Open
VincentTam opened this issue Feb 4, 2021 · 1 comment
Open

Wrong answer for a simple binary LP #7

VincentTam opened this issue Feb 4, 2021 · 1 comment

Comments

@VincentTam
Copy link

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

@travis-leith
Copy link

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

gaborcsardi added a commit that referenced this issue Sep 27, 2022
Following lpSolveAPI.

Original error:
```
> ### Name: lp
> ### Title: Linear and Integer Programming
> ### Aliases: lp
> ### Keywords: optimize
>
> ### ** Examples
>
> #
> # Set up problem: maximize
> #   x1 + 9 x2 +   x3 subject to
> #   x1 + 2 x2 + 3 x3  <= 9
> # 3 x1 + 2 x2 + 2 x3 <= 15
> #
> f.obj <- c(1, 9, 1)
> f.con <- matrix (c(1, 2, 3, 3, 2, 2), nrow=2, byrow=TRUE)
> f.dir <- c("<=", "<=")
> f.rhs <- c(9, 15)
> #
> # Now run.
> #
> lp ("max", f.obj, f.con, f.dir, f.rhs)
lp_presolve.c:193:34: runtime error: applying non-zero offset 16 to null pointer
    #0 0x7f9ffc9213db in presolve_rebuildUndo /data/gannet/ripley/R/packages/tests-clang-SAN/lpSolve/src/lp_presolve.c:193:34
    #1 0x7f9ffc988821 in postsolve /data/gannet/ripley/R/packages/tests-clang-SAN/lpSolve/src/lp_presolve.c:5620:5
    #2 0x7f9ffc9fcbff in spx_solve /data/gannet/ripley/R/packages/tests-clang-SAN/lpSolve/src/lp_simplex.c:2062:9
    #3 0x7f9ffc9fe260 in lin_solve /data/gannet/ripley/R/packages/tests-clang-SAN/lpSolve/src/lp_simplex.c:2154:12
    #4 0x7f9ffca13511 in lpslink /data/gannet/ripley/R/packages/tests-clang-SAN/lpSolve/src/lpslink56.c:377:25
    #5 0x74ef88 in do_dotCode /data/gannet/ripley/R/svn/R-devel/src/main/dotcode.c
    #6 0x83f0a5 in bcEval /data/gannet/ripley/R/svn/R-devel/src/main/eval.c:7126:14
    #7 0x829ade in Rf_eval /data/gannet/ripley/R/svn/R-devel/src/main/eval.c:748:8
    #8 0x891fd3 in R_execClosure /data/gannet/ripley/R/svn/R-devel/src/main/eval.c
    #9 0x88dc7f in Rf_applyClosure /data/gannet/ripley/R/svn/R-devel/src/main/eval.c:1844:16
    #10 0x82a518 in Rf_eval /data/gannet/ripley/R/svn/R-devel/src/main/eval.c:871:12
    #11 0x95ade6 in Rf_ReplIteration /data/gannet/ripley/R/svn/R-devel/src/main/main.c:264:2
    #12 0x95e340 in R_ReplConsole /data/gannet/ripley/R/svn/R-devel/src/main/main.c:316:11
    #13 0x95e149 in run_Rmainloop /data/gannet/ripley/R/svn/R-devel/src/main/main.c:1194:5
    #14 0x95e482 in Rf_mainloop /data/gannet/ripley/R/svn/R-devel/src/main/main.c:1201:5
    #15 0x4f30ba in main /data/gannet/ripley/R/svn/R-devel/src/main/Rmain.c:29:5
    #16 0x7fa00c1cab74 in __libc_start_main (/lib64/libc.so.6+0x27b74) (BuildId: 08df60634339b221bb854d4e10b7278cafde70c4)
    #17 0x43231d in _start (/data/gannet/ripley/R/R-clang-SAN/bin/exec/R+0x43231d)

SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior lp_presolve.c:193:34 in
Success: the objective function is 40.5
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants