加权顶点覆盖(作为线性规划)在R与ROI包



我正试图用R来解决一个加权顶点覆盖问题的实例,我似乎不能得到它的权利。我正在使用ROI包(也可以使用linprog)。

实例如下:

Edges:
A-B,    A-C,    A-G,
B-C,    B-D,    B-E,    B-G,
C-E,    C-F,
D-F,
E-G,
F-H,    F-I,
G-H
Weights:
A - 10,
B - 7,
C - 4,
D - 7,
E - 12,
F - 25,
G - 27,
H - 3,
I - 9
我的代码是:
    #                                    a  b  c  d  e  f  g  h  i
    constraints <- L_constraint(matrix(c(1, 1, 0, 0, 0, 0, 0, 0, 0, # a b
                                         1, 0, 1, 0, 0, 0, 0, 0, 0, # a c
                                         1, 0, 0, 0, 0, 0, 1, 0, 0, # a g
                                         0, 1, 1, 0, 0, 0, 0, 0, 0, # b c
                                         0, 1, 0, 1, 0, 0, 0, 0, 0, # b d
                                         0, 1, 0, 0, 1, 0, 0, 0, 0, # b e
                                         0, 1, 0, 0, 0, 0, 1, 0, 0, # b g
                                         0, 0, 1, 0, 1, 0, 0, 0, 0, # c e
                                         0, 0, 1, 0, 0, 1, 0, 0, 0, # c f
                                         0, 0, 0, 1, 0, 1, 0, 0, 0, # d f
                                         0, 0, 0, 0, 1, 0, 1, 0, 0, # e g
                                         0, 0, 0, 0, 0, 1, 0, 1, 0, # f h
                                         0, 0, 0, 0, 0, 1, 0, 0, 1, # f i
                                         0, 0, 0, 0, 0, 0, 1, 1, 0, # g h
                                         # end of u + v >= 1
                                         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, 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,
                                         0, 0, 0, 0, 0, 0, 0, 1, 0,
                                         0, 0, 0, 0, 0, 0, 0, 0, 1,
                                         # end of u >= 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, 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,
                                         0, 0, 0, 0, 0, 0, 0, 1, 0,
                                         0, 0, 0, 0, 0, 0, 0, 0, 1),
                                         # end of u <= 1
                                       ncol = 9), # matrix
                                dir = c(rep(">=", 14+9), rep("<=", 9)),
                                rhs = c(rep(1, 14), rep(0, 9), rep(1, 9))) # L_constraint
    objective <- L_objective(c(10, 7, 4, 7, 12, 25, 27, 3, 9))
    problem <- OP(objective, constraints, rep("C", 9),
                  maximum = FALSE)
    solution <- ROI_solve(problem, solver = "glpk")

结果是No solution found.我不知道我做错了什么,但它也可能是显而易见的。我无法理解它——解决方案应该总是存在的,即使它占用了所有的顶点(即所有变量都>= 0.5)。

如果这很重要,我在Arch Linux上运行R从存储库(版本。2.14),并通过install.packages("...")安装软件包。

谢谢!

好了,解决了。问题是我没有将byrows = TRUE添加到矩阵定义中。另外我把ncol = 9改成了nrow = ...。显然,matrix()函数没有像我预期的那样工作。

相关内容

  • 没有找到相关文章

最新更新