Synchronous vs. asynchronous agent activation example

February 24, 2012
By

(This article was first published on R snippets, and kindly contributed to R-bloggers)

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) {
   base.x <- c(-1, 1, 10, 01, 1, 1)
   base.y <- c(-101, 1, 1, 1, 0, 1)
   1 + ((cbind(x + base.x, y + base.y) 1) %% size)
}
step.syn <- function(space) {
    size <- nrow(space)
    new.space <- space
    for (x in 1:size) {
        for (y in 1:size) {
            nei.pref <- sum(space[nei8(x, y, size)])
            if (nei.pref > 0) { new.space[x, y] <- 1 }
            if (nei.pref < 0) { new.space[x, y] <- 1 }
        }
    }
    return(new.space)
}
step.asyn <- function(space) {
    size <- nrow(space)
    old.space <- space
    all.x <- rep(1:size, size)
    all.y <- rep(1:size, each = size)
    dec.seq <- sample.int(length(all.x))
    for (i in dec.seq) {
        x <- all.x[i]
        y <- all.y[i]
        nei.pref <- sum(space[nei8(x, y, size)])
        if (nei.pref > 0) { space[x, y] <- 1 }
        if (nei.pref < 0) { space[x, y] <- 1 }
    }
    return(space)
}
simulate <- function (size, is.syn, do.plot) {
    x <- rep(1:size, size)
    y <- rep(1:size, each = size)
    space <- 2 * matrix(rbinom(size ^ 2, 1, 0.5), nrow = size) 1
    rep <- 0
    while (TRUE) {
        rep <- rep + 1
        old.space <- space
        if (is.syn) {
            space <- step.syn(space)
        } else {
            space <- step.asyn(space)
        }
        if (do.plot) {
            par(pin=c(3,3))
            plot(x , y, axes = FALSE, xlab=“”, ylab=“”,
                 col = space + 2, pch = 15, cex = 40 / size)
        }
        if (all(old.space == space)) {
            return(c(rep, mean(space)))
        }
        if (rep > 100) {
            return(c(NA, mean(space)))
        }
    }
}

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))
as.r <- replicate (1024, simulate(32, FALSE, FALSE))
par(mfrow = c(1, 2))
boxplot(cbind(“synchronous”=s.r[1,],“asynchronous”=as.r[1,]),
        main = “time”)
boxplot(cbind(“synchronous”=s.r[2,],“asynchronous”=as.r[2,]),
        main = “mean preference”)

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 on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Sponsors

Mango solutions



plotly webpage

dominolab webpage



Zero Inflated Models and Generalized Linear Mixed Models with R

Quantide: statistical consulting and training

datasociety

http://www.eoda.de





ODSC

ODSC

CRC R books series





Six Sigma Online Training









Contact us if you wish to help support R-bloggers, and place your banner here.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)