globals [ selected-car ;; the currently selected car average-speed ;; average speed of all the cars ] turtles-own [ speed ;; the current speed of the car speed-limit ;; the maximum speed of the car (different for all cars) lane ;; the current lane of the car target-lane ;; the desired lane of the car patience ;; the driver's current patience max-patience ;; the driver's maximum patience change? ;; true if the car wants to change lanes ] to setup ca draw-road set-default-shape turtles "car" cct number [ setup-cars ] set selected-car one-of turtles ;; color the selected car red so that it is easy to watch ask selected-car [ set color red ] setup-plots end to draw-road ask patches [ set pcolor green if ((pycor > -4) and (pycor < 4)) [ set pcolor grey ] if ((pycor = 0) and ((pxcor mod 3) = 0)) [ set pcolor yellow ] if ((pycor = 4) or (pycor = -4)) [ set pcolor black ] ] end to setup-cars set color black set lane (random 2) set target-lane lane ifelse (lane = 0) [ setxy (random-float screen-size-x) -2 ] [ setxy (random-float screen-size-x) 2 ] set heading 90 set speed 0.1 + random 9.9 set speed-limit (((random 11) / 10) + 1) set change? false set max-patience ((random 50) + 10) set patience (max-patience - (random 10)) ;; make sure no two cars are on the same patch loop [ ifelse any? other-turtles-here [ fd 1 ] [ stop ] ] end ;; All turtles look first to see if there is a turtle directly in front of it, ;; if so, set own speed to front turtle's speed and decelerate. Otherwise, if ;; look-ahead is set for 2, look ahead one more patch and do the same. If no front ;; turtles are found, accelerate towards speed-limit to drive ;; first determine average speed of the cars set average-speed ((sum values-from turtles [speed]) / number) set-current-plot "Car Speeds" set-current-plot-pen "average" plot average-speed set-current-plot-pen "max" plot (max values-from turtles [speed]) set-current-plot-pen "min" plot (abs (min values-from turtles [speed]) ) set-current-plot-pen "selected-car" plot (speed-of selected-car) ask turtles [ ifelse (any? turtles-at 1 0) [ set speed (speed-of (one-of (turtles-at 1 0))) decelerate ] [ ifelse (look-ahead = 2) [ ifelse (any? turtles-at 2 0) [ set speed (speed-of (one-of turtles-at 2 0)) decelerate ] [accelerate] ] [accelerate] ] if (speed < 0.01) [ set speed 0.01 ] if (speed > speed-limit) [ set speed speed-limit ] ifelse (change? = false) [ signal ] [ change-lanes ] ;; Control for making sure no one crashes. ifelse (any? turtles-at 1 0) and (xcor != (- screen-edge-x) - .5) [ set speed speed-of (one-of turtles-at 1 0) ] [ ifelse ((any? turtles-at 2 0) and (speed > 1.0)) [ set speed (speed-of (one-of turtles-at 2 0)) fd 1 ] [jump speed] ] ] end ;; increase speed of cars to accelerate ;; turtle procedure set speed (speed + (speed-up / 1000)) end ;; reduce speed of cars to decelerate ;; turtle procedure set speed (speed - (slow-down / 1000)) end ;; undergoes search algorithms to change-lanes ;; turtle procedure ifelse (patience <= 0) [ ifelse (max-patience <= 1) [ set max-patience (random 10) + 1 ] [ set max-patience (max-patience - (random 5)) ] set patience max-patience ifelse (target-lane = 0) [ set target-lane 1 set lane 0 ] [ set target-lane 0 set lane 1 ] ] [ set patience (patience - 1) ] ifelse (target-lane = lane) [ ifelse (target-lane = 0) [ set target-lane 1 set change? false ] [ set target-lane 0 set change? false ] ] [ ifelse (target-lane = 1) [ ifelse (pycor = 2) [ set lane 1 set change? false ] [ ifelse (not any? turtles-at 0 1) [ set ycor (ycor + 1) ] [ ifelse (not any? turtles-at 1 0) [ set xcor (xcor + 1) ] [ decelerate if (speed <= 0) [ set speed 0.1 ] ] ] ] ] [ ifelse (pycor = -2) [ set lane 0 set change? false ] [ ifelse (not any? turtles-at 0 -1) [ set ycor (ycor - 1) ] [ ifelse (not any? turtles-at 1 0) [ set xcor (xcor + 1) ] [ decelerate if (speed <= 0) [ set speed 0.1 ] ] ] ] ] ] end to signal ifelse (any? turtles-at 1 0) [ if (speed-of (one-of (turtles-at 1 0))) < (speed) [ set change? true ] ] [ set change? false ] end to select-car locals [ mx my ] if (mouse-down?) [ set mx mouse-xcor set my mouse-ycor if (any? turtles-at mx my) [ set color-of selected-car black set selected-car (one-of (turtles-at mx my)) set color-of selected-car red ] ] end to setup-plots ;; This plot shows the different car speeds set-current-plot "Car Speeds" set-plot-y-range 0 ((max values-from turtles [speed-limit]) + .5) end ; *** NetLogo Model Copyright Notice *** ; ; This model was created as part of the project: CONNECTED MATHEMATICS: ; MAKING SENSE OF COMPLEX PHENOMENA THROUGH BUILDING OBJECT-BASED PARALLEL ; MODELS (OBPML). The project gratefully acknowledges the support of the ; National Science Foundation (Applications of Advanced Technologies ; Program) -- grant numbers RED #9552950 and REC #9632612. ; ; Copyright 1998 by Uri Wilensky. All rights reserved. ; ; Permission to use, modify or redistribute this model is hereby granted, ; provided that both of the following requirements are followed: ; a) this copyright notice is included. ; b) this model will not be redistributed for profit without permission ; from Uri Wilensky. ; Contact Uri Wilensky for appropriate licenses for redistribution for ; profit. ; ; This model was converted to NetLogo as part of the project: ; PARTICIPATORY SIMULATIONS: NETWORK-BASED DESIGN FOR SYSTEMS LEARNING IN ; CLASSROOMS. The project gratefully acknowledges the support of the ; National Science Foundation (REPP program) -- grant number REC #9814682. ; Converted from StarLogoT to NetLogo, 2001. Updated 2002. ; ; To refer to this model in academic publications, please use: ; Wilensky, U. (1998). NetLogo Traffic 2 Lanes model. ; http://ccl.northwestern.edu/netlogo/models/Traffic2Lanes. ; Center for Connected Learning and Computer-Based Modeling, ; Northwestern University, Evanston, IL. ; ; In other publications, please use: ; Copyright 1998 by Uri Wilensky. All rights reserved. See ; http://ccl.northwestern.edu/netlogo/models/Traffic2Lanes ; for terms of use. ; ; *** End of NetLogo Model Copyright Notice *** @#$#@#$#@ GRAPHICS-WINDOW 271 10 689 209 25 10 8.0 1 10 1 1 1 CC-WINDOW 3 304 295 430 Command Center BUTTON 9 10 84 43 Setup setup NIL 1 T OBSERVER T BUTTON 11 95 86 128 Go drive T 1 T OBSERVER T BUTTON 5 51 92 84 Go Once drive NIL 1 T OBSERVER T BUTTON 1 155 98 188 Select Car select-car T 1 T OBSERVER T MONITOR 152 253 266 302 average speed average-speed 2 1 SLIDER 104 10 266 43 number number 0 134 54 1 1 NIL SLIDER 104 158 266 191 slow-down slow-down 0.0 100.0 20.0 1.0 1 NIL SLIDER 104 110 266 143 speed-up speed-up 0.0 100.0 100.0 1.0 1 NIL SLIDER 102 58 264 91 look-ahead look-ahead 1 2 2 1 1 NIL PLOT 298 211 664 430 Car Speeds Time Speed 0.0 300.0 0.0 2.5 true true PENS "average" 1.0 0 -11352576 true "max" 1.0 0 -16711681 true "min" 1.0 0 -16776961 true "selected-car" 1.0 0 -65536 true @#$#@#$#@ WHAT IS IT? ----------- This project is a more sophisticated two-lane version of the "Traffic Basic" model. Much like the simpler model, this model demonstrates how traffic jams can form. In the two-lane version, drivers have a new option; they can react by changing lanes, although this often does little to solve their problem. As in the traffic model, traffic may slow down and jam without any centralized cause. HOW TO USE IT ------------- Click on the SETUP button to set up the cars. Click on DRIVE to start the cars moving. The STEP button drives the car for just one tick of the clock. The NUMBER slider controls the number of cars on the road. The LOOK-AHEAD slider controls the distance that drivers look ahead (in deciding whether to slow down or change lanes). The SPEED-UP slider controls the rate at which cars accelerate when there are no cars ahead. The SLOW-DOWN slider controls the rate at which cars decelerate when there is a car close ahead. You may wish to slow down the model with the speed slider to watch the behavior of certain cars more closely. The SELECT-CAR button allows you to pick a car to watch. It turns the car red, so that it is easier to keep track of it. SELECT-CAR is best used while DRIVE is turned off. If the user does not select a car manually, a car is chosen at random to be the "selected car". The AVERAGE-SPEED monitor displays the average speed of all the cars. The CAR SPEEDS plot displays four quantities over time: - the maximum speed of any car - CYAN - the minimum speed of any car - BLUE - the average speed of all cars - GREEN - the speed of the selected car - RED THINGS TO NOTICE ---------------- Traffic jams can start from small "seeds." Cars start with random positions and random speeds. If some cars are clustered together, they will move slowly, causing cars behind them to slow down, and a traffic jam forms. Even though all of the cars are moving forward, the traffic jams tend to move backwards. This behavior is common in wave phenomena: the behavior of the group is often very different from the behavior of the individuals that make up the group. Just as each car has a current speed and a maximum speed, each driver has a current patience and a maximum patience. When a driver decides to change lanes, he may not always find an opening in the lane. When his patience expires, he tries to get back in the lane he was first in. If this fails, back he goes... As he gets more 'frustrated', his patience gradually decreases over time. When the number of cars in the model is high, watch to find cars that weave in and out of lanes in this manner. This phenomenon is called "snaking" and is common in congested highways. Watch the AVERAGE-SPEED monitor, which computes the average speed of the cars. What happens to the speed over time? What is the relation between the speed of the cars and the presence (or absence) of traffic jams? Look at the two plots. Can you detect discernible patterns in the plots? THINGS TO TRY ------------- What could you change to minimize the chances of traffic jams forming, besides just the number of cars? What is the relationship between number of cars, number of lanes, and (in this case) the length of each lane? Explore changes to the sliders SLOW-DOWN, SPEED-UP, and LOOK-AHEAD. How do these affect the flow of traffic? Can you set them so as to create maximal snaking? EXTENDING THE MODEL ------------------- Try to create a 'traffic-3 lanes', 'traffic-4 lanes', 'traffic-crossroads' (where two sets of cars might meet at a traffic light), or 'traffic-bottleneck' model (where two lanes might merge to form one lane). Note that the cars never crash into each other- a car will never enter a patch or pass through a patch containing another car. Remove this feature, and have the turtles that collide die upon collision. What will happen to such a model over time? NETLOGO FEATURES ---------------- Note the use of mouse-down? and mouse-xcor/mouse-ycor to enable selecting a car for special attention. Each turtle has a shape, unlike in some other models. NetLogo uses SET SHAPE to alter the shapes of turtles. You can, using the shapes editor in the Tools menu, create your own turtle shapes or modify existing ones. Then you can modify the code to use your own shapes. RELATED MODELS -------------- Traffic Basic CREDITS AND REFERENCES ---------------------- To refer to this model in academic publications, please use: Wilensky, U. (1998). NetLogo Traffic 2 Lanes model. http://ccl.northwestern.edu/netlogo/models/Traffic2Lanes. Center for Connected Learning and Computer-Based Modeling, Northwestern University, Evanston, IL. In other publications, please use: Copyright 1998 by Uri Wilensky. All rights reserved. See http://ccl.northwestern.edu/netlogo/models/Traffic2Lanes for terms of use. @#$#@#$#@ default true 0 Polygon -7566196 true true 150 5 40 250 150 205 260 250 car false 0 Rectangle -7566196 true true 29 44 237 218 Rectangle -7566196 true true 224 154 292 216 Circle -6524078 true false 64 181 63 Circle -6524078 true false 168 182 61 Polygon -7566196 true true 232 74 285 161 226 161 232 74 Polygon -16777216 true false 239 112 239 146 261 146 Circle -256 true false 178 197 38 Line -16777216 false 195 200 199 231 Line -16777216 false 182 219 211 209 Line -16777216 false 184 206 210 224 Circle -256 true false 76 195 40 Line -16777216 false 94 198 94 229 Line -16777216 false 82 203 110 224 Line -16777216 false 80 220 108 206 @#$#@#$#@ NetLogo 2.0beta5 @#$#@#$#@ setup repeat 50 [ drive ] @#$#@#$#@ @#$#@#$#@