Netlogo:如何使用route变量实际沿路径移动

问题描述 投票:2回答:1

我使用两种类型的海龟,汽车和房屋。两者都是随机定位的。我的目标是从组合路线矢量开始为每辆车获取路线,并让每辆车移动并访问已分配给它的每个房屋。首先,我从组合路线向量为每辆车创建一条路线。我在下面提供我的代码。但是现在,我试着让汽车按照各自的路线行驶......

globals [ route-vector ]
breed [carr car]
breed [hous housess]
carr-own [ route ]

to setup
clear-all
create-carros
create-casas
make-routes
end

to create-carros
create-carr cars [ set color green ]
ask carr  [
set shape "car"
set size 1.5
setxy random-xcor random-ycor
 ]
end

to create-casas
create-hous house [ set color red ]
ask hous  [
set shape "house"
set size 1.5
setxy random-xcor random-ycor
]
end


to make-routes
set route-vector [ 3 4 5 6 7 0 1 2 0 1 ] ;5 10 15 20 25
let houses sublist route-vector 0 (length route-vector / 2 )
let carlist sublist route-vector (length route-vector / 2 ) (length route-
vector)
ask carr [ set route [] ]
(foreach carlist houses
 [ [the-car the-house] ->
  ask carr with [who = the-car] [ set route lput the-house route ]
 ]
  )
 end

 to go
  ask carr  [
    ;; if at target, choose a new random target
    ;        if distance route = 0
     ;          [ set route one-of route-vector
     ;            face route ]
    ;        ;; move towards target.  once the distance is less than 1,
     ;        ;; use move-to to land exactly on the target.
    ;        ifelse distance route < 1

    ;let mylist [1 2 3]
     ;foreach route
     face route
     fd 1
;print map last filter first route
    ;    face housess 3
     ;    fd 1

     ;    move-to one-of route
     ;    fd 1

      ]
     ; move-to housess 3
       ;fd 1

      ;tick
      end

我想使用route变量实际沿路径移动。但我不知道如何告知每辆车各自的路线,并让他们搬到自己的家中。

我试着只用一辆车的go按钮

