Parking

No preview image

1 collaborator

Default-person Murilo Moraes (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.0.5 • Viewed 243 times • Downloaded 26 times • Run 0 times
Download the 'Parking' 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
[
  selected-car   ;; the currently selected car
]

turtles-own
[
  speed         ;; the current speed of the car
  speed-limit   ;; the maximum speed of the car (different for all cars)
  lane          ;; the current lane of the car
  target-lane   ;; the desired lane of the car
  park-time     ;; the driver's current patience
  park          ;; the driver's probability to be searching for a parking space
  park?         ;; true if the car wants to park
  parked?       ;; true if the car is parked
  leaving?      ;; true if the car is leaving
  just-parked?  ;; true if the car has recently parked
  just-count    ;; time to park
]

to setup
  clear-all
  draw-road
  set-default-shape turtles "car"
  crt number
  [ setup-cars ]
  crt 20
  [ setup-parked ]
  reset-ticks
end 

to draw-road
  ask patches
  [
    set pcolor green
    if ((pycor > -1) and (pycor < 2))
    [ set pcolor gray ]
    if ((pycor = 1) and ((pxcor mod 5) = 0)) and (pxcor != -60) and (pxcor != 60)
    [ set pcolor yellow ]
    if ((pycor = 2) or (pycor = -1) or (((pxcor = -1) or (pxcor = 1)) and (pycor < -1)))
    [ set pcolor black ]
    if ((pycor = 2) or (pycor = -1))
    [ set pcolor black ]
    if ((pxcor = 0) and (pycor < 0))
    [ set pcolor gray ]
  ]
end 

to setup-parked
  set color blue
  set park? false
  set park 1000
  set parked? true
  setxy (random 120) 1
  set heading 90
  separate-parked
  set heading 180
  set park-time 1 + random parking-time
  set speed-limit  1
  set just-parked? false
end 

to separate-parked
  if any? other turtles-here [ fd 1
    separate-parked]
  if (pcolor != yellow) [ fd 1
    separate-parked]
end 

to setup-cars
  set color black
  setxy random-xcor 0
  set heading 90
  
  ifelse (gb-park = true)
  [set park random 1000]
  [set park 1000]
  
  ifelse (park > park-ratio * 10)
  [set park? false
   set speed  0.1 + random-float .9
   set speed-limit  1
   set park-time 0
    ]
  [set park? true
   set park-time 1 + random parking-time
   set speed  0.1 + random-float ((park-drive-speed - 10 ) / 100) ;; cars that are looking for a parking space are slower
   set speed-limit  park-drive-speed / 100
   set color red
   ]
  set just-parked? false
  set parked? false
  ;; make sure no two cars are on the same patch
  separate-cars
end 

to separate-cars  ;; turtle procedure
  if any? other turtles-here
    [ fd 1
      separate-cars ]
end 

;; All turtles look first to see if there is a turtle directly in front of it,
;; if so, set own speed to front turtle's speed and decelerate.  Otherwise, if
;; look-ahead is set for 2, look ahead one more patch and do the same.  If no front
;; turtles are found, accelerate towards speed-limit

to drive
  ;; first determine average speed of the cars
  ask turtles
  [ ifelse (parked? = true)
  [leave-parkspace]
  [
   ifelse ([pcolor] of (patch-at 0 1) = yellow) and (not any? turtles-at 0 1) and (park? = true)
   [park-car]
   [
   ifelse ([pcolor] of (patch-at 1 0) = red)
   [set speed 0]
   [ 
    ifelse (any? turtles-at 1 0)
    [
      set speed ([speed] of (one-of (turtles-at 1 0)))
      decelerate
    ]
    [
      ifelse (look-ahead = 2)
      [
        ifelse (any? turtles-at 2 0)
        [
          set speed ([speed] of (one-of turtles-at 2 0))
          decelerate
        ]
        [accelerate]
      ]
      [accelerate]
    ]
    
    if (speed < 0.01)
    [ set speed 0.01 ]
    if (speed > speed-limit)
    [ set speed speed-limit ]
    
    ;; Control for making sure no one crashes.
    
    if ((xcor + speed > 60) or ((xcor + speed > 0) and  (xcor + speed < 1.1))) and (leaving? = true) [setup-newcars]
    ifelse (any? turtles-at 1 0) and (xcor != min-pxcor - .5)
    [ set speed [speed] of (one-of turtles-at 1 0) ]
    [
      ifelse ((any? turtles-at 2 0) and (speed > 1.0))
      [
        set speed ([speed] of (one-of turtles-at 2 0))
        fd 1
      ]
      [jump speed]
    ]
   ]
  ]
  ]
  ]
  tick
end 

to setup-newcars
  set color black
  
  ifelse (gb-park = true)
  [set park random 1000]
  [set park 1000]
  
  ifelse (park > park-ratio * 10)
  [set park? false
   set leaving? true
   set speed  0.1 + random-float .9
   set speed-limit  1
   set park-time 0
    ]
  [set park? true
   set leaving? false
   set park-time 1 + random parking-time
   set speed  0.1 + random-float ((park-drive-speed - 10 ) / 100) ;; cars that are looking for a parking space are slower
   set speed-limit  park-drive-speed / 100
   set color red
   ]

  set parked? false
  ;; make sure no two cars are on the same patch
  separate-cars
end 

to park-car
      ask patch-at 0 0
     [set pcolor red]
      set heading 0
      fd 1
      set heading 180
      set leaving? false
      set park? false
      set parked? true
      set color blue 
      set just-parked? true
      set just-count time-to-park
end 

to leave-parkspace
 ifelse (just-parked? = true)
 [
   ifelse (just-count > 0)
 [set just-count just-count - 1]
 [ask patch-at 0 -1
      [set pcolor gray]
  set just-parked? false
  set just-count time-to-park
   ]
 ]
 [ 
    ifelse (park-time > just-count)
    [set park-time park-time - 1]
    [
    ifelse (any? turtles-at 0 1) or (park-time > 0)
    [ask patch-at 0 -1
      [set pcolor red]
    set park-time park-time - 1
      ]
    [
      fd 1
      ask patch-at 0 0
     [set pcolor gray]
      set heading 90
      set leaving? true
      set park? false
      set parked? false
      set color black
      set speed  0.1 + random-float .9
      set speed-limit  1
      set park-time 0
    ]
    ]
 ]
end 

;; increase speed of cars

to accelerate  ;; turtle procedure
  set speed (speed + (speed-up / 1000))
end 

;; reduce speed of cars

to decelerate  ;; turtle procedure
  set speed (speed - (slow-down / 1000))
end 

There is only one version of this model, created almost 7 years ago by Murilo Moraes.

Attached files

No files

This model does not have any ancestors.

This model does not have any descendants.