我怎样才能告诉海龟避免踩到某些斑块?

How can I tell turtles to avoid stepping on certain patches?

我目前正在模拟火灾疏散模型,我试图告诉海龟在不踩到黑色补丁的情况下进入出口,但我的代码无法正常工作。

这是我一直用来解决这个问题的代码: 去

 ask people
  [face one-of patches with [pycor <= -24  and pycor >= -30  and 
  pxcor <= 26  and pxcor >= 20 ]
  fd 0.5 ]

 ask people with [ points = 1 ] [ set color pink ]
 ask people with [ points = 0 ] [ set color red ]
 ask people with [ color = red ] [ fd 0 ]
tick
end

to avoid-black

 ask people [
 if [pcolor] of patch-ahead 0.5 = grey - 3
 [ set heading ( - heading ) fd 0.5 ]]
 end

Ave555,您好!您来对地方了!

您的代码存在几个不同的问题。

  • black 与 "grey - 2" 不同,因此您对 black 的测试可能会失败,具体取决于 关于如何在墙上设置补丁颜色。

  • 命令 "avoid-black" 从未在您的 "go" 步骤中调用,因此它没有任何效果

  • 我猜你把屏幕设置得比默认的大了 坐标在 -16 和 16 之间,否则您对 32 左右的补丁的测试将失败。

  • 您可能还没有忘记要求视口不要环绕。 你必须设置它,否则你会得到非常奇怪的行为。 (试一试)

  • 顺便说一下,将航向设置为-heading 不是您想要的。航向是 一个角度。您想从中减去 180 以反转方向。或加180.

  • 试试下面的代码。逐步完成 如所写,一次一步,您可以看到它大部分都有效。检测到黑色 人旋转了180度,跳了5步!到目前为止,还不错。

  • 但是,他们只是转身回到墙上,因为什么都没有 他们不会。

下面的代码非常冗长。 运行 一次一步,观察输出。它告诉您避免黑色决策代码正在查看什么。

希望对您有所帮助!


;; This code is incomplete but should make it easier to see why the people are not going where you expect

breed [people person]
people-own [ points]

to setup
  clear-all
  ask patches [ set pcolor white]
  ask patches with [pycor < -5] [ set pcolor black ]

  create-people 1 [ setxy random-pxcor 5 set points random 2 set color green set size 3 ]
  reset-ticks
end

to go
  let chosen-patch one-of patches with [pycor <= -12  and pycor >= -15  and 
    pxcor <= 15  and pxcor >= 10 ]


  ask chosen-patch [ set pcolor red]

  ask people
    [
          face chosen-patch
      fd 0.5 ]

 ask people with [ points = 1 ] [ set color pink ]
 ask people with [ points = 0 ] [ set color red ]
 ask people with [ color = red ] [ fd 0 ]

  avoid-black

  tick
end

to avoid-black
  ;; note:    grey -3   is 2.   black is zero. 
  ;;let wall-color grey - 3
  let wall-color black

 ask people [
    print " "
   type ticks type ", pcolor of patch-ahead is " type [pcolor] of patch-ahead 0.5 type ", and wall-color is " print wall-color
 if-else ([pcolor] of patch-ahead 0.5 = wall-color)
 [    print "test succeded, wall ahead, reverse and jump"
      set heading (  heading - 180 ) fd 5]
  [ print "test failed, no wall ahead. Keep going." ]
  ]
 end