📄 aaa---sotl.nlogo
字号:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Variable declarations ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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 acceleration ;; the constant that controls how much a car speeds up or slows down by if it is to accelerate or decelerate phase ;; keeps track of the phase clock ;; keeps track of the total times thru the go procedure num-cars-stopped ;; the number of cars that are stopped during a single pass thru the go procedure display-which-metric old-display-which-metric ;; holds the value of display-which-metric for the last time through the go procedure ;; patch agentsets intersections ;; agentset containing the patches that are intersections roads ;; agentset containing the patches that are roads vroads ;; agentset containing the patches that are vertical roads hroads ;; agentset containing the patches that are horizontal roads Sroads ;; Southbound roads Nroads ;; Northbound roads Eroads ;; Eastbound roads Wroads ;; Westbound roads exits ;; agentset containing the patches where cars will go out of simulation if torus off gates ;; agentset containing the patches where cars will sprout from if torus off ;vgates ;; vertical gates ;hgates ;; horizontal gates Ngates ;; Northbound gates Sgates ;; Southbound gates Egates ;; Eastbound gates Wgates ;; Westbound gates ;; string and list variables that hold data passed accumulated in the model run wait-data ;; list that holds the average wait time of the cars for each pass through the go procedure stopped-data ;; list that holds the number of stopped of the cars for each pass through the go procedure speed-data ;; list that holds the average speed of the cars for each pass through the go procedure cars-data ;; list that holds the number of cars for each pass through the go procedure time-data ;; list that holds the value of clock for each pass through the go procedure avg-range ;; range (time steps) for plot averages (blue lines) ;; vectors for keeping averages in range stopped-avgs speed-avgs wait-avgs]turtles-own[ speed ;; the speed of the turtle v-car? ;; this will be true if the turtle moves vertically and false if it moves horizontally SE-car? ;; true if car is southboung OR eastbount (all true if four-dirs? = false) wait-time ;; the amount of time since the last time a turtle has moved]patches-own[ intersection? ;; this is true if the patch is at the intersection of two roads accident? ;; this is true if a crash has occurred at this intersection. this will never be true for a non-intersection patch green-light-v? ;; this is true if the green light is for the vertical road. otherwise, it is false. this is only true for patches that are intersections. yellow-light? ;; this is true if there is any yellow light in the intersection. otherwise, it is false. this is only true for patches that are intersections. my-row ;; this holds the row of the intersection counting from the upper left corner of the graphics window. it is -1 for patches that are not intersections. my-column ;; this holds the column of the intersection counting from the upper left corner of the graphics window. it is -1 for patches that are not intersections. ;p = ticks-per-cycle = period ;; for now global p q ;; translation; this holds the phase for the intersection. it is -1 for patches that are not intersections. pg1 ;; period of green light 1 (north-south) pg2 ;; period of green light 2 (east-west) ;;pgx... could add more directions... kappa1 ;; number of cars * number of time steps waiting (north-south) kappa2 ;; number of cars * number of time steps waiting (east-west) lambda1 ;; number of tolerances reached (kappa1 resetted) lambda2 ;; number of tolerances reached (kappa2 resetted) greensteps ;; steps since last change of lights no-corr-p ;; p for no-corr method faulty? ;; if faulty?, then use no-corr... intersection-dirs ;; indicates directions of streets at intersection ;0=SE , 1= SW, 2= NW, 3= NE dir ;; direction ;0=E , 1= S, 2= W, 3= N southbound? eastbound? northbound? westbound?];;;;;;;;;;;;;;;;;;;;;;; Setup Functions ;;;;;;;;;;;;;;;;;;;;;;;to startup if not four-dirs?[;; patch not to have empty variables even in 2dirs... cloc! set four-dirs? true setup true set four-dirs? false ] setup trueend;; Initialize the display by giving the global and patch variables initial values.;; Create num-cars of turtles if there are enough road patches for one turtle to be created per road patch.;; Setup the plots;; "setup false" is done by the re-run button.to setup [full-setup?] clear-output if full-setup? ;; We only want to clear the patches if the we are doing a full-setup [ clear-patches ] clear-turtles clear-all-plots setup-globals full-setup? ;; First we ask the patches to draw themselves and set up a few variables setup-patches full-setup? set-default-shape turtles "car" if (((number * %vertical * %southbound) / 10000) > count Sroads ) or (((number * (100 - %vertical) * %eastbound) / 10000) > count Eroads ) or (((number * %vertical * (100 - %southbound)) / 10000) > count Nroads ) or (((number * (100 - %vertical) * (100 - %eastbound)) / 10000) > count Wroads ) [ user-message "There are too many cars for the amount of road. " + "Either increase the amount of roads by increasing the GRID-SIZE-X " + "or GRID-SIZE-Y sliders, or decrease the number of cars by lowering " + "the NUMBER slider.\nThe setup has stopped." stop ] ;; Now create the turtles and have each created turtle call the functions setup-cars and set-car-color cct number [ setup-cars true ;;true for initial setup set-car-color record-data ] ;; give the turtles an initial speed ask turtles [ set-car-speed ] update-list-info setup-plotsend;; Initialize the global variables to appropriate valuesto setup-globals [full-setup?] set phase 0 set clock 0 set num-cars-stopped 0 set display-which-metric 4 set avg-range 100 if full-setup? [ set grid-x-inc screen-size-x / grid-size-x set grid-y-inc screen-size-y / grid-size-y ] ;; initialize the lists and string set wait-data [] set stopped-data [] set speed-data [] set cars-data [] set time-data [] set wait-avgs n-values avg-range [0] set stopped-avgs n-values avg-range [0] set speed-avgs n-values avg-range [0] ;; don't make acceleration 0.1 since we could get a rounding error and end up on a patch boundary set acceleration 0.099end;; Make the patches have appropriate colors, setup the roads and intersections agentsets,;; and initialize the traffic lights to one settingto setup-patches [full-setup?] if full-setup? [ ;; initialize the patch-own variables and color the patches to a base-color ask patches [ set intersection? false set accident? false set green-light-v? true set yellow-light? false set my-row -1 set my-column -1 set q -1 set no-corr-p -1 set faulty? false set intersection-dirs -1 set dir -1 set southbound? false set eastbound? false set northbound? false set westbound? false set pcolor brown + 3 ] ;; initialize the global variables that hold patch agentsets ifelse (homo?) [;homogeneous streets set roads patches with [ (floor ((pxcor + screen-edge-x - floor(grid-x-inc / 2)) mod grid-x-inc) = 0) or (floor ((pycor + screen-edge-y - floor(grid-y-inc / 2 )) mod grid-y-inc) = 0) ] set intersections roads with [ (floor ((pxcor + screen-edge-x - floor(grid-x-inc / 2)) mod grid-x-inc) = 0) and (floor ((pycor + screen-edge-y - floor(grid-y-inc / 2 ) ) mod grid-y-inc) = 0) ] set vroads roads with [ (floor ((pxcor + screen-edge-x - floor(grid-x-inc / 2 )) mod grid-x-inc) = 0)] set hroads roads with [ (floor ((pycor + screen-edge-y - floor(grid-y-inc / 2 ) ) mod grid-y-inc) = 0)] ] [;non-homo streets ;set roads patches with [ (floor ((pxcor + screen-edge-x - floor(grid-x-inc / 2)) mod grid-x-inc) = abs floor(pxcor / grid-x-inc)) or ; (floor ((pycor + screen-edge-y - floor(grid-y-inc / 2 )) mod grid-y-inc) = abs floor(pycor / grid-y-inc))] ;works nice for 8x8 set roads patches with [ (floor ((pxcor + screen-edge-x - floor(grid-x-inc / 2)) mod grid-x-inc) = floor((pxcor ^ 2 )/ (grid-x-inc ^ 2 ))) or (floor ((pycor + screen-edge-y - floor(grid-y-inc / 2 )) mod grid-y-inc) = floor((pycor ^ 2 )/ (grid-y-inc ^ 2 )))] set intersections roads with [ (floor ((pxcor + screen-edge-x - floor(grid-x-inc / 2)) mod grid-x-inc) = floor((pxcor ^ 2 )/ (grid-x-inc ^ 2 ))) and (floor ((pycor + screen-edge-y - floor(grid-y-inc / 2 ) ) mod grid-y-inc) = floor((pycor ^ 2 )/ (grid-y-inc ^ 2 ))) ] set vroads roads with [ (floor ((pxcor + screen-edge-x - floor(grid-x-inc / 2 )) mod grid-x-inc) = floor((pxcor ^ 2 )/ (grid-x-inc ^ 2 )))] set hroads roads with [ (floor ((pycor + screen-edge-y - floor(grid-y-inc / 2 ) ) mod grid-y-inc) = floor((pycor ^ 2 )/ (grid-y-inc ^ 2 )))] ] ifelse (four-dirs?)[ set Sroads vroads with [(floor ((pxcor + screen-edge-x) / grid-x-inc ) mod 2) = 0] set Nroads vroads with [(floor ((pxcor + screen-edge-x) / grid-x-inc ) mod 2) = 1] set Eroads hroads with [(floor ((pycor + screen-edge-y) / grid-y-inc ) mod 2) = 0] set Wroads hroads with [(floor ((pycor + screen-edge-y) / grid-y-inc ) mod 2) = 1] set exits roads with [(((floor ((pxcor + screen-edge-x) / grid-x-inc ) mod 2) = 0) and (pycor = (- screen-edge-y))) or (((floor ((pxcor + screen-edge-x) / grid-x-inc ) mod 2) = 1) and (pycor = screen-edge-y)) or (((floor ((pycor + screen-edge-y) / grid-y-inc ) mod 2) = 0) and (pxcor = screen-edge-x)) or (((floor ((pycor + screen-edge-y) / grid-y-inc ) mod 2) = 1) and (pxcor = (- screen-edge-x))) ] set gates roads with [(((floor ((pxcor + screen-edge-x) / grid-x-inc ) mod 2) = 0) and (pycor = screen-edge-y)) or (((floor ((pxcor + screen-edge-x) / grid-x-inc ) mod 2) = 1) and (pycor = (- screen-edge-y))) or (((floor ((pycor + screen-edge-y) / grid-y-inc ) mod 2) = 0) and (pxcor = (- screen-edge-x))) or (((floor ((pycor + screen-edge-y) / grid-y-inc ) mod 2) = 1) and (pxcor = screen-edge-x)) ] set Sgates vroads with [((floor ((pxcor + screen-edge-x) / grid-x-inc ) mod 2) = 0) and (pycor = screen-edge-y)] set Ngates vroads with [((floor ((pxcor + screen-edge-x) / grid-x-inc ) mod 2) = 1) and (pycor = (- screen-edge-y))] set Egates hroads with [((floor ((pycor + screen-edge-y) / grid-y-inc ) mod 2) = 0) and (pxcor = (- screen-edge-x))] set Wgates hroads with [((floor ((pycor + screen-edge-y) / grid-y-inc ) mod 2) = 1) and (pxcor = screen-edge-x)] ][ set Sroads vroads set Eroads hroads set exits roads with [(pxcor = screen-edge-x) or (pycor = (- screen-edge-y))] set gates roads with [(pxcor = (- screen-edge-x)) or (pycor = screen-edge-y)] set Egates gates with [(pxcor = (- screen-edge-x))] set Sgates gates with [(pycor = screen-edge-y)] ] ;0=E , 1= S, 2= W, 3= N if (four-dirs?)[ ask Wroads [ set dir 2 set westbound? true ] ask Nroads [ set dir 3 set northbound? true ] ] ask Eroads [ set dir 0 set eastbound? true ] ask Sroads [ set dir 1 set southbound? true ] ask roads [ set pcolor white ] ask gates [ set pcolor sky] ask exits [ set pcolor gray] ] setup-intersections full-setup?end;; Give the intersections appropriate values for the intersection?, my-row, and my-column ;; patch variables. Make all the traffic lights start off so that the lights are red ;; horizontally and green vertically. to setup-intersections [full-setup?] reset-traffic-lights ask intersections [ set intersection? true set green-light-v? true set yellow-light? false if full-setup? [ set my-row floor ((pycor + screen-edge-y) / grid-y-inc ) set my-column floor ((pxcor + screen-edge-x) / grid-x-inc ) ;intersection-dirs 0=SE , 1= SW, 2= NW, 3= NE ifelse southbound?[ ifelse eastbound?[ set intersection-dirs 0 ][ set intersection-dirs 1 ] ][ ifelse eastbound?[ set intersection-dirs 3 ][ set intersection-dirs 2 ] ] ] set-signal-colors ]end;; Initialize the turtle variables to appropriate values and place the turtle on an empty road patch.to setup-cars [ini?] ;; turtle procedure set speed 0 set wait-time 0 if ini?[ put-on-empty-road ] ifelse intersection? [ ifelse ((random 100) < %vertical) [ set v-car? true ] [ set v-car? false ] ;intersection-dirs 0=SE , 1= SW, 2= NW, 3= NE ifelse (v-car?)[ ifelse (intersection-dirs < 2) [ set SE-car? true ] [ set SE-car? false ] ][ ifelse (intersection-dirs = 0) or (intersection-dirs = 3) [ set SE-car? true ] [ set SE-car? false ] ] ] [ ;ifelse (floor ((pxcor + screen-edge-x - floor(grid-x-inc / 2)) mod grid-x-inc) = 0) ;dir 0=E , 1= S, 2= W, 3= N ifelse ((dir mod 2) = 1) [ set v-car? true ] ; car going in vertical direction [ set v-car? false ] ifelse (dir < 2) [ set SE-car? true ] ; car going south or east [ set SE-car? false ] ] ifelse SE-car?[ ifelse v-car? [ set heading 180 ];S [ set heading 90 ];E ][ ifelse v-car? [ set heading 0 ];N [ set heading 270 ];W ]end;; Find a road patch without any turtles on it and place the turtle there.to put-on-empty-road ;; turtle procedure locals [road] ifelse four-dirs?[ ifelse ((random 100) < %vertical)[ ifelse ((random 100) < %southbound) [set road random-one-of Sroads] [set road random-one-of Nroads] ][ ifelse ((random 100) < %eastbound) [set road random-one-of Eroads] [set road random-one-of Wroads] ] ][ ifelse ((random 100) < %vertical) [set road random-one-of vroads] [set road random-one-of hroads] ] setxy (pxcor-of road) (pycor-of road) if any? other-turtles-here [ put-on-empty-road ]end;; Initialize the plotsto setup-plots set-current-plot "% Stopped Cars" set-plot-y-range 0 100 set-current-plot-pen "default" plot num-cars-stopped set-current-plot "Average Wait Time of Cars" set-current-plot-pen "default" plot mean values-from turtles [wait-time] set-current-plot "Average Speed of Cars" set-current-plot-pen "default" set-plot-y-range 0 speed-limit plot mean values-from turtles [speed] set-current-plot "Number of Cars" set-current-plot-pen "default" plot count turtlesend;"uncorrelates" self-org. T.L.to reset-traffic-lights ask intersections [ set q 0 set pg1 (p / 2) set pg2 (p / 2) ;set pg1 round (p / 3) ;set pg2 round (2 * p / 3) ;set pg1 (p / 3) ;set pg2 (2 * p / 3) set kappa1 0 set kappa2 0 set lambda1 0 set lambda2 0 set greensteps 0 set no-corr-p (random (screen-edge-x + 3)) if yellow-light? [set-signal-colors] ]end;;;;;;;;;;;;;;;;;;;;;;;;; Runtime Functions ;;;;;;;;;;;;;;;;;;;;;;;;;;; receives information from the clients and runs the simulationto go every delay [ ;; clear any accidents from the last time thru the go procedure clear-accidents ;; if there are any intersections that are to switch automatically, have them change their color set-signals set num-cars-stopped 0 ;; set the turtles speed for this time thru the procedure, move them forward their speed, ;; record data for plotting, and set the color of the turtles ;; to an appropriate color based on their speed ask turtles [ set-car-speed fd speed record-data set-car-color ] ;; crash the cars if crash? is true if crash? [ crash-cars ] if not torus?[ cars-remove-create ] ;; update the information in the lists of plot data update-list-info ;; update the plots with the new information from this pass thru the procedure if plots? [do-plotting] ;; update the clock and the phase clock-tick ]end;;removes leaving cars, creates incoming carsto cars-remove-create ask turtles-on exits[;;if you please die ;; thank you... ] ifelse ((random 100) < %vertical) [ ifelse (((random 100) < %southbound) or (not four-dirs?)) [ ask Sgates[ if ((random (1 + number)) > count turtles) [;;need to do this for each gate to keep up numbers... do we? ;if ((random-float 1) >= ((count turtles) / number)) [ sprout 1 [] ] ]
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -