2015-04-17 148 views
2

我有一个搜索功能的海龟搜索'首选补丁'在自己面前的直线设置的距离。我使用循环创建了他们搜索的补丁数组。代码经常卡在循环中(我想!)。我不知道为什么会发生这种情况......我想编码告诉海龟通过补丁搜索一个首选补丁,如果不是在随机补丁上着陆,有70%的几率着陆在其中一个补丁上搜索区域。如果在搜索区域中的任何补丁上都不存在首选补丁。netlogo乌龟搜索功能陷入循环Netlogo

海龟并不总是移动,所以我的代码显然是非常错误的。

let move-distance random 20 
loop [set search-area (patch-set patch-ahead move-distance) 
set move-distance move-distance - 1 
if move-distance = 1 [stop]] 
let preferred-patches search-area with [flight-tendency = 0.05] 
ifelse any? preferred-patches [ 
ifelse random-float 1 < 0.7 [ 
    set target one-of top-patches move-to target] 
[set target one-of other-patch move-to target]] 
[set target one-of other-patch move-to target] 

回答

1

random 20可能返回0或1,然后你做move-distance循环内的第一件事就是从它减去1,是因为它已经低于1

尝试更换move-distance = 1检查将失败move-distance = 1move-distance <= 1,和/或用2 + random 18代替random 20