R lpsolve binary 找到所有可能的解决方案 [英] R lpsolve binary find all possible solutions

查看:31
本文介绍了R lpsolve binary 找到所有可能的解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个线性规划问题.所有变量都是二进制的,我想得到所有可能的解决方案.我知道我可以设置参数 num.bin.solns 来提供多种解决方案.但是有什么简单的方法可以要求所有可能的解决方案吗?

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?

例如在下面的例子中,我知道答案的最大数量是 6.但是如果我不知道最大可能的解决方案,那么我如何设置 num.bin.solns 参数,以便它返回所有可能的解决方案?

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)

推荐答案

这是一种方法.lpSove 实际上为您提供了它找到的 num.bin.solns,您可以使用 $ 符号访问它.

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.

您可以最初将 num.bin.solns 设置为某个较大的数字(例如 1000).然后访问 mylp$num.bin.solns 以获取确切值.

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

这篇关于R lpsolve binary 找到所有可能的解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