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
]
Related
I write a code that evacuates turtles to the only exit door. But as I noticed that the turtles are divided into several groups. There are turtles that walk toward the exit door and the others walk to the nearest wall. I want to create a simulation that all the turtles will go to the exit doors. Here's my code. Can anyone advise, please?
globals [
;wall
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"
ask n-of number-room1 patches with [pxcor < 47 and pxcor > 0 and pycor < 45 and pycor > 0 and pcolor = black]
[
sprout 1 [
facexy 41 45
set direction 1
set color yellow
]
]
reset-ticks
end
to setup-patches
draw-wall
draw-exit
ask patch 21 41 [
set plabel-color white
set plabel "Room1"
]
end
to draw-wall
ask patches with [pxcor <= 38 and pxcor >= 0 and pycor = 45]
[set pcolor blue]
ask patches with [pxcor <= 47 and pxcor >= 44 and pycor = 45]
[set pcolor blue]
ask patches with [pxcor <= 47 and pxcor >= 0 and pycor = 0]
[set pcolor blue]
ask patches with [pxcor = 0 and pycor <= 45 and pycor >= 0]
[set pcolor blue]
ask patches with [pxcor = 47 and pycor <= 45 and pycor >= 0]
[set pcolor blue]
end
to draw-exit
ask patches with [pxcor <= 43 and pxcor >= 39 and pycor = 45]
[set pcolor green]
end
to go
if ticks >= 100 [stop]
ask turtles [walk]
tick
end
to walk
let room1 patches with [pxcor < 47 and pxcor > 0 and pycor < 45 and pycor > 0]
;let exitpoint patches with [pxcor < 46 and pxcor > 33 and pycor < 45 and pycor > 32]
ask turtles-on room1
[face one-of patches with [pxcor <= 43 and pxcor >= 39 and pycor = 47]
fd 0.01
]
;ask turtles-on exitpoint
;[face one-of patches with [pxcor <= 42 and pxcor >= 38 and pycor = 47]
; fd 0.01
;]
end
You can try these 2 steps to make it work:
Check the setting of your simulation (besides the simulation speed slider on the top right side), make sure to uncheck world wrap horizontally & world wrap vertically. If it is still checked, it means that the turtles can go beyond your vertical/horizontal boundary and appear on the other side. That's why some group of turtles is moving towards the wall, they assume they can pass the boundary and arrive at the exit, while they will be stuck there because of your code constraint.
After making sure that the setting is okay, if you still have some of them stuck in the upper side of the wall near the exit, you can try to replace your to walk code with this:
to walk
set heading towards one-of patches with [pcolor = green]
fd 1
end
Hopefully, this will help you solve the problem
I am trying to create a patch that will be a bottleneck structure in the middle of a path where the turtles have to go through.
I have created the parabolas but I would like to add a slider so that the size of the bottleneck can be changed but I don't know how to create a code that will move it.
Here is the code (I am very bad with the maths of the parabola so I had help with the top one and I just manually created the down one)
to setup-road
;; patch procedure
;; colour patches to paint a horizontal road
let bound (max-pycor / 3)
ifelse (pycor < (max-pycor - bound)) and (pycor > (min-pycor + bound))
[ set pcolor red - 3 ]
[ set pcolor green ]
set upper-road-bound (max-pycor - bound - 2 )
set lower-road-bound (min-pycor + bound + 2 )
setup-top-parabola-on-road
setup-down-parabola-on-road
end
;; I want to be able to expand the parabolas so that the opening between them will be wider as well as narrow according a slider.
to setup-top-parabola-on-road
ask patches with
[pxcor > -25 and pxcor < 25] [
if pycor > ((pxcor ^ 2) / 20 + 10)
[ set pcolor green ]
]
end
to setup-down-parabola-on-road
ask patches with [pxcor >= -4 and pxcor <= 4 and pycor = -11][set pcolor green]
ask patches with [pxcor >= -5 and pxcor <= 5 and pycor = -12][set pcolor green]
ask patches with [pxcor >= -6 and pxcor <= 6 and pycor = -13][set pcolor green]
ask patches with [pxcor >= -7 and pxcor <= 7 and pycor = -14][set pcolor green]
ask patches with [pxcor >= -8 and pxcor <= 8 and pycor = -15][set pcolor green]
ask patches with [pxcor >= -9 and pxcor <= 9 and pycor = -16][set pcolor green]
ask patches with [pxcor >= -10 and pxcor <= 10 and pycor = -17][set pcolor green]
ask patches with [pxcor >= -11 and pxcor <= 11 and pycor = -18][set pcolor green]
ask patches with [pxcor >= -12 and pxcor <= 12 and pycor = -19][set pcolor green]
ask patches with [pxcor >= -13 and pxcor <= 13 and pycor = -20][set pcolor green]
ask patches with [pxcor >= -14 and pxcor <= 14 and pycor = -21][set pcolor green]
ask patches with [pxcor >= -15 and pxcor <= 15 and pycor = -22][set pcolor green]
ask patches with [pxcor >= -16 and pxcor <= 16 and pycor = -23][set pcolor green]
ask patches with [pxcor >= -17 and pxcor <= 17 and pycor = -24][set pcolor green]
ask patches with [pxcor >= -18 and pxcor <= 18 and pycor = -25][set pcolor green]
end
Can anyone help?
Here you go.
This assumes you have a button called setup
and a slider for variable "width" that ranges from 0 to 11.
I added an "ask patches [ ]" in near the top where you are setting red and green.
to setup
clear-all
setup-road
reset-ticks
end
to setup-road
;; patch procedure
;; colour patches to paint a horizontal road
let bound (max-pycor / 3)
ask patches [
ifelse (pycor < (max-pycor - bound)) and (pycor > (min-pycor + bound))
[ set pcolor red - 3 ]
[ set pcolor green ]
]
let upper-road-bound (max-pycor - bound - 2 )
let lower-road-bound (min-pycor + bound + 2 )
setup-top-parabola-on-road
setup-down-parabola-on-road
end
;; I want to be able to expand the parabolas so that the opening between them will be wider as well as narrow according a slider.
to setup-top-parabola-on-road
ask patches with
[pxcor > -25 and pxcor < 25] [
if pycor > ((pxcor ^ 2) / 20 + width)
[ set pcolor blue ]
]
end
to setup-down-parabola-on-road
ask patches with
[pxcor > -25 and pxcor < 25] [
if pycor < (-1 * (pxcor ^ 2) / 20) - width + 1
[ set pcolor yellow ]
]
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 behaviour from one floor I wrote my code and there isn't any error from Netlogo, but I didn't know why my model doesn't work.
to go
ask turtles with [pcolor = yellow] [walk]
tick
end
to walk
;room1
ask turtles with [pxcor < 71 and pycor < 79 and pxcor > 0 and pycor > 50]
[ face one-of patches with [pxcor <= 58.57 and pxcor >= 49.42 and pycor = 50]]
;room2
ask turtles with [pxcor < 112 and pycor < 79 and pxcor > 71 and pycor > 50]
[ face one-of patches with [pxcor <= 108.57 and pxcor >= 99.42 and pycor = 50]]
;room3
ask turtles with [pxcor < 82 and pycor < 36 and pxcor > 0 and pycor > 0]
[face one-of patches with [pxcor <= 79.575 and pxcor >= 70.425 and pycor = 36]]
;room4
ask turtles with [pxcor < 132 and pycor < 36 and pxcor > 82 and pycor > 0]
[face one-of patches with [pxcor <= 93.575 and pxcor >= 84.425 and pycor = 36]]
fd 0.5
;corridor
ask turtles with [pxcor < 132 and pycor < 50 and pxcor > 0 and pycor > 36]
[face one-of patches with [pxcor <= 132 and pxcor >= 112 and pycor = 50]]
;exit
ask turtles with [pxcor < 132 and pycor < 79 and pxcor >= 112 and pycor >= 50]
[face one-of patches with [pxcor <= 129.57 and pxcor >= 120.42 and pycor = 79]]
fd 1
end
Your question is not particularly clear because 'doesn't work' doesn't provide enough information about the problem. To get a more useful answer, you should describe the behaviour you expect and the behaviour you actually get. Having said that, however, there's an obvious problem with your code that is probably what you are asking about.
In your go statement, you have the turtles call the walk procedure. In your walk procedure, you have the face command inside the ask turtles brackets, but the fd command outside the brackets. I am assuming that is intentional and that what you are trying to do is have the four if statements make the turtle face the direction you want and then the fd move it.
However, within the walk procedure you are repeating the ask turtles. What you probably want to do is only change the heading of one turtle (the one that is being asked to walk).
to go
ask turtles with [pcolor = yellow] [walk]
tick
end
to walk
;room1
if pxcor < 71 and pycor < 79 and pxcor > 0 and pycor > 50
[ face one-of patches with [pxcor <= 58.57 and pxcor >= 49.42 and pycor = 50]
]
;room2
if pxcor < 112 and pycor < 79 and pxcor > 71 and pycor > 50
[ face one-of patches with [pxcor <= 108.57 and pxcor >= 99.42 and pycor = 50]
]
;room3
if pxcor < 82 and pycor < 36 and pxcor > 0 and pycor > 0
[ face one-of patches with [pxcor <= 79.575 and pxcor >= 70.425 and pycor = 36]
]
;room4
if pxcor < 132 and pycor < 36 and pxcor > 82 and pycor > 0
[ face one-of patches with [pxcor <= 93.575 and pxcor >= 84.425 and pycor = 36]
]
fd 0.5
;corridor
if pxcor < 132 and pycor < 50 and pxcor > 0 and pycor > 36
[ face one-of patches with [pxcor <= 132 and pxcor >= 112 and pycor = 50]
]
;exit
if pxcor < 132 and pycor < 79 and pxcor >= 112 and pycor >= 50
[face one-of patches with [pxcor <= 129.57 and pxcor >= 120.42 and pycor = 79]
]
fd 1
end
There's some other issues in this code. The first is that you appear to be mixing up xcor and pxcor (and similarly for y coordinates). xcor is the turtle coordinate and pxcor is the patch coordinate. A turtle does have access to the patch's variables, including coordinates, so the code won't generate an error. But I am concerned by statements like face one-of patches with [pxcor <= 108.57 and pxcor >= 99.42 and pycor = 50]. This is equivalent to face one-of patches with [pxcor <= 108 and pxcor >= 100 and pycor = 50] because pxcor and pycor are integers. The fact that you have decimals in this is what makes me think you are confused about coordinates.
The second is that all turtles who are asked to walk are going to move forward by 1.5 but I think you want some to move 0.5 and others to move 1. Once the turtle has entered the walk procedure, the if statements (or the with in your version) only change the heading. The fd statements apply to all turtles.
Third, I'd be tempted to use patch-sets to simplify the readability of this code. Something like:
to go
ask turtles with [pcolor = yellow] [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 and pxcor >= 50 and pycor = 50
fd 0.5
]
ask turtles-on room2
[ face one-of patches with [pxcor <= 108 and pxcor >= 100 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 and pxcor >= 85 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
Note that this is not the same as yours because I don't know what patches are yellow. You would have to modify to deal with this issue.
The reason I moved the ask turtles inside the procedure was so the patch-sets are only created once. If you are going to use these rooms etc in other places, it would be worth having them as global variables and putting the construction into your setup.
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