Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
543 views
in Technique[技术] by (71.8m points)

optimization - R lpsolve binary find all possible solutions

I have a linear programming problem. All variables are binary and I want to get all possible solutions.I know that I can set parameter num.bin.solns to provide multiple solutions. But is there any easy way to ask for all possible solutions?

For example in below case I know that the maximum number of answers is 6. But if I don't know the maximum possible solutions then how can I set the num.bin.solns parameter such that it would return all possible solutions?

library("lpSolve")
A=matrix (c(1,1,1,1), nrow=1, byrow=TRUE)
b=(2)
signs='=='
c_=rep(0,4)
res = lpSolve::lp('max', c_, A, signs, b,  all.bin = TRUE, num.bin.solns=6)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's one way to do it. lpSove actually gives you the num.bin.solns that it finds, and you can access that using the $ notation.

You can initially set the num.bin.solns to be some large number (say 1000). And then access the mylp$num.bin.solns to get the exact value.

mylp <- lp('max', c_, A, signs, b,  all.bin = TRUE, num.bin.solns=1000)
numcols <- 4
numsols <- mylp$num.bin.solns

solutions <- matrix(head(mylp$solution, numcols*numsols), nrow=numsols, byrow=TRUE)

> numsols
[1] 6

And you can print out the individual solutions:

> solutions
     [,1] [,2] [,3] [,4]
[1,]    0    0    1    1
[2,]    0    1    0    1
[3,]    1    0    0    1
[4,]    1    0    1    0
[5,]    1    1    0    0
[6,]    0    1    1    0

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...