Parking with reservation

Parking with reservation preview image

1 collaborator

Default-person zhibin chen (Author)

Tags

(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by the author
Model was written in NetLogo 5.2.0 • Viewed 260 times • Downloaded 19 times • Run 0 times
Download the 'Parking with reservation' 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

Globals [ticks1 z w t walk avgwalk]
turtles-own [parktimer distparked diststart disttraveled parked reservation_made]
patches-own [empty reserved reserved-by]
;;parked is a boolean indicating whether a car is parked or not
;;parktimer is a counter that counts down to when a parked car departs
;;ticks2 is a counter that resets every time a new car is created

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

to setup
  clear-all
  setup-patches
  setup-turtles
  reset-ticks
  set w list "null" "Walking Distance"
  set t list "null" "Cruise Distance"
  set z round(random-exponential arrival_headway)
end 

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

to setup-patches
  ask patch 0 1 [ set pcolor 75]
  ask patches with [ pycor = -1]
  [
    set empty true
    set reserved false
    set pcolor green
    if (pxcor mod 2 = 0) 
    [
      set pcolor green + 2
    ]
  ]
  ask patches with [pycor = 0]
  [
    set pcolor gray
  ]
end 

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

to setup-turtles 
  ask n-of 1 patches with [pycor = -1] 
  [                    
    sprout 1 [set parktimer round((random-exponential depart_headway))]
  ]
  
  ;print max [count turtles-here] of patches
  ask turtles with [who > 0]
    [set color blue
    set shape "car"
    set heading 90
    set pcolor red
    set empty false
    ]

  Ask turtle 0 
  [
    set color orange
    set shape "car"
    set heading 90
    set xcor (- startposition)                                           ;;This xcor indicates the spot where someone will start searching
    set ycor 0                                                           ;;ycor of 0 indicates this car is on the road
    set diststart abs(xcor)
    set parked false
    set reservation_made false
    reserve-closest-free-spot patch 0 -1
  ]
end 

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

to advance
 
  ifelse ticks = 0 []                                                   
  [                                                                     ;;This is the arrival rate/creation of new cars searching for parking
   ifelse (ticks1 >= z) 
   [  
    create-turtles 1 
    [    
          set color orange - 2
          set shape "car"
          set heading 90
          set xcor (- startposition)
          set diststart abs( xcor)
          set parked false
          reserve-closest-free-spot patch 0 -1
          set parktimer round(random-exponential depart_headway)
    ]
    set ticks1 0
    set z round(random-exponential arrival_headway)
    ]                                   
    [
      set ticks1 ticks1 + 1
    ]
  ]
  

    ask turtles with [ (color = orange) and (ycor = 0)] 
    [
      ifelse(not any? turtles-on patch-at 0 -1 and [reserved-by] of patch-at 0 -1 = self)
      [
        right 90
        forward 1
        set pcolor red
        set parked true
        set distparked abs( xcor)
        set disttraveled (xcor + startposition)
        set w lput (abs(distparked)) w
        set t lput (disttraveled) t
        set parked true
        let walk1 but-first w
        set walk but-first walk1
        set avgwalk (sum walk / length walk)
      ]
      [
        forward 1
      ]
    ]
    
    
    ask turtles with [ (color = orange - 2) and (ycor = 0)]
    [
      set color orange
    ] 
   
     ask turtles with [color = yellow] 
     [                               
       forward 1             
     ]
    
     ask turtles with [ycor = -1]                                       ;;Departure code-- Cars will depart when their parktimer is 0. Parktimer is set randomly (exponentially) upon creation.  
     [
       ifelse (parktimer <= 0 and (not any? turtles-on patch-at 0 1 ))
       [
         depart
       ]
       [
         set parktimer (parktimer - 1)
       ]
     ]
        
     ask turtles with [ (xcor = max-pxcor) and (color = yellow)] 
     [
       die
     ]   
     tick
end 


;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

to depart
  set pcolor blue
  set parked false
  set reserved-by false
         set ycor 0
          set heading 90
          set color yellow
end 
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

to reserve-closest-free-spot [destination]
  let free-spots patches with [pycor = -1 and not is-turtle? reserved-by and not any? turtles-here]
  let closest-to-destination free-spots with-min [distance destination]
  let closest-to-me min-one-of closest-to-destination [distance myself]
  ask closest-to-me
  [
    set reserved-by myself
    set pcolor magenta
  ]
end 

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

There is only one version of this model, created almost 9 years ago by zhibin chen.

Attached files

File Type Description Last updated
Parking with reservation.png preview Preview for 'Parking with reservation' almost 9 years ago, by zhibin chen Download

This model does not have any ancestors.

This model does not have any descendants.