迷宫中的NetLogo乌龟 [英] NetLogo turtles in labyrinth

查看:171
本文介绍了迷宫中的NetLogo乌龟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的是NetLogo编程的新手,我需要帮助.这只是我的第二个任务,我做了大部分.我不得不让机器人在迷宫中行走.机器人只能在黑色补丁上行走(紫色补丁代表障碍物).机器人可以向前,向后,向左和向右前进,并且它必须到达目标.当谈到目标时,它必须停止. 在作业的第一部分中,我必须进行迷宫"程序,该程序将挑选15个随机色块并将其涂成紫色(紫色代表障碍物),包括一个代表目标的绿色色块.每次我调用该程序时,都会给我不同的迷宫.我在两件事上需要帮助:

I'm really new at programming in NetLogo and i need help. This is just my second assignment and i did most of it. I had to make robot walk in labyrinth. Robot can walk only on a black patches (violet patches represent the obstacles). Robot can go forward, back, left and right and it must go to the target. When it comes to the target it must stop. At the first part of the assignment i had to make procedure 'labyrinth' that will pick 15 random patches and paint them in violet (violet represents the obstacles) including one green patch that represents the target. Every time i call that procedure it gives me different labyrinth. I need help with two things:

  • 我必须执行一个新程序,该程序将始终为我提供相同的迷宫(总是用紫罗兰色绘制的相同的15个随机色块)

  • i have to make a new procedure that will always give me the same labyrinth (always the same 15 random patches painted in violet)

我必须进行一个新过程搜索",这将使机器人步行到目标,因为我只调用一次该过程.机器人必须环顾四周,并始终将其指向有更多空间的方向.如果在他周围的每个方向上都有相同数量的免费补丁,则机器人必须随机选择它将到达目标的方向.当谈到目标时,它必须停止.

i have to make a new procedure 'search' that will make robot walk to the target as i call only once that procedure. Robot must look around and always be directed in the direction where there is more space. If there is in every direction around him the same number of free patches, robot must randomly pick the direction in which it will go to the target. When it comes to the target, it must stop.

这是我的代码:

breed [robots robot]
robots-own [
target
zforward
zright
zleft]

to paint-walls  
ask patches with
[(pxcor = max-pxcor) or (pxcor = min-pxcor) or (pycor = max-pycor) or (pycor =  
min-pycor)]
[ set pcolor violet]
end

to labyrinth
ask n-of 15 patches with[ pcolor != violet ] [set pcolor violet]
ask n-of 1 patches with [pcolor != violet ] [ set pcolor green]
end

to create-agent 
set-default-shape robots "robot" 
ask patch 5 5 [ sprout-robots 1 ] 
ask robots [          
set heading 0
set color grey
set target false] 
ask robots [ask patch-here [set pcolor black ] ]        
end

to setup
clear-all
paint-walls
labyrinth
create-agent
end

to forward
ask robot 0 [if [pcolor] of patch-ahead 1 = black or [pcolor] of patch-ahead 1 = green 
[fd 1]]
check
end

to backward
ask robot 0 [if [pcolor] of patch-ahead -1 = black or [pcolor] of patch-ahead 1 = 
green[back 1 ]]
check
end

to rot-right
ask robot 0 [right 90 ]
end

to rot-left
ask robot 0 [left 90 ]
end

to right
ask robot 0[rot-right
if [pcolor] of patch-ahead 1 = black  or [pcolor] of patch-ahead 1 = green[
forward]] 
check
end

to left
ask robot 0 [rot-left
if [pcolor] of patch-ahead 1 = black or [pcolor] of patch-ahead 1 = green[
forward]]
check
end

to check-target
ask robot 0[ifelse [pcolor = green ] of patch-here
[set target true]
[set target false]]
end

to check-forward
ask robot 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 zforward 2]
[set zforward 1]]
[set zforward 0]]
end

to check-right
ask robot 0[ifelse [pcolor = black] of patch-right-and-ahead 90 1[
ifelse [pcolor = black] of patch-right-and-ahead 90 2 
[set zright 2]
[set  zright 1]]
[set  zright 0]]
end

to check-left
ask robot 0[ifelse [pcolor = black] of patch-left-and-ahead 90 1[
ifelse [pcolor = black] of patch-left-and-ahead 90 2 
[set zleft 2]
[set  zleft 1]]
[set  zleft 0]]
end

to check
check-forward
check-right
check-left
check-target
end

推荐答案

我可以回答有关始终获得相同随机迷宫的部分;您需要random-seed原语 http://ccl.northwestern .edu/netlogo/5.0/docs/dictionary.html#random-seed .另请参见模型库的代码示例"部分中的随机种子示例".

I can answer the part about always getting the same random labyrinth; you want the random-seed primitive, http://ccl.northwestern.edu/netlogo/5.0/docs/dictionary.html#random-seed . See also Random Seed Example, in the Code Examples section of the Models Library.

至于其余的,为什么不刺它或它的一些非常简化的版本,如果您卡在特定的东西上,请发表另一个问题?

As for the rest, why not take a stab at it, or some very simplified version of it, and if you get stuck on something specific, post another question?

我之所以说它的非常简化的版本",是因为试图立即解决一个大问题通常不是程序员编写程序的方式.相反,最好是将您可以 解决的问题缩小版,并获得可行的解决方案.然后对其进行一些改进,使其更像是更大的问题,然后使其再次起作用.依此类推,越来越接近整个问题的解决方案.

I say "very simplified version of it" because trying to solve an entire big problem at once isn't usually how programmers program things. Instead, it's best to make a smaller version of the problem that you can solve, and get a solution working. Then improve it a little, to make it more like the bigger problem, and get that working again. And so on, getting closer and closer to a solution to the whole thing.

例如,您可能只是从机器人必须环顾四周,并始终朝着有更多空间的方向"部分开始.看来您已经有了一些代码,即check过程.你测试过了吗?它能正常工作吗?你怎么知道的?在继续进行其他部分之前,您应该确信自己已经掌握了足够的权利.

For example you might just start with the "Robot must look around and always be directed in the direction where there is more space" part. It looks like you already have some code for that, namely, your check procedure. Have you tested it? Does it work correctly? How do you know? You should be confident you've got that much right before moving on to the other parts.

这篇关于迷宫中的NetLogo乌龟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