执行:询问汽车1 [面部路线fd 1但总是得到错误(“FACE预期输入为代理但得到列表[4 7]而不是。”)结束

在这种情况下,我想让汽车1首先移动到房子4,然后移动到房子7,然后回到原来的位置......我尝试了几种方法,但我找不到解决方案。我试图单独做,我从每个车的“路线”列表中选择了第一项,但我仍然不能..

如果有人可以帮助我,我真的很感激。谢谢

routes netlogo
1个回答
1
投票

使用who数字来索引海龟可能会导致问题 - 在这种情况下,您将遇到无法真正动态更新列表的问题,因为houscarr数字仅基于其创建的顺序。如果可能的话,将海龟直接存放在列表中要好得多。使用您的设置的修改版本查看此示例:

globals [ route-vector ]
breed [carr car]
breed [hous housess]
breed [spawns spawn]
carr-own [ route route-counter spawn-target target]

to setup
  clear-all
  create-carros
  create-casas
  make-routes
  reset-ticks
end

to create-carros
  create-carr 3 [ set color green ]
  ask carr  [
    set size 1.5
    setxy random-xcor random-ycor

    ; set up a 'route-counter' to act as an index for a car's route
    set route-counter 0
    set target nobody
    set route []    
    pd
  ]

  ; hatch a 'spawn-target' turtle that can be used to return
  ; the carr back to their starting position
  ask carr [    
    hatch 1 [
      set breed spawns
      ht
    ]
    set spawn-target one-of other turtles-here with [ 
      xcor = [xcor] of myself
    ]
  ]
end

to create-casas
  create-hous 5 [ set color red ]
  ask hous  [
    set shape "house"
    set size 1.5
    setxy random-xcor random-ycor
  ]
end

现在,不要依靠who数字来索引房屋,而是直接在carr路线中使用房屋清单:

to make-routes
  ; Just use the car-order 
  let car-order [ 0 1 2 0 1 ] 

  ; Create a list of hous directly by sorting them
  let houses sort hous

  ; Your same foreach procedure, but in this case the carr
  ; are storing the house in the list directly to avoid
  ; indexing problems
  ask carr [  ]
  (foreach car-order houses
    [ [the-car the-house] ->
      ask carr with [who = the-car] [ set route lput the-house route ]
    ]
  )
end

然后,carr可以迭代他们的路线,根据route-counter的指数值选择一个新的目标(他们的spawn-target有一个小的中断)。

to go 
  ask carr [

    ; If a car has no target, set the target to the
    ; item indexed by 'route-counter'
    if target = nobody [
      set target item route-counter route
    ]

    ; Movement chunk
    face target
    ifelse distance target > 1 [
      fd 1
    ] [ 
      move-to target

      ; Only advance the route counter if the current target
      ; was not the original spawn point
      if target != spawn-target [
        set route-counter route-counter + 1
      ]
      set target nobody

    ]

    ; If the route counter would index outside of the 
    ; list boundaries, reset it to 0
    if route-counter > length route - 1 [
      set route-counter 0
      set target spawn-target
    ]
  ]  

  tick  
end

这仍然不是超级程序化的,因为你依赖的是你的汽车订单与你的房屋数量一样长,但是我不确定你真正尝试做什么,也许它会起作用。

编辑

根据your comment-如果你必须使用who数字,你仍然可以使用“产卵目标”示例让海龟回到它们的起始位置 - 只是在carrhous产生之后发生。再次,这绝对不是理想的,因为如果你不小心产卵顺序,carr / house的数量等,你的模型可以“破坏”。

所以,基本的setupcreate-casas程序如上所述,这是你的新create-carros程序:

to create-carros
  create-carr 3 [ set color green ]
  ask carr  [
    set size 1.5
    setxy random-xcor random-ycor

    ; set up a 'route-counter' to act as an index for a car's route
    set route-counter 0
    set target nobody
    set route []
    pd
  ]
end

现在,您的make-routes可以包含spawn目标海龟(此示例包含您评论中的无序房屋):

to make-routes
  set route-vector [4 7 6 3 5 0 1 2 0 1]
  let houses sublist route-vector 0 (length route-vector / 2 )
  let carlist sublist route-vector (length route-vector / 2 ) (length route-vector)

  (foreach carlist houses
    [ [the-car the-house] ->
      ask car the-car [ 
        set route lput ( housess the-house ) route 
      ]
    ]
  )

  ; hatch a 'spawn-target' turtle that can be used to return
  ; the carr back to their starting position
  ask carr [
    hatch 1 [
      set breed spawns
      ht
    ]
    set spawn-target one-of other turtles-here with [
      xcor = [xcor] of myself
    ]
  ]
end

然后,上面的go程序应该没有任何改变。

编辑2

使carr停止的一种简单方法是设置逻辑标志,以便只有满足特定条件的carr才会移动。考虑这个修改过的car-owncreate-carros设置:

carr-own [ route route-counter spawn-target target route-complete? ]

to create-carros
  create-carr 3 [ set color green ]
  ask carr  [
    set size 1.5
    setxy random-xcor random-ycor

    ; set up a 'route-counter' to act as an index for a car's route
    set route-counter 0
    set target nobody
    set route []
    set route-complete? false
    pd
  ]
end

在这里,我们现在有一个名为route-complete?的布尔(逻辑)变量,对于所有新的carr,它被设置为false。现在,您可以在go过程中添加一行,该过程说“只有将route-complete?设置为false的汽车才能执行这些操作”。

to go
  ; ask carr with route-complete set to false
  ask carr with [ not route-complete? ] [

    ; If a car has no target, set the target to the
    ; item indexed by 'route-counter'
    if target = nobody [
      set target item route-counter route
    ]

    face target
    ifelse distance target > 1 [
      fd 1
    ] [
      move-to target

      ; Only advance the route counter if the current target
      ; was not the original spawn point. ADDITIONALLY,
      ; if the target is the starting target, set route-complete?
      ; to true for that carr
      ifelse target != spawn-target [
        set route-counter route-counter + 1
      ] [
        set route-complete? true
      ]
      set target nobody
    ]

    ; If the route counter would index outside of the
    ; list boundaries, reset it to 0
    if route-counter > length route - 1 [
      set route-counter 0
      set target spawn-target
    ]
  ]
  tick
end

你会注意到在move-to块中有一个修改过的位,如果carr回到它的起始位置,它也将它的route-complete?设置为true, so that the next timegois called, thatcarr`将不会移动。

请注意,如果您希望route-complete?在其路线上运行一定次数,您可以将carr更改为计数器而不是true / false。

© www.soinside.com 2019 - 2024. All rights reserved.