【问题标题】:R Ipsolve how to see all solutionsR Ipsolve 如何查看所有解决方案
【发布时间】:2014-05-24 12:27:46
【问题描述】:

我正在查看8 queens puzzle。我使用了下面的 R 代码,它直接来自 R lpsolve 文档。参数 num.bin.solution 设置为 3。在 R 文档中,它说 num.bin.solns 代表返回的解决方案数量的数字指标。在那种情况下,我怎样才能看到 3 种可能的解决方案?我使用了命令 chessing$solution,但输出并不容易理解。还有没有办法返回所有可能的解决方案?

chess.obj <- rep (1, 64)
q8 <- make.q8 ()
chess.dir <- rep (c("=", "<"), c(16, 26))#first 16 cosntraints are for row and columns, remaining constraints are for diagonals
chess.rhs <- rep (1, 42)
chessing=lp ('max', chess.obj, , chess.dir, chess.rhs, dense.const = q8,
    all.bin=TRUE, num.bin.solns=3)
chessing$solution

更新:我的主要问题得到了解答。但仍然想知道是否有任何有效的方法来获得所有可能的解决方案。

【问题讨论】:

    标签: r mathematical-optimization lpsolve


    【解决方案1】:

    解决方案编码为chessing$solution。每块 64 个整数值是一个最优解,最后一个值 (-1) 应该被忽略。您可以通过以下方式提取您的解决方案:

    res <- split(chessing$solution[1:(3*64)], rep(1:3, each=64))
    res
    # $`1`
    #  [1] 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1
    # [52] 0 0 0 0 0 0 0 0 0 0 1 0 0
    # 
    # $`2`
    #  [1] 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
    # [52] 0 0 0 1 0 0 0 0 0 1 0 0 0
    # 
    # $`3`
    #  [1] 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0
    # [52] 0 0 0 0 0 0 0 0 0 1 0 0 0
    

    您现在可以使用res[[1]]res[[2]]res[[3]] 访问各个解决方案。

    【讨论】:

    • 是否有可能得到所有可能的解决方案。如果是 64 方格的棋盘,则有 92 种可能的解法。如果我们不知道有多少解决方案可用,我们如何告诉 lpsolve 返回所有可能的解决方案?
    • @user2543622 我通过了num.bin.solns=1000,它返回说它找到了 92 个解决方案。因此,您似乎只需将 num.bin.solns 设置为一个较大的值即可。
    • 我运行了 num.bin.solns=1000,但没有收到任何消息 :( 你从哪里得到“找到 92 个解决方案”的消息?
    猜你喜欢
    • 1970-01-01
    • 2017-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-19
    • 1970-01-01
    • 2021-04-06
    • 1970-01-01
    相关资源
    最近更新 更多