我是Netlogo的新手。在这里,我正在尝试让红海龟走向高峰。补丁。黄龟不动。我做到了!但是我也想要求红海龟避开那些有黄色或红色海龟的斑块并移动到高浓度的邻居。在我的代码中,我要求他们一旦它们成为一个被占用的补丁旁边就停止因为我不能这样做。我也想避免在任何时候在同一个补丁上获得2只乌龟。有人可以帮我这个吗?
patches-own [conc]
to set-up
clear-all
ask patch random-pxcor random-pycor [
set conc 200
set pcolor scale-color red conc 0 1]
crt 5 [setxy random-xcor random-ycor set shape "circle" set color red]
crt 20 [setxy random-xcor random-ycor set shape "circle" set color yellow]
reset-ticks
end
to go
diffuse conc 0.1
ask patches [set pcolor scale-color red conc 0 1]
ask turtles with [color = red]
[ifelse not any? turtles-on neighbors
[if [conc] of max-one-of neighbors [conc] > conc [
face max-one-of neighbors4 [conc]
move-to max-one-of neighbors4 [conc]]]
[stop]
]
tick
end
答案 0 :(得分:3)
如果您使用let
来避免重复,我认为您的代码会更好一点,如下所示:
let target max-one-of neighbors [conc]
if [conc] of target > conc [
face target
move-to target
]
对于一些不同的可能方法,每个补丁强制执行一只乌龟"规则,请参阅NetLogo模型库的代码示例部分中的One Turtle Per Patch示例模型。
我认为ifelse not any? turtles-on neighbors
是你试图让海龟避免被占用的补丁。但正如你已经写过的那样,它的效果要强于它 - 它使得任何一只带有相邻被占用补丁的乌龟都不会移动。
我认为你的意思可能更像:
ask turtles with [color = red] [
let targets neighbors with [not any? turtles-here]
let target max-one-of targets [conc]
if target != nobody and [conc] of target > conc [
face target
move-to target
]
]
希望这有帮助。