NetLogo turtles in labyrinth

2019-07-27 20:32发布

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:

  • 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.

Here is my code:

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

标签: netlogo
1条回答
小情绪 Triste *
2楼-- · 2019-07-27 21:08

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.

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.

查看更多
登录 后发表回答