【问题标题】:NetLogo move turtle in labyrinthNetLogo 在迷宫中移动乌龟
【发布时间】:2013-11-04 06:54:33
【问题描述】:

我需要一些关于 NetLogo 编程的帮助。我不得不让机器人在迷宫中行走。机器人只能在黑色斑块上行走(紫色斑块代表障碍物)。有一个绿色补丁​​代表目标或终点线。机器人可以前进、后退、左转和右转,它必须到达目标。

我必须创建一个程序“搜索”,让机器人走到目标,因为我只调用一次该程序。机器人必须环顾四周,并始终朝着有更多空间的方向前进。如果在他周围的每个方向都有相同数量的空闲补丁,机器人必须随机选择它将去目标的方向。当涉及到目标时,它必须停止。

我创建了三个程序(check-forward、check-left 和 check-right)来为我提供有关免费补丁数量和程序 check-target 的信息,以检查代理何时到达目标。我进行了“搜索”程序,但它有时有效,有时无效。我找不到问题所在。请告诉我我做错了什么?!

这是图片: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

【问题讨论】:

    标签: netlogo


    【解决方案1】:

    这不可能是您的实际代码,因为 NetLogo 不允许您将程序命名为 forwardleftright

    你不需要写[pcolor = green] of patch-here,你可以写pcolor = green。海龟可以直接访问它所在的补丁的变量。

    仅通过阅读代码中的问题所在,我并不清楚问题在哪里,所以除了提供标准的调试建议之外我不知道该说什么:

    • 您说代码有时有效,有时无效。什么情况下有效,什么情况下无效?如果你能在那里找到一种模式,那将为你提供线索。

    • 此代码的最后一个有效版本是什么?回到那个版本再试一次,但这一次,不要一次添加这么多新代码。只需添加一点新代码,并在添加更多代码之前确保它按预期工作。回想一下我对你上一个问题的回答:先尝试解决一个更简单的问题,让它发挥作用,然后稍微改进一下,等等。

    • 尝试在您的代码中添加一些print 语句,以便代码在运行时“对话”并向您展示它在做什么。这可能是程序员用来帮助他们发现代码中的问题的第一技术。

    【讨论】:

      【解决方案2】:

      Seth 在您之前提出的另一个问题上回答了您的问题:

      NetLogo turtles in labyrinth

      您可能需要订阅您提出的问题,以便在回答时获得更新:)

      【讨论】: