netlogo - 海龟沿着链接列表移动

标签 netlogo

我有一个链接列表:[(链接2226 2417)(链接1650 2226)(链接1650 1916)(链接1682 1916)(链接1107 1682)]。如何将海龟从 (link 2226 2417) 移动到 (link 1107 1682) ?

通过使用 NW 扩展中的 nw:turtles-on-path-to 并使用具有最低成本路径的 Marine 代码(我将其修改为工作):

to least-cost-path [ID-individual ID-polygon] 
let cost-of-path -1
let path []
let individuals-on-path []

ask wolves with [ who = ID-individual] [ 
 foreach sort nodes-on patches with [plabel = ID-polygon] [ 
  let node-on-polygon ? 
  nw:set-snapshot nodes links

  ask nodes-here [ 
   let cost nw:weighted-distance-to node-on-polygon "cost-of-link" 
   if cost-of-path = -1 or cost < cost-of-path [ 
    set cost-of-path cost 
    set path nw:weighted-path-to node-on-polygon "cost-of-link" 
    set individuals-on-path nw:turtles-on-path-to node-on-polygon ] ] ] ]

print cost-of-path
print path
print individuals-on-path

foreach path [
 ask ? [ set color red
 set thickness 0.2 ] ]

ask wolves [
foreach individuals-on-path [
face ? 
move-to ? ] ]

end

“path”和“individuals-on-path”的结果有问题:逻辑上,我应该在(节点982)之后有(节点1669)。因此,狼沿直线移动,而不是沿着路径移动。

路径 = [(链接 982 1669) (链接 1353 1669) (链接 1115 1353) (链接 1115 1276) (链接 1276 1983) (链接 479 1983) (链接 479 2319) (链接 345 2319) (链接 345 1023 ) (链接 145 1023) (链接 145 1808) (链接 738 1808) (链接 738 1793) (链接 1097 1793) (链接 1097 2523) (链接 380 2523) (链接 380 1582) (链接 469 1582) (链接 469 1278 ) (链接 1277 1278) (链接 391 1277) (链接 391 2175) (链接 208 2175)]

路径上的个体 = [(节点 982) (节点 1616) (节点 1623) (节点 2438) (节点 749) (节点 1435) (节点 1584) (节点 1396) (节点 928) (节点 939) (节点209) (节点1160) (节点1191) (节点1537) (节点806) (节点1222) (节点1762) (节点1245) (节点1274) (节点208)]

非常感谢您的帮助。

最佳答案

这比看起来更棘手,对吧?

我不知道你的整体背景是什么,但是 nw:turtles-on-path-to来自NW extension的原始也许对你有用。

如果您无法使用nw:turtles-on-path-to,则必须在 NetLogo 中执行此操作。我在下面写的内容要求您提供起始节点。 (消除该要求并非易事。)如下:

to-report node-list [ link-list start-node ]

  ; report an empty list when we're done:
  if empty? link-list [ report [] ]

  ; also report an empty list if given start
  ; node is not part of the first link:
  let ends [ both-ends ] of first link-list
  if not member? start-node ends [ report [] ]

  ; the "other node" is the end that is not the start-node
  let other-node [ one-of other ends ] of start-node

  ; if we had only one link, report a list with the two nodes
  if length link-list = 1 [ report list start-node other-node ]

  ; if we still have other links, put our start node at the front 
  ; of the result list and build the rest recursively, using
  ; other-node as a starting point for the rest of link-list  
  report fput start-node node-list but-first link-list other-node

end

现在让我们看看它的实际效果:

to setup
  ca
  ; create a simple "path" network for demoing:
  crt 1
  crt 9 [ create-link-with turtle (who - 1) ]
  ask turtles [ set shape "dot" ]
  layout-circle turtles 8
end

to walk
  let list-of-links (sort links) ; supply your own list here...
  let list-of-nodes node-list list-of-links turtle 0
  crt 1 [ ; create our "walker"
    foreach list-of-nodes [
      face ?
      display wait 0.2 ; just to show what's going on
      move-to ?
      display wait 0.2 ; just to show what's going on
    ]
  ]
end

关于netlogo - 海龟沿着链接列表移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20271127/

相关文章:

netlogo - 在 NetLogo 中使用向量

Netlogo,检查两个代理是否连接到同一个代理

graphics - 如何优化代码以使设置更快

patch - 在偶数和奇数场景中,每 block 海龟的填充量有何差异?

netlogo - 如何要求一只或两只乌龟(如果有的话)做一个 Action ?

python - 用python解析netlogo代码

graph-theory - 在 NetLogo 中有效地获取连接海龟集中海龟的所有链接

netlogo - 如何计算两个非邻居节点之间的总链路成本?

netlogo - 概率和百分比的解释

NetLogo,从列表 : how to use rnd-extension? 中加权随机抽取