Passenger Volume Simulation on Yamanote Line in Tokyo

No preview image

1 collaborator

Default-person Xiaosong Yin (Author)

Tags

transport 

Tagged by Xiaosong Yin about 9 years ago

Model group MAM-2015 | Visible to everyone | Changeable by everyone
Model was written in NetLogo 5.2.0 • Viewed 558 times • Downloaded 51 times • Run 0 times
Download the 'Passenger Volume Simulation on Yamanote Line in Tokyo' modelDownload this modelEmbed this model

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


Info tab cannot be displayed because of an encoding error

Comments and Questions

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

Click to Run Model

;;declare global variables
globals 
[
  total-length-of-tracks 
  number-of-trains 
  move-counter 
  move-counter2 
  station-volume-list 
  passengers-per-minute-list 
  total-wait-time
  emergency-on
]
;;declare breeds
breed [trains train]
breed [stations station]
breed [innerstations innerstation]
breed [passengers passenger]
stations-own
[
  station-number
  outer-loop-capacity
  how-many-expand
]
innerstations-own
[
  station-number
  inner-loop-capacity
]
passengers-own
[
  destination
  heading-direction
  boarded
  wait-time
  just-generated
]
trains-own
[
  capacity
  max-capacity
  destination-station
  have-stopped
  running-direction
  target-station
]

;setup the system

to setup
  clear-all
  ask patches 
  [
    set pcolor white
  ]
  setup-innerstations
  setup-stations
  setup-passengers
  setup-trains
  reset-ticks
end 

to setup-basics
  ;;total-length-of-tracks
  set total-length-of-tracks 0
  ask links
  [
    set total-length-of-tracks total-length-of-tracks + link-length
  ]
  set total-length-of-tracks total-length-of-tracks / 2
  ;;number-of-trains
  set number-of-trains time-each-loop / interval
  ;;read input about passenger volume at each station
  set station-volume-list read-from-string station-volume 
  ;;read input about passengers per minute
  set passengers-per-minute-list read-from-string passengers-per-minute
  ;;initialize total waiting time to 0
  set total-wait-time 0
end 

to setup-stations
  ask innerstations [
    hatch 1 [
      set breed stations
    ]
  ]
  ask stations
  [
    create-links-from stations with [station-number = [station-number + 1] of myself mod count stations]
    set shape "circle"
    set color blue
  ]
  setup-basics
end 

to setup-innerstations
  let station-number-temp 0
  create-ordered-innerstations number-of-stations
  [
    set station-number station-number-temp
    fd 12
    set station-number-temp station-number-temp + 1
  ]
  ask innerstations
  [
    create-links-to innerstations with [station-number = [station-number + 1] of myself mod count innerstations]
    set shape "circle"
    set color blue
  ]
  layout-circle sort-on [station-number] innerstations 12
end 

to setup-passengers
  generate-passengers 1
end 

to generate-passengers [case]
  ask stations
  [
    ;;case 1: generate passengers initially. 
    ;;case 2: generate passengers while the model is running
    ifelse case = 1
    [
      hatcher-passenger (item station-number station-volume-list)
    ]
    [
      hatcher-passenger (random-poisson (item station-number passengers-per-minute-list))
    ]
    ;;update outer_loop capacity/inner_loop capacity
    let outer-temp 0
    let inner-temp 0
    ask passengers-here
    [
      ifelse heading-direction = 2
      [
        set outer-temp outer-temp + 1
      ]
      [
        set inner-temp inner-temp + 1
      ]
    ]
    set outer-loop-capacity outer-temp
    ask one-of innerstations-here
    [set inner-loop-capacity inner-temp]
  ]
end 

to hatcher-passenger [number]
    hatch-passengers number
    [
      set boarded 0
      set wait-time 0
      set just-generated 1
      ;;assign each passenger a random station
      set destination ((random (number-of-stations - 1)) + 1)
      if destination <= [station-number] of myself
      [
        set destination destination - 1
      ]
      ;;determine the heading direction of each passenger
      let station-number-new 0
      ifelse ([station-number] of myself - destination) < 0 
      [ set station-number-new ([station-number] of myself) + number-of-stations ]
      [ set station-number-new [station-number] of myself ]
      ifelse (station-number-new - destination < number-of-stations / 2) 
      [ set heading-direction 2 ] ;;counterclockwise or outer
      [ set heading-direction 1 ] ;;clockwise or inner 
    ]
end 

