i need a little help with programming in NetLogo. I had to make robot walk in labyrinth. Robot can walk only on a black patches (violet patches represent the obstacles). There's one green patch that represents the target or finish line. Robot can go forward, back, left and right and it must go to the target.
I had to make a procedure 'search' that will make robot walk to the target as i call only once that procedure. Robot must look around and always go 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.
I made three procedures (check-forward,check-left and check-right) to give me information about number of free patches and procedure check-target to check when the agent comes on the target. I made procedure 'search' but it sometimes works and sometimes doesn't..I can't find where is the problem. Please tell me what am I doing wrong?!
This is the picture: http://i.imgur.com/LPU2dmN.jpg
Here is my code:
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
This can't be your actual code, because NetLogo wouldn't let you name a procedure forward or left or right.
You don't need to write [pcolor = green] of patch-here, you can just write pcolor = green. A turtle has direct access to the variables of the patch it is standing on.
It isn't obvious to me where the problem just from reading it where the problem in your code is, so I don't know what to say other than offer standard debugging advice:
You say the code sometimes works and sometimes doesn't work. Under what circumstances does it work, and what circumstances doesn't it? If you can find a pattern there, that would give you clues to what's going on.
What was the last version of this code that worked? Go back to that version and try again, but this time, don't add so new much code all at once. Just add a little new code, and make sure that it's working as expected before adding any more. Recall my response to your last question: try solving a simpler problem first, get it working, then improve it a little, and so on.
Try adding some print statements to your code so the code will "talk" as it runs and show you what it is doing. This is probably the number one technique that programmers use to help them spot problems in their code.
Your question is answered by Seth at another question that you asked earlier :
NetLogo turtles in labyrinth
you might need to subscribe to questions you ask to get updates when they are answered :)
Related
I am trying to write some code in Netlogo to ask my turtles to go to the second destination after reaching the first destination. Here's my code and hope it will help explain my question.
SETUP:
First, the patch will generate turtles, and I will give each turtle a variable called my-patch:
ask n-of 40 patches with [ pxcor > -23 and pxcor < -5 and pycor < 21 and pycor > -6]
[
set quad-patch 1
sprout 1 [
set origin-turtle 1
set color yellow
set my-patches patch-here
set goal one-of patches with [pxcor = 0 and pycor = 12]
set speed 0.1 + random-float 0.9
set size 0.5
set shape "person"]
]
Secondly I will ask these turtles to move to the road which is outside the building:
set road patches with [
(pxcor > 45 and pycor <= 8 and pycor >= -18)
]
ask turtles [
move-to one-of road with [not any? other turtles]
]
GO:
This is the tricky part. Now I have set up a path to ask my turtles walk along that path and go into the building. So first I set a goal which is like a rest point in the building. All the turtles will head off to that rest point first and then they will start to find their origins (which I saved as a variable as mentioned before called my-patches).
to go
ask turtles with [should-stay = false] [
move
]
tick
end
to move
face best-way-to goal
avoid-walls
ifelse patch-here != goal
[fd 1]
[find-origin]
end
to avoid-walls
if [wall] of patch-ahead 1 = 1
[set heading heading - 180]
end
to find-origin
face my-patches
fd 1
end
to-report best-way-to [ destination ]
; of all the visible route patches, select the ones
; that would take me closer to my destination
let visible-routes patches with [ path = 1 ]
set routes-that-take-me-closer visible-routes with [
distance destination < [ distance destination - 1 ] of myself
]
ifelse any? routes-that-take-me-closer [
; from those route patches, choose the one that is the closest to me
report min-one-of routes-that-take-me-closer [ distance myself ]
] [
; if there are no nearby routes to my destination
report destination
]
end
So my problem is after turtles move along the walking path that I have set, they seem to stop at the rest point instead of moving on to find their origins (my-patch). I know this is because the code I set here:
ifelse patch-here != goal
[fd 1]
[find-origin]
But I am not sure how to ask these turtles go to find their origins next after they already reached at this rest point? Any help is really appreciated. Thanks!
I am trying to create home ranges for 3 types of hosts for a model that aims to simulate the movement of ticks across a landscape based on the presence of three host-types.
Using some code that was described in a previous post I created the following code. When I run the model I eventually get the following error message:
DISTANCE expected input to be an agent but got the number 0 instead.
Any help with the issue is greatly appreciated.
to-go
if ((week-id = 13) or (week-id = 25) or (week-id = 33))
[ask patches
[sprout-mice 2
[set color gray
set shape "mouse side"
set size 0.5
setxy random-xcor random-ycor]
]
]
if week-id = 17[
let total-deer count deer
ask n-of (total-deer / 2) patches
[sprout-deer 1
[set color brown
set shape "deer"
set size 1
setxy random-xcor random-ycor]
]
]
if week-id = 17[
let total-raccoons count raccoons
ask n-of (total-raccoons) patches
[sprout-raccoons 1
[set color black
set shape "wolf 2"
set size 0.5
setxy random-xcor random-ycor]
]
]
mice-mortality
;print (count mice)
if (week-id = 40)
[deer-mortality]
;print(count deer)
if (week-id = 45)
[raccoon-mortality]
print (count raccoons)
ask deer
[deer-move]
ask raccoons
[raccoons-move]
ask mice
[mice-move]
the submodels:
to deer-move
right random 50
left random 50
forward 1
end
to raccoons-move
ifelse distance home-patch > 10
[face home-patch]
[right random 90
left random 90]
forward 1
end
to mice-move
ifelse distance home-patch > 2
[face home-patch]
[right random 90
left random 90]
forward 1
end
I am trying to ask turtles with any heading (random 360) to avoid the patches with red color. But, I observed that if a turtle is asked to move " fd 1 + random-float 2.0" then sometime turtles turns (set heading heading - 180) when there is a red patch ahead and sometimes(even most of the times) do not turn. Also when I ask the turtles to move " fd 1 " or " fd 0.1 + random-float 0.9 " the code works all fine. Hopefully the reason behind is the number of patches I am asking the turtles to move in one step. What will be the next patch for the move "fd 0.1 + random-float 0.9" and how could I make this working with patch-ahead 1. My code and the interface is added.
to setup
clear-all
ask patches [set pcolor green ]
ask patches with [pycor = 3] [set pcolor red]
create-turtles 40
[
set color blue
set xcor random-pxcor
set ycor random-pycor
set heading random 360
set size 1
set speed 1 + random-float 2.0
]
end
to go
ask turtles [
fd speed
avoid-walls
]
end
to avoid-walls
if [pcolor] of patch-ahead 1 = red [set heading heading - 180]
end
Try using in-cone instead of patch-ahead
to avoid-walls
let front-patches patches in-cone 2 75
if pcolor of one-of front-patches = red [set heading heading - 180]
end
I would like to create a "gridded" world of resources, in specific distance from the central patch and keep distances equal between these patches. Due to calculation demand, I prefer not to use turtles to create this patchy world. I expect to create something like this:
Equally, I would like to define distance between patches as a slider tool. I was wandering to use turtle lattice walk and then turn patches to different color, but is there any way how to do that without turtles ? Thanks for any suggestions !
My not totally working exemple:
to setup
clear-all
ask patches [set pcolor green]
foreach [5 10 15] [
repeat 9 [
make-red-patch ?
]
]
reset-ticks
end
to make-red-patch [dist]
crt 1 [
fd dist
rt 90
while [pcolor = red] [
bk dist
rt 90
fd 2 * dist
]
set pcolor red
die
]
end
I am not exactly sure what you need, first you mentioned you don't want to use turtles and in your own answer you have problem with the patch without a turtle.
There might be another way to approach this question:
to setup
clear-all
ask patches with [pxcor mod Grid = 0 and pycor mod Grid = 0] [set pcolor red]
end
And these are examples with different Grid size:
After more detailed search I found my answer here: http://netlogo-users.18673.x6.nabble.com/Setting-up-agents-in-a-grid-formation-td4864083.html
They consider to distribute turtles, not patches and then attribute patches turtles' qualities.
Here is the code:
to setup
clear-all
create-turtles 1
[ let $n 0 ; actual number of turtles on this patch
let $s 0 ; current number of turtles on a side of square pattern - 1
set heading 0
ask patch-here [set pcolor red]
repeat 16 ; number of needed turtles
[ hatch 1 jump Grid ; make one turtle and move
set $n $n + 1 ; increment count of curent side
ask patch-here [set pcolor red]
if $n > $s ; if side finished...
[
right 90 set $n 0 ; turn and reset count
ask patch-here [set pcolor red]
; if headed up or down, increment side count
if heading mod 180 = 0 [ set $s $s + 1
]
]
]
die
]
end
which produce:
I still don't know how to deal with 1 patch without turtle (bottom right corner), but this exemple helped me a lot ! :)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
This model is a system model of centralized coordination of volunteers for the Response Phase of a volcanic eruption. Agent (Volunteers) were placed randomly will try to reach the point of barracks (red colored patches). Each agent (volunteers) has energy capacity will be channelled to the point barracks if a volunteer has been able to achieve. Point barracks have demand could be reduced by the capacity of volunteers if the volunteer is able to reach the point of the refugee camps.
Problems on the program I created is a volunteer movement was not run in accordance with a predetermined color patch and move back and forth. Volunteers can’t choose to turn left or turn right if the color patch in front of it instead of the specified color. Is there a solution for the movement of agents (volunteers) about this model?
Besides how volunteers can measure the shortest distance to the agent could reach the point quickly barracks?
Furthermore, is there any way that volunteers can perform a search where the evacuation point that must be reached in advance if the demand of each point varies barracks or upon priorities?
This is my code:
to go
if ticks = 180 [ stop]
ask Volunteers
[ifelse capacity = 0 [ fd 0 frozen][ move search ]]
;search
;update-demand
tick
display-labels
do-plots
end
to move
ask Volunteers
[
move-to one-of patches in-radius 2 with [pcolor = 0]
ifelse [pcolor] of patches in-radius 2 = 0 [move-to one-of patches in-radius 2 with [pcolor = 0]]
[
ifelse [pcolor] of patch-ahead 1 = 105 [set heading -180 move-to one-of patches in-radius 2 with [pcolor = 0] ]
[
ifelse [pcolor] of patch-ahead 1 = 8 [set heading -180 move-to one-of patches in-radius 2 with [pcolor = 0]]
[
if [pcolor] of patch-ahead 1 = red [move-to one-of patches in-radius 2 with [pcolor = red] fd 0 ]
]
]
]
]
end
to search
if any? turtles-on patches with [ pcolor = red ]
[
ifelse capacity < demand
[
set demand (( 1 + Rate-of-Demand) * (demand - (capacity * (1 + Rate-of-Capacity))))
set capacity 0
]
[set capacity (( 1 + Rate-of-Capacity) * (capacity - demand ))
set demand 0 ]
]
end
to frozen
if capacity = 0
[ fd 0
set waiting-time waiting-time + 1
if waiting-time > 5 [set capacity 1000 / Jumlah-Relawan set waiting-time 0]
]
end
I've just recently finish, not the exact same but mostly the same, this kind of model.
The model I made is about evacuation of the people. and just like you, I am a student in UGM too.
The solution I used to solve the agent's movements is implementing a shortest-path algorithm in the netlogo code.
You could look up, Djikstra Algorithm or other. In my case, I use a*(aStar) algorithm. There are some example of a* implementation in netlogo as well. you could look it up.
I could only help this far, just like Seth Tisue Said, any details answer would take hours to write.
Hope it helps.