1 of 40

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

2 of 40

TOC

Objective

Agent-based modelling

R Codes

Outcomes

Summary

Files, Codes and Tutorials:

http://tiny.cc/nlrx

3 of 40

Project Objective

4 of 40

Project objective

  • This study aims to estimate pedestrian’s exposure to acute air pollution in Seoul districts using agent-based simulation
    • How does socioeconomic group potentially affect health outcomes?
    • How could health levels change under different pollution scenarios?

5 of 40

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

  • Agents = turtles
  • Environments = patches

6 of 40

ABM setup

7 of 40

How ABM works: NetLogo example

8 of 40

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

9 of 40

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

  • The population of Gangnam is allocated into three groups: �Under 15, 15-64, Over 65
  • A day is splitted into two time sequences: �Work hours (09-19 hrs), Home (20-08 hrs)
  • Agents have no previous exposure experienced
  • Agents aged 15-64 follow OD matrix while restricting other groups' movement range close to their origin

10 of 40

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

11 of 40

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

12 of 40

You will end up doing this...

Time-consuming!

13 of 40

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

14 of 40

ABM with nlrx: �a tool to setup and execute NetLogo simulations from R�Netlogo + R + XML

15 of 40

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

16 of 40

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

17 of 40

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

18 of 40

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

19 of 40

Stage 5: Submit batch jobs to reduce time

20 of 40

Post-simulation

21 of 40

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

22 of 40

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())

23 of 40

Density plot with ggplot2 &Animations with gganmiate

Codes and tutorials: https://tiny.cc/nlrx

24 of 40

gridextra & directlabels for HQ images

Codes and tutorials: http://tiny.cc/nlrx

grid.arrange()

direct.label()

25 of 40

Summary and Contribution

26 of 40

Summary

  • Contents
    • Disparities in health outcomes are likely to depend on demographic status
    • When the vulnerable group (the old and young) is exposed over a long period, road proximity causes additional health degradation
  • R & nlrx
    • Workload: 60% on NetLogo, 30% on R, 10% HPC
    • nlrx works as a compiler
    • nlrx results in a tibble format that can be plotted in a variety of figures for different purposes with ggplot, gganimate

27 of 40

Contributions from nlrx

  • Time saving
    • Fast iterative process
    • The simulation ends with only a paragraph of codes
  • Solves fat finger issues
    • No need to delete readme text from the NetLogo output
  • Convenience
    • Doesn’t need rJava installation (Really helps when running HPC)
    • Silent machines
    • Code categorical variables

28 of 40

For more information...

Paper

Tutorial

29 of 40

Thank you!

@hyesop

hs621@cam.ac.uk

@mrsensible

30 of 40

31 of 40

32 of 40

33 of 40

Agent-based modelling: advantages

  • Is able to simulate human-environment interaction
  • Can analyse adverse health impact by social groups
  • Follows trajectories of individuals over time
  • Envisages effects from possible scenarios ("What if…?")
  • NetLogo is the most widely used software in the ABM world

34 of 40

Air Pollution in South Korea (March, 2019)

Songdo, Incheon (Mar.5th, 2019)

Central Seoul (Mar.6th, 2019)

PM2.5 > 200

PM2.5 > 140

35 of 40

Urbanisation and Air Pollution

NO2 trend in a decade (2005-2015), Satellite imagery taken by NASA, 2015

36 of 40

Urbanisation and Air Pollution

NO2 change in East Asian countries

Traffic in Seoul CBD

37 of 40

Health threat may differ by demographic factors

  • Children are inherently more susceptible to air pollution as their lung function and immunological systems are still developing (Pearce et al., 2006)
  • Higher risks due to the PM10 exposure were observed for elderly individuals - COPD, stroke, etc (Halonen et al., 2016; Wang et al., 2016)

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

38 of 40

Stage 1: Install and finish coding in NetLogo

  • Install NetLogo >= 5.3, NetLogo 6 is preferred
  • Java required

39 of 40

Agent-based modelling: advantages

  • Is able to simulate human-environment interaction
  • Can analyse adverse health impact by social groups
  • Follows trajectories of individuals over time
  • Envisages effects from possible scenarios ("What if…?")
  • NetLogo is the most widely used software in the ABM world

NetLogo Example

40 of 40

96822 patches