Site icon R-bloggers

Synchronous vs. asynchronous agent activation example

[This article was first published on R snippets, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
This time I have implemented NetLogo Voting model to verify how agent activation scheme influences the results.
The code executing the simulation is given below. It simulates two types of voter preferences encoded as 1 and 1. In this way average preference equal to 0 indicates 50/50 split. Voters are arranged on square grid with vertical and horizontal wrapping.

nei8 <- function(x, y, size) {< o:p>
   base.x <- c(-1, 1, 10, 01, 1, 1)< o:p>
   base.y <- c(-101, 1, 1, 1, 0, 1)< o:p>
   1 + ((cbind(x + base.x, y + base.y) 1) %% size)< o:p>
}< o:p>

step.syn <- function(space) {< o:p>
    size <- nrow(space)< o:p>
    new.space <- space< o:p>
    for (x in 1:size) {< o:p>
        for (y in 1:size) {< o:p>
            nei.pref <- sum(space[nei8(x, y, size)])< o:p>
            if (nei.pref > 0) { new.space[x, y] <- 1 }< o:p>
            if (nei.pref < 0) { new.space[x, y] <- 1 }< o:p>
        }< o:p>
    }< o:p>
    return(new.space)< o:p>
}< o:p>

step.asyn <- function(space) {< o:p>
    size <- nrow(space)< o:p>
    old.space <- space< o:p>
    all.x <- rep(1:size, size)< o:p>
    all.y <- rep(1:size, each = size)< o:p>
    dec.seq <- sample.int(length(all.x))< o:p>

    for (i in dec.seq) {< o:p>
        x <- all.x[i]< o:p>
        y <- all.y[i]< o:p>
        nei.pref <- sum(space[nei8(x, y, size)])< o:p>
        if (nei.pref > 0) { space[x, y] <- 1 }
        if (nei.pref < 0) { space[x, y] <- 1 }
    }< o:p>
    return(space)< o:p>
}< o:p>

simulate <- function (size, is.syn, do.plot) {< o:p>
    x <- rep(1:size, size)< o:p>
    y <- rep(1:size, each = size)< o:p>

    space <- 2 * matrix(rbinom(size ^ 2, 1, 0.5), nrow = size) 1< o:p>
    rep <- 0< o:p>
    while (TRUE) {< o:p>
        rep <- rep + 1< o:p>
        old.space <- space< o:p>
        if (is.syn) {< o:p>
            space <- step.syn(space)< o:p>
        } else {< o:p>
            space <- step.asyn(space)< o:p>
        }< o:p>
        if (do.plot) {< o:p>
            par(pin=c(3,3))< o:p>
            plot(x , y, axes = FALSE, xlab=“”, ylab=“”,< o:p>
                 col = space + 2, pch = 15, cex = 40 / size)< o:p>
        }< o:p>
        if (all(old.space == space)) {< o:p>
            return(c(rep, mean(space)))< o:p>
        }< o:p>
        if (rep > 100) {< o:p>
            return(c(NA, mean(space)))< o:p>
        }< o:p>
    }< o:p>
}< o:p>

It uses either step.syn function which assumes that all voters make decision at the same time in each time step and step.asyn where all voters are activated in random order.

To reproduce graph similar to NetLogo original the do.plot option should be set to TRUE. For example simulate(32, FALSE, TRUE) generates the following picture:


In order to compare synchronous and asynchronous voter activation regimes I have run the following code:

s.r <- replicate (1024, simulate(32, TRUE, FALSE))< o:p>
as.r <- replicate (1024, simulate(32, FALSE, FALSE))< o:p>

par(mfrow = c(1, 2))< o:p>
boxplot(cbind(“synchronous”=s.r[1,],“asynchronous”=as.r[1,]),< o:p>
        main = “time”)< o:p>
boxplot(cbind(“synchronous”=s.r[2,],“asynchronous”=as.r[2,]),< o:p>
        main = “mean preference”)< o:p>

As shown on the picture below the distribution of mean voter preferences is very similar. However convergence speed of both methods varies greatly. Moreover under synchronous activation around 0.5% of simulations do not reach stable state.

To leave a comment for the author, please follow the link and comment on their blog: R snippets.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.