Traffic with lane changing
No preview image
Model was written in NetLogo 5.0.2
•
Viewed 1635 times
•
Downloaded 129 times
•
Run 0 times
Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)
Comments and Questions
Please start the discussion about this model!
(You'll first need to log in.)
Click to Run Model
globals [ loop-counter roads lane-ycord west-lane-one west-lane-two east-lane-one east-lane-two max-speed-limit min-speed-limit lead-car lead-car-moves prev-xcor prev-ycor car-ahead ] breed [ dividers divider ] breed [ cars car ] cars-own [ speed lane previous-x agressive? ] to setup clear-all setup-display set max-speed-limit 1 set min-speed-limit 0 setup-cars watch lead-car reset-ticks end to go ask lead-car [ set prev-xcor xcor set prev-ycor ycor ] cars-drive tick ask lead-car [ set lead-car-moves (lead-car-moves + distancexy prev-xcor prev-ycor) ] ; plot-data end to cars-drive ask cars [ ifelse (any? vehicles-directly-ahead) [ let new-lane try-change-lane if (new-lane = nobody) [ ; couldn't change lane set speed [ speed ] of vehicle-directly-ahead slow-down ] ; so slow down ] [ speed-up ; else speed up ] forward speed ] end to-report try-change-lane if (divider?) [ report nobody ; no lane changing allowed ] let dist abs(previous-x - xcor) if (dist > 0 and dist < 5) [ report nobody ; make a car that just changed lane wait before going back ] if (not agressive?) [ report nobody ] let open-patch change-to-fast-lane if (open-patch != nobody) [ report open-patch ] report change-to-slow-lane end to-report change-to-fast-lane let y 0 let on-left patch-left-and-ahead 90 2 ask on-left [ set y abs(pycor) ] if (y = 2 and not any? cars-on on-left) [ set previous-x xcor set label (label + 1) move-to on-left report on-left ] report nobody end to-report change-to-slow-lane let y 0 let on-right patch-right-and-ahead 90 2 ask on-right [ set y abs(pycor) ] if (y = 4 and not any? cars-on on-right) [ set previous-x xcor set label (label + 1) move-to on-right report on-right ] report nobody end to-report vehicles-directly-ahead ifelse (agressive?) [ report cars-on patch-ahead 1 ] [ report (turtle-set (cars-on patch-ahead 1) (cars-on patch-ahead buffer)) ] end to-report vehicle-directly-ahead report one-of vehicles-directly-ahead end to setup-cars set-default-shape turtles "car-west" set west-lane-one 2 set west-lane-two 4 setup-traffic 270 west-lane-one west-lane-two set east-lane-one -2 set east-lane-two -4 set-default-shape turtles "car-east" setup-traffic 90 east-lane-one east-lane-two set lead-car one-of cars ask lead-car [ set color red if (not divider?) [ set agressive? true set label 0 ] ] end to setup-traffic [ direction fast-lane slow-lane ] create-cars total-cars [ set color one-of [ 5 27 36 45 55 65 75 85 95 105 115 125 135 ] ; skip red set heading direction ifelse ((random 2) = 0) [ set lane-ycord fast-lane ] [ set lane-ycord slow-lane ] setxy random-xcor lane-ycord set agressive? false if (not divider?) [ set previous-x max-pxcor if ((random 5) = 0) [ ; roughly 1 in 5 get to be lane changers set agressive? true set label 0 ] ] set speed 0.1 + random-float 0.9 ; set speed random max-speed-limit avoid-collision ;locate-empty-road-spot ] end to avoid-collision set loop-counter (loop-counter + 1) let max-iterations 100 ; let cars-near (turtle-set (turtles-on patch-ahead 1) (turtles-here) (turtles-on patch-ahead -1)) ; if any? other cars-near [ if any? other cars-here [ forward random 3 if (loop-counter < 100) [ avoid-collision ] ] end to locate-empty-road-spot move-to one-of roads with [ not any? turtles-on self ] end to speed-up ; car procedure ifelse speed >= (max-speed-limit + random-float .2) [ set speed max-speed-limit ] [ set speed speed + car-acceleration ] end to slow-down ; car procedure set speed (speed - car-deceleration) if speed < min-speed-limit [ set speed min-speed-limit ] end to setup-display setup-median setup-dividers setup-grass end to setup-median ask patches [ if (pycor = 0) [ set pcolor yellow ] ] end to setup-dividers let c max-pycor / 2 setup-divider c setup-divider (- c) end to setup-divider [ y ] create-dividers 1 [ set shape "line" set color white ; lanes on California roads are white setxy min-pxcor y ; start on the far left set heading 90 ; draw towards the right ] let line-length 2 ; default let line-spacing 1 ; default if (divider?) [ ; user can choose if cars are allowed to change lanes set line-length 1 set line-spacing 0 ] ; ask dividers [ ; paint-line line-length line-spacing ; while [ xcor > min-pxcor ] [ ; paint-line line-length line-spacing ; ] ; ] ; this is cleaner than the above repeat (world-width / (line-length + line-spacing)) [ ask dividers [ paint-line line-length line-spacing ] ] ask dividers [ die ] ; don't need the line painting agents any longer end to paint-line [ line-length line-spacing ] pen-down forward line-length pen-up forward line-spacing end to setup-grass ask patches [ if (pycor > (max-pycor - 1) or pycor < (min-pycor + 1)) [ set pcolor green ] ] end
There are 5 versions of this model.
Attached files
File | Type | Description | Last updated | |
---|---|---|---|---|
CarlEdwardsCOMP575project.pptx | powerpoint | Class presentation | over 11 years ago, by Carl Edwards | Download |
This model does not have any ancestors.
This model does not have any descendants.