Implementing Circles 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 week I reimplemented part of Conic Sections 1 model from NetLogo. In the model turtles seek to to be in target distance from center.

My code takes only one center point, so only circles can be obtained. Apart from turtle location plot given in NetLogo implementation I added:
  1. plot showing maximal difference between turtle distance and target distance;
  2. decreasing turtle step size.
Here is the plot showing final simulation state, but it is also nice to watch the simulation run:



Below is the code generating the simulation:
# n: number of turtles
# p.x, p.y: location of center
# range: turtles have random position from [0,range]
#        and will move in random angle a
# step: how fast turtles move
# target: target distance from center
# time: simulation time
init <- function(n, p.x, p.y, range, step, target, time) {
    sim <- list(
        turtles = data.frame(x = runif(n, max = range),
                             y = runif(n, max = range),
                             a = runif(n, max = 2 * pi)),
        p.x = p.x, p.y = p.y, step = step, target = target,
        time = time, max.dist = rep(NA, time))

    # Calculate turtle distance from center
    sim$turtles$dist <- sqrt((sim$turtles$x - p.x) ^ 2 +
                             (sim$turtles$y - p.y) ^ 2)
    return(sim)
}

step <- function(sim) {
    x <- sim$turtles$x
    y <- sim$turtles$y

    # Remember last distance and save current distance
    o.dist <- sim$turtles$dist
    n.dist <- sqrt((x - sim$p.x) ^ 2 + (y - sim$p.y) ^ 2)
    sim$turtles$dist <- n.dist

    # For turtles that are too far and are moving out
    # or too close and are moving in randomly change direction
    w.dist <- ((n.dist < o.dist) & (n.dist < sim$target)) |
               ((n.dist > o.dist) & (n.dist > sim$target))
    sim$turtles$a[w.dist] <- runif(sum(w.dist), max = 2 * pi)

    sim$turtles$x <- x + sin(sim$turtles$a) * sim$step
    sim$turtles$y <- y + cos(sim$turtles$a) * sim$step
    return(sim)
}

do.plot <- function(sim) {
    rng <- quantile(c(sim$turtles$x, sim$turtles$y),
                    c(0.05, 0.95))
    rng <- round(rng, -1) + c(-10, 10)
    par(mai = rep(0.5, 4), mfrow = c(1, 2))
    plot(sim$turtles$x, sim$turtles$y, pch = ".",
         xlim = rng, ylim = rng, xlab = "", ylab = "",
         main = "Turtle location")
    points(sim$p.x, sim$p.y, col = "red", pch = 20, cex = 2)
    plot(sim$max.dist, type = "l",
         ylim = c(0, max(sim$max.dist, na.rm = TRUE) + 5),
         xlab = "", ylab = "", main = "Max difference from target")
}

run <- function(sim) {
    for (i in 1:sim$time) {
        sim <- step(sim)
        sim$step <- sim$step * 127 / 128
        sim$max.dist[i] <- max(sim$turtles$dist) - sim$target
        do.plot(sim)
    }
}

sim <- init(4096, 128, 128, 256, 2, 128, 512)
set.seed(0)
run(sim)

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.

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)