当一个人调用 nw:weighted-path-to 时,会返回一个链接列表,描述原始和目标海龟之间的最短路径。
同样,调用 nw:turtles-on-weighted-path-to 会返回原点和目的地之间最短路径上的海龟列表。
据我了解,如果原点和目的地之间有两条相等加权的路径,则两个函数随机返回其中一条路径。这是独立发生的,因此可以为最短路径生成一组链接,但是另一组链接。可以使用以下代码复制:
extensions [nw]
links-own [ weight ]
to go
clear-all
create-turtles 4
ask turtle 0 [ create-link-with turtle 1 [ set weight 2 ] ]
ask turtle 0 [ create-link-with turtle 2 [ set weight 2 ] ]
ask turtle 1 [ create-link-with turtle 3 [ set weight 2] ]
ask turtle 2 [ create-link-with turtle 3 [ set weight 2] ]
ask turtle 0
[
let pathLinks nw:weighted-path-to turtle 3 "weight"
let pathNodes nw:turtles-on-weighted-path-to turtle 3 "weight"
let pathUtility nw:weighted-distance-to turtle 3 "weight"
show pathLinks
show pathNodes
show pathUtility
]
end
哪个会愉快地产生:
(turtle 0): [(link 0 2) (link 2 3)]
(turtle 0): [(turtle 0) (turtle 1) (turtle 3)]
(turtle 0): 4
显然,这不是一个错误,但遗憾的是它让我失望了。
我的问题是 - 将这两个程序链接起来以产生构成一个随机选择的最短路径的链接和海龟列表的最明智的方法是什么?
我假设最好使用 nw:weighted-path-to 返回链接,然后要求链接返回两端并进行某种排序在该路径上产生一组海龟的独特操作,如果是这种情况我不知道如何保持海龟的顺序。这有意义吗?这是你怎么做的?
一如既往,感谢阅读。
编辑:这也适用于具有多个相等长度路径的拓扑网络中的路径到达和路径上的乌龟。
答案 0 :(得分:3)
好问题!你可以从另一个生成列表,但我认为to-path to link-path更容易:
;; Construct the turtle path, putting the current turtle on the front:
let turtle-path fput self nw:turtles-on-weight-path-to turtle 3 "weight"
;; Iterate through pairs of turtles, getting the link connecting them
let link-path (map [[link-with ?2] of ?1] but-last turtle-path but-first turtle-path)
编辑:
Nicolas绝对是关于"链接到龟路径的路径"。但是,他的评论让我意识到你可以使用全能的reduce
和永远有用的other-end
来做到这一点!
reduce [ lput [[other-end] of ?2] of (last ?1) ?1 ] fput (list self) nw:weighted-path-to turtle 3 "weight"
Edit2:" to-path to turtle-path"代码非常不透明。这是一个澄清它的尝试:
to-report add-link-to-turtle-path [ turtle-path next-link ]
let last-turtle last turtle-path
report lput [[other-end] of next-link] of last-turtle
end
;; turtle-procedure - Assumes the current turtle is the starting point of the path
to-report link-path-to-turtle-path [ link-path ]
let start-of-path (list self)
report reduce add-link-to-turtle-path fput start-of-path link-path
end