Netlogo:如何使用route变量实际沿路径移动 [英] Netlogo: How using the route variable to actually move along the path

查看:204
本文介绍了Netlogo:如何使用route变量实际沿路径移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用两种乌龟,汽车和房屋.两者都是随机放置的. 我的目标是从组合的路线向量开始为每辆汽车获得一条路线,并让每辆汽车移动并参观已分配给它的每个房屋. 首先,我根据合并的路线向量为每辆汽车创建一条路线. 我在下面介绍我的代码. 但是现在,我正在尝试让汽车遵循各自的路线...

I use two types of turtles, cars and houses. both are randomly positioned. My goal is to get a route for each car starting from a combined route vector, and have each car move and visit every home that has been assigned to it. First i create a route for each car from the combined route vector. I present my code below. But now, im trying make car follow respective route...

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变量实际沿路径移动. 但我不知道如何告知每辆车各自的路线,并导致他们搬回家.

I want use the route variable to actually move along the path. but I do not know how to inform each car of their respective route and cause them to move to their homes.

我试图只在一辆车上使用前进"按钮

I tried to use the go button with only one car

执行: 问车1 [ 面对路线 fd 1 但总是会收到错误消息("FACE期望输入是代理,但得到了 列出[4 7].") ] 结束

do: ask car 1 [ face route fd 1 but always get the error ("FACE expected input to be an agent but got the list [4 7] instead.") ] end

在这种情况下,我想让汽车1先移至4号房,然后移至7号房,然后再返回其原始位置... 我已经尝试了几种方法,但是找不到解决方案.我尝试单独进行操作,我从路线"列表中为每辆车选择了第一项,但我仍然无法..

in this case I wanted to cause car 1 to move first to house 4 and second to house 7, then back to its original position ... I've tried several ways, but I can not find a solution. I tried to do it separately, I chose the first item from the "route" list for each car but I still could not ..

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

If someone can help me, I really appreciate it. Thank you

推荐答案

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

Using who numbers to index turtles can cause problems- in this case, you'll run into an issue where you can't really dynamically update the list, because hous and carr numbers are based solely on the order of their creation. It's far better to store the turtles directly in a list, if possible. Check out this example using a modified version of your setup:

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路线中使用房屋清单:

Now, instead of relying on who numbers to index the houses, use a list of the houses directly in your carr routes:

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稍作休息),只需遍历其路线即可选择新的目标.

Then, the carr can just iterate over their route to select a new target based on the index value of route-counter (with a small break for their 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

这仍然不是超级编程,因为您所依赖的购车订单长度与房屋数量相同,但是我不确定您实际上在试图做什么,也许行得通.

This is still not super programmatic, as you are relying on your car-order being the same length as the count of your houses, but I'm not sure what you're actually trying to do so maybe it will work.

编辑

按照carr和hous之后发生这种情况.同样,这绝对不是理想的选择,因为如果您不小心生成顺序,carr/house的数量等原因,模型可能会崩溃".

As per your comment- if you must use the who number, you can still use the "spawn target" example to have the turtles return to their starting position- just have it happen after the carr and hous have been spawned. Again, this is definitely not ideal as your model can 'break' if you're not careful with spawn order, number of carr / house, etc.

因此,上面的基本setupcreate-casas过程,将其作为新的create-carros过程:

So, basic setup and create-casas procedures as above, with this as your new create-carros procedure:

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目标乌龟(此示例包含您注释中乱序的房屋):

Now, your make-routes can contain the spawn target turtles (this example has the out-of-order houses from your comment):

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步骤应该可以正常运行.

Then, the go procedure from above should work without any change.

编辑2

让您的卡尔停下来的一种简单方法是设置一个逻辑标志,以便只有满足特定条件的卡尔才可以移动.考虑修改后的car-owncreate-carros设置:

A simple way to have your carr stop is to set a logical flag so that only carr that meet a certain criteria will move. Consider this modified car-own and create-carros setup:

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的汽车才能执行这些操作".

Here, we now have a boolean (logical) variable called route-complete?, which is set to false for all new carr. Now, you can add a line into the go procedure that says "only cars that have route-complete? set to false, do these actions."

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 time go is called, that carr`不动

You'll notice that there's a modified bit in the move-to chunk where if the carr is moving back to its starting position, it also sets its route-complete? to true, so that the next timegois called, thatcarr` won't move.

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

Note that you could change that route-complete? to a counter instead of a true/false if you wanted carr to run through their route a set number of times.

这篇关于Netlogo:如何使用route变量实际沿路径移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