海龟按照最短路径在节点之间移动

Movement of turtles between nodes following the shortest path

我正在尝试将我的公民从一个节点(位置)移动到另一个节点(新位置),计算最短路径。 我只能使用 set total-expected-path [distance [slocation] of myself] of new-location.

来计算从 slocation 到 new-location 的距离

但是,我很确定设置总预期路径后的以下行不正确。我收到以下错误:此代码不能被乌龟 运行,只有节点 35 运行ning LINK-LENGTH

时出现 link 错误

如何使用节点之间的 link 连接将总预期路径中的此距离计算定义为节点之间的最小值? 然后,我怎样才能沿着这条短路径移动海龟? 去 计时员 问市民 [查找日活动] 结束

to set-timekeeper
tick 
let counter ticks 
if (counter = 2) 
[set timekeeper 2]
end

to find-day-activities
if (timekeeper = 2) 
[Do_7AM_9AM]
end

to Do_7AM_9AM
if (sex = 0 and age = 1 and employment = 0 and household-size = 0 [move-work]
end

to move-work
to move-work
set slocation min-one-of nodes [distance myself]
let new-location min-one-of nodes [distance one-of workbuildings]
let llocation one-of [link-neighbors with-min [link-length]] of new-location
move-to llocation
end

您可能希望使用 nw 扩展来利用 nw:turtles-on-path-tonw:turtles-on-weighted-path-to 原语。使用这些扩展和变量:

extensions [nw]
breed [ nodes node ]
breed [ walkers walker ]
links-own [ weight ]

此设置:

to setup-example
  ca
  let xs [ 10 -5 -5 -5 -5 2 ]
  let ys [ 0 0 3 6 9 9 ]
  ( foreach xs ys [
    [ x y ] ->
    ask patch x y [
      sprout-nodes 1 [
        set shape "circle"
        set color white
        set size 2.5
      ]
    ]
  ])
  let ind ( range 0 4 )
  foreach ind [
    i ->
    let x item i xs
    let y item i ys
    let xn item ( i + 1 ) xs
    let yn item ( i + 1 ) ys
    ask nodes-on patch x y [
      create-links-with nodes-on patch xn yn
    ]
  ]
  while [ any? nodes with [ count my-links < 2 ] ] [
    ask one-of nodes with [ count my-links < 2 ] [
      let linkable min-one-of other nodes with [ count my-links < 2 ] [distance myself]
      if linkable != nobody [
        create-link-with linkable
      ]
    ] 
  ]
  ask nodes-on patch -5 0 [ set color green ]
  ask nodes-on patch 2 9 [ set color red ]  
end

这将创建一个循环网络 - 假设绿色是起始节点,红色是目的地。

现在,使用 nw:turtles-on-path-to 您可以识别路径中通过最少链接到达目的地的节点:

to fewest-links
  let start one-of nodes-on patch -5 0
  let target one-of nodes-on patch 2 9

  let path nobody 
  ask start [
    set color green
    set path but-first nw:turtles-on-path-to target
    ask turtle-set path [ set color yellow ]
    ask target [ set color red ]
  ]  
end

或者,用link-length作为nw:turtles-on-weighted-path-to中的weight变量,可以得到最短距离:

to shortest-distance
  let start one-of nodes-on patch -5 0
  let target one-of nodes-on patch 2 9
  ask links [ 
    set weight link-length
  ]
  let path nobody 
  ask start [
    set color green
    set path but-first nw:turtles-on-weighted-path-to target weight
    ask turtle-set path [ set color yellow ]
    ask target [ set color red ]
  ]  
end

要真正让您的人员沿着特定路径移动,您可以结合使用上述路径识别代码和 foreach。示例设置:

to setup
  ca
  create-nodes 10 [ 
    set shape "circle" 
    set color white 
    set size 2.5
  ]
  layout-circle nodes 10
  while [ any? nodes with [ count my-links < 2 ] ] [
    ask one-of nodes with [ count my-links < 2 ] [
      let linkable min-one-of other nodes with [ count my-links < 2 ] [distance myself]
      if linkable != nobody [
        create-link-with linkable
      ]
    ] 
  ]
  ask one-of nodes [ 
   set color green + 1
   hatch-walkers 1 [
      set color blue
      set size 1.5
    ]
  ] 
  reset-ticks
end

以及运动本身:

to go
  ask walkers [
    ; Randomly choose a target node to walk to
    let target one-of nodes with [ color = white ]
    if target != nobody [
      ; Remember the starting node
      let current one-of nodes-here 
      ; Define a path variable from the current node- take all but
      ; the first item (as first item is current node)
      let path nobody
      ask links [ set weight link-length ]
      ask current [ 
        set path but-first nw:turtles-on-weighted-path-to target weight
      ]
      ; Indicate the end node
      ask last path [ 
        set color red
        set size 2.5
      ]
      ; Move along the path node-to-node 
      foreach path [
        next-target ->
        face next-target
        move-to next-target
        wait 0.25
        ask next-target [
        set color yellow
        ]
      ]
    ]
    wait 1
    ; Reset
    ask nodes [ set color white ]
    ask one-of nodes-here [ set color green ]
  ]
end