在我的模型中,我有海龟从地图的右侧移动到左侧。当他们穿越时,他们正在寻找绿色斑块。当他们找到一个并且在他们的视锥中时,他们转向并朝向它。如果有多个等距离的补丁,他们会随机选择要去哪个补丁。然而,当它们移动时,它们的运动中似乎存在许多不必要的抖动。有谁能说出原因?看看这张照片http://imgur.com/qOftVPJ。他们应该直奔他们看到绿色。
to move-bug
ask bugs [
count-steps
if pxcor = min-pxcor [
file-open data-filename
file-type data-filename
file-type " "
file-type data-header
file-write vision-width
file-write vision-distance
file-write greenroof-percent
file-write gray-steps
file-write green-steps
file-write steps
file-type "\n"
file-close
]
if pxcor = min-pxcor [die]
set heading 270
pen-down
let green_target nobody
let perceived_patches patches in-cone vision-distance vision-width
set green_target perceived_patches with [ pcolor = green ]
ifelse count green_target > 0 [face min-one-of green_target [vision- distance]][face min-one-of perceived_patches [vision-distance]] ;; added equivalent jitter to non-green squares
前进1 ]
end
答案 0 :(得分:1)
每次错误移动时,它都会运行ifelse count green_target
块。 Ifelse
将运行您给出的两个命令块中的一个,因此每次执行此过程时,错误面向绿色目标补丁(如果绿色补丁在其视锥内)或感知补丁(如果< strong>没有绿色补丁对于bug是可见的)。因此,如果在move-bug
之后调用移动错误的过程,则错误将因其标题发生变化而抖动,因为在实际移动之前运行了face
行。你真正想要的是如果看到绿色的那个bug只面对一个补丁,所以试着使用if语句而不是ifelse。此外,您正在使用
min-one-of green_target [vision-distance]
但你可能想要
min-one-of green_target [distance myself]
选择最近的绿色补丁。我得到了下面的代码为我工作,但需要注意的是,我只是在我的字段上随机放置了绿色圆圈(documentation)。
set heading 270
pd ;; shorthand for pen-down
let green_target nobody
let perceived_patches patches in-cone vision-distance vision-width
set green_target perceived_patches with [ pcolor = green ]
print green_target
if count green_target > 0 [
face min-one-of green_target [ distance myself ]
]
fd 1