BML Model with rerouting

BML Model with rerouting preview image

This model is seeking new collaborators — would you please help?

1 collaborator

Default-person Jorge Laval (Author)

Tags

pedestrians 

Tagged by Jorge Laval 3 months ago

traffic flow/ 

Tagged by Jorge Laval 3 months ago

urban congestion 

Tagged by Jorge Laval 3 months ago

Visible to everyone | Changeable by everyone
Model was written in NetLogo 6.4.0 • Viewed 92 times • Downloaded 3 times • Run 0 times
Download the 'BML Model with rerouting' 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

globals [
  average-speed   ; average speed of all cars
  actual-density
  prop-orange
]

turtles-own [
  move-flag       ; auxiliary variable to track if a car should move
  speed           ; speed of the car
]

to setup
  clear-all
  set-default-shape turtles "default"  ; use the default arrow shape
  ;ask patches [  set pcolor gray]

  ; Set up the plot
  ;set-current-plot "Average Speed"
  ;set-plot-x-range 0 100  ; Adjust x-axis range as needed
  ;set-plot-y-range 0 1   ; Adjust y-axis range as needed

  ; Place cars randomly on the grid and assign a speed
  ask patches [
    if random-float 1 < density [ ; approximately 30% of the cells will have cars
      sprout 1 [
        ifelse random-float 1 < 0.5 [
          set heading 90   ; East (right)
          set color cyan
        ] [
          set heading 180  ; South (down)
          set color orange
        ]
        set speed 1  ; Speed can be adjusted if needed
        set move-flag false
      ]
    ]
  ]

  ; Initialize global variables
  set average-speed 1
  set actual-density count turtles / count patches
  set prop-orange 100 * count turtles with [ color = orange ] / count turtles

  reset-ticks
end 

to go
  ;; 180 Move Stage 1: Identify which cars should move
  ask turtles with [ heading = 180] [
    let next-patch patch-ahead 1  ; Get the patch directly ahead based on heading
    ifelse not any? turtles-on next-patch [  set move-flag true    ] [
      set move-flag false
      set speed 0  ; Cars that cannot move have their speed set to 0
    ]
  ]
  ;; Move Stage 2: Perform the moves based on `move-flag`
  ask turtles with [move-flag and heading = 180] [
    set speed 1  ; Cars that can move have their speed set to 1
    move-to patch-ahead 1
  ]
 ;; Re-route
  if prob_reroute > 0 [
    ask turtles with [not move-flag and heading = 180] [
      if random-float 1 < prob_reroute [
        set heading 90
        set color cyan
      ]
    ]
  ]

  ;; 90 Move Stage 1: Identify which cars should move
  ask turtles with [ heading = 90] [
    let next-patch patch-ahead 1  ; Get the patch directly ahead based on heading
    ifelse not any? turtles-on next-patch [  set move-flag true    ] [
      set move-flag false
      set speed 0  ; Cars that cannot move have their speed set to 0
    ]
  ]
  ;; Move Stage 2: Perform the moves based on `move-flag`
  ask turtles with [move-flag  and heading = 90] [
    set speed 1  ; Cars that can move have their speed set to 1
    move-to patch-ahead 1
  ]
 ;; Re-route
  if prob_reroute > 0 [
   ask turtles with [not move-flag and heading = 90] [
    if random-float 1 < prob_reroute [
      set heading 180
      set color orange
    ]
  ]]

  ; Calculate the average speed
  set average-speed mean [speed] of turtles
  ;if average-speed = 0 [ stop ]
  set prop-orange 100 * count turtles with [ color = orange ] / count turtles

  ; Clear the auxiliary variable for the next iteration
  ;ask turtles [ set move-flag false ]

  tick
end 

; Public Domain:
; To the extent possible under law, Jorge Laval has waived all
; copyright and related or neighboring rights to this code.

There is only one version of this model, created 3 months ago by Jorge Laval.

Attached files

File Type Description Last updated
BML Model with rerouting.png preview Preview for 'BML Model with rerouting' 3 months ago, by Jorge Laval Download

This model does not have any ancestors.

This model does not have any descendants.