【问题标题】:How to make turtles move at the same time?如何让乌龟同时移动?
【发布时间】:2022-10-15 01:22:59
【问题描述】:

我正在模拟三个有乘客的公交车站。当一个汽车站的所有乘客都上车时,我希望公共汽车(一只乌龟)开走。这应该同时发生,但目前不是。公共汽车一个接一个地离开。有谁知道我做错了什么?提前致谢!我的代码:

    globals [time]
turtles-own [target]
breed [bus busses]
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
  ]
  
  
 ;;middle
  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
  ]
  
  
  ;;below
  ask patches with [pxcor = 2 and pycor = -15][set pcolor white]
  ask patches with [pxcor = 2 and pycor = -14][set pcolor white]
  ask patches with [pxcor = 0 and pycor = -14][set pcolor white]
  ask patches with [pxcor = 0 and pycor = -15][set pcolor white]
  ask patches with [pxcor = 1 and pycor = -14][set pcolor white]
  ask patches with [pxcor = 1 and pycor = -15][set pcolor white]
  ask patches with [pxcor = 3 and pycor = -14][set pcolor white]
  ask patches with [pxcor = 3 and pycor = -15][set pcolor white]
  ask patches with [pxcor = 4 and pycor = -14][set pcolor white]
  ask patches with [pxcor = 4 and pycor = -15][set pcolor white]
  ask patches with [pxcor = 5 and pycor = -14][set pcolor white]
  ask patches with [pxcor = 5 and pycor = -15][set pcolor white]
  ask patches with [pycor = -13][
    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 middle
  ask n-of Passengers_1 patches with [pcolor = white and pycor < 0 and pycor > -12][                     
      sprout 1[                                                                    
      set color grey                                                              
      set size 1       
      set shape "person"
      set target patches with [pxcor = 3 and pycor = -3]
      
  ]]
  
  ;; passengers below
  ask n-of Passengers_3 patches with [pcolor = white and pycor < -12][                     
      sprout 1[                                                                    
      set color grey                                                              
      set size 1       
      set shape "person"
      set target patches with [pxcor = 3 and pycor = -12] 
      
  ]]
  
  
   ;; 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 middle
    create-bus Bus_1[
    set color red
    set size 5
    set xcor 3
    set ycor -3
    set shape "bus"
    set heading 90

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

]
    reset-ticks

end

to check-in
  ask turtles with [pycor < 0 and pycor > -12 ] [                                   ;; middle
    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]
  ]
  
  ask turtles with [ycor < -12 ] [                                                  ;; below
    move-to one-of patches with [pxcor = 3 and pycor = -12]
    if any? neighbors with [pxcor = 3 and pycor = -13] and shape != "bus"            ;; if passenger neighbors this patch, it dies
    [
      die]
  ]
    tick
end


to drive
  set time ticks
 if time > 0 [
    ask turtles  [
    forward 33  
  ]]
end



to go
  check-in
  drive
  tick
end

    

【问题讨论】:

    标签: model netlogo


    【解决方案1】:

    它仍然发生在同一个滴答声中,因此几乎是同时发生的,因为它们对彼此没有任何影响。 “一起离开”在这里的唯一效果是视觉效果。如果您真的希望它们一起移动,您应该使用链接将它们组合在一起,然后使用tie 以便它们的移动被链接,或者创建一个循环,它们都连续采取非常小的步骤。

    还有一些说明:

    • 顺便问一下time 参数的用途是什么?为什么不直接使用ticks

    • 你打电话给tick 两次。一次在go 和一次在drive

    • if 的用法在您的 check-in 过程中仍然感觉很奇怪,特别是因为您将它与 move-to 结合使用。

    • neighbor 的用法也很奇怪。为什么不用patch-here 构造呢?

    • one-of patches with [pxcor = 3 and pycor = 8] 这样的结构可以更方便地重写为patch 3 8

    • 我仍然建议为您的乘客制作一个单独的品种。

    • 公共汽车品种的命名仍然感觉很不一样,因为您使用单数公共汽车作为复数

    (我故意不给出任何实际代码作为答案,因为这一系列帖子感觉非常像一个学校项目)

    【讨论】:

    • 非常感谢您详尽的回答。 - 如果我使用 'ticks' 而不是 'time' 代码不知何故不起作用。它可能是额外的,但至少它是这样工作的。 - 同样在尝试 patch-here 命令时,代码不起作用。我的代码对你来说可能看起来不太合乎逻辑,但我也真的没有这方面的经验。而且我觉得很难理解。因此,我还写出了整个“pxcor = 3”,这让我更清楚。
    • 关于所有事情都发生在同一个滴答声中,您确实是对的。但是,如果我非常缓慢地播放代码,您会看到总线之间的差异,这是一个问题。他们必须同时制作,即使在缓慢播放代码时也是如此。我试图让它与“领带”一起工作,但我一直在失败。这确实是一个学校项目,我真的花了很多时间。但正如我所说,我觉得这很难。你愿意帮助我吗?正如你所建议的,我在代码中调整了一些东西。
    猜你喜欢
    • 2014-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多