沿着最短路径的节点之间的海龟移动

时间:2018-04-05 13:29:06

标签: location netlogo shortest-path path-finding

我试图将我的公民从一个节点(滑动)移动到计算最短路径的另一个节点(新位置)。 我只能使用new-location的set total-expected-path [distance [slocation]]来计算从滑点到新位置的距离。

但是,我很确定设置total-expected-path之后的以下行不正确。我收到以下错误:此代码无法由乌龟运行,只有链接错误,而节点35运行LINK-LENGTH

如何在总预期路径中将此距离计算定义为使用节点之间的链接连接的节点之间的最小值? 之后,我怎样才能沿着这条短路移动海龟?enter image description here     去     设置计时     问市民     [找到天的活动]     端

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

1 个答案:

答案 0 :(得分:2)

您可能希望使用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

这会创建一个循环网络 - 假装绿色是起始节点而红色是目的地。

enter image description here

现在,使用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

enter image description here

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

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

enter image description here

要实际让您的员工沿着特定路径移动,您可以使用上面的路径识别代码和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

enter image description here