Mumbai-Dabbawala-Main

Mumbai-Dabbawala-Main preview image

1 collaborator

Tags

(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by the author
Model was written in NetLogo 6.1.0 • Viewed 38 times • Downloaded 3 times • Run 0 times
Download the 'Mumbai-Dabbawala-Main' 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

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

Click to Run Model

extensions [ ls ]

globals
[
  grid-x-inc         ;; the amount of patches in between two roads in the x direction
  grid-y-inc         ;; the amount of patches in between two roads in the y direction
  roads              ;; agentset containing the patches that are roads
  station-1          ;; refers to a station at one end of a railway line user creates
  station-2          ;; refers to a station at another end of a railway line user creates
  names              ;; list contaning the names of stations
  collect-model-A    ;; for referencing the collect model at station-A
  collect-model-B    ;; for referencing the collect model at station-B
  collect-model-C    ;; for referencing the collect model at station-C
  deliver-model-D    ;; for referencing the deliver model at station-D
]

breed [ trains train ]
breed [stations station ]

stations-own [ name tiffins-with-me collect? next-opt-dest can-load? finished-collect?]
trains-own [ train-start train-dest tiffins-in-train moving? loaded? pick-up drop-off ]

;;;;;;;;;;;;;;;;;;;;;;
;; Setup Procedures ;;
;;;;;;;;;;;;;;;;;;;;;;

;; Initialize the display by giving the global and patch variables initial values

to setup
  ls:reset ; reset LevelSpace
  clear-all
  setup-globals
  setup-ls
  setup-roads
  setup-stations
  setup-railway
  reset-ticks
end 

;; Initialize the necessary global variables to appropriate values

to setup-globals
  set grid-x-inc world-width / 10
  set grid-y-inc world-height / 10
  set names  [ "Station-A" "Station-B" "Station-C" "Station-D"]
  set deliver-model-D 3
end 

;; Setup levelspace child models for stations at collection area

to setup-ls
  ;; create interactive levelspace child models for stations at collection area
  ls:create-interactive-models 1 "collect.nlogo"
  set collect-model-A last ls:models
  ls:create-interactive-models 1 "collect.nlogo"
  set collect-model-B last ls:models
  ls:create-interactive-models 1 "collect.nlogo"
  set collect-model-C last ls:models

  ls:let dw-capacity capacity                                      ;; the maximum number of tiffins each dabbawala can carry
  ls:let tiffins-count-per-area tiffins-count-per-area             ;; number of tiffins near each collection station
  ls:let collect-strategies collect-strategies                     ;; strategy used by the dabbawala to collect tiffins

  ;; setup levelspace childmodels for stations at collection area and initialize
  ls:ask collect-model-A [ setup "Station-A" dw-capacity tiffins-count-per-area collect-strategies]
  ls:ask collect-model-B [ setup "Station-B" dw-capacity tiffins-count-per-area collect-strategies]
  ls:ask collect-model-C [ setup "Station-C" dw-capacity tiffins-count-per-area collect-strategies]
end 

to setup-roads
  ;; initialize the patch-owned variables and color the patches to a base-color
  ask patches
  [
    set pcolor brown
    if random 100 < 70
    [ set pcolor grey - 1 ]
    if random 100 < 20
    [ set pcolor green - 1 ]
  ]
  ;; initialize the global variables that hold patch agentsets
  set roads patches with
    [(floor((pxcor + max-pxcor - floor(grid-x-inc - 1)) mod grid-x-inc) = 0) or
    (floor((pycor + max-pycor) mod grid-y-inc) = 0)]
  ask roads [ set pcolor black ]
end 

to setup-stations
  create-stations 4 [
    let xy one-of patches with [ pxcor != [pxcor] of roads
      and pycor != [pycor] of roads
      and count stations in-radius 20 = 0 ]
    setxy [pxcor] of xy [pycor] of xy
    set shape "house two story"
    set size 5
    set name item who names
    ifelse name = "Station-D"
    [ set collect? False              ;; Station-D is at the distribution area
      set can-load? True
      set finished-collect? True ]
    [ set collect? True               ;; Other stations are at the collection area
      set can-load? False
      set finished-collect? False ]
    set label name
    set tiffins-with-me []
    set next-opt-dest 0
  ]
end 

to setup-railway
  ask stations [
    let next-station min-one-of (other stations) [ distance myself ]        ;; choosing closest station as next-station
    if link-neighbor? next-station
    [ set next-station max-one-of (other stations) [ distance myself ]]
    create-link-with next-station [ set shape "rail" ]                      ;; Create rail-line between two stations
    ask patch-here [
      sprout 1 [
        set breed trains
        set shape "train passenger car"
        set size 4
        set train-start one-of stations-here
        set train-dest next-station
        set moving? False
        set loaded? False
        set tiffins-in-train []
  ]]]
end 

to select-station-1
  if mouse-down? [
    set station-1 stations with [ round xcor = round mouse-xcor and round ycor = round mouse-ycor ]
    if any? station-1 [stop]
  ]
end 

to select-station-2
  if mouse-down? [
    set station-2 stations with [ round xcor = round mouse-xcor and round ycor = round mouse-ycor  ]
    if station-2 != nobody and station-1 != nobody [
      ask station-2 [
        ifelse not link-neighbor? one-of station-1 [
          create-link-with one-of station-1 [ set shape "rail" ]
          ask patch-here [
            sprout 1 [
              set breed trains
              set size 4
              set shape "train passenger car"
              set train-start one-of station-2
              set train-dest one-of station-1
              set loaded? False
              set tiffins-in-train []
              set moving? False ]]]
        [
          ask link one-of [ who ] of station-1 one-of [ who ] of station-2 [ die]
            ask trains with [ (train-start = one-of station-2 and train-dest = one-of station-1)
            or (train-start = one-of station-1 and train-dest = one-of station-2) ]
            [ die ]
          ]
      ]
      set station-1 nobody
      set station-2 nobody
      stop ]
  ]
end 

to go
  ask stations
  [ set label (word name ": " length tiffins-with-me) ]
  find-path-to-dest
  run-train
  load-tiffins-in-train
  ask trains with [loaded?]
  [
    drop-tiffins-at-station
  ]

  ;; Run collect-model-A
  if ls:model-exists? collect-model-A
  [
    ls:ask collect-model-A [go]
    if [done?] ls:of collect-model-A
    [
      output-show "Station-A"
      output-show ( word "Collected: " length [ tiffins-dest-list ] ls:of collect-model-A )
      output-show ( word "Gap: " [delay] ls:of collect-model-A )
      output-show ( word "Time taken to collect: " [time-taken] ls:of collect-model-A )
      update-tiffins-at-station-A
    ]
  ]

  ;; Run collect-model-B
  if ls:model-exists? collect-model-B
  [
    ls:ask collect-model-B [go]
    if [done?] ls:of collect-model-B
    [
      output-show "Station-B"
      output-show ( word "Collected: " length [ tiffins-dest-list ] ls:of collect-model-B )
      output-show ( word "Gap: " [delay] ls:of collect-model-B )
      output-show ( word "Time taken to collect: " [time-taken] ls:of collect-model-B )
      update-tiffins-at-station-B
    ]
  ]

  ;; Run collect-model-C
  if ls:model-exists? collect-model-C
  [
    ls:ask collect-model-C [go]
    if [done?] ls:of collect-model-C
    [
      output-show "Station-C"
      output-show ( word "Collected: " length [ tiffins-dest-list ] ls:of collect-model-C )
      output-show ( word "Gap: " [delay] ls:of collect-model-C)
      output-show ( word "Time taken to collect: " [time-taken] ls:of collect-model-C )
      update-tiffins-at-station-C
    ]
  ]

  ;; Run deliver-model-D
  ifelse ls:model-exists? deliver-model-D
  [
    ls:ask deliver-model-D [go]
    if [done?] ls:of deliver-model-D
    [
      output-show ( word "Delivered: " [ count tiffins with [ not hidden? ]] ls:of deliver-model-D )
      output-show ( word "Gap: " [delay] ls:of deliver-model-D)
      output-show ( word "Time taken to deliver: " [time-taken] ls:of deliver-model-D )
      ls:close deliver-model-D
      stop
    ]
  ]
  [
    if count stations with [ collect? and can-load? and length tiffins-with-me = 0 ] = 3 and count trains with [ length tiffins-in-train > 0 ] = 0
    [
      setup-child-model-deliver
    ]
  ]
  tick
end 

;;;;;;;;;;;;;;;;;;;;;;
;;   Go Procedures  ;;
;;;;;;;;;;;;;;;;;;;;;;

to find-path-to-dest
  ask stations with [collect?]
  [
    let adj-stations in-link-neighbors
    set next-opt-dest min-one-of adj-stations [ distance one-of stations with [ name = "Station-D" ]]
    if finished-collect?
    [
      let prev-station stations with [ next-opt-dest = myself ]
      ifelse one-of prev-station != nobody
      [
        if not any? prev-station with [not can-load?]
        [ set can-load? True ]
      ]
      [ set can-load? True ]
    ]
  ]
end 

to update-tiffins-at-station-A
  ask stations with [ name = "Station-A" ]
  [
    ifelse tiffins-with-me = []
    [ set tiffins-with-me [ tiffins-dest-list ] ls:of collect-model-A ]
    [
      foreach [ tiffins-dest-list ] ls:of collect-model-A [ x -> set tiffins-with-me  lput x tiffins-with-me ]
    ]
    set finished-collect? True
    ls:close collect-model-A
  ]
end 

to update-tiffins-at-station-B
  ask stations with [ name = "Station-B" ]
  [
    ifelse tiffins-with-me = []
    [ set tiffins-with-me [ tiffins-dest-list ] ls:of collect-model-B ]
    [
      foreach [ tiffins-dest-list ] ls:of collect-model-B [ x -> set tiffins-with-me  lput x tiffins-with-me ]
    ]
    set finished-collect? True
    ls:close collect-model-B
  ]
end 

to update-tiffins-at-station-C
  ask stations with [ name = "Station-C" ]
  [
    ifelse tiffins-with-me = []
    [ set tiffins-with-me [ tiffins-dest-list ] ls:of collect-model-C ]
    [ foreach [ tiffins-dest-list ] ls:of collect-model-C [ x -> set tiffins-with-me  lput x tiffins-with-me ]
    ]
    set finished-collect? True
    ls:close collect-model-C
  ]
end 

to load-tiffins-in-train
  ask stations with [ collect? and can-load? ]
  [
    let pref-train trains-here with [ train-start = myself and train-dest = [ next-opt-dest ] of myself ]
    if any? pref-train and tiffins-with-me != []
    [
      ask one-of pref-train
      [
        set pick-up train-start
        set drop-off train-dest
        ifelse tiffins-in-train = []
        [
          set tiffins-in-train [ tiffins-with-me ] of myself
        ]
        [
          foreach [ tiffins-with-me ] of myself [ x -> set tiffins-in-train lput x tiffins-in-train ]
        ]
        set loaded? True
        set label length tiffins-in-train
      ]
      set tiffins-with-me []
    ]
  ]
end 

to setup-child-model-deliver
  ls:create-interactive-models 1 "deliver.nlogo"
  set deliver-model-D last ls:models
  ls:let dw-capacity capacity
  ls:let tiffins-to-deliver length  [tiffins-with-me] of station 3
  ls:let main-delivery-cors [tiffins-with-me] of station 3
  ls:let delivery-strategies delivery-strategies
  output-show (word "Total lunchboxes to deliver: " length [tiffins-with-me] of station 3 )
  ls:ask deliver-model-D [ setup "Station-D" dw-capacity tiffins-to-deliver main-delivery-cors delivery-strategies ]
end 

to run-train
  ask trains
  [
    if ticks mod ( 150 + random 50 ) = 0 or moving?
    [
      set moving? True
      if moving?
      [
        face train-dest ;; not strictly necessary, but improves the visuals a bit
        fd 0.5
        if one-of stations-here = train-dest
        [
          set moving? False
          let temp train-dest
          set train-dest train-start
          set train-start temp
        ]
      ]
    ]
  ]
end 

to drop-tiffins-at-station
  let drop-station stations-here with [ name = [[ name ] of drop-off ] of myself ]
  if any? drop-station
  [
    ask one-of drop-station
    [
      ifelse tiffins-with-me = []
      [ set tiffins-with-me [ tiffins-in-train ] of myself ]
      [ foreach [ tiffins-in-train ] of myself [ x -> set tiffins-with-me lput x tiffins-with-me ]]
      ]
    set tiffins-in-train []
    set loaded? False
    set label length tiffins-in-train
  ]
end 

There is only one version of this model, created 10 months ago by Sai Tanya Kumbharageri.

Attached files

File Type Description Last updated
Mumbai-Dabbawala-Main.png preview Preview for 'Mumbai-Dabbawala-Main' 10 months ago, by Sai Tanya Kumbharageri Download

This model does not have any ancestors.

Children:

Graph of models related to 'Mumbai-Dabbawala-Main'