Netlogo - 有序运动
Netlogo - Ordered Movement
我在 Netlogo 中遇到以下错误,我不确定原因。我试图让海龟以有序的方式四处移动,但是当我改变 d 角度时我总是收到错误:"FACE expected input to be an agent but got NOBODY instead"。
任何帮助将不胜感激。
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
你可以揭示问题,但是 运行 这个测试:
to test
ca
crt 1
let x -10E307 * 10
show x
ask turtle 0 [rt x]
inspect turtle 0
end
你会看到 heading
是 NaN
因为你给了它一个 -Infinity 的转弯。现在如果你移动乌龟,xcor
和 ycor
将变成 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
我在 Netlogo 中遇到以下错误,我不确定原因。我试图让海龟以有序的方式四处移动,但是当我改变 d 角度时我总是收到错误:"FACE expected input to be an agent but got NOBODY instead"。 任何帮助将不胜感激。
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
你可以揭示问题,但是 运行 这个测试:
to test
ca
crt 1
let x -10E307 * 10
show x
ask turtle 0 [rt x]
inspect turtle 0
end
你会看到 heading
是 NaN
因为你给了它一个 -Infinity 的转弯。现在如果你移动乌龟,xcor
和 ycor
将变成 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