【问题标题】:How to increase the speed of certain turtles (Netlogo)?如何提高某些海龟的速度(Netlogo)?
【发布时间】:2022-10-18 01:05:16
【问题描述】:

我创建了一个代表 2 个巴士站的模型。此刻海龟登上公共汽车,然后公共汽车将开走。但是,我希望海龟的登机速度有所不同。换句话说,我希望上站的海龟比下站的海龟移动得更快(或者底部的比顶部的慢)。我不知道该怎么做......你们有什么建议吗?我的代码如下。提前致谢!

globals [time]
turtles-own [target]
breed [bus a-bus]
to setup

clear-all
  
  ;; above
  ask patches with [pxcor = 2 and pycor = 6][set pcolor white]
  ask patches with [pxcor = 2 and pycor = 5][set pcolor white]
  ask patches with [pxcor = 0 and pycor = 5][set pcolor white]
  ask patches with [pxcor = 0 and pycor = 6][set pcolor white]
  ask patches with [pxcor = 1 and pycor = 5][set pcolor white]
  ask patches with [pxcor = 1 and pycor = 6][set pcolor white]
  ask patches with [pxcor = 3 and pycor = 5][set pcolor white]
  ask patches with [pxcor = 3 and pycor = 6][set pcolor white]
  ask patches with [pxcor = 4 and pycor = 5][set pcolor white]
  ask patches with [pxcor = 4 and pycor = 6][set pcolor white]
  ask patches with [pxcor = 5 and pycor = 5][set pcolor white]
  ask patches with [pxcor = 5 and pycor = 6][set pcolor white]
  ask patches with [pycor = 7][
    set pcolor gray
  ]
  
  
 ;;below
  ask patches with [pxcor = 2 and pycor = -6][set pcolor white]
  ask patches with [pxcor = 2 and pycor = -5][set pcolor white]
  ask patches with [pxcor = 0 and pycor = -5][set pcolor white]
  ask patches with [pxcor = 0 and pycor = -6][set pcolor white]
  ask patches with [pxcor = 1 and pycor = -5][set pcolor white]
  ask patches with [pxcor = 1 and pycor = -6][set pcolor white]
  ask patches with [pxcor = 3 and pycor = -5][set pcolor white]
  ask patches with [pxcor = 3 and pycor = -6][set pcolor white]
  ask patches with [pxcor = 4 and pycor = -5][set pcolor white]
  ask patches with [pxcor = 4 and pycor = -6][set pcolor white]
  ask patches with [pxcor = 5 and pycor = -5][set pcolor white]
  ask patches with [pxcor = 5 and pycor = -6][set pcolor white]
  ask patches with [pycor = -4][
    set pcolor gray
  ]
  
  ;; passengers above
  ask n-of Passengers_2 patches with [pcolor = white and pycor > 0][                     
      sprout 1[                                                                    
      set color grey                                                              
      set size 1       
      set shape "person"
      set target patches with [pxcor = 3 and pycor = 8]
            
  ]]
  ;; passengers below
  ask n-of Passengers_1 patches with [pcolor = white and pycor < 0][                     
      sprout 1[                                                                    
      set color grey                                                              
      set size 1       
      set shape "person"
      set target patches with [pxcor = 3 and pycor = -3]
      
  ]]
  
  
  
   ;; bus above
  create-bus Bus_2[
    set color red
    set size 5
    set xcor 3
    set ycor 8
    set shape "bus"
    set heading 90

]
  
  ;; bus below
  create-bus Bus_1[
    set color red
    set size 5
    set xcor 3
    set ycor -3
    set shape "bus"
    set heading 90

]
  
    reset-ticks

end

to check-in
  ask turtles with [ycor < 0 ] [                                                  ;; below
    move-to one-of patches with [pxcor = 3 and pycor = -3]
    if any? neighbors with [pxcor = 3 and pycor = -4] and shape != "bus"          ;; if passenger neighbors this patch, it dies
    [
      die]
  ]
  
    ask turtles with [ycor > 1 ] [                                                  ;; above
    move-to one-of patches with [pxcor = 3 and pycor = 8]
    if any? neighbors with [pxcor = 4 and pycor = 8] and shape != "bus"            ;; if passenger neighbors this patch, it dies
    [
      die]
  ]
  
    tick
end


to drive
  set time ticks
 if time > 0 [
    ask turtles with [pycor < 0 or pycor > 1][
    forward 33
      if any? turtles [ stop ]  
  ]]
  tick
end



to go
  check-in
  drive
  tick
end

【问题讨论】:

  • 要在速度上有差异,首先需要一个速度的概念。您使用move-to,它会立即将海龟从当前位置“传送”到目的地。你需要让他们采取一系列小步骤
  • @LeirsW 感谢您的提示!起初我试图这样做,但我无法让它工作......关于如何做到这一点的任何建议?
  • @LeirsW如果我尝试将公共汽车设置为目标,海龟将随机移动到世界上,这是不应该发生的
  • 您可以使用face 转向您希望他们去的公共汽车。你可以给他们一个名为speed的乌龟自己的参数,让他们每一步都向前走,如果他们与公共汽车的距离小于他们的速度值,就会到达

标签: model netlogo


【解决方案1】:

这是一个例子,我给每只海龟自己的速度变量在 0 到 0.5 之间,让它们都以自己的速度走向中间的绿色斑块。

go-1 中,这发生在单个tick 中。我使用while 循环,直到所有海龟都到达为止。像这样的while 循环要记住的一件重要事情是,您需要确保最终满足条件。否则,您的程序会陷入无限循环(如果发生这种情况,请转到代码选项卡并进行一次更改。这样做会退出当前正在进行的任何循环)。建议添加第二个条件来结束 while,例如每次迭代都会增加一个计数器,如果它超过一个值(例如 10000),则无论是否满足其他条件,while 都会停止。我在这里使用error 命令,但您也可以使用stop,或者将其作为while 运行的第二个条件。

如您所见,while 很有用,但需要一些额外的思考。 go-2 将所有内容都与滴答声联系起来,并使用界面上的永久按钮运行。我建议使用这种结构,但如果您需要您的乘客在第一次滴答之前登机,您将不得不使用类似我的go-1

turtles-own [speed]

to setup
  ca
  ask patch 0 0 [set pcolor green]
  crt 5 [
    setxy random-xcor random-ycor 
    set heading towards patch 0 0
    set speed random-float 0.4 + 0.1 ;Sets speed between 0.1 and 0.5
  ]
  
  reset-ticks
  
end

to go-1 ;In this one, the entire walk happens within a single tick
  
  let emergency-stop 0
  while [any? turtles with [patch-here != patch 0 0]] [
    ask turtles with [patch-here != patch 0 0] [fd speed]
    set emergency-stop emergency-stop + 1
    display
    if emergency-stop > 10000 [error "emergency stop exited the while loop"]
  ]
  
  tick
  
end


to go-2 ;In this one, the walk happens over multiple ticks

  if not any? turtles with [patch-here != patch 0 0] [stop]
  
  ask turtles with [patch-here != patch 0 0] [fd speed]
  
  tick

end

【讨论】:

    猜你喜欢
    • 2022-12-20
    • 2017-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-09
    • 1970-01-01
    相关资源
    最近更新 更多