如何让乌龟在海龟从三个方向到达的斑块的交叉点转弯

时间:2015-07-15 10:27:54

标签: netlogo

我创造了人(海龟)随机移动的道路(灰色斑块)。在交叉点(白色斑点)我正在制作它们(海龟)可能会或可能不会转向它们的标题。按照我编码的方式,如果他们的标题是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

1 个答案:

答案 0 :(得分:2)

我以慢速减速并创建了一个人来运行您的代码。我不认为问题是你的随机方向创造 - 速度意味着人在交叉点上花费多个刻度并且仅在生成的随机方向再次向下移动时离开。例如,代理进入标题为0的交叉点并获得随机标题90,因此向右移动一段距离&lt; 1(从速度开始)并且仍然在交叉点,因此得到一个新的随机标题,如90,仍在交叉点,然后在交叉点得到270,然后得到180,所以离开。

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

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])