Traffic
WHAT IS IT?
This project models cars and trucks driving on a highway and swans swimming in a nearby river. The cars in the traffic model follow three very simple rules: If there is a car ahead, then slow down. If there is no car ahead, then speed up. If you see a radar trap, then slow down.
HOW TO USE IT
Use the buttons "setup", "go", and "stop-it" to control the simulation. The "number" slider controls the initial number of cars on the road. (You will have to setup the simulation again for a change in this slider to take effect.) The "lookahead" slider controls how many spaces ahead each car looks to see if there is open driving room, and "speedup" and "slowdown" control the rates for acceleration and deceleration.
THINGS TO NOTICE
In this project there are four breeds: breeds [lefts rights swans police] If you look through the code, you will see that the swimming turtles have breed = swans and the driving turtles in the top lane have breed = lefts (similarly, those in the bottom lane have breed = rights). This project uses breeds to give different behaviors to different groups of turtles.
EXPLORATIONS
Under the what conditions do the cars form traffic jams? What is the affect of adding a police / radar area? How does the lookahead variable rate affect the occurances of traffic jams? How can the two quantities speedup and slowdown used to control the occurances of traffic jams?
STARLOGO FEATURES
The swans in this project seems to be flocking as they swim downstream. But actually they are simply moving randomly in small increments downstream while keeping within an bounded area. Take a look at the turtle swim procedure for details.
Turtle procedures
breeds [
lefts ; left facing cars
rights ; right facing cars
swans ; swans
polices ; police car
]
turtles-own [
speed ; the car's speed
SpeedLimit ; the max car speed
]
;; setup-cars procedure
;; called by the observer setup procedure
;; Sets the position, speed, SpeedLimit and shape for right and left bound cars.
;;
to setup-cars
if breed = rights [
setxy (random (screen-width)) -1
seth 90
setspeed ((random 10) / 10) + 0.1
setSpeedLimit 1
setshape right-car-shape
]
if breed = lefts [
setxy random (screen-width) 3
seth 270
setspeed ((random 10) / 10) + 0.1
setSpeedLimit 1
setshape left-car-shape
]
end
;; swim procedure
;; called by the observer go procedure and by the swim button
;; Swans follow the river contour when they swim, they do not actually flock.
;;
to swim
if breed = swans [
ifelse xcor < -18
[
seth 85 + random 10
]
[
ifelse xcor < 5
[seth 58 + random 10]
[seth 115 + random 10]
]
jump (random 500) / 10000
]
end
;; drive procedure
;; called by the go procedure and by the drive button
;; Car behavior goes like this:
;; if there is a car 1 space ahead of me, decelerate
;; else if lookahead is 2, then also check 2 spaces ahead of me
;; check if there is a car 2 spaces ahead of me, if so decelerate, if not, accelerate.
;; if lookahead is not 2, then accelerate
;; Then adjust speed to stay within bounds (0.01 - SpeedLimit) and adjust speed down in the radar zone.
;;
to drive
if breed = rights [
ifelse (count-turtles-at 1 0) > 0 ;if there is a turtle 1 space ahead, decelerate
[
setspeed speed-of one-of-turtles-at 1 0
decelerate
]
[
ifelse lookahead = 2 ;if lookahead=2, check 2 spaces ahead also
[
ifelse (count-turtles-at 1 0) > 0
[
setspeed speed-of one-of-turtles-at 2 0
decelerate
]
[accelerate]
]
[accelerate]
]
if speed < 0.01 [setspeed 0.01] ;also adjust speed based on SpeedLimit and radar
if speed > SpeedLimit [setspeed SpeedLimit]
if radar = true [setspeed .10]
jump speed
]
if breed = lefts [
ifelse (count-turtles-at (-1) 0) > 0 ;if there is a turtle 1 space ahead, decelerate
[
setspeed speed-of one-of-turtles-at (-1) 0
decelerate
]
[
ifelse lookahead = 2 ;if lookahead=2, check 2 spaces ahead also
[
ifelse (count-turtles-at (-2) 0) > 0
[
setspeed speed-of one-of-turtles-at (-2) 0
decelerate
]
[accelerate]
]
[accelerate]
]
if speed < 0.01 [setspeed 0.01] ;also adjust speed based on SpeedLimit and radar
if speed > SpeedLimit [setspeed SpeedLimit]
if radar = true [setspeed .03]
jump speed
]
end
;; accelerate procedure
;; called by the drive procedure
;; Increases the speed of the car
;;
to accelerate
setspeed (speed + (speedup / 10000))
end
;; decrease procedure
;; called by the drive procedure
;; Decreases the speed of the car
;;
to decelerate
setspeed speed - (slowdown / 1000)
end
Observer procedures
patches-own [
flock ; is a swan on this patch?
radar ; is the radar on this patch?
picture ; the background picture
]
;; setup procedure
;; called by the setup button
;; Cleans up any turtles remaining from previous runs, creates a number of cars
;; heading right and left specified by the user using the number slider. Sets up
;; the cars and the flock, then sets up the background picture with radar off.
;;
to setup
ask-turtles [die]
create-rights number
create-lefts number
ask-turtles [
setup-cars
if color = gray [setc red]
]
swan-flock
ask-patches [
if pc = (red - 2) ; the radar is on
[setpc picture] ; remove radar by reloading bkgd.
setpicture pc
setradar false
]
end
;; setup-police procedure
;; called by the setup-police button
;; Creates a turtle of breed police, sets its location, shape and color.
;; Sets up radar patches and makes them visible.
;;
to setup-police
create-polices 1
ask-polices [
setxy -10 1.5
setshape right-car-shape
setc blue
]
ask-patches [
setradar false
if (ycor > 0) and ((distance -10 1) < 3.5) ;turn the police radar on
[setradar true]
]
see-radar
end
;; remove-police procedure
;; called by the remove-police button
;; Kills off the police turtle and resets the radar patches to stored background
;; picture and turns radar off by setting the radar patch variable to false.
;;
to remove-police
ask-polices [die]
ask-patches [
if radar = true
[setpc picture] ;turn off the radar
setradar false
]
end
;; see-radar procedure
;; called by the setup-police procedure
;; Asks the radar=true patches to change their color to (red - 2), the radar
;; color so the user can see it.
;;
to see-radar
ask-patches [
if radar = true [
setpc (red - 2)
]
]
end
;; swan-flock procedure
;; called by the setup procedure
;; Creates swans at the patches where the patch variable flock is set to true
to swan-flock
ask-patches [
setflock ((distance -22.5 -21.5) < 2)
if flock = true [
sprout [
setbreed swans
setc white
setshape swan-shape
setspeed .01
seth 350 + random 10
]
]
]
end
;; go procedure
;; called by go button
;; Starts the drive and the swim buttons.
;;
to go
startdrivebutton
startswimbutton
end
;; stop-it procedure
;; called by stop-it button
;; Stops the drive and the swim buttons.
;;
to stop-it
stopdrivebutton
stopswimbutton
end
