Netlogo - 有序运动

时间:2016-11-05 17:09:07

标签: netlogo

我在Netlogo中有以下错误,我不确定为什么。我试图让乌龟以有序的方式移动,但是当我改变d角度时我不断收到错误:“FACE预期输入是一个代理但是没有得到NOBODY”。 任何帮助将不胜感激。

globals [Angles]
to setup
  clear-all
  create-turtles amount [ setxy random-xcor random-ycor ]

  ask turtles [
  set Angles (random 360)

]

  reset-ticks
end

to monitor
 show count patches
      show ticks
end

to go

  if (all? patches [pcolor = yellow]) [stop]
  ask turtles [

    face min-one-of patches with [ pcolor = black ] [ distance myself ]
    ;; This line of code tells the turtle to head towards the nearest patch containing the colour of black.
    set Angle  d  Angle * 1 - Angle
    rightt Angle
    forwardd 1




    ifelse show-travel-line? [pen-down][pen-up]
    set color red


    if pcolor = black [
      set pcolor yellow



    ]

  ]

tick
end

1 个答案:

答案 0 :(得分:2)

您可以解决问题但运行此测试:

to test
  ca
  crt 1
  let x -10E307 * 10
  show x
  ask turtle 0 [rt x]
  inspect turtle 0
end

您会看到headingNaN,因为您给了它一个-Infinity。现在,如果您移动乌龟,xcorycor将成为Nan

要避免此问题,您需要限制angle所采用的值。例如,

globals [turn angle]

to setup
  clear-all
  set turn random-float 1
  create-turtles 10 [
    setxy random-xcor random-ycor
    set color red
    pen-down
  ]
  reset-ticks
end

to go
  if (all? patches [pcolor = yellow]) [stop]
  ask turtles [
    part1
    part2
    if pcolor = black [
      set pcolor yellow
    ]
  ]
  tick
end

to part1
    let _patches (patches with [ pcolor = black ])
    face min-one-of _patches [ distance myself ]
end

to part2
    set turn (4 * turn * (1 - turn))
    set angle turn * 360
    rt angle
    fd 1
end