I am building an evacuation model for my university lecture hall. The problem I am facing is that the people always walk over the gray patch that is not supposed to be walked over even though I have included the procedure to avoid walls in the code. This is the current code for my evacuation model. Also is it possible for the turtles to stop moving when arrived at the exit which in this case is the green patch?
breed [people person]
to setup
clear-all
reset-ticks
setup-patches
setup-people
end
to setup-people
set-default-shape people "person"
ask n-of n-people (patches with [pcolor = white]) [sprout-people 1]
ask people [set color cyan]
end
to setup-patches
draw-wall
draw-exit
;change the color of the floor for better visibility
ask patches[
if pcolor = black [set pcolor white ]
]
end
to draw-wall
; Make 4 boundary walls
ask patches with [ pycor >= -25 and pycor >= 25][ set pcolor gray ]
ask patches with [ pycor <= -25 and pycor <= 25][ set pcolor gray ]
ask patches with [ pxcor >= -25 and pxcor >= 25][ set pcolor gray ]
ask patches with [ pxcor <= -25 and pxcor <= 25][ set pcolor gray ]
; make rows of walls inside that look like seats in a lecture hall
; left rows of chairs
ask patches with [pxcor <= 21 and pxcor >= 13 and pycor = 20][set pcolor gray]
ask patches with [pxcor <= 21 and pxcor >= 13 and pycor = 18][set pcolor gray]
ask patches with [pxcor <= 21 and pxcor >= 13 and pycor = 16][set pcolor gray]
ask patches with [pxcor <= 21 and pxcor >= 13 and pycor = 14][set pcolor gray]
ask patches with [pxcor <= 21 and pxcor >= 13 and pycor = 12][set pcolor gray]
ask patches with [pxcor <= 21 and pxcor >= 13 and pycor = 10][set pcolor gray]
ask patches with [pxcor <= 21 and pxcor >= 13 and pycor = 8][set pcolor gray]
ask patches with [pxcor <= 21 and pxcor >= 13 and pycor = 6][set pcolor gray]
ask patches with [pxcor <= 21 and pxcor >= 13 and pycor = 4][set pcolor gray]
ask patches with [pxcor <= 21 and pxcor >= 13 and pycor = 2][set pcolor gray]
ask patches with [pxcor <= 21 and pxcor >= 13 and pycor = 0][set pcolor gray]
ask patches with [pxcor <= 21 and pxcor >= 13 and pycor = -2][set pcolor gray]
ask patches with [pxcor <= 21 and pxcor >= 13 and pycor = -4][set pcolor gray]
ask patches with [pxcor <= 21 and pxcor >= 13 and pycor = -6][set pcolor gray]
ask patches with [pxcor <= 21 and pxcor >= 13 and pycor = -8][set pcolor gray]
ask patches with [pxcor <= 21 and pxcor >= 13 and pycor = -10][set pcolor gray]
; middle rows of chairs
ask patches with [pxcor >= -9 and pxcor <= 9 and pycor = 20][set pcolor gray]
ask patches with [pxcor >= -9 and pxcor <= 9 and pycor = 18][set pcolor gray]
ask patches with [pxcor >= -9 and pxcor <= 9 and pycor = 16][set pcolor gray]
ask patches with [pxcor >= -9 and pxcor <= 9 and pycor = 14][set pcolor gray]
ask patches with [pxcor >= -9 and pxcor <= 9 and pycor = 12][set pcolor gray]
ask patches with [pxcor >= -9 and pxcor <= 9 and pycor = 10][set pcolor gray]
ask patches with [pxcor >= -9 and pxcor <= 9 and pycor = 8][set pcolor gray]
ask patches with [pxcor >= -9 and pxcor <= 9 and pycor = 6][set pcolor gray]
ask patches with [pxcor >= -9 and pxcor <= 9 and pycor = 4][set pcolor gray]
ask patches with [pxcor >= -9 and pxcor <= 9 and pycor = 2][set pcolor gray]
ask patches with [pxcor >= -9 and pxcor <= 9 and pycor = 0][set pcolor gray]
ask patches with [pxcor >= -9 and pxcor <= 9 and pycor = -2][set pcolor gray]
ask patches with [pxcor >= -9 and pxcor <= 9 and pycor = -4][set pcolor gray]
ask patches with [pxcor >= -9 and pxcor <= 9 and pycor = -6][set pcolor gray]
ask patches with [pxcor >= -9 and pxcor <= 9 and pycor = -8][set pcolor gray]
ask patches with [pxcor >= -9 and pxcor <= 9 and pycor = -10][set pcolor gray]
; right rows of chairs
ask patches with [pxcor >= -21 and pxcor <= -13 and pycor = 20][set pcolor gray]
ask patches with [pxcor >= -21 and pxcor <= -13 and pycor = 18][set pcolor gray]
ask patches with [pxcor >= -21 and pxcor <= -13 and pycor = 16][set pcolor gray]
ask patches with [pxcor >= -21 and pxcor <= -13 and pycor = 14][set pcolor gray]
ask patches with [pxcor >= -21 and pxcor <= -13 and pycor = 12][set pcolor gray]
ask patches with [pxcor >= -21 and pxcor <= -13 and pycor = 10][set pcolor gray]
ask patches with [pxcor >= -21 and pxcor <= -13 and pycor = 8][set pcolor gray]
ask patches with [pxcor >= -21 and pxcor <= -13 and pycor = 6][set pcolor gray]
ask patches with [pxcor >= -21 and pxcor <= -13 and pycor = 4][set pcolor gray]
ask patches with [pxcor >= -21 and pxcor <= -13 and pycor = 2][set pcolor gray]
ask patches with [pxcor >= -21 and pxcor <= -13 and pycor = 0][set pcolor gray]
ask patches with [pxcor >= -21 and pxcor <= -13 and pycor = -2][set pcolor gray]
ask patches with [pxcor >= -21 and pxcor <= -13 and pycor = -4][set pcolor gray]
ask patches with [pxcor >= -21 and pxcor <= -13 and pycor = -6][set pcolor gray]
ask patches with [pxcor >= -21 and pxcor <= -13 and pycor = -8][set pcolor gray]
ask patches with [pxcor >= -21 and pxcor <= -13 and pycor = -10][set pcolor gray]
end
to draw-exit
; Setting 4 exits assuming all UTAR lecture halls have 4 exits
; two at the top left and right and two at the bottom left and right
ask patches with [pxcor <= 23 and pxcor >= 21 and pycor = 25][set pcolor green]
ask patches with [pxcor >= -23 and pxcor <= -21 and pycor = 25][set pcolor green]
ask patches with [pxcor <= 23 and pxcor >= 21 and pycor = -25][set pcolor green]
ask patches with [pxcor >= -23 and pxcor <= -21 and pycor = -25][set pcolor green]
end
to go
let hall patches with [pycor <= 0 and pycor >= -25 and pxcor <= 0 and pxcor >= -25 ]
ask people[
move-people
avoid-walls
]
tick
end
to move-people
face min-one-of patches with [pcolor = green ] [distance myself ]
fd 0.1
end
to avoid-walls
ifelse [pcolor] of patch-ahead 1 = gray
[lt random-float 360 ] ;; we see a gray patch ahead of us. We turn a random amount
[fd 0.1] ;; otherwise, it is safe to move forward
end
This will be a long answer but I want to guide you step by step.
Let's start from why you are facing an unwanted behaviour.
In go, first you ask people to move (move-people) and only later you ask them to avoid walls (avoid-walls).
Even looking at it more closely, you can see that your avoid-walls procedure is not having any effect on your people's walk. Let's read your code from the point of view of your agents:
I am a person and I enter the move-people procedure, which makes me face the nearest exit. There is a wall in front of me, but nothing in the code is telling me that I should care about it: in fact the next line of code is forward 0.1, so I just move forward.
Now I enter the avoid-walls procedure. As I said, there is a wall in front of me, so I randomly change my heading. Ah, there is no wall in front of me anymore! However I will not move forward in this new direction, because there is no line of code telling me to do it now that I changed my heading.
I exit avoid-walls and this means that I also need to exit the ask people [...] command block in go. I have finished my actions for this iteration.
Here we are in the next iteration. I am now heading towards a patch without walls (because I randomly selected this heading from avoid-walls in the previous iteration)... however, before I can move in this correct direction, I am again in move-people, and I am asked again to change my heading to face the nearest green patch. This means that I am back to the initial situation, when I had a wall in front of me! The next line of code is asking me to move forward 0.1, so I continue moving on the wall...
and on, and on, and on...
So let's see, step by step, how you can solve this.
First of all, let's tidy up a couple of things:
Your people are the agents executing the move-people procedure, so a better name for this procedure would be just move so that you can have ask people [move].
In order to have agents stop moving when they reached the exit, you can simply make sure that only those agents who are not on a green patch will be asked to move: ask people with [pcolor != green] [move] (note that turtles can directly read and modify patches-own variables of the patch they are standing on, this is why we are able to use pcolor in the context of a turtle).
You will also probably want to have your model stop when everyone reached an exit, so you can add a stop condition at the beginning of the go procedure: if (not any? people with [pcolor != green]) [stop].
The result of the three points above will be the following go procedure:
to go
if (not any? people with [pcolor != green]) [stop]
ask people with [pcolor != green] [
move
]
tick
end
Now, back to the content of move.
As you can see, I only have move inside go (i.e. I removed any procedure that is specific to the goal of avoiding walls). This is because, following the journey of a person as we described it above, it is essential that the actions for avoiding walls are not carried out after moving, but upon choosing where to move.
An equally important thing is to make sure that when a person finds a direction where there are no walls, it has to move in that direction before being asked to face the exit again.
Therefore, a first idea could be to do:
to move
face min-one-of patches with [pcolor = green] [distance myself]
while [[pcolor] of patch-ahead 1 = gray] [
right random 360
]
forward 1
end
This way, we make sure that at each iteration of go people will:
Face the nearest exit.
If the patch ahead of them is clear, they will ignore the while loop and jump directly to forward 1.
If the patch ahead of them has a wall, they will enter the loop and only exit it when they found a clear direction (i.e. when the condition of the while loop evaluates as false).
In any case, on each iteration of go every turtle will move exactly by 1 in a direction without walls. On the next iteration of go each turtle will simply start again by facing the nearest exit and testing the wall condition again.
This works, and it is similar to the Look Ahead Example model in the NetLogo library (with the difference that, here, the while loop makes it possible that every turtle moves at every iteration of go, instead of having to spend a full iteration only to change heading if they need to). However, if you try it, you will see that the result is that people who find themselves between the lines of walls will move randomly until they find a way out. This is not very realistic, but we can make it more realistic.
For example, we can make sure that people who find a wall will not move completely randomly to find a way away from the wall, but instead will lean somewhat towards the direction of the nearest exit.
An idea could be to divide the 360° spectrum into four quadrants (0°-89°; 90°-179°; 180°-269°; 270°-359°), so that each person facing a wall can:
Keep note of the quadrant in which the nearest exit is;
If there is a wall in front of them, look for a new direction in the same quadrant.
This way, the wandering of people will be less randomic (i.e. we will see less back and forth, and more going around obstacles).
For this purpose, let's create a new reporter called new-direction. To use this we can create a people-own variable called target (to avoid repeating min-one-of patches with [pcolor = green] [distance myself]):
people-own [
target
]
to move
set target min-one-of patches with [pcolor = green] [distance myself]
face target
while [[pcolor] of patch-ahead 1 = gray] [
set heading new-direction
]
forward 1
end
to-report new-direction
let direction-quadrant ceiling (towards target / 90)
if (direction-quadrant = 0) [
set direction-quadrant 1
]
report random 91 + (90 * (direction-quadrant - 1))
end
You can work out the maths yourself and see that new-direction always reports a new heading that is in the same quadrant (including its borders) as the direction towards the nearest exit. Given that this happens inside the while loop, each agent will then exit the loop only once they found a direction that is more or less towards the nearest exit and that can avoid the wall. Once they found it, they move forward.
However you will also see that sometimes people get stuck in a loop: this happens when they find themselves in a location where another step forward causes the nearest exit to change quadrant (as we defined quadrants from the point of view of turtles), and the subsequent step forward causes the nearest exit to go back to the previous quadrant.
We can solve this by making sure that people remember what was the exact patch they were standing on in the previous step (I call it last-patch in the code below). If such patch is the same as the one they are planning to go to now (checked with (patch-ahead 1 = last-patch)), it means that they are stuck in a loop. In that case, instead of executing forward 1 they execute forward -1 which basically means "go backwards by 1". Considering that at this point forward refers to the heading the turtle has acquired after exiting the while block, this means that in this way the turtle is forced outside the loop:
to move
let last-patch patch-at-heading-and-distance (heading) (-1)
set target min-one-of patches with [pcolor = green] [distance myself]
face target
while [[pcolor] of patch-ahead 1 = gray] [
set heading new-direction
]
ifelse (patch-ahead 1 = last-patch)
[forward -1]
[forward 1]
end
Note that this solution is improvable: it works in your scenario but it may not be sufficient with more complicated designs of the hall and walls.
You run "move" in which the turtles face a direction, then move.
Then you run "avoid" in which the turtles look for gray and turn.
So your turtles have already moved before they look for gray.
My advice: aim, then correct, then move. Note that this very simple navigation rule will not move like a person would move.
to aim
;; face nearest green patch
face min-one-of patches with [pcolor = green ] [distance myself ]
end
to correct
;;if about to step on a gray patch, turn away from it
if ([ pcolor ] of patch-ahead .1 = gray)
[ ;; where is the center of the next patch?
let towards-patch-center towards patch-ahead .1
;; calculate a direction away from the center of the patch
let diff subtract-headings towards-patch-center heading
;; turn left (CCW) or right (CW)
if (diff >= 0 ) ;; it's ahead or clockwise
[ set heading heading - 1 ] ;; turn counter-clockwise
if (diff < 0 ) ;; its counter-clockwise
[ set heading heading + 1 ] ;; turn clockwise
]
end
to walk
jump .1
end
to navigate
aim
correct
walk
end
I modeled an evacuation behavior form a building as in the figure:
globals [
wall
window
door
goal-x goal-y
n
]
patches-own [patch?
walls
windows
doors]
turtles-own [direction
fast?
fear?
other-nearby]
to setup
ca
setup-patches
set-default-shape turtles "person" ;like people
ask n-of number-room1 patches with [pxcor < 71 and pycor < 79 and pxcor >
0 and pycor > 50 and pcolor = black] ; makes turtles sprout on black patches = room1
[sprout 1 [
if [count turtles-here] of neighbors4 = 3 [congest]
set size 10 ;; setting turtle size
facexy 124 79 ; initial direction faceing
set direction 1
set color yellow ;initial color
if xcor mod 2 != 0 [set fast? false]
if xcor mod 2 = 0 [set fast? true]
if turtles != red [set fear? false]
]
]
ask n-of number-room2 patches with [pxcor < 112 and pycor < 79 and pxcor > 71 and pycor > 50 and pcolor = black] ; makes turtles sprout on black patches= room2
[sprout 1 [
if [count turtles-here] of neighbors = 3 [congest]
set size 10 ;; setting turtle size
facexy 124 79 ; initial direction faceing
set direction 1
set color yellow ;initial color
if xcor mod 2 != 0 [set fast? false]
if xcor mod 2 = 0 [set fast? true]
if turtles != red [set fear? false]
]
]
ask n-of number-room3 patches with [pxcor < 82 and pycor < 36 and pxcor > 0 and pycor > 0 and pcolor = black] ; makes turtles sprout on black patches= room3
[sprout 1 [
if [count turtles-here] of neighbors = 3 [congest]
set size 10 ;; setting turtle size
facexy 124 79 ; initial direction faceing
set direction 1
set color yellow ;initial color
if xcor mod 2 != 0 [set fast? false]
if xcor mod 2 = 0 [set fast? true]
if turtles != red [set fear? false]
]
]
ask n-of number-room4 patches with [pcolor = brown] ; makes turtles sprout on black patches= room4
[sprout 1 [
if [count turtles-here] of neighbors = 3 [congest]
set size 10 ;; setting turtle size
facexy 124 79 ; initial direction faceing
set direction 1
set color yellow ;initial color
if xcor mod 2 != 0 [set fast? false]
if xcor mod 2 = 0 [set fast? true]
if turtles != red [set fear? false]
if pxcor <= 120 and pxcor >= 90 and pycor = 12 [walk1]
if pxcor <= 120 and pxcor >= 90 and pycor = 24 [walk1]
if pxcor < 90 and pxcor >= 85 and pycor >= 12 and pycor < 24 [set heading 0]
]
]
fear ;calls function fear
reset-ticks
end
to walk1 ;function earlier called
ifelse ((random 2) = 0); to allow turtles randomly decide if they will go
left or right in rows.
[
go-left
]
[
go-right
]
end
to go-left
set heading 90
end
to go-right
set heading 270
end
to congest
stop
end
to setup-patches
draw-wall;wall, Windows and Doors
draw-exit ; Doors
draw-row ; sitting area
draw-corridor-cafeteria
;Labels
ask patch 41 60[
set plabel-color white
set plabel "Room1 = Office"
]
ask patch 95 60[
set plabel-color white
set plabel "Room2 = Management"
]
ask patch 77 44[
set plabel-color white
set plabel "Corridor"
]
ask patch 41 18[
set plabel-color white
set plabel "Room3 = Meeting Room"
]
ask patch 112 18[
set plabel-color white
set plabel "Room4 = Cafeteria"
]
end
to draw-wall
ask patches with [pxcor <= 120 and pxcor >= 0 and pycor = 79]
[set pcolor blue]
ask patches with [pxcor <= 132 and pxcor >= 129 and pycor = 79]
[set pcolor blue]
ask patches with [pycor <= 79 and pycor >= 0 and pxcor = 132]
[set pcolor blue]
ask patches with [pycor <= 79 and pycor >= 0 and pxcor = 0]
[set pcolor blue]
ask patches with [pxcor <= 34 and pxcor >= 0 and pycor = 0]
[set pcolor blue]
ask patches with [pxcor <= 132 and pxcor >= 43 and pycor = 0]
[set pcolor blue]
ask patches with [pxcor <= 112 and pxcor >= 109 and pycor = 50]
[set pcolor blue]
ask patches with [pxcor <= 50 and pxcor >= 0 and pycor = 50]
[set pcolor blue]
ask patches with [pxcor <= 84 and pxcor >= 79 and pycor = 36]
[set pcolor blue]
ask patches with [pxcor <= 70 and pxcor >= 0 and pycor = 36]
[set pcolor blue]
ask patches with [pxcor <= 132 and pxcor >= 93 and pycor = 36]
[set pcolor blue]
ask patches with [pycor <= 79 and pycor >= 50 and pxcor = 71]
[set pcolor blue]
ask patches with [pycor <= 79 and pycor >= 50 and pxcor = 112]
[set pcolor blue]
ask patches with [pycor <= 36 and pycor >= 0 and pxcor = 82]
[set pcolor blue]
ask patches with [pxcor <= 100 and pxcor >= 59 and pycor = 50]
[set pcolor blue]
end
to draw-exit
;room1
ask patches with [pxcor < 59 and pxcor > 50 and pycor = 50]
[set pcolor green]
;room2
ask patches with [pxcor < 109 and pxcor > 100 and pycor = 50]
[set pcolor green]
;room3
ask patches with [pxcor < 79 and pxcor > 70 and pycor = 36]
[set pcolor green]
;room4
ask patches with [pxcor < 93 and pxcor > 84 and pycor = 36]
[set pcolor green]
;main exit
ask patches with [pxcor < 130 and pxcor > 120 and pycor = 79]
[set pcolor red]
;second exit
ask patches with [pxcor < 43 and pxcor > 34 and pycor = 0]
[set pcolor red]
ask patches with [pxcor < 132 and pxcor > 112 and pycor = 50]
[set pcolor 1]
end
to draw-row
ask patches with [pxcor <= 123 and pxcor >= 93 and pycor = 12]
[set pcolor brown]
ask patches with [pxcor <= 123 and pxcor >= 93 and pycor = 24]
[set pcolor brown]
end
to draw-corridor-cafeteria
ask patches with [pxcor <= 93 and pycor <= 24 and pxcor > 82 and pycor >= 12] [set pcolor 2]
ask patches with [pxcor < 132 and pycor <= 24 and pxcor >= 123 and pycor >= 12] [set pcolor 2]
end
to go
ask turtles with [pcolor != red] [walk] ;asks turtles with patch color not equal to red carry out function walk
spread-fear
tick
end
to spread-fear ; When an agent with fear is near another agent withiut fear, the other has a chance of also getting scared according to slider on interface, spreadfear
ask turtles with [fear? = true and pcolor != red]
[ ask other turtles-here with [fear? = false]
[if (random-float 100) < spreadfear [getscared]]
]
end
to getscared ;changes turtles
set color red
set fear? true
end
to fear
;ask some initialy scared turtles to get scared
ask n-of initially-scared-1 turtles with [size = 10] [getscared]
ask n-of initially-scared-2 turtles with [size = 10] [getscared]
ask n-of initially-scared-3 turtles with [size = 10] [getscared]
ask n-of initially-scared-4 turtles with [size = 10] [getscared]
end
to walk
let room1 patches with [pxcor <= 70 and pycor <= 78 and pxcor >= 1 and pycor >= 51]
let room2 patches with [pxcor < 112 and pycor < 79 and pxcor > 71 and pycor > 50]
let room3_lft patches with [pxcor < 20 and pycor < 36 and pxcor > 0 and pycor > 0]
let room3_rgt patches with [pxcor < 82 and pycor < 36 and pxcor >= 20 and pycor > 0]
let room4 patches with [pxcor < 132 and pycor < 36 and pxcor > 82 and pycor > 24]
let corridor_1 patches with [pxcor <= 70 and pycor <= 50 and pxcor >= 0 and pycor >= 36]
let corridor_2 patches with [pxcor <= 131 and pycor <= 50 and pxcor >= 71 and pycor >= 36]
ask turtles-on room1
[ face one-of patches with [pxcor < 59 and pxcor > 50 and pycor = 51]
]
let exit-room-1 patches with [pxcor < 59 and pxcor > 50 and pycor > 50 and pycor <= 51]
ask turtles-on exit-room-1
[set heading 180]
ask turtles-on room2
[ face one-of patches with [pxcor < 109 and pxcor > 100 and pycor = 51]
]
let exit-room-2 patches with [pxcor < 109 and pxcor > 100 and pycor > 50 and pycor <= 51]
ask turtles-on exit-room-2
[set heading 180]
ask turtles-on room3_rgt
[ face one-of patches with [pxcor < 79 and pxcor > 70 and pycor = 35]
]
ask turtles-on room3_lft
[ face one-of patches with [pxcor = 70 and pycor > 0 and pycor < 36]
]
let exit-room-3 patches with [pxcor < 79 and pxcor > 70 and pycor >= 35 and pycor < 36]
ask turtles-on exit-room-3
[set heading 0]
ask turtles-on room4
[ face one-of patches with [pxcor < 93 and pxcor > 84 and pycor = 35]
]
let exit-room-4 patches with [pxcor < 93 and pxcor > 84 and pycor >= 35 and pycor < 36]
ask turtles-on exit-room-4
[set heading 0]
ask turtles-on corridor_1
[set heading 90]
ask turtles-on corridor_2
[ face one-of patches with [pxcor <= 131 and pxcor >= 113 and pycor = 50]
]
let mainexit patches with [pxcor <= 131 and pycor < 79 and pxcor >= 113 and pycor >= 50]
ask turtles-on mainexit
[ face one-of patches with [pxcor <= 129 and pxcor >= 121 and pycor = 79]
]
let mainexitdoor patches with [pxcor <= 129 and pxcor >= 121 and pycor = 79]
if fear? = true [fd .5]
if fear? = false [fd .8]
ask turtles-on mainexitdoor [evacuate]
end
to evacuate
set color green
if all? turtles [pcolor = green] [ stop ]
end
I can't stop ticks after all agents arrive at main exit door and I can't show evacuated agents (count turtles with [pcolor = green]) in monitor box and number of evacuated in duration in plot space (plot count turtles with [pcolor = green]).
Would you please guide me how to solve these problems?
Thanks in advance,
I am modelling an evacuation model from a simple plan with 4 rooms and one corridor. I have a problem for navigate agents from rooms to corridor and from corridor to main exit. would you please guide me.
The codes that I wrote are there:
globals [
wall
window
door
goal-x goal-y
n
]
patches-own [path?]
turtles-own [direction
fast?
other-nearby]
to setup
clear-all
setup-patches
set-default-shape turtles "person" ;makes them look like people
ask n-of number-room1 patches with [pxcor < 71 and pycor < 79 and pxcor > 0 and pycor > 50 and pcolor = black] ; makes turtles sprout on black patches = room1
[
sprout 1 [
set size 5 ;; setting turtle size
facexy 124 79 ; initial direction faceing
set direction 1
set color yellow ;initial color
]
]
ask n-of number-room2 patches with [pxcor < 112 and pycor < 79 and pxcor > 71 and pycor > 50 and pcolor = black] ; makes turtles sprout on black patches= room2
[
sprout 1 [
set size 5 ;; setting turtle size
facexy 124 79 ; initial direction faceing
set direction 1
set color yellow ;initial color
]
]
ask n-of number-room3 patches with [pxcor < 82 and pycor < 36 and pxcor > 0 and pycor > 0 and pcolor = black] ; makes turtles sprout on black patches= room3
[
sprout 1 [
set size 5 ;; setting turtle size
facexy 124 79 ; initial direction faceing
set direction 1
set color yellow ;initial color
]
]
ask n-of number-room4 patches with [pxcor < 132 and pycor < 36 and pxcor > 82 and pycor > 0 and pcolor = black] ; makes turtles sprout on black patches= room4
[
sprout 1 [
set size 5 ;; setting turtle size
facexy 124 79 ; initial direction faceing
set direction 1
set color yellow ;initial color
]
]
reset-ticks
end
to setup-patches
draw-wall;wall, Windows and Doors
draw-exit
;Labels
ask patch 35 54[
set plabel-color white
set plabel "Room1"
]
ask patch 90 54[
set plabel-color white
set plabel "Room2"
]
ask patch 77 44[
set plabel-color white
set plabel "Corridor"
]
ask patch 41 13[
set plabel-color white
set plabel "Room3"
]
ask patch 105 13[
set plabel-color white
set plabel "Room4"
]
end
to draw-wall
ask patches with [pxcor <= 120.42 and pxcor >= 0 and pycor = 79]
[set pcolor blue]
ask patches with [pxcor <= 132 and pxcor >= 129.57 and pycor = 79]
[set pcolor blue]
ask patches with [pycor <= 79 and pycor >= 0 and pxcor = 132]
[set pcolor blue]
ask patches with [pycor <= 79 and pycor >= 0 and pxcor = 0]
[set pcolor blue]
ask patches with [pxcor <= 34.43 and pxcor >= 0 and pycor = 0]
[set pcolor blue]
ask patches with [pxcor <= 132 and pxcor >= 43.58 and pycor = 0]
[set pcolor blue]
ask patches with [pxcor <= 112 and pxcor >= 108.57 and pycor = 50]
[set pcolor blue]
ask patches with [pxcor <= 49.42 and pxcor >= 0 and pycor = 50]
[set pcolor blue]
ask patches with [pxcor <= 84.425 and pxcor >= 79.575 and pycor = 36]
[set pcolor blue]
ask patches with [pxcor <= 70.425 and pxcor >= 0 and pycor = 36]
[set pcolor blue]
ask patches with [pxcor <= 132 and pxcor >= 93.575 and pycor = 36]
[set pcolor blue]
ask patches with [pycor <= 79 and pycor >= 50 and pxcor = 71]
[set pcolor blue]
ask patches with [pycor <= 79 and pycor >= 50 and pxcor = 112]
[set pcolor blue]
ask patches with [pycor <= 36 and pycor >= 0 and pxcor = 82]
[set pcolor blue]
ask patches with [pxcor <= 99.42 and pxcor >= 58.57 and pycor = 50]
[set pcolor blue]
end
to draw-exit
;room1
ask patches with [pxcor <= 58.57 and pxcor >= 49.42 and pycor = 50]
[set pcolor green]
;room2
ask patches with [pxcor <= 108.57 and pxcor >= 99.42 and pycor = 50]
[set pcolor green]
;room3
ask patches with [pxcor <= 79.575 and pxcor >= 70.425 and pycor = 36]
[set pcolor green]
;room4
ask patches with [pxcor <= 93.575 and pxcor >= 84.425 and pycor = 36]
[set pcolor green]
;main exit
ask patches with [pxcor <= 129.57 and pxcor >= 120.42 and pycor = 79]
[set pcolor red]
;second exit
ask patches with [pxcor <= 43.58 and pxcor >= 34.43 and pycor = 0]
[set pcolor red]
end
to go
ask turtles [walk]
tick
end
to walk
let room1 patches with [pxcor < 71 and pycor < 79 and pxcor > 0 and pycor > 50]
let room2 patches with [pxcor < 112 and pycor < 79 and pxcor > 71 and pycor > 50]
let room3 patches with [pxcor < 82 and pycor < 36 and pxcor > 0 and pycor > 0]
let room4 patches with [pxcor < 132 and pycor < 36 and pxcor > 82 and pycor > 0]
let corridor patches with [pxcor < 132 and pycor < 50 and pxcor > 0 and pycor > 36]
let exitpoint patches with [pxcor < 132 and pycor < 79 and pxcor > 112 and pycor > 50]
ask turtles-on room1
[face one-of patches with [pxcor <= 58.6 and pxcor >= 49.42 and pycor = 50]
fd 0.5
]
ask turtles-on room2
[ face one-of patches with [pxcor <= 108.6 and pxcor >= 99.5 and pycor = 50]
fd 0.5
]
ask turtles-on room3
[ face one-of patches with [pxcor <= 79 and pxcor >= 71 and pycor = 36]
fd 0.5
]
ask turtles-on room4
[ face one-of patches with [pxcor <= 93.6 and pxcor >= 84.5 and pycor = 36]
fd 0.5
]
ask turtles-on corridor
[ face one-of patches with [pxcor <= 132 and pxcor >= 112 and pycor = 50]
fd 1
]
ask turtles-on exitpoint
[ face one-of patches with [pxcor <= 129 and pxcor >= 121 and pycor = 79]
fd 1
]
end
I could navigate agents from rooms to door of each room but I want to enter them in the corridor then from corridor to main exit.
I just modified your code by adding a "doors" variable underneath "walk". This tells the turtles that touch a green patch - your doors - to take two steps forward into the corridor. Once they are in the corridor they already have commands to move toward the exitpoint.
to walk
let room1 patches with [pxcor < 71 and pycor < 79 and pxcor > 0 and pycor > 50]
let room2 patches with [pxcor < 112 and pycor < 79 and pxcor > 71 and pycor > 50]
let room3 patches with [pxcor < 82 and pycor < 36 and pxcor > 0 and pycor > 0]
let room4 patches with [pxcor < 132 and pycor < 36 and pxcor > 82 and pycor > 0]
let corridor patches with [pxcor < 123 and pycor < 50 and pxcor > 0 and pycor > 36]
let exitpoint patches with [pxcor < 130 and pycor < 80 and pxcor > 122 and pycor > 38]
let doors patches with [pcolor = green]
ask turtles-on room1
[face one-of patches with [pxcor <= 58.6 and pxcor >= 49.42 and pycor = 50]
fd 0.5
]
ask turtles-on room2
[ face one-of patches with [pxcor <= 108.6 and pxcor >= 99.5 and pycor = 50]
fd 0.5
]
ask turtles-on room3
[ face one-of patches with [pxcor <= 79 and pxcor >= 71 and pycor = 36]
fd 0.5
]
ask turtles-on room4
[ face one-of patches with [pxcor <= 93.6 and pxcor >= 84.5 and pycor = 36]
fd 0.5
]
ask turtles-on corridor
[ face one-of patches with [pxcor <= 130 and pxcor >= 125 and pycor = 45]
fd 1
]
ask turtles-on exitpoint
[ face one-of patches with [pxcor <= 129 and pxcor >= 121 and pycor = 79]
fd 1
]
ask turtles-on doors
[ face one-of patches with [pxcor = 77 and pycor = 44 ]
fd 2
]
this code contains a road setup and turtle creation , after that on the go procedure that makes turtles take the pink path , and am trying to make turtles change color with they get to a red colored patch , but for unknown reason this code is not working and the cars never change their color and stay blue as there creation color
i hope i get some help to do it
turtles-own [
speed
s?
]
to setup-road
clear-all
ask patches [
ifelse pycor < -8 and pycor > -17 [set pcolor black ]
[set pcolor gray - 3 ]
if pycor < -9 and pycor > -15 [ set pcolor gray ]
if pycor > -10 and pycor < -8 and pxcor > 20 and pxcor < 27
[set pcolor white ]
if pycor < -2 and pycor > -4 and pxcor < 20 and pxcor > -24
[set pcolor pink ]
if pycor < -3 and pycor > -10 and pxcor < -22 and pxcor > -24
[set pcolor green ]
if pxcor = 20 and pycor = -3
[set pcolor red
]
]
set-default-shape turtles "car"
create-turtles 2 [
set color blue
set size 2
set xcor random-xcor
set ycor -12
set heading 90
set speed 0.1 + random-float 0.9
set s? true
]
reset-ticks
end
to go
ask turtles [
let car-ahead one-of turtles-on patch-ahead 2
if car-ahead != nobody
[ fd speed]
let gate one-of patches in-radius 5 with [pcolor = green ]
let path one-of patches in-radius 5 with [pcolor = pink ]
if s?
[ ifelse gate != nobody
[ move-to gate
fd speed
ifelse path != nobody
[
move-to path
fd speed
if patch-ahead 1 != nobody and pcolor != pink
[set color green ]
]
[fd speed]]
[fd speed]
]
]
tick
end
I am new to Netlogo, and have some questions. That would be great if you could help me.
I would like to create some fruit flies moving around a tree that is made up by green patches. Fruit flies are attracted to the tree. They will turn back to the tree if fruit flies move away certain distance (such as 5 grids) from the tree.
In the beginning, they will not stop on the green patches because they have enough energy. As time passed, they will loss their energy. They will find the closest green patch, and stay on it for certain time once their energy reaches 0. They gain energy after that, and they only can do short hops from the bottom branch to the top one (3 hops). Flies will move back to the bottom branch when they are on the top part of tree. I think I need to do a WHILE loop, but I have no idea how to do that. Please look at my codes.
breed [flies fly]
breed [suns sun]
turtles-own [energy]
flies-own [count-down]
to setup
clear-all
setup-suns
setup-flies
setup-patches
reset-ticks
end
to setup-suns
;; Create the sun
set-default-shape suns "sun"
create-suns 1 [
setxy max-pxcor - 3
max-pycor - 3
set color yellow
set size 7
]
end
to setup-flies
set-default-shape flies "bee 2"
create-flies number-fly [ set color white setxy random-xcor random-ycor set count-down 15]
end
to setup-patches
;; Create sky and grass
ask patches
[ set pcolor blue ]
ask patches with [pycor < min-pycor + 2]
[ set pcolor 66 ]
;; Create trunk and branches
ask patches with [ pxcor = -15 and pycor <= 0 ] [ set pcolor brown ]
ask patches with [ pxcor = -15 and pycor < 8 and pycor > 0] [ set pcolor lime ]
ask patches with [ pxcor = pycor - 15 and pycor <= 5 and pycor > 0 ] [ set pcolor lime ]
ask patches with [ pxcor = (- pycor) - 15 and pycor <= 5 and pycor > 0 ] [ set pcolor lime ]
ask patches with [ pxcor = pycor - 8 and pycor <= 2 and pxcor > -15 ] [ set pcolor lime ]
ask patches with [ pxcor = (- pycor) - 22 and pycor <= 2 and pxcor < -15 ] [ set pcolor lime ]
ask patches with [ pxcor = pycor - 1 and pycor <= -1 and pxcor > -15 ] [ set pcolor lime ]
ask patches with [ pxcor = (- pycor) - 29 and pycor <= -1 and pxcor < -15 ] [ set pcolor lime ]
ask patches with [ pxcor = 15 and pycor <= 0 ] [ set pcolor brown ]
ask patches with [ pxcor = 15 and pycor < 8 and pycor > 0] [ set pcolor lime ]
ask patches with [ pxcor = pycor + 15 and pycor <= 5 and pycor > 0 ] [ set pcolor lime ]
ask patches with [ pxcor = (- pycor) + 15 and pycor <= 5 and pycor > 0 ] [ set pcolor lime ]
ask patches with [ pxcor = pycor + 22 and pycor <= 2 and pxcor > 15 ] [ set pcolor lime ]
ask patches with [ pxcor = (- pycor) + 8 and pycor <= 2 and pxcor < 15 ] [ set pcolor lime ]
ask patches with [ pxcor = pycor + 29 and pycor <= -1 and pxcor > 15 ] [ set pcolor lime ]
ask patches with [ pxcor = (- pycor) + 1 and pycor <= -1 and pxcor < 15 ] [ set pcolor lime ]
ask patches with [ pxcor = -26 and pycor = -3 ] [ set pcolor red ]
ask patches with [ pxcor = -10 and pycor = 5 ] [ set pcolor red ]
ask patches with [ pxcor = 21 and pycor = -1 ] [ set pcolor red ]
end
to go
move-flies
tick
end
to move-flies
ask flies [
set energy 6
ifelse energy > 10 [
;rt random 50 lt random 50 jump random-float 1 ;
let nearest-leaf min-one-of (patches with [pcolor = lime] ) [distance myself] ; Find the closest leaf within 5 grids - long range search.
if is-patch? nearest-leaf [ ; If it is a leaf, and flies will be attracted to the leaf.
face nearest-leaf
;set heading 45
;fd distance nearest-leaf
rt random 50 lt random 50 jump random-float 5 ; Protential resources make flies move actively (fast movement).
;move-to nearest-leaf ;
]
]
[
ifelse pcolor != lime
[ rt random 50 lt random 50 jump random-float 1 ; Initialization - flies fly around to search their resources.
continue]
[ stay ]
]
]
end
to continue
let nearest-leaf min-one-of (patches in-radius 1 with [pcolor = lime] ) [distance myself] ; Find the closest leaf within 2 grids - short hops.
ifelse is-patch? nearest-leaf [ ; If it is a leaf, and flies will be attracted to the leaf.
face nearest-leaf
fd random distance nearest-leaf
;ask patches in-radius 1 [set pcolor red]
;rt random 50 lt random 50 jump random-float 5 ; Protential resources make flies move actively (fast movement).
;move-to nearest-leaf ;
]
[
let turn-back min-one-of (patches with [pcolor = lime] ) [distance myself] ;
;set heading 180
face turn-back
jump random-float 5 ]
move-up ;Flies tend to move up through all branches by short hops. Need a while loop.
; Move down if they reach the top of tree
let canopy patch-at-heading-and-distance 0 1
let canopy-left patch-left-and-ahead 45 1
let canopy-right patch-right-and-ahead 45 1
if canopy != lime or canopy-left != lime or canopy-right != lime
[
move-down
]
end
to move-up
ask flies [
set heading one-of [0 30 -30]
]
end
to move-down
ask flies [
set heading one-of [180 120 -120]
]
end
to stay
set count-down count-down - 1 ;;decrement timer
set label count-down
if count-down = 0
[
rt random 50 lt random 50 jump random-float 1
set label ""
reset-count-down ;;it's another procedure
]
end
to reset-count-down
set count-down 30
end
I am sorry if you are confused by my codes. I appreciate your help. Thanks.
Kind Regards,
Ming
From your description, I think you almost certainly don't want a while loop. In a NetLogo model's go procedure, you're only specifying what happens in a single tick. So you would only use while if need it to express something that happens all in a single instant, not a process that unfolds over multiple ticks.
(Agree with Frank this is too much code to post to have a good chance of getting help. It would take quite a while for me to read and study this much code, let alone try to help you with it. In this answer I've tried to extract and address a single aspect.)