0

我正在尝试使用 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 上从存储库(版本 2.14)运行 R 并通过install.packages("...").

谢谢!

4

1 回答 1

-1

好的,解决了。问题是我没有添加byrows = TRUE到矩阵定义中。另外我变成ncol = 9nrow = .... 显然该matrix()功能没有按我的预期工作。

于 2012-01-05T20:54:40.503 回答