首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何提高某些海龟的速度(Netlogo)?

如何提高某些海龟的速度(Netlogo)?
EN

Stack Overflow用户
提问于 2022-10-13 13:16:03
回答 1查看 52关注 0票数 0

我创建了一个模型,表示两个公交站点。这时海龟们上了公共汽车,然后公共汽车就开走了。然而,我希望海龟的登船速度是不同的。换句话说,我希望上站的海龟比下站的海龟移动得更快(或者底部的海龟比上面的海龟慢)。我不知道怎么做..。你们有什么建议吗?我的密码在下面。提前感谢!代码:

代码语言:javascript
运行
复制
turtles-own [speed]
breed [busses bus]
globals [time] 

to setup

clear-all
  
  ;; above
  ask patches with [pxcor = 2 and pycor = 5][set pcolor white]
  ask patches with [pxcor = 2 and pycor = 4][set pcolor white]
  ask patches with [pxcor = 0 and pycor = 4][set pcolor white]
  ask patches with [pxcor = 0 and pycor = 5][set pcolor white]
  ask patches with [pxcor = 1 and pycor = 4][set pcolor white]
  ask patches with [pxcor = 1 and pycor = 5][set pcolor white]
  ask patches with [pxcor = 3 and pycor = 4][set pcolor white]
  ask patches with [pxcor = 3 and pycor = 5][set pcolor white]
  ask patches with [pxcor = 4 and pycor = 4][set pcolor white]
  ask patches with [pxcor = 4 and pycor = 5][set pcolor white]
  ask patches with [pxcor = 5 and pycor = 4][set pcolor white]
  ask patches with [pxcor = 5 and pycor = 5][set pcolor white]
  ask patches with [pycor = 6][
    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
  ]
    ;; bus above 
    create-busses 1[
    set color red
    set size 7
    set xcor 3
    set ycor 8
    set shape "bus"
    set heading 90
  ]
  
    ;; bus middle
    create-busses 1[
    set color red
    set size 7
    set xcor 3
    set ycor -2
    set shape "bus"
    set heading 90

]

     ;; bus below
  create-busses 1[
    set color red
    set size 7
    set xcor 3
    set ycor -11
    set shape "bus"
    set heading 90
    

] 
  
  ;; passengers above
      create-turtles Nr_Passengers_1[                        ;; slider
      setxy one-of (range 0 6) one-of (range 4 6)                                                                    
      set color grey                                                              
      set size 1       
      set shape "person"
      set heading towards patch 3 7
      set speed random-float 0.6 + 0.1  
  ]
  
    ;; passengers middle
      create-turtles Nr_Passengers_2[                        ;;slider
      setxy one-of (range 0 6) one-of (range -6 -4)                                                           
      set color blue                                                              
      set size 1       
      set shape "person"
      set heading towards patch 3 -3 
      set speed random-float 0.6 + 0.5
      
  ]
  
    ;; passengers below
      create-turtles Nr_Passengers_3[                        ;; slider
      setxy one-of (range 0 6) one-of (range -15 -13)                                                                     
      set color green                                                              
      set size 1       
      set shape "person"
      set heading towards patch 3 -12
      set speed random-float 0.6 + 0.5 
      
  ]
  
  ask patch 8 10 [set plabel "platform 1"]
  ask patch 4 0 [set plabel "platform 2"]
  ask patch 4 -9 [set plabel "platform 3"]
  
 
  reset-ticks
end



to check-in-1
  ;;above 
  ask turtles[ 
    if shape != "bus"[                                  
      if any? neighbors with [pcolor = grey or pcolor = black] and shape != "bus" [ die]
      ask turtles with [patch-here != patch 3 -12] [fd speed]
        ]
  ]
  
  tick
end



to drive
  
  let SC_3 count turtles with [pycor >= -15 and pycor <= -10 and shape = "people"] 
  if SC_3 <= 0 [
    ask turtles with [pycor = -11 and shape = "bus"][
    forward 33
  ]]
      ask patch 10 -10 [
    set plabel "20 seconds"]

  
  let SC_2 count turtles with [pycor >= -6 and pycor <= -1 and shape = "people"]
  if SC_2 <= 0[
    ask turtles with [pycor = -2 and shape = "bus"][
    forward 33
  ]]
    
  
  let SC_1 count turtles with [pycor >= 4 and pycor <= 8 and shape = "people"] 
  if SC_1 <= 0 [
    ask turtles with [pycor = 8 and shape = "bus"][
     forward 33
  ]
    stop]
  
  
   
  
end



to go
  check-in-1
  drive
  tick 
end 
EN

回答 1

Stack Overflow用户

发布于 2022-10-17 17:05:16

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

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

正如您所看到的,while是有用的,但需要一些额外的思考。相反,go-2将所有内容绑定到滴答键上,并在界面上使用一个永久按钮运行。我建议使用这个结构,但如果你需要乘客在第一个滴答前登机,你必须使用类似我的go-1

代码语言:javascript
运行
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74056423

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档