如何规划露台灯最有效的路线

时间:2017-08-17 02:29:35

标签: r algorithm igraph

我试图把一些庭院灯串起来。基于another question我问过,我意识到我需要一个算法来解决Route Inspection Problem以找出灯应该采用的最有效路线,以便用灯光覆盖最小的重复边缘。经过一番搜索,我意识到这可能是我最好的选择:Solving Chinese Postman algorithm with eulerization

但是,我在创建图表时遇到了问题。

以下是它需要的样子:

enter image description here

  • 粉色圆圈代表结构中我可以悬挂灯光的地方
  • "开始"是唯一可用的电源插座
  • 黄色圆点表示灯光应覆盖的所有位置

以下是我的图表在引用此帖子后的样子:Visualizing distance between nodes according to weights - with R

enter image description here

正如您所看到的,所有节点都在正确的位置,但边缘连接在它们不应该连接的位置。这是我的代码:

library(igraph)
gg<-graph.ring(20)
ll=matrix(
  c( 0,0,    75.25,0,    150.5,0,    225.8125,0,    302.8125,0, 
     0,-87,                                          302.8125,-87,
     0,-173.8125,                                    302.8125,-173.8125,
     0,-260.9375,                                    302.8125,-260.9375,
     16,-384.3125,                                   302.8125,-384.3125,
     16,-435.9575,                                   302.8125,-435.9375,
     16,-525.1875, 75.25,-525.1875, 150.5,-525.1875, 225.8125,-525.1875, 302.8175,-525.1875),
  ncol=2,byrow=TRUE)
plot(gg,layout=ll)

我认为这与graph.ring的性质有关,但我无法找到另一种定义图表的方法。边缘&#39;长度没有错误。

1 个答案:

答案 0 :(得分:2)

我认为您可以使用graph_from_edgelist来精确指定要连接的节点。指定哪个节点以哪种顺序连接就足够了。不错的问题顺便说一句!

  gg <- graph_from_edgelist(cbind(c(1:4, 6, 8, 10, 12, 14, 16:19, 1, 6, 8, 21, 12, 14, 5, 7, 9, 11, 13, 15), 
                                  c(2:5, 7, 9, 11, 13, 15, 17:20, 6, 8, 10, 12, 14, 16, 7, 9, 11, 13, 15, 20)))
  ll=matrix(
    c( 0,0,    75.25,0,    150.5,0,    225.8125,0,    302.8125,0, 
       0,-87,                                          302.8125,-87,
       0,-173.8125,                                    302.8125,-173.8125,
       0,-260.9375,                                    302.8125,-260.9375,
       16,-384.3125,                                   302.8125,-384.3125,
       16,-435.9575,                                   302.8125,-435.9375,
       16,-525.1875, 75.25,-525.1875, 150.5,-525.1875, 225.8125,-525.1875, 302.8175,-525.1875, 16, -260.9375),
    ncol=2,byrow=TRUE)
  plot(gg,layout=ll, edge.arrow.size = 0, vertex.size = c(rep(18, 20), 0),
       edge.color="orange")

我添加了一个节点(n 21)以允许与您的方案类似的分支。这看起来或多或少看起来应该吗?

enter image description here 我看了上一篇关于Stack Overflow(你建议的那篇)的帖子,试图让它成为一个Euler循环。实际上,自定义功能开箱即用,但您可能需要仔细检查是否可以使用生成的解决方案。也许,您可以尝试在“eulerizing”电路之前定义更好的连接设计。这就是我得到的。

# load custom f(x) as in
# https://stackoverflow.com/questions/40576910/solving-chinese-postman-algorithm-with-eulerization/40596816#40596816

eulerian <- make.eulerian(gg)
eulerian$info
g <- eulerian$graph

# set the layout as before to keep the circuit formatted according to your specs
par(mfrow=c(1,2))
plot(gg,layout=ll, edge.arrow.size = 0, vertex.size = c(rep(18, 20), 0),
     edge.color="orange", main = "Proposed")
plot(g,layout=ll, edge.arrow.size = 0, vertex.size = c(rep(18, 20), 0),
     edge.color="orange", main = "Eulerized")

enter image description here