Traffic Flow at Toll Barriers
Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)
WHAT IS IT?
This model shows how traffic jams can form at toll barriers when cars arrive at a road with limited service capacity. Cars enter from the left and move toward a toll plaza, where each lane has a barrier that serves one car at a time.
Each car must stop at the barrier and wait for a randomly determined service time before being allowed to pass. When traffic demand is high or service times are long, queues build up and congestion emerges.
The model illustrates how simple vehicle interactions and bottlenecks can lead to traffic jams, even without accidents or road closures.
HOW TO USE IT
Click on the SETUP button to set up the cars. Click on GO to start the cars moving. The GO ONCE button drives the cars for just one tick of the clock.
The NUMBER-OF-CARS slider controls the number of cars on the road. If you change the value of this slider while the model is running, cars will be added or removed “on the fly”, so you can see the impact on traffic right away.
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.
The MAX-PATIENCE slider controls how many times a car can slow down before a driver loses their patience and tries to change lanes.
You may wish to slow down the model with the speed slider to watch the behavior of certain cars more closely.
The NUM-OF-LANES slider control how many lanes the world will have, up to a maximum of 8.
The BARRIER-WAIT-MIN & BARRIER-WAIT-MAX randomly controlls the minimum and maximum service time before being allowed to pass.
The SELECT CAR button allows you to highlight a particular car. It turns that car red, so that it is easier to keep track of it. SELECT CAR is easier to use while GO is turned off. If the user does not select a car manually, a car is chosen at random to be the “selected car”.
You can either watch or follow the selected car using the WATCH SELECTED CAR and FOLLOW SELECTED CAR buttons. The RESET PERSPECTIVE button brings the view back to its normal state.
The SELECTED CAR SPEED monitor displays the speed of the selected car. The MEAN-SPEED monitor displays the average speed of all the cars.
The YCOR OF CARS plot shows a histogram of how many cars are in each lane, as determined by their y-coordinate. The histogram also displays the amount of cars that are in between lanes while they are trying to change lanes.
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
The DRIVER PATIENCE plot shows four quantities for the current patience of drivers: the max, the min, the average and the current patience of the driver of the selected car.
THINGS TO NOTICE
Traffic jams can start from small “seeds.” Cars start with random positions. 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, each driver has a current patience. Each time the driver has to hit the brakes to avoid hitting the car in front of them, they loose a little patience. When a driver’s patience expires, the driver tries to change lane. The driver’s patience gets reset to the maximum patience.
When the number of cars in the model is high, drivers lose their patience quickly and start weaving in and out of lanes. This phenomenon is called “snaking” and is common in congested highways.
Watch the MEAN-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?
The grass patches on each side of the road are all a slightly different shade of green. The road patches, to a lesser extent, are different shades of grey. This is not just about making the model look nice: it also helps create an impression of movement when using the FOLLOW SELECTED CAR button.
Look at what happens when the service time at the barriers varies. Does higher variability lead to longer queues or more unstable traffic flow?
Observe how congestion builds up upstream of the barriers and how traffic jams propagate backward along the road.
Notice the relationship between the number of cars in the system and the capacity of the toll plaza. At high traffic demand, small delays at the barriers can cause large traffic jams.
THINGS TO TRY
Change the average service time at the barriers and observe how it affects queue length and traffic flow.
Increase or decrease the number of cars entering the system and see when traffic jams start to appear.
Compare scenarios with low and high variability in service time. Does variability alone cause congestion?
Experiment with different numbers of lanes and observe how this changes the overall throughput of the toll plaza.
EXTENDING THE MODEL
Lane choice before the toll: Let cars choose the lane with the shortest queue (or a probabilistic choice), and study whether this reduces congestion or creates lane-switching turbulence.
Breakdowns and incidents: Introduce a small probability that a car stops unexpectedly or a barrier temporarily fails, and measure how quickly queues grow and how long recovery takes (this may be done by deleting one lane while maintaining the same number of cars)
Dynamic demand patterns: Replace steady demand with rush-hour waves (e.g., a time-dependent arrival rate) to see how peaks trigger jams and how fast the system clears afterward.
Performance metrics: Track and plot average waiting time, maximum queue length, and cars served per minute (throughput) to quantitatively compare scenarios.
RELATED MODELS
This model has been built from the "NetLogo Traffic 2 lanes model" * Wilensky, U. & Payette, N. (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.
Using the NetLogo software: * Wilensky, U. (1999). NetLogo. http://ccl.northwestern.edu/netlogo/. Center for Connected Learning and Computer-Based Modeling, Northwestern University, Evanston, IL.
HOW TO CITE
If you mention this model or the NetLogo software in a publication, I ask you include the citations below. For the model itself:
- Rubio Carrión, Pau. (2026). NetLogo Traffic Flow at Toll Barriers
COPYRIGHT AND LICENSE
Copyright 1998 Uri Wilensky.

NetLogo itself is free software for non-commercial use under the terms of the GNU General Public License (see full license information here).
To inquire about commercial licenses for either NetLogo or specific models from the models library, please contact netlogo-commercial-admin@ccl.northwestern.edu.
Comments and Questions
globals [ selected-car ; the currently selected car lanes ; a list of the y coordinates of different lanes barrier-x ; position x of the barriers ] breed [ cars car] breed [ barriers barrier] cars-own [ speed ; the current speed of the car top-speed ; the maximum speed of the car (different for all cars) target-lane ; the desired lane of the car patience ; the driver's current level of patience passed-barrier? ] barriers-own [ lane-y ; y of the lane where the barrier is serving-car ; car that is being served in the barrier remaining ; ticks left to finish the "plate reading" open? ; if the barrier is open (it allows car to go through) ] to setup clear-all set-default-shape cars "car" draw-road set barrier-x (max-pxcor - 3) ; for example, 3 patches before the end draw-toll-plaza setup-barriers create-or-remove-cars set selected-car one-of cars ask selected-car [ set color red ] reset-ticks end to setup-barriers ; remove old barriers (if num-of-lanes is changed after new setup) ask barriers [die] ; one barrier at each lane foreach lanes [y -> create-barriers 1 [ set lane-y y setxy barrier-x lane-y ;hide-turtle set serving-car nobody set remaining 0 set open? false set shape "line" set size 1.2 set color yellow set heading 0 ] ] end to create-or-remove-cars ; make sure we don't have too many cars for the room we have on the road let road-patches patches with [ member? pycor lanes ] if number-of-cars > count road-patches [ set number-of-cars count road-patches ] create-cars (number-of-cars - count cars) [ set passed-barrier? false set color car-color move-to one-of free road-patches set target-lane pycor set heading 90 set top-speed 0.5 + random-float 0.5 set speed 0.5 set patience random max-patience ] if count cars > number-of-cars [ let n count cars - number-of-cars ask n-of n [ other cars ] of selected-car [ die ] ] end to-report free [ road-patches ] ; turtle procedure let this-car self report road-patches with [ not any? cars-here with [ self != this-car ] ] end to draw-toll-plaza ; toll zone --> 3 patches wide let x0 barrier-x let booth-x (barrier-x + 1) ; stop line in each lane ask patches with [ pxcor = x0 and member? pycor lanes ] [ set pcolor grey ] end to draw-road ask patches [ ; the road is surrounded by green grass of varying shades set pcolor green - random-float 0.5 ] set lanes n-values number-of-lanes [ n -> number-of-lanes - (n * 2) - 1 ] ask patches with [ abs pycor <= number-of-lanes ] [ ; the road itself is varying shades of grey set pcolor grey - 2.5 + random-float 0.25 ] draw-road-lines end to draw-road-lines let y (last lanes) - 1 ; start below the "lowest" lane while [ y <= first lanes + 1 ] [ if not member? y lanes [ ; draw lines on road patches that are not part of a lane ifelse abs y = number-of-lanes [ draw-line y yellow 0 ] ; yellow for the sides of the road [ draw-line y white 0.5 ] ; dashed white between lanes ] set y y + 1 ; move up one patch ] end to draw-line [ y line-color gap ] ; We use a temporary turtle to draw the line: ; - with a gap of zero, we get a continuous line; ; - with a gap greater than zero, we get a dasshed line. create-cars 1 [ setxy (min-pxcor - 0.5) y hide-turtle set color line-color set heading 90 repeat world-width [ pen-up forward gap pen-down forward (1 - gap) ] die ] end to go create-or-remove-cars ask barriers [ manage-barrier ] ask cars [ move-forward ] ask cars with [ patience <= 0 ] [ choose-new-lane ] ask cars with [ ycor != target-lane ] [ move-to-target-lane ] tick end to manage-barrier ; barrier procedure ; if the barrier is serving a car if serving-car != nobody [ if remaining > 0 [ set remaining remaining - 1 set open? false ; the barrier is closed while serving ] ; when the serving time reaches zero, open the barrier if remaining = 0 [ set open? true ] ; once the served car has passed the barrier, ; release the barrier so it can serve the next car if open? and ([xcor] of serving-car > barrier-x + 0.6) [ set serving-car nobody set open? false ] ] ; visual: open = green, closed = red ifelse open? [ set color green set heading 90 ] [ set color red set heading 0 ] end to move-forward ; turtle procedure set heading 90 ; --- BARRIER CONTROL --- if not passed-barrier? [ let b one-of barriers with [ lane-y = [pycor] of myself ] if b != nobody [ let stop-x (barrier-x - 0.5) ; if the car has not passed the barrier and it is in the stop line or close if xcor >= (stop-x - 0.2) and xcor <= (barrier-x + 0.2) [ ; if the barrier is free, the car is asigned as serving-car and the process starts if [serving-car] of b = nobody [ ask b [ set serving-car myself set remaining barrier-wait-min + random (barrier-wait-max - barrier-wait-min + 1) set open? false ] ] ; if the car is not the one being served or the barrier ; is not open --> it stops at stop-x if ([serving-car] of b != self) or (not [open?] of b) [ set xcor stop-x set speed 0 stop ] ; if the car is the one being serves, continue as normal ] ] ] ; --- END BARRIER CONTROL --- speed-up-car ; we tentatively speed up, but might have to slow down let blocking-cars other cars in-cone (1 + speed) 180 with [ y-distance <= 1 ] let blocking-car min-one-of blocking-cars [ distance myself ] if blocking-car != nobody [ ; match the speed of the car ahead of you and then slow ; down so you are driving a bit slower than that car. set speed [ speed ] of blocking-car slow-down-car ] forward speed ; marc as the car passed the toll if (not passed-barrier?) and xcor > (barrier-x + 0.6) [ set passed-barrier? true ] ; after the wrap, the car has to pass the toll again if xcor < (min-pxcor + 1) [ set passed-barrier? false ] end to slow-down-car ; turtle procedure set speed (speed - deceleration) if speed < 0 [ set speed deceleration ] ; every time you hit the brakes, you loose a little patience set patience patience - 1 end to speed-up-car ; turtle procedure set speed (speed + acceleration) if speed > top-speed [ set speed top-speed ] end to choose-new-lane ; turtle procedure ; Choose a new lane among those with the minimum ; distance to your current lane (i.e., your ycor). let other-lanes remove ycor lanes if not empty? other-lanes [ let min-dist min map [ y -> abs (y - ycor) ] other-lanes let closest-lanes filter [ y -> abs (y - ycor) = min-dist ] other-lanes set target-lane one-of closest-lanes set patience max-patience ] end to move-to-target-lane ; turtle procedure set heading ifelse-value target-lane < ycor [ 180 ] [ 0 ] let blocking-cars other cars in-cone (1 + abs (ycor - target-lane)) 180 with [ x-distance <= 1 ] let blocking-car min-one-of blocking-cars [ distance myself ] ifelse blocking-car = nobody [ forward 0.2 set ycor precision ycor 1 ; to avoid floating point errors ] [ ; slow down if the car blocking us is behind, otherwise speed up ifelse towards blocking-car <= 180 [ slow-down-car ] [ speed-up-car ] ] end to-report x-distance report distancexy [ xcor ] of myself ycor end to-report y-distance report distancexy xcor [ ycor ] of myself end to select-car ; allow the user to select a different car by clicking on it with the mouse if mouse-down? [ let mx mouse-xcor let my mouse-ycor if any? cars-on patch mx my [ ask selected-car [ set color car-color ] set selected-car one-of cars-on patch mx my ask selected-car [ set color red ] display ] ] end to-report car-color ; give all cars a blueish color, but still make them distinguishable report one-of [ blue cyan sky ] + 1.5 + random-float 1.0 end to-report number-of-lanes report num-of-lanes end ; See Info tab for full copyright and license.
There are 2 versions of this model.
This model does not have any ancestors.
This model does not have any descendants.
Download this model