to setup-trains
  ;;generate all of the trains at one station first
  ask stations with [station-number = 0]
  [
    hatch-trains number-of-trains
    [ 
      set capacity 0
      set max-capacity max-capacity-of-train
      set destination-station (list 0)
      let list-size number-of-stations - 1
      while [list-size != 0]
      [
        set destination-station sentence destination-station 0
        set list-size list-size - 1
      ]
      set running-direction 2
      set target-station number-of-stations - 1
    ]
    ;;get the trains move according to intervals
    let sequence number-of-trains - 1
    ask trains with [running-direction = 2]
    [
       setup-trains-move (total-length-of-tracks / number-of-trains * sequence)
       set sequence sequence - 1
    ]     
  ]
  ;;generate inner stations
  ask innerstations with [station-number = 0]
  [
    hatch-trains number-of-trains
    [ 
      set capacity 0
      set max-capacity max-capacity-of-train
      set destination-station (list 0)
      let list-size number-of-stations - 1
      while [list-size != 0]
      [
        set destination-station sentence destination-station 0
        set list-size list-size - 1
      ]
      set running-direction 1
      set target-station 1
    ]
    ;;get the trains move according to intervals
    let sequence number-of-trains - 1
    ask trains with [running-direction = 1]
    [
       setup-trains-move-inner (total-length-of-tracks / number-of-trains * sequence)
       set sequence sequence - 1
    ]     
  ]
end 

to setup-trains-move [steps]
  let steps-taken 0
  let station-now 0
  let link-ahead 0
  ask stations with [station-number = station-now]
  [
    set link-ahead out-link-to one-of out-link-neighbors
  ]
  set heading [link-heading] of link-ahead
  while [steps-taken <= steps]
  [
    fd 0.2 
    set steps-taken steps-taken + 1 / 5
    if one-of stations-here != nobody and [station-number] of one-of stations-here = target-station
    [
      set xcor [xcor] of one-of stations-here
      set ycor [ycor] of one-of stations-here
      set station-now (station-now + 1) mod number-of-stations
      ask one-of stations-here
      [
        set link-ahead out-link-to one-of out-link-neighbors
      ]
      set heading [link-heading] of link-ahead
      set target-station (target-station - 1)
      if target-station < 0
      [
        set target-station target-station + number-of-stations
      ]
    ]
  ]
  set shape "train"
end 

to setup-trains-move-inner [steps]
  let steps-taken 0
  let station-now 0
  let station-already 0
  let link-ahead 0
  ask innerstations with [station-number = station-now]
  [
    set link-ahead out-link-to one-of out-link-neighbors
  ]
  set heading [link-heading] of link-ahead
  while [steps-taken <= steps]
  [
    fd 0.2
    set steps-taken steps-taken + 1 / 5
    if one-of innerstations-here != nobody and [station-number] of one-of stations-here = target-station
    [
      set xcor [xcor] of one-of innerstations-here
      set ycor [ycor] of one-of innerstations-here
      set station-now (station-now + 1) mod number-of-stations
      ask one-of innerstations-here
      [
        set link-ahead out-link-to one-of out-link-neighbors
      ]
      set heading [link-heading] of link-ahead
      set target-station (target-station + 1) mod number-of-stations
    ]
  ]
  set shape "train"
end 

;;running codes

to go
  ;;move-counter: makes the train move every 100 ticks. 
  ;;move-counter2: makes the passengers be generated every 1000 ticks (1 physically minute)
  set move-counter move-counter + 1
  set move-counter2 move-counter2 + 1
  ;emergency
  ;;move only when there is no emergency
  if(move-counter = 100 and emergency-on = 0)
  [
    ask trains with [running-direction = 2]
    [
        move-train
    ]
    ask trains with [running-direction = 1]
    [
      move-train-inner
    ]
    set move-counter 0
  ]
  if emergency-on = 1
  [ set move-counter 0]
  if(move-counter2 = 1000)
  [
    ask passengers with [boarded = 0 and just-generated = 0]
    [
      set wait-time wait-time + 1
    ]
    set total-wait-time 0
    ask passengers
    [
      set total-wait-time total-wait-time + wait-time
    ]
    ask passengers with [just-generated = 1] [ set just-generated 0]
    generate-running-passengers
    if represent-station-volume-on = 1
    [represent-station-volume]
    set move-counter2 0
  ]
  tick
end 

to move-train
  ifelse(one-of stations-here = nobody)
  [
    ;;have to move very small steps, so /10 is added
    fd total-length-of-tracks / time-each-loop / 10
  ]
  ;;if at a station
  [
  ifelse(one-of stations-here != nobody and target-station = [station-number] of one-of stations-here)
  [
    ;;if the train has stopped here. The train stops at a station for two sets of 100 ticks
    ifelse (have-stopped = 1)
    [
      set have-stopped 0
      ;;new direction once stopped at a station
      let link-ahead 0
      let station-here [station-number] of one-of stations-here
      ask one-of stations-here
      [
        set link-ahead out-link-to one-of out-link-neighbors
      ]
      set heading [link-heading] of link-ahead
      fd total-length-of-tracks / time-each-loop / 10
      set target-station target-station - 1
      if target-station < 0
      [
        set target-station target-station + number-of-stations
      ]
    ]
    ;;if the train just arrives and haven't stopped here
    [
      set xcor [xcor] of one-of stations-here
      set ycor [ycor] of one-of stations-here
      stop-at-station 2
      set have-stopped have-stopped + 1
    ]
  ]
  [ fd total-length-of-tracks / time-each-loop / 10 ]
  ]
end 

