Traffic 2 Lanes

Traffic 2 Lanes preview image

1 collaborator

Uri_dolphin3 Uri Wilensky (Author)

Tags

social science 

Tagged by Reuven M. Lerner over 11 years ago

Model group CCL | Visible to everyone | Changeable by group members (CCL)
Model was written in NetLogo 5.0.4 • Viewed 2381 times • Downloaded 139 times • Run 4 times
Download the 'Traffic 2 Lanes' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


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

HOW TO CITE

If you mention this model in a publication, we ask that you include these citations for the model itself and for the NetLogo software:

  • Wilensky, U. (1998). NetLogo Traffic 2 Lanes model. http://ccl.northwestern.edu/netlogo/models/Traffic2Lanes. Center for Connected Learning and Computer-Based Modeling, Northwestern Institute on Complex Systems, Northwestern University, Evanston, IL.
  • Wilensky, U. (1999). NetLogo. http://ccl.northwestern.edu/netlogo/. Center for Connected Learning and Computer-Based Modeling, Northwestern Institute on Complex Systems, Northwestern University, Evanston, IL.

COPYRIGHT AND LICENSE

Copyright 1998 Uri Wilensky.

CC BY-NC-SA 3.0

This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

Commercial licenses are also available. To inquire about commercial licenses, please contact Uri Wilensky at uri@northwestern.edu.

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.

This model was converted to NetLogo as part of the projects: PARTICIPATORY SIMULATIONS: NETWORK-BASED DESIGN FOR SYSTEMS LEARNING IN CLASSROOMS and/or INTEGRATED SIMULATION AND MODELING ENVIRONMENT. The project gratefully acknowledges the support of the National Science Foundation (REPP & ROLE programs) -- grant numbers REC #9814682 and REC-0126227. Converted from StarLogoT to NetLogo, 2001.

Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

globals
[
  selected-car   ;; the currently selected car
]

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
  clear-all
  draw-road
  set-default-shape turtles "car"
  crt 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 ]
  reset-ticks
end 

to draw-road
  ask patches
  [
    set pcolor green
    if ((pycor > -4) and (pycor < 4))
    [ set pcolor gray ]
    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-xcor -2 ]
  [ setxy random-xcor  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
  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 != min-pxcor - .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]
    ]
  ]
  tick
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
  if mouse-down?
  [
    let mx mouse-xcor
    let my mouse-ycor
    if any? turtles-on patch mx my
    [
      ask selected-car [ set color black ]
      set selected-car one-of turtles-on patch mx my
      ask selected-car [ set color red ]
      display
    ]
  ]
end 


; Copyright 1998 Uri Wilensky.
; See Info tab for full copyright and license.

There are 10 versions of this model.

Uploaded by When Description Download
Uri Wilensky over 11 years ago Updated to NetLogo 5.0.4 Download this version
Uri Wilensky about 12 years ago Updated version tag Download this version
Uri Wilensky about 12 years ago Updated to version from NetLogo 5.0.3 distribution Download this version
Uri Wilensky almost 13 years ago Updated to NetLogo 5.0 Download this version
Uri Wilensky over 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky over 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky over 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky over 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky over 14 years ago Model from NetLogo distribution Download this version
Uri Wilensky over 14 years ago Traffic 2 Lanes Download this version

Attached files

File Type Description Last updated
Traffic 2 Lanes.png preview Preview for 'Traffic 2 Lanes' over 11 years ago, by Uri Wilensky Download

This model does not have any ancestors.

This model does not have any descendants.