- [R lpsolve二进制找出所有可能的解决方案 [英] R lpsolve binary find all possible solutions

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

问题描述

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

例如下面的情况下,我知道答案的最大数量为6。但是,如果我不知道最大可能的解决方案,然后我怎么可以设置num.bin.solns等参数,它会返回所有可能的解决方案?

 库(lpSolve)
A =矩阵(C(1,1,1,1),nrow = 1,byrow = TRUE)
B =(2)
招牌='=='
C_ =代表(0,4)
RES = lpSolve :: LP(最大,C_,A,体征,B,all.bin = TRUE,num.bin.solns = 6)


解决方案

下面是做到这一点的方法之一。 lpSove 其实给你的 num.bin.solns 它找到,并且可以访问使用$符号。

您可以在最初设定的num.bin.solns出现一些较大的数字(比如1000)。然后访问 mylp $ num.bin.solns ,以得到确切的价值。

  mylp<  -  LP(最大,C_,A,体征,B,all.bin = TRUE,num.bin.solns = 1000)
数numCols< - 4
numsols< - mylp $ num.bin.solns解决方案< - 矩阵(头(mylp

解决方案

,数numCols * numsols),nrow = numsols,byrow = TRUE)> numsols
[1] 6

你可以打印出个性化解决方案:

 >解决方案
     [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 1 0 0
[6,] 0 1 1 0

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)

解决方案

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

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

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