首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Netlogo,沿链接移动海龟

Netlogo,沿链接移动海龟
EN

Stack Overflow用户
提问于 2019-05-29 15:34:13
回答 1查看 317关注 0票数 0

我正在做模型,我希望我的代理沿着节点之间的链接移动。这个想法是所有的海龟在每次滴答后移动一段恒定的距离。(请注意,我不希望它们从一个节点跳到另一个节点,也假设不可能使节点保持恒定的距离,因为我正在尝试在gis模型上实现它,当然,顶点的距离不是恒定的。)

对于此模型,有两种类型的代理:蝶形代理和收集器。每个代理被放置在彼此链接的节点上。收集器使用半径内找到离它们最近的一只蝴蝶,然后使用nw:海龟权重路径函数找到路径。例如,让我们假设收集器-02已经定位到了蝶形-19。路径是node-2 node-5 node-1 node-9。我希望收集器移动到这些节点以到达蝶形,而不是跳到每个节点。他必须在从节点-2到节点-5的链路上行进0.01个netlogo单元的距离,最终到达蝶蝶所在的节点-9。函数的跟随路径是不完整的,它是我遇到麻烦的地方。ANy提出了如何解决这个问题的想法。谢谢

代码语言:javascript
复制
extensions [nw]
globals [current-loc-global butterfly-location link-distance]

breed [nodes node]
breed [collectors collector]
breed [butterflys butterfly]

links-own [dist]
nodes-own [consist-butterfly linked-to]
collectors-own [ distance-from-location current-loc new-location targeted- 
butterfly initial-node next-node current-node previous-node
node-butterfly-at commute-path route-length next-pos]
butterflys-own [ initial-node-collectors targeted? node-butterfly-on]

to setup
  clear-all

  ;creating nodes
  create-nodes 60[
    set color blue
    set shape "circle"
    set consist-butterfly nobody
  ]
  ask nodes [create-link-with one-of other nodes]


  repeat 500[layout]
  ask nodes [setxy 0.95 * xcor * 0.95 ycor]

 create-butterflys number-of-butterflys[
    set node-butterfly-on one-of nodes
    set shape "butterfly"
    move-to node-butterfly-on
    set initial-node-collectors current-loc-global
    set targeted? false
  ]

  ask nodes [
    set consist-butterfly min-one-of butterflys in-radius 0.0001 [distance 
myself]
  ]


  create-collectors number-of-collectors[
    set current-loc one-of nodes with [consist-butterfly = nobody]
    set color red
    set targeted-butterfly nobody
    move-to current-loc
  ]

  ask collectors[
  set current-loc-global  current-loc
  ]

  ask links [
    let end-point-1 end1
    let end-point-2 end2
    set link-distance 0
    ask end1[set linked-to end-point-2
      set link-distance distance end-point-2
    ]
    set dist link-distance
    ask end2[
      set linked-to end-point-1
    ]
  ]

  reset-ticks
end

to layout
  layout-spring nodes links 0.5 1 1
end

to go
  find-agents
  find-path
  follow-path 
end

to find-agents
  ask collectors [
    if targeted-butterfly = nobody[set targeted-butterfly min-one-of 
    butterflys in-radius 10 with [not targeted?] [distance myself]]
    if targeted-butterfly != nobody [ask targeted-butterfly [set targeted? 
    true]]
  ]

  ask collectors with [targeted-butterfly != nobody][

    ask targeted-butterfly [set butterfly-location node-butterfly-on]
    set node-butterfly-at butterfly-location
  ]
end

to find-path
  ask collectors with [targeted-butterfly != nobody ][

    set initial-node min-one-of nodes in-radius 0.0001 [distance myself]
    let end-location node-butterfly-at

    let path []
    ask initial-node [set path nw:turtles-on-weighted-path-to end-location 
    dist]
    set commute-path path

    set route-length length commute-path

    set current-node initial-node
    set previous-node current-node
    set next-node current-node
  ]
end

to follow-path
  ask collectors with [targeted-butterfly != nobody][
  set distance-from-location distance current-node
    ifelse 0.09 < distance-from-location
    [jump 0.09]
    [move-to current-node
     set current-node next-node
      face next-node]

]

结束

EN

回答 1

Stack Overflow用户

发布于 2019-05-29 17:14:15

设法把它弄清楚了。需要将follow-path函数更改如下:

代码语言:javascript
复制
to follow-path
 ask collectors with [targeted-butterfly != nobody][
  set distance-from-location distance current-node

  set next-pos ((position(current-node) commute-path) + 1)
  if next-pos < route-length [set next-node item(next-pos) commute-path]

    ifelse 0.09 < distance-from-location
    [jump 0.09]
    [move-to current-node
     set current-node next-node
      face next-node]
  ]

end
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56355277

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档