如果这是一个愚蠢的问题,我先向您道歉。
当一个人调用nw:weighted-path-到链接列表时,它描述了原始乌龟和目的地乌龟之间的最短路径。
同样,调用nw:turtles-on-weighted-path-返回起点和终点之间最短路径上的海龟列表。
据我了解,如果起点和终点之间有2条平均权重的路径,则两个函数都会随机返回这些路径之一。这是独立发生的,因此可以为最短路径生成一组链接,但为另一组海龟生成链接。可以使用以下代码复制它:
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的链接,然后让链接返回两端并进行某种独特的操作以在该路径上产生一组乌龟,如果那是案例我不确定如何保留乌龟的顺序。这有意义吗?那是你会怎么做的?
一如既往,感谢您的阅读。
编辑:这也适用于具有多个等长路径的拓扑网络中的路径到和路径上的海龟。
最佳答案
好问题!您可以从另一个列表中生成一个列表,但我认为将turtle-path转换为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-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
关于netlogo - nw:加权路径到,nw:加权路径上的海龟和多个等加权路径,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29982305/