nw:weighted-path-to, nw:turtles-on-weighted-path-to 和多个等权重路径
nw:weighted-path-to, nw:turtles-on-weighted-path-to and multiple equally weighted paths
如果这是一个愚蠢的问题,我先道歉。
当调用 nw:weighted-path-to 时,links 的列表被 returned 描述了起始和目标海龟之间的最短路径.
类似地,调用 nw:turtles-on-weighted-path-to returns 是起点和终点之间最短路径上的海龟列表。
据我了解,如果起点和终点之间有 2 条权重相等的路径,则两条函数 return 都会随机选择其中一条路径。这是独立发生的,因此可以为最短路径生成一组 links,但另一组海龟。这可以使用以下代码复制:
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
显然,这不是一个错误,但不幸的是它让我绊倒了。
我的问题是 - link 这两个程序生成构成单个随机选择的最短路径的 links 和海龟列表的最明智方法是什么?
我假设最好使用 nw:weighted-path-to return links,然后询问 links 到 return both-ends 并进行某种独特的操作以在该路径上生成一组海龟,如果是这样我不确定如何以保持海龟的秩序。这有意义吗?你会这样做吗?
一如既往,感谢阅读。
编辑:这也适用于具有多个等长路径的拓扑网络中的 path-to 和 turtles-on-path-to。
好问题!您可以从另一个生成列表,但我认为 link-path 的 turtle-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 对 "link-path to turtle-path" 的看法完全正确。然而,他的评论让我意识到你可以使用全能的 reduce
和永远有用的 other-end
来做到这一点!
reduce [ lput [[other-end] of ?2] of (last ?1) ?1 ] fput (list self) nw:weighted-path-to turtle 3 "weight"
Edit2:"link-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
如果这是一个愚蠢的问题,我先道歉。
当调用 nw:weighted-path-to 时,links 的列表被 returned 描述了起始和目标海龟之间的最短路径.
类似地,调用 nw:turtles-on-weighted-path-to returns 是起点和终点之间最短路径上的海龟列表。
据我了解,如果起点和终点之间有 2 条权重相等的路径,则两条函数 return 都会随机选择其中一条路径。这是独立发生的,因此可以为最短路径生成一组 links,但另一组海龟。这可以使用以下代码复制:
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
显然,这不是一个错误,但不幸的是它让我绊倒了。
我的问题是 - link 这两个程序生成构成单个随机选择的最短路径的 links 和海龟列表的最明智方法是什么?
我假设最好使用 nw:weighted-path-to return links,然后询问 links 到 return both-ends 并进行某种独特的操作以在该路径上生成一组海龟,如果是这样我不确定如何以保持海龟的秩序。这有意义吗?你会这样做吗?
一如既往,感谢阅读。
编辑:这也适用于具有多个等长路径的拓扑网络中的 path-to 和 turtles-on-path-to。
好问题!您可以从另一个生成列表,但我认为 link-path 的 turtle-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 对 "link-path to turtle-path" 的看法完全正确。然而,他的评论让我意识到你可以使用全能的 reduce
和永远有用的 other-end
来做到这一点!
reduce [ lput [[other-end] of ?2] of (last ?1) ?1 ] fput (list self) nw:weighted-path-to turtle 3 "weight"
Edit2:"link-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