我需要一些关于 NetLogo 编程的帮助。我不得不让机器人在迷宫中行走。机器人只能在黑色斑块上行走(紫色斑块代表障碍物)。有一个绿色补丁代表目标或终点线。机器人可以前进、后退、左转和右转,它必须到达目标。
我必须进行一个程序“搜索”,这将使机器人走到目标,因为我只调用一次该程序。机器人必须环顾四周,并始终朝着有更多空间的方向前进。如果在他周围的每个方向上都有相同数量的空闲补丁,机器人必须随机选择它要去目标的方向。当涉及到目标时,它必须停止。
我制作了三个程序(check-forward、check-left 和 check-right)来为我提供有关免费补丁数量和程序检查目标的信息,以检查代理何时到达目标。我做了程序“搜索”,但它有时有效,有时无效..我找不到问题所在。请告诉我我做错了什么?!
这是图片:http: //i.imgur.com/LPU2dmN.jpg
这是我的代码:
breed [agents agent]
agents-own[
target // finish
num_forward //number of free patches forward
num_right //number of free patches right
num_left //number of free patches left
chance] //number of directions where there is the same number of free patches
(pick one of them randomly)
to check-target
ask agent 0[ifelse [pcolor = green] of patch-here
[set target true]
[set target false]]
end
to check-forward
ask agent 0 [ifelse [pcolor] of patch-ahead 1 = black or [pcolor] of patch-ahead 1 =
green
[ifelse [pcolor] of patch-ahead 2 = black or [pcolor] of patch-ahead 2 = green
[set num_forward 2]
[set num_forward 1]]
[set num_forward 0]]
end
to check-left
ask agent 0 [ifelse [pcolor] of patch-left-and-ahead 90 1 = black or [pcolor] of patch
left-and-ahead 90 1 = green
[ifelse [pcolor] of patch-left-and-ahead 90 2 = black or [pcolor] of patch-
left-and-ahead 90 2 = green
[set num_left 2]
[set num_left 1]]
[set num_left 0] ]
end
to check-right
ask agent 0 [ifelse [pcolor] of patch-right-and-ahead 90 1 = black or [pcolor] of
patch-right-and-ahead 90 1 = green
[ifelse [pcolor] of patch-right-and-ahead 90 2 = black or [pcolor] of patch-right-
and-ahead 90 2 = green
[set num_right 2]
[set num_right 1]]
[set num_right 0]]
end
to search
ask agent 0[
while [target = false][
if((num_forward = 2 and num_right = 2 and num_left = 2) or (num_forward = 1 and
num_right = 1 and num_left = 1))
[set chance random 3
if chance = 0 [forward] //procedure 'forward' moves by one patch forward
if chance = 1 [right] //procedure 'right' rotates 90° right and moves forward
if chance = 2 [left]] //procedure 'left' rotates 90° left and moves forward
if(num_forward > num_left and num_right > num_left and num_forward = 2 and num_right =
2) or (num_forward > num_left and num_right > num_left and num_forward = 1 and
num_right = 1)
[set chance random 2
ifelse chance = 0 [forward][right]]
if(num_forward > num_right and num_left > num_right and num_forward = 2 and num_left =
2) or (num_forward > num_right and num_left > num_right and num_forward = 1 and
num_left = 1)
[set chance random 2
ifelse chance = 0 [forward][left]]
if(num_right > num_forward and num_left > num_forward and num_right = 2 and num_left =
2) or (num_right > num_forward and num_left > num_forward and num_right = 1 and
num_left = 1)
[set chance random 2
ifelse chance = 0 [right][left]]
if(num_forward > num_right and num_forward > num_left)[forward]
if(num_right > num_left and num_right > num_forward)[right]
if(num_left > num_forward and num_left > num_right)[left]
if(num_forward = 0 and num_right = 0 and num_left = 0)[backward] //procedure
'backward' moves by
one patch back
check-target]]
end