Traffic Simulation w/ Music

No preview image

1 collaborator

Default-person Mehrabi hasan (Author)

Tags

(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by the author
Model was written in NetLogo 6.0.4 • Viewed 146 times • Downloaded 6 times • Run 0 times
Download the 'Traffic Simulation w/ Music' modelDownload this modelEmbed this model

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


Comments and Questions

Model Description

This model is an extension of Uri Wilensky Model with the variable of listening to different music tempos. The goal of this model is to observe if theres a relationship between the speed of music that drivers are listening too and the flow of traffic. there is an assumption that listening to faster paced music will make you drive faster.

Posted almost 4 years ago

Click to Run Model

breed [flame flames]

globals [
  lanes          ; a list of the y coordinates of different lanes
  acceleration
  deceleration
  constants
  max-patience
  crashed
  crash-rate
  music
  likert- scale
]

turtles-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
  crash?
  birth-ticks
  stopped?
  stress
  drive-quality
  countdown
]

flame-own [
  age
]

to setup
  clear-all
  reset-ticks
  set-default-shape turtles "car"
  set acceleration ((likert-scale * music) * 0.012 )
  set deceleration ((likert-scale * music) * 0.0012 )
  set max-patience random 101
  set crashed 0
  draw-road
  create-or-remove-cars
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-turtles (number-of-cars - count turtles) [
    if types? = True [
      set size random-float 1 + 1
    ]
    set color car-color
    set music random 7 + 1
    move-to one-of free road-patches
    set target-lane pycor
    set heading 90
    set top-speed (0.5 + music) + random-float 0.5
    set speed 0.5 + music
    set crash? false
    set countdown 10
    set patience random max-patience
    set stress 1
    set drive-quality ((patience - (stress + music))
  )]

  if count turtles > number-of-cars [
    let n count turtles - number-of-cars
  ]
end 

to-report free [ road-patches ] ; turtle procedure
  let this-car self
  report road-patches with [
    not any? turtles-here with [ self != this-car ]
  ]
end 

to draw-road
  ask patches [
    let choice random 2
    ifelse choice = 0
    [set pcolor green + 3]
    [set pcolor green - 1]
  ]
  repeat 20 [diffuse pcolor 0.25]
  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
  ]
  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-turtles 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-report find-obstruction-ahead
  let obstruction nobody
  ask turtles
  [ set obstruction min-one-of turtles [distance myself]
  ]
 report obstruction
end 

to-report crashed?

let meturtle self

  let obstruction-ahead-for-crash min-one-of other turtles [distance meturtle]
  report distance obstruction-ahead-for-crash < 1
end 

to grow-old
  set age age + 1
  if age > 50 [die]
end 

;stress > random 100 + 300 and random 100 = 0

to go
  create-or-remove-cars
  ask turtles [ move-forward ]
  ask turtles with [ patience <= 1 ] [ choose-new-lane ]
  ask turtles with [ ycor != target-lane ] [ move-to-target-lane ]
  ask turtles with [stress >= 400] [destress]
  ask turtles with [ycor != target-lane and random-float 6 < ((music / 400) * .0001)] [crash]
  ask turtles [look-ahead]
  tick
  ;ask turtles [ if patch-ahead 1 = "flame" [choose-new-lane]]
  ;if hazards = True and random 100 > 1 [obstruction-back]
end 

to look-ahead
  if any? flame-on neighbors [choose-new-lane]
  if any? flame-on neighbors [choose-new-lane]
  if ycor != target-lane [move-to-target-lane]
  set acceleration (0.01)
  move-forward
end 

to move-forward ; turtle procedure
  set acceleration (likert-scale * 0.0012)
  set deceleration (likert-scale * 0.012)
  set heading 90
  speed-up-car ; we tentatively speed up, but might have to slow down
  let blocking-cars other turtles 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
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 * music))
  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 turtles 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 and random 10000 = 0 [crash] [speed-up-car]
    ifelse towards blocking-car <= 180 [slow-down-car] [ speed-up-car ]
    stressed
    ]
end 

to stressed
  set stress stress + random 2
end 

to destress
  let temp1 random 7 + 1
  let temp2 random 7 + 1
  set acceleration (temp1 * 0.0012)
  set deceleration (temp2 * 0.012)
  set countdown countdown - 1
  if countdown = 0 [
    set countdown 10
    set stress 1
    set acceleration (likert-scale * 0.0012)
    set deceleration (likert-scale * 0.012)
  ]
end 

to-report x-distance
  report distancexy [ xcor ] of myself ycor
end 

to-report y-distance
  report distancexy xcor [ ycor ] of myself
end 

to crash
  accident
  set crash? true
  set crashed crashed + 1
  die
end 

to accident
  hatch-flame 1[
    set age ticks
    set heading 90
    set shape "fire"
    set acceleration 0
    set deceleration 0
    set speed 0

  ]
end 

to aging
  if ticks - age > time-crash [die]
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 





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

There is only one version of this model, created almost 4 years ago by Mehrabi hasan.

Attached files

No files

This model does not have any ancestors.

This model does not have any descendants.