乌龟以图案运动(Netlogo) [英] Turtles moving in a pattern (Netlogo)

查看:297
本文介绍了乌龟以图案运动(Netlogo)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下午好,我正在尝试让海龟在一组4个蓝色斑块之间移动.我可以使它们到达那些补丁,但之后它们只呆在那里,我需要的是它们不断地(按顺序)移动到它们右边的下一个蓝色补丁.我不知道该怎么做.

Good afternoon, I'm trying to make my turtles to move between a set of 4 blue patches. I can make them get to those patches but after that they just stay there, and what I need is to them continuously move (in order) to the next blue patch to their right. I don't know how to do it.

这是我正在谈论的代码部分:

This is the section of the code I'm talking about:

to move-turtles

    ask turtles 
      [while [[pcolor] of patch-here != blue]
         [
           face min-one-of patches with [pcolor = blue ] [ distance myself ]
           forward 1
         ]
      ]  
    tick
    end

提前,谢谢!

推荐答案

您是对的-使用while循环,您的海龟将被卡在它们来到的第一个蓝色斑块上,因为如果它们踩到了相邻的斑块上他们将立即想移回最接近的蓝色补丁(他们刚刚离开的那个).此外,while循环中发生的所有事件都在一个刻度内发生-如果您只是希望它们在设置过程中移至最近的蓝色补丁,则只需使用move-to即可.如果对您来说,移动到最近的蓝色补丁 很重要,那么最好使用if语句而不是while.

You're right- with a while loop your turtles will be stuck on the first blue patch they come to, since if they step onto a neighboring patch they will immediately want to move back to the closest blue patch (the one they just left). Additionally, everything that happens in the while loop occurs within a single tick- if you just want them to move to the nearest blue patch as part of your setup, just use move-to. If their movement to the nearest blue patch is important to you, it's probably better here to use an if statement rather than a while.

此外,您正在描述两种不同的运动模式".首先,您希望海龟移动到您希望它们跟随的赛道上.然后,如果它们在该电路中,则希望它们遵循一条有序的路径,将它们定位为电路中的下一个蓝色补丁,然后移至该补丁.因此,设置两个单独的过程并在适当的时间调用它们可能会更容易.如果海龟知道下一步应该去哪里(当前目标)以及应该执行哪种移动模式,那也可能会有所帮助.因此,您可以设置turtles-own变量,例如:

Additionally, you are describing two different "modes" of movement. First, you want the turtles to move to a circuit that you want them to follow. Then, if they are in that circuit, you want them to follow an ordered path where they target the next blue patch in the circuit and then move to that patch. Accordingly, it's probably easier to set up two separate procedures and call them at the appropriate time. It would probably also help if the turtles know where they are supposed to go next (a current target) and which movement mode they should be executing. So, you could set up turtles-own variables such as:

turtles-own [
  on-circuit?
  my-target 
]

请确保在设置中设置了这些变量,以使它们不是未定义变量的默认"0":

Make sure you set those variables in your setup so that they are not the default "0" of undefined variables:

to setup
  ca
  reset-ticks
  ask (patch-set patch 5 5 patch 5 -5 patch -5 5 patch -5 -5 ) [
    set pcolor blue
  ]

  crt 1  [

    set on-circuit? false   ;;; so a starting turtle knows which movement procedure to use
    set my-target nobody    
    setxy random 30 - 15 random 30 - 15    
    pd
  ]

end

然后,您可以运行您的go过程,这样,如果乌龟在电路上",它们就会尝试爬上电路.是错误的,如果他们在线",他们会在电路上行走吗?是真的.

Then, you can run your go procedure such that the turtles will try to get onto the circuit if their "on-circuit?" is false, and they will walk the circuit if their "on-circuit?" is true.

to go

  ask turtles [
    ifelse on-circuit? = false [  ;;; do this if turtle is not yet on the circuit
      get-to-circuit
    ]
    [  ;;;  do this if the turtle has been designated as on the circuit
      walk-circuit
    ]
  ]
  tick

end

现在,执行get-to-circuitwalk-circuit过程.我将向您展示如何设置get-to-circuit,但看看是否可以弄清walk-circuit的其余部分:

Now you make your get-to-circuit and walk-circuit procedures. I'll show you how I set up my get-to-circuit, but see if you can figure out the rest of the walk-circuit:

to get-to-circuit

  set my-target min-one-of other patches with [pcolor = blue ] [ distance myself ]
  face my-target
  fd 1
  if distance my-target < 1 [
    move-to my-target 
    ;;; This lets the turtle know it can switch to "walk-circuit" on the next tick 
    set on-circuit? true   
    set heading one-of [ 0 90 180 270 ] 
  ]
end


to walk-circuit
  if  my-target =  nobody [
    set my-target one-of ( other patches with [ pcolor = blue ] ) in-cone 10 180
  ]
?
?
? ...

这篇关于乌龟以图案运动(Netlogo)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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