【问题标题】:How to ask turtles to turn at the intersection point of patches where turtles are arriving from three direction如何让海龟在海龟从三个方向到达的斑块的交点处转弯
【发布时间】:2015-07-20 19:02:45
【问题描述】:

我已经创建了人们(乌龟)随机移动的道路(灰色区域)。在交叉点(白色斑块),我让它们(海龟)可能会或可能不会转向他们的航向。以我编码的方式,如果他们的标题是 90 和 270,它工作正常。但是,如果航向为 0,则几乎 95% 的时间他们会返回 (180) 而不是向左 (270) 或向右 (90)。代码中没有错误。我只是想知道,我们能否以其他方式编码,使 0 航向的海龟也可以像其他两个航向一样在所有三个转弯中都有 50% 的机会。

以下是我的代码:

breed [people person]
globals [ streets intersections head]
people-own [ speed direction]
patches-own [ intersection? ]

to setup
   clear-all
   ask patches [set pcolor green
                set intersection? false]
   setup-street
   setup-people
end

to setup-street
  ;set streets patches with [ (( pxcor < max-pxcor and pxcor > min-pxcor ) ;and (pycor = 4) or (pxcor = 0 and ( pycor > min-pycor and pycor < 4))]

  set streets patches with [ (( pxcor < max-pxcor and pxcor > min-pxcor ) and (pycor = 4 or pycor = 5)) or ((pxcor = 0 or pxcor = 1) and ( pycor > min-pycor and pycor < 4))]

  set intersections patches with [ (pxcor = 0 or pxcor = 1) and (pycor = 4 or pycor = 5)]

   ask streets [set pcolor gray]
   ask intersections [ set pcolor white]
   setup-intersection
end

to setup-intersection
     ask intersections [ set intersection? true]
end

to setup-people
   ask n-of No-of-person patches with [ pcolor = gray or pcolor = white] [
     sprout-people 1 [
           set color brown
           set size 1
           set shape "person"
           set speed 0.01 + random-float 0.9
           set direction random 2
           set-default-headings direction
      ]
   ]
end

to set-default-headings [ direct]
   if direct = 0 [
     ;ask people with [ ( pxcor < max-pxcor and pxcor > min-pxcor ) and  pycor = 4 ]
     ask people with [ (pxcor < max-pxcor and pxcor > min-pxcor ) and (pycor  = 4 or pycor = 5)]
      [ set head random 2
          if head = 0 [ set heading 90]
          if head = 1 [ set heading 270]
      ]
    ]

   if direct = 1 [
   ;ask people with [ pxcor = 0 and ( pycor > min-pycor and pycor < 4) ]
     ask people with [(pxcor = 0 or pxcor = 1) and ( pycor > min-pycor and pycor < 4)]
      [ set head random 2
          if head = 0 [ set heading 0]
          if head = 1 [ set heading 180]
      ]
   ]
 end

 to go
    ask people [ fd speed 
                 check-intersection
                 check-end]
 end

 to check-intersection
      if intersection? [
           if heading = 90 [if random 20 < 10 [ set heading 180] ]
           if heading = 270 [if random 20 < 10 [ set heading 180] ]
           if heading = 0 [ let new-heading random 2
                         if (new-heading = 0) [rt 90 ]
                         if (new-heading = 1) [lt 90 ] ]
           ]
  end

  to check-end
      ask people [ if [pcolor] of patch-ahead 1 = green [set heading  [heading] of self - 180 ] ]
  end

【问题讨论】:

  • 请提出一些想法。我会很感激的。

标签: netlogo


【解决方案1】:

我以减慢速度运行您的代码并创建了一个人。我认为问题不在于您的随机方向创建-速度意味着该人在交叉路口花费了多个刻度,并且仅在生成的随机方向再次将它们向下移动时才离开。例如,代理以 0 的航向进入交叉路口并获得随机航向 90,因此向右移动一段距离

解决此问题的一种方法是创建一个计数器/标志。到达交叉路口时设置,如果标志仍然设置,则不检查交叉路口。

to go
  ask people [ fd speed
               set just-turned max (list 0 just-turned - 1) 
               check-intersection
               check-end]
end

to check-intersection
  if intersection? and just-turned = 0 [
       set just-turned 1 / speed + 1
       if heading = 90 [if random 20 < 10 [ set heading 180] ]
       if heading = 270 [if random 20 < 10 [ set heading 180] ]
       if heading = 0 [ let new-heading random 2
                     if (new-heading = 0) [rt 90 ]
                     if (new-heading = 1) [lt 90 ] ]
       ]
end

顺便说一句,当我进行此测试时,我还注意到在您的 T 形街道底部创建的至少一些(可能是所有)代理的标题为 45。

另外,一些代码建议,而不是像

set head random 2
  if head = 0 [ set heading 0]
  if head = 1 [ set heading 180]

1/ 你可以使用(所以它不会检查每个 if 语句)

set head random 2
  ifelse head = 0 [ set heading 0] [ set heading 180]

2/ 或(因此您不必为随机数创建局部变量)

set heading ifelse-value (random 2 = 0) [0] [180]

3/ 或(因此您可以针对不同的情况使用不同的命令)

ifelse random 2 = 0 [ set heading 0] [ set heading 180]

4/或(因为本例中存在数学关系)

set heading random 2 * 180

在这种情况下,数字 4 是最好的,但数字 3 也适用于您有不同命令的部分(如 [lt 90][rt 90])

【讨论】:

  • 嗨 JenB 我明白你的意思并感谢您提供的代码建议,但他们是否可以解决我的问题。如果请帮助我。
  • 当然,这是一个逻辑问题,但不是编码问题。你可以通过让代理在它离开交叉路口时移动一个完整的补丁距离来解决它,但我认为这可能与你想要的速度相反。我会修改我的答案以做出更好的解决方案。
  • 我很抱歉地说,但我不明白逻辑。变量 Just-turned 需要初始化吗?在刚刚转弯的值和 0 最大值之间将始终是刚刚转弯的,因此在交叉路口永远不会出现条件。虽然我尝试过没有任何初始化并且它运行得很好,但我真的不知道它是如何工作的。你能不能让我明白。谢谢。
  • 是的,它应该被初始化(为 0,当海龟被创建时),但我很懒。这无论如何都会起作用,因为 NetLogo 会自动初始化为 0,但是即使您想要值 0,良好的做法也会对初始化进行显式编码。max 用于处理 1/speed 不是整数的情况 - 随着您减少乘以 1,则非整数将永远不会达到 0(例如,它将是 0.5,然后是 -0.5,然后是 -1.5 等)。
  • 假设速度为 0.2,那么 1/speed + 1 将是 6。对于那只海龟,完全穿过交叉点需要 5 个刻度(因为交叉点的宽度为 1 并且它移动0.2 每滴答声)。但是,由于有计数器,一旦它进入交叉路口(并设置了计数器),它不会检查它是否在交叉路口持续 6 个刻度,到那时它已经通过了。
猜你喜欢
  • 2018-05-30
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多