to move-train-inner
  ifelse(one-of innerstations-here = nobody)
  [
    fd total-length-of-tracks / time-each-loop / 10
  ]
  [
  ifelse(one-of innerstations-here != nobody and target-station = [station-number] of one-of innerstations-here)
  [
    ifelse (have-stopped = 1)
    [
      set have-stopped 0
      ;;new direction once stopped at a station
      let link-ahead 0
      let station-here [station-number] of one-of innerstations-here
      ask one-of innerstations-here
      [
        set link-ahead out-link-to one-of out-link-neighbors
      ]
      set heading [link-heading] of link-ahead
      fd total-length-of-tracks / time-each-loop / 10
      set target-station (target-station + 1) mod number-of-stations
    ]
    [
      set xcor [xcor] of one-of innerstations-here
      set ycor [ycor] of one-of innerstations-here
      stop-at-station 1
      set have-stopped have-stopped + 1
    ]
  ] 
  [ fd total-length-of-tracks / time-each-loop / 10 ]
  ]
end 

to stop-at-station [direction-arrival]
  let mylist destination-station
  let myCapacity capacity
  let disembarked2 0
  let disembarked 0
  let embarked2 0
  let embarked 0
  ;DISEMBARK
  ;get the station number
  let station-here 0
  ;;splits code based on whether it's outer loop or inner loop and update accordingly.
  ifelse direction-arrival = 2
  [
    set station-here [station-number] of one-of stations-here
    set disembarked2 item station-here destination-station
    set mylist replace-item (station-here) (mylist) (0)
    set myCapacity myCapacity - disembarked2
    ;embark
    set embarked2 0
    ask passengers-here with [boarded = 0 and heading-direction = 2]
    [ 
      if (myCapacity < [max-capacity] of myself)
      [
        let i destination
        set mylist replace-item (destination) (mylist) ((item i mylist) + 1)
        set myCapacity myCapacity + 1
        set embarked2 embarked2 + 1
        set boarded 1
        set wait-time 0
        die
      ]
    ]
  ]
  ;; inner loop
  [
    set station-here [station-number] of one-of innerstations-here
    set disembarked item station-here destination-station
    set mylist replace-item (station-here) (mylist) (0)
    set myCapacity myCapacity - disembarked
    ;embark
    set embarked 0
    ask passengers-here with [boarded = 0 and heading-direction = 1]
    [ 
      if (myCapacity < [max-capacity] of myself)
      [
        let i destination
        set mylist replace-item (destination) (mylist) ((item i mylist) + 1)
        set myCapacity myCapacity + 1
        set embarked embarked + 1
        set boarded 1
        set wait-time 0
        die
      ]
    ]
  ]
  ;update train
  set capacity myCapacity
  set destination-station mylist 
  ;update station
  ifelse direction-arrival = 2
  [
    ask one-of stations-here
    [
      set outer-loop-capacity outer-loop-capacity + disembarked2 - embarked2
    ]
  ]
  [
    ask one-of innerstations-here
    [
      set inner-loop-capacity inner-loop-capacity + disembarked - embarked
    ]
  ]
end 

to generate-running-passengers
  generate-passengers 2
end 

;;the station turtles grow in size once the number of people grow here

to represent-station-volume
  ask stations
  [
    set how-many-expand int (outer-loop-capacity / level)
    let how-many-expand-temp 0
    ask one-of innerstations with [station-number = [station-number] of myself]
    [
      set how-many-expand-temp how-many-expand-temp + int (inner-loop-capacity / level)
    ]
    set how-many-expand how-many-expand + how-many-expand-temp 
    if how-many-expand > 4
    [ set how-many-expand 4]
    set size how-many-expand + 1  
    set shape "circle"
  ]
end 

;handles emergency button being pressed

to emergency
  ifelse emergency-on = 0
  [set emergency-on 1]
  [set emergency-on 0]
end 

There are 4 versions of this model.

Uploaded by When Description Download
Xiaosong Yin about 9 years ago Final Version Download this version
Xiaosong Yin about 9 years ago Final Download this version
Xiaosong Yin about 9 years ago Network representation Download this version
Xiaosong Yin about 9 years ago Initial upload Download this version

Attached files

File Type Description Last updated
EECS 372 Progress Report 2.docx word v2 about 9 years ago, by Xiaosong Yin Download
EECS 372 Project Progress Report 1.docx word v1 about 9 years ago, by Xiaosong Yin Download
EECS 372 Project Proposal.docx word Original Proposal about 9 years ago, by Xiaosong Yin Download
EECS372 Progress Report 3.docx word v3. Network representation about 9 years ago, by Xiaosong Yin Download
Final Poster.pdf pdf Poster for the poster fair about 9 years ago, by Xiaosong Yin Download
Final Report on the NetLogo Project.docx word Final Report about 9 years ago, by Xiaosong Yin Download
Xiaosong_Yin_Slam.pptx pdf Presentation Slam about 9 years ago, by Xiaosong Yin Download

This model does not have any ancestors.

This model does not have any descendants.