📄 ---trafficmanagementmendoza.nlogo
字号:
;global variablesglobals [ clock ;time steps cars-size ;the size (height as well as width) of the vehicles lane-width ;the width of the lanes speed-limit ;speed limit for the vehicles total-finished-cars ;number of cars that have finished their trip total-cars ;number of cars that have been spawned (finished and unfinished) active-cars ;number of cars that are still traveling maximum-delay ;the max value that a car has been delayed expected-maximum-delay ;the expected value for maximum-delay total-delay ;the sum of the delays of all the cars that have finished their trip expected-average-delay ;the expected value for the average delay tiles ;a matrix representing the tiles in the intersection when using Reservation System model tile-width ;the width of the tiles in the intersection when using Reservation System model tile-height ;the height of the tiles in the intersection when using Reservation System model green-breed ;the turtle breed that has the green light (north, east, or none) second ;Is 1 / ticks-by-second ;;Patches working as lights north-ligths east-lights west-lights south-ligths ;;They are some list used in the experiment 3 list-Period10 list-Period30 list-Period50 list-reservation ]turtles-own [ speed max-speed delay acceleration acceleration-step deceleration-step ;speed and acceleration related variables reservation-made reservation-time reservation-xcor reservation-ycor reservation-speed ;reservation related variables reservation-tiles traveled-distance ;;The distance the car has traveled. ]breeds [ east west north south ];;;;;;;;;;;;;;;;;;;;;;;;;SETUP PROCEDURES;;;;;;;;;;;;;;;;;;;;;;;;; to setup ;;setup button ca start-variablesendto start-variables ;;set global variables to their initial values set clock 0 set lane-width 4 set cars-size 4 set tile-width (lanes * 2 * lane-width) / granularity set tile-height tile-width set tiles n-values granularity [(list -1)] set tiles n-values granularity [tiles] set speed-limit 27 set total-delay 0 set total-cars 0 set active-cars 0 set maximum-delay 0 set second 1 / ticks-by-second set total-finished-cars 0 set expected-maximum-delay (1 - alpha) * period set expected-average-delay (1 / 2) * ((1 - alpha) ^ 2) * period draw-lanes let light-height lanes * lane-width set north-ligths patches with [ pxcor <= 0 and pxcor >= (- light-height) and pycor = light-height ] set east-lights patches with [ pxcor = (- light-height) and pycor <= 0 and pycor >= (- light-height) ] set west-lights patches with [ pxcor = light-height and pycor >= 0 and pycor <= light-height ] set south-ligths patches with [ pxcor >= 0 and pxcor <= light-height and pycor = (- light-height) ] if (model = "Trafic Light") [ draw-lights ifelse green-light north [set green-breed north] [ ifelse green-light east [set green-breed east] [set green-breed "none"] ] ]end to draw-lanes ;; draws the lanes ask patches [ set pcolor green - 1 if (abs pxcor <= lane-width * lanes or abs pycor <= lane-width * lanes) [ set pcolor black ] ]endto draw-lights ;; draws the lights in the intersection when using Traffic Light model let color-east-west black let color-north-south black ifelse green-light north [ set color-north-south green set color-east-west red ] [ ifelse green-light east [ set color-north-south red set color-east-west green ] [if green-light "none" [ set color-east-west red set color-north-south red ]] ] let light-height lanes * lane-width ask east-lights [set pcolor color-east-west] ;draw east light ask west-lights [set pcolor color-east-west] ;draw west light ask north-ligths [set pcolor color-north-south] ;draw north light ask south-ligths [set pcolor color-north-south] ;draw south lightendto go ;;go button set second 1 / ticks-by-second without-interruption [ spawn-cars ;spawns new cars using the spawn probabilities move-cars ;move the cars, performs one time step set clock clock + 1 ;increases the time steps counter if (model = "Trafic Light") [ ; checks when lights have to be redrawn when using Trafic Light model if not green-light green-breed [ ifelse green-light north [set green-breed north] [ ifelse green-light east [set green-breed east] [set green-breed "none"] ] draw-lights ] ] if plotting [ do-plotting1] ]endto move-cars ;;move the cars, performs one time step ask turtles [ move ]end to spawn-cars ;;spawns new cars using the spawn probabilities ;spawn cars in east direction let ycor-east (- ((lane-width / 2) + ((random lanes) * lane-width))) if (random-float 100 < spawn-probability-E * 100) and not any? turtles-at (- screen-edge-x) ycor-east [ create-custom-east 1 [ set xcor (- screen-edge-x) set ycor ycor-east set heading 90 set-common-values ifelse not should-spawn [ die] [ set total-cars total-cars + 1 set active-cars active-cars + 1 ] ] ] ;spawn cars in west direction let ycor-west ((lane-width / 2) + ((random lanes) * lane-width)) if (random-float 100 < spawn-probability-W * 100) and not any? turtles-at (- screen-edge-x) ycor-west [ create-custom-west 1 [ set xcor (screen-edge-x) set ycor ycor-west set heading 270 set-common-values ifelse not should-spawn [ die] [ set total-cars total-cars + 1 set active-cars active-cars + 1 ] ] ] ;spawn cars in north direction let xcor-north ((lane-width / 2) + ((random lanes) * lane-width)) if (random-float 100 < spawn-probability-N * 100) and not any? turtles-at xcor-north (- screen-edge-y) [ create-custom-north 1 [ set xcor xcor-north set ycor (- screen-edge-y) set heading 0 set-common-values ifelse not should-spawn [ die] [ set total-cars total-cars + 1 set active-cars active-cars + 1 ] ] ] ;spawn cars in south direction let xcor-south (- ((lane-width / 2) + ((random lanes) * lane-width))) ;(- ((random lanes) + 1) * lane-width / 2) if (random-float 100 < spawn-probability-S * 100) and not any? turtles-at 0 (- screen-edge-y) [ create-custom-south 1 [ set xcor xcor-south set ycor (screen-edge-y) set heading 180 set-common-values ifelse not should-spawn [ die] [ set total-cars total-cars + 1 set active-cars active-cars + 1 ] ] ]endto-report should-spawn ;; reports true if the vehicle can be spawn without a collision danger (due to a vehicle ahead too close) ;; false otherwise report (not vehicle-ahead-too-close)endto set-common-values ;; set common values for turtles variables set shape "car" set color blue set size cars-size set max-speed 50 set delay 0 set speed min (list max-speed speed-limit) set acceleration 0 set acceleration-step 3 set deceleration-step 15 set reservation-made false set traveled-distance 0end;;;;;;;;;;;;;;;;;;;;;;;;;TURTLES PROCEDURES;;;;;;;;;;;;;;;;;;;;;;;;;to move;;move the car one time step ;;behavior of the driver agent (steps in paper) coast if speed < speed-limit ;and green-light breed [accelerate] if vehicle-ahead-too-close [decelerate] if (model = "Trafic Light") or (model = "Reservation") [ interact-with-intersection ] interact-with-physics ;; sets delay value for the car set delay delay + (second - (speed * second) / min(list max-speed speed-limit)) ;; sets the speed for the vehicle, according to the acceleration value set in the steps above (behavior of the agent) let d1 speed + acceleration * second ifelse (d1 > max-speed) [ set speed max-speed ] [ ifelse(d1 < 0.0) [set speed 0.0] [set speed d1] ] ;; moves one secondth of second (1 second = tikcs-by-second timesteps) in the paper it is a 1/50 fd speed * second set traveled-distance traveled-distance + speed * second ;; checks for vehicles that have to be removed from the screen (finished) ;;if (breed = east and pxcor >= screen-edge-x) or (breed = west and pxcor <= (- screen-edge-x)) or (breed = north and pycor >= screen-edge-y) or (breed = south and pycor <= (- screen-edge-y)) if traveled-distance >= screen-size-x [ ;turtle have left the domain of the simulator set total-finished-cars total-finished-cars + 1 set active-cars active-cars - 1 set total-delay total-delay + delay if delay > maximum-delay [set maximum-delay delay] ;; kill the turtle die ] endto interact-with-intersection;; interacts with intersection as specified for each control policy in section 5 of paper if speed > 0 [ if distance-to-intersection >= 0 [ ifelse (model = "Trafic Light") [ ;; Traffic Light model control policies ifelse (any? turtles-on patch-ahead (cars-size)) and (not green-light breed) ; [decelerate] [ ifelse (distance-to-intersection < cars-size) and (not green-light breed) [decelerate] [ if distance-to-intersection > 0 [ let d1 distance-to-intersection / speed let d2 next-green-period d1 let current-time (clock / (1 / second)) if d2 != current-time + d1 [ if distance-to-intersection <= speed * second + (speed * speed) / (deceleration-step) [decelerate] ] ] ]] ] [ ;; Reservation System model control policies ifelse (reservation-made) [ ifelse (can-keep-reservation) [ coast ] [ cancel-reservation make-reservation ] ] [ make-reservation ] ] ]]end to interact-with-physics;; some needed rules if (speed = max-speed) and (acceleration > 0) [coast] if (speed = 0) and (acceleration < 0) [coast] if (speed > speed-limit) [decelerate]endto-report distance-to-intersection;; reports the distance from this turtle to the beggining of the intersection let result 1000000 let intersection-width lanes * lane-width if breed = north [ set result distancexy xcor (- intersection-width) if ycor > (- intersection-width) [set result (- result)] ] if breed = south [ set result distancexy xcor intersection-width if ycor < intersection-width [set result (- result)] ] if breed = east [ set result distancexy (- intersection-width) ycor if xcor > (- intersection-width) [set result (- result)] ] if breed = west [ set result distancexy intersection-width ycor if xcor < intersection-width [set result (- result)] ] report result endto-report can-keep-reservation;; reports true if the vehicle can keep the reservation it has already made;; false otherwise let result false let d1 speed * second let d2 distance-to-intersection mod d1 let j distance-to-intersection / d1 let i j + clock let d speed let sigo true if (vehicle-ahead != nobody) [if (i <= reservation-time-of vehicle-ahead) [ set sigo false ]] if sigo [ ifelse (abs (d - reservation-speed) < 0.001) and (abs (i - reservation-time) < 1) [ set result true ] [ set result false ] ] report resultendto cancel-reservation;; called when the vehicle determined that it can not keep the reservation;; cancels the reservation it has already made set reservation-made false foreach reservation-tiles [ ;; make the tiles in the intersection available at the times the vehicle had a reservation on them set tiles replace-item (item 0 ?) tiles (replace-item (item 1 ?) (item (item 0 ?) tiles) remove (item 2 ?) (item (item 1 ?) (item (item 0 ?) tiles))) ]endto make-reservation;; reservation making process let vahead vehicle-ahead let sigo true if (vahead != nobody) [set sigo reservation-made-of vahead] if sigo [ ifelse (request-reservation) [ coast ] ;; if the proccess of requesting a reservation to the intersection is successful, the vehicle keep the same speed [ decelerate ] ;; otherwise, decelerate and it will try again at the next time step ]endto-report request-reservation;; proccess of requesting a reservation to the intersection ;; reports true if a reservation can be made;; false otherwise let result false let d1 speed * second let d2 distance-to-intersection mod d1 let steps-to-intersection floor (distance-to-intersection / d1) let i steps-to-intersection + clock let xpoint-before-reaching-intersection xcor let ypoint-before-reaching-intersection ycor if breed = north [ set ypoint-before-reaching-intersection (- (lanes * lane-width + d2)) ] if breed = east [ set xpoint-before-reaching-intersection (- (lanes * lane-width + d2)) ] if breed = south [ set ypoint-before-reaching-intersection lanes * lane-width + d2 ] if breed = west [ set xpoint-before-reaching-intersection lanes * lane-width + d2 ] let sigo true if (vehicle-ahead != nobody) [if (i <= reservation-time-of vehicle-ahead) [ set sigo false ]] if sigo [ let tiles-to-reserve intersection-is-free i speed xpoint-before-reaching-intersection ypoint-before-reaching-intersection ifelse empty? tiles-to-reserve [ ;; reservation cannot be made set result false
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -