Here you have an introduction to the model.
The image below is a static picture of the simulator (GIS applets still do not work in remote), but you can find the code
here and run it in your computer (NetLogo 4.1.3 suggested).
powered by NetLogo
view/download model file: traffic_and_pollution_in_torino.nlogo
This model uses GIS extension in order to simulate traffic in the city of Torino (Italy) and it shows how traffic conditions influence pollution in the city; in addition it is possible to approximate the amount of money lost due to traffic delays.
Patches not intersecting the GIS represent streets and, of course, turtles (cars) can only move on these patches. Turtles are expected to "drive" on the right, speed is represented by the number of patches they go forward and it decreases if there are any other turtles ahead.
Each patch has an indicator of its pollution levels depending on the number of cars on it and pollution diffuses to other patches as time passes.
Each turtle shows speed, waiting time and motion time.
First of all setup allows to import the GIS, then it is possible to move on the map and choose different areas of the city of Torino; there is a zoom utility but it is recommended to set zoom between 0.05 and 0.08 before going on.
Display-streets-in-patches (a bit slow) makes the distinction between streets and buildings, while draw-streets draws roadways. Notice that it is possible to open or close parts of a street using close-street-here and open-street-here.
Setup-cars creates the selected number of turtles and Go makes them move.
Two buttons allow to follow one of the turtles and to change color to its path.
Two switches allow to consider pollution and to show its concentration on the map; notice that original colors can be brought back using cancel-colors.
Change-street-color makes streets fade at car passage.
It is advisable to follow this order in pressing buttons:
setup / display-streets-in-patches / draw-streets / setup-cars / go.
Sliders:
cost-of-working represents the average cost of an hour of work (in euros or dollars)
poll-dispersion, if positioned at 1.00 no pollution is dissipated, if positioned at 2.00 50% of pollution disappears
The pollution growth caused by cars is represented by a normal probability distribution with parameters pollution-mean and deviation-mean
SantaFeStreets model -- http://backspaces.net/wiki/NetLogo_Bag_of_Tricks#NetLogo_GIS
Traffic Grid model -- Netlogo Library
;;;;;;;;;;;;; LUCA ALASIO - Scuola di Studi Superiori dell'Università di Torino ;;;;;;;;;;;;;;;;;;; extensions [gis] globals [ torino ;; GIS dataset streets ;; patches representing streets center-x ;; center-y ;; center of the map roads ;; patches representing roadways mean-poll ;; average pollution indicator mean-wait-time ;; average time turtles stay at rest mean-motion-time ;; average time turtles stay in motion mean-speed ;; turtles'speed indicator total-cost ;; cost of time spent waiting ] patches-own [ street? ;; true if the patch is part of a street. color-p ;; indicates right hand traffic turtles-num ;; number of turtles on the patch pollution ;; amount of pollution on the patch ] turtles-own [ speed ;; the speed of the turtle wait-time ;; the amount of time since the last time a turtle has moved motion-time ;; the amount of time with positive speed color-c ;; indicates right hand traffic ] ;; WORLD ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; to setup clear-all ask patches [set pcolor white] set torino gis:load-dataset "torino/file.shp" draw end to draw clear-drawing setup-world-envelope gis:set-drawing-color gray + 1 gis:draw torino 1 end to setup-world-envelope gis:set-world-envelope gis:envelope-of torino let world gis:world-envelope let x0 (item 0 world + item 1 world) / 2 + center-x; center let y0 (item 2 world + item 3 world) / 2 + center-y let W0 zoom * (item 1 world - item 0 world) / 2 ; half-widths let H0 zoom * (item 3 world - item 2 world) / 2 set world (list (x0 - W0) (x0 + W0) (y0 - H0) (y0 + H0)) gis:set-world-envelope world end to center-here while [not mouse-down?] [wait .01] set center-x center-x + (mouse-xcor * gis-patch-size) set center-y center-y + (mouse-ycor * gis-patch-size) draw end to-report gis-patch-size ;; note: assume width & height same let world gis:world-envelope report (item 1 world - item 0 world) / (max-pxcor - min-pxcor) end to zoom-in set zoom max list .01 precision (zoom - .1) 2 draw end to zoom-out set zoom min list 1.2 precision (zoom + .1) 2 draw end to zoom-std set zoom 0.05 draw end to move-right set center-x center-x + shift * gis-patch-size draw end to move-left set center-x center-x - shift * gis-patch-size draw end to move-up set center-y center-y + shift * gis-patch-size draw end to move-down set center-y center-y - shift * gis-patch-size draw end to display-streets-in-patches ask patches [ set pcolor black set street? true ] ask patches gis:intersecting torino [ set pcolor white set street? false ] set streets patches with [street? = true] end ;; STREETS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; to draw-streets-E ask streets [ if ( [pcolor] of patch-at 1 0 = white and [pcolor] of patch-at 1 1 = white and [pcolor] of patch-at 1 -1 = white ) [set pcolor red] ] end to draw-streets-W ask streets [ if ( [pcolor] of patch-at -1 0 = white and [pcolor] of patch-at -1 1 = white and [pcolor] of patch-at -1 -1 = white ) [set pcolor blue] ] end to draw-streets-N ask streets [ if ( [pcolor] of patch-at 0 1 = white and [pcolor] of patch-at 1 1 = white and [pcolor] of patch-at -1 1 = white ) [set pcolor yellow ] ] end to draw-streets-S ask streets [ if ( [pcolor] of patch-at 0 -1 = white and [pcolor] of patch-at -1 -1 = white and [pcolor] of patch-at 1 -1 = white ) [set pcolor green] ] end to draw-streets ;; set a different color in order to make roadways recognisable draw-streets-N draw-streets-S draw-streets-E draw-streets-W set roads streets with [ pcolor != black ] ask streets [set color-p pcolor] end to cancel-colors ask patches [ if street? = false [set pcolor white] ] ask streets [set pcolor black] end ;; TURTLES ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; to setup-cars set-default-shape turtles "car" create-turtles num-cars put-on-street ask turtles [set speed 1] end to put-on-street ask turtles [move-to one-of roads] end to go if poll-flag = true [ ask streets [set turtles-num count turtles-on self] evaluate-pollution ] if plot-flag = true [ do-plots ] ask turtles [ if color-p != black [set color-c color-p] let turtles-ahead turtles in-cone 2 45 ifelse any? turtles-ahead [ slow rt 7 + random-float 3 ] [speed-up] if ([pcolor] of patch-ahead 1 != white) and ([street?] of patch-ahead 1 = true) [fd 1] ifelse ([pcolor] of patch-ahead 1 = white) or ([street?] of patch-ahead 1 = false) [ lt 30 ] [ ifelse (color-c = [color-p] of patch-at 1 0) or (color-c = [color-p] of patch-at 1 1) or (color-c = color-p) [ speed-up if ([pcolor] of patch-ahead speed != white) and ([street?] of patch-ahead speed = true) [fd speed] ] [ rt -15 + random-float 30 speed-up if ([pcolor] of patch-ahead speed != white) and ([street?] of patch-ahead speed = true) [fd speed] ] ] ifelse speed = 0 [ set wait-time wait-time + 1 set motion-time 0] [ set wait-time 0 set motion-time motion-time + 1] ] tick end to slow if (speed > 0) [set speed speed - 1] end to speed-up let turtles-ahead turtles-at 0 1 if ([pcolor] of patch-ahead 2 != white) and (not any? turtles-ahead) [ set speed speed + 1] end to cancel-cars clear-turtles set total-cost 0 end to add-cars set-default-shape turtles "car" create-turtles num-cars ask turtles [set speed 1 move-to one-of streets] end ;; POLUTION AND PLOTS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; to evaluate-pollution ask streets [ if turtles-num != 0 [ set pollution pollution + abs ( random-normal (turtles-num * pollution-mean) (turtles-num * pollution-deviation) ) ] ] diffuse pollution 0.7 ask patches [ set pollution pollution / poll-dispersion ] if show-poll = true [show-pollution] set mean-poll mean [pollution] of patches set-current-plot "Average Pollution" set-current-plot-pen "mean-poll" plot mean-poll end to do-plots set mean-wait-time mean [wait-time] of turtles set-current-plot "Average Wait Time" set-current-plot-pen "mean-wait-time" plot mean-wait-time set total-cost total-cost + (count turtles with [speed = 0]) * (cost-of-working / 3600) set-current-plot "Waiting Time Cost" set-current-plot-pen "total-cost" plot total-cost set mean-motion-time mean [motion-time] of turtles set-current-plot "Average Motion Time" set-current-plot-pen "mean-motion-time" plot mean-motion-time set mean-speed mean [speed] of turtles set-current-plot "Average Speed" set-current-plot-pen "mean-speed" plot mean-speed end to show-pollution ask patches [ if (pollution >= 0) [set pcolor 10 + pollution * 2 / 3] ] end to cancel-plots clear-all-plots end to cancel-pollution ask patches [set pollution 0] end ;; OBSERVATION ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; to inspect-car inspect turtle 1 ask turtle 1 [set pcolor cyan] end to map-car ask turtle 1 [set pcolor cyan] end to cancel-cyan ask streets [if pcolor = cyan [set pcolor black] ] end to street-change-color ask turtles [ if pcolor < 9 [set pcolor (pcolor + 0.2)] ] end to close-street-here while [not mouse-down?] [wait .01] let posx mouse-xcor let posy mouse-ycor ask patches [if distance patch posx posy < 2 [set pcolor white] ] end to open-street-here while [not mouse-down?] [wait .01] let posx mouse-xcor let posy mouse-ycor ask patches [if distance patch posx posy < 2 [set pcolor black] ] end ;;;;;;;;;;;;; LUCA ALASIO - Scuola di Studi Superiori dell'Università di Torino ;;;;;;;;;;;;;;;;;;;