Bridging agent-based modelling and R with nlrx: simulating pedestrian’s long-term exposure to �air pollution
Hyesop Shin
Ph.D Candidate
Dept. of Geography and Fitzwilliam College
University of Cambridge
@hyesop
TOC
Objective
Agent-based modelling
R Codes
Outcomes
Summary
Files, Codes and Tutorials:
http://tiny.cc/nlrx
Project Objective
Project objective
Agent-based modelling
Agent-based modelling (ABM) is a computational method that focuses on individual’s movements and interactions that can affect the system structure�e.g. Social media, epidemics, decision-making
Components
ABM setup
How ABM works: NetLogo example
Agent-based modelling: Settings
Study area + demographic info
Agent setup and create destination
Measure health loss and recovery
Export file as export.csv
Import files to R
96,822 patches
Agent-based modelling: Settings
Study area + demographic info
Agent setup and create destination
Measure health loss and recovery
Export file as export.csv
Import files to R
Agent-based modelling: Settings
Study area + demographic info
Agent setup and create destination
Measure health loss and recovery
Export file as export.csv
Import files to R
If PM10 >100
Agent-based modelling: Settings
Study area + demographic info
Agent setup and create destination
Measure health loss and recovery
Export file as export.csv
Import files to R
1.2 hours �for a single run
Low Quality�Images
Exported as (uncleaned) .csv
50 iterations
You will end up doing this...
Time-consuming!
Solving current problems from NetLogo
How can R reduce human intervention during analysis?
How can R improve the quality of figures?
How can R increase the simulation's clock speed?
01
02
03
ABM with nlrx: �a tool to setup and execute NetLogo simulations from R�Netlogo + R + XML
Stage 1: Install packages nlrx
Sys.setenv(JAVA_HOME= ‘XXXX‘) #Varies by OS
library(nlrx)
library(tidyverse)
library(rcartocolor)
library(ggthemes)
nlpath <- file.path("/home/hs621/NetLogo 6.0.4")
modelpath <- file.path(path,"Gangnam.nlogo")
outpath <- file.path("/home/hs621/out")
nl <- nl(nlversion = "6.0.4",
nlpath = nlpath,
modelpath = modelpath,
jvmmem = 1024)
Java setup
Load pkgs
Assign path
Create an nl object
Codes and tutorials: http://tiny.cc/nlrx
Stage 2: Attach an experiment (1/2)
nl@experiment <- experiment(expname = "seoul",
outpath = outpath,
repetition = 1,
tickmetrics = "true",
idsetup = "setup",
idgo = "go",
runtime = 8764,
evalticks=seq(1,8764, by = 100),
Codes and tutorials: http://tiny.cc/nlrx
Stage 3: Attach an experiment (2/2)
constants = list("PM10-parameters" = 100,
"Scenario" = "\"BAU\"",
"scenario-percent" =
"\"inc-sce\""),
variables = list('AC' = list(values=c(100,150,200))),
metrics.turtles = list("people" = c("xcor", "ycor",
"homename", "destinationName", "age",
"health")
metrics.patches = list("patch" = c("pxcor", "pycor", "pcolor"))
Codes and tutorials: http://tiny.cc/nlrx
Stage 4: Attach a simulation design and run model
nl@simdesign <- simdesign_distinct(nl=nl,
nseeds=1)
init <- Sys.time()
results <- run_nl_all(nl = nl)
Sys.time() - init
setsim(nl, "simoutput") <- results
write_simoutput(nl)
Iteration sampling
Run
Add results to nl list
Codes and tutorials: http://tiny.cc/nlrx
Stage 5: Submit batch jobs to reduce time
Post-simulation
Result structure: nested tibble
General info: Iteration, Scenario, Random seed, Step,
Agent info: Count turtles, Breed, X, Y, home, destination, age, health
Patch info: X, Y, patch colour
⇒ unnested� result
Mapping unhealthy population with ggplot2
Codes and tutorials: http://tiny.cc/nlrx
ggplot() +
facet_wrap(~`[step]`, ncol= 10) +
coord_equal() +
geom_tile(data=patches, aes(x=pxcor, y=pycor, fill=pcolor), alpha = .2) +
geom_point(data=turtles, aes(x = pxcor, y = pycor, color = age), size=1) +
scale_fill_gradient(low = "white", high = "grey20") +
scale_color_manual(breaks=c("young", "active", "old"),
values = c("young" = "#56B4E9",
"active" = "#E69F00",
"old" = "#999999")) +
guides(fill=guide_legend(title="PM10")) +
ggtitle("Unhealthly Population after a long-term exposure") +
theme_minimal() +
theme(axis.line=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank())
Density plot with ggplot2 & �Animations with gganmiate
Codes and tutorials: https://tiny.cc/nlrx
gridextra & directlabels for HQ images
Codes and tutorials: http://tiny.cc/nlrx
grid.arrange()
direct.label()
Summary and Contribution
Summary
Contributions from nlrx
For more information...
https://tiny.cc/nlrx
Paper
Tutorial
Thank you!
Agent-based modelling: advantages
Air Pollution in South Korea (March, 2019)
Songdo, Incheon (Mar.5th, 2019)
Central Seoul (Mar.6th, 2019)
PM2.5 > 200
PM2.5 > 140
Urbanisation and Air Pollution
NO2 trend in a decade (2005-2015), Satellite imagery taken by NASA, 2015
Urbanisation and Air Pollution
NO2 change in East Asian countries
Traffic in Seoul CBD
Health threat may differ by demographic factors
More attention should be given to how travel behaviours differ by social groups (e.g. age), and how health loss are manifested in each group after a long-term pollution exposure
Stage 1: Install and finish coding in NetLogo
Agent-based modelling: advantages
NetLogo Example
96822 patches