NIFAKG98
NIFAKG98

Reputation: 23

Linear Programming in R using lpSolve dismisses constrains

I am currently working on a paper that involves solving a problem similar to this one.

Given is a set of 100 numbers and I want to check if you can reach a certain value with the given numbers by multiplying them with 0 or 1.

See the following simplified problem

A + 2B + 3C + 4D = 10, with A, B, C, D from {0,1}.

I tried to solve this problem with R by using the package lpSolve and solving binary LP. I chose the objective function as constant, because, if everything is calculated correctly, the constraints still must be met.

In the following my code:

> 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)

As result I would now expect A=B=C=D=1. However, I get in R as solution A=1, B=1, C=0, D=1.

Can anyone understand why this is so?

Maybe you guys know a better algorithm to solve my problem. I am thankful to anybody who can help me

Thank you very much!

Upvotes: 2

Views: 712

Answers (1)

Erwin Kalvelagen
Erwin Kalvelagen

Reputation: 16797

Try to transpose the constraint vector:

> library(lpSolve)
> f.obj <- c(1,1,1,1)
> f.con <- t(c(1,2,3,4))
> f.dir <- c("=")
> f.rhs <- c(10)
> result <- lp("max", f.obj, f.con, f.dir, f.rhs, binary.vec = 1:4, all.bin=TRUE)
> result
Success: the objective function is 4 
> result$solution
[1] 1 1 1 1

Upvotes: 3

Related Questions