SteppingStone Algo - MarkovChain
Purpose - Stepping Stone Algos
I came across in a reference to Markov chain
20 by 20 layout with 2 colors 10000 trials are not enough
> library(grid) > library(RColorBrewer) > vplayout <- function(x, y) viewport(layout.pos.row = x, layout.pos.col = y) > colors <- c("white", "black") > data <- matrix(data = NA, nrow = 20, ncol = 20) > getGrid <- function(data) { + data.ref <- matrix(data = NA, nrow = 22, ncol = 22) + for (i in 2:21) { + for (j in 2:21) { + data.ref[i, j] <- data[(i - 1), (j - 1)] + } + } + data.ref[1, 2:21] <- data[20, ] + data.ref[22, 2:21] <- data[1, ] + data.ref[2:21, 1] <- data[, 20] + data.ref[2:21, 22] <- data[, 1] + data.ref[1, 1] <- data[20, 20] + data.ref[22, 22] <- data[1, 1] + data.ref[1, 22] <- data[20, 1] + data.ref[22, 1] <- data[1, 20] + return(data.ref) + } > for (i in 1:20) { + for (j in 1:20) { + data[i, j] <- sample(1:2, 1) + } + } > counter <- 0 > while (counter < 10000) { + temp <- getGrid(data) + cell <- sample(1:20, 2, replace = T) + cell.ref <- cell + 1 + x <- cell.ref[1] + y <- cell.ref[2] + temp.ref <- temp[(x - 1):(x + 1), (y - 1):(y + 1)] + temp.col <- sample(c(temp.ref)[-5], 1) + x.orig <- cell[1] + y.orig <- cell[2] + data[x.orig, y.orig] <- temp.col + counter <- counter + 1 + } > print(counter) [1] 10000 |
8 by 8 layout with 2 colors
> library(grid) > library(RColorBrewer) > colors <- c("white", "black") > data <- matrix(data = NA, nrow = 8, ncol = 8) > getGrid <- function(data) { + data.ref <- matrix(data = NA, nrow = 10, ncol = 10) + for (i in 2:9) { + for (j in 2:9) { + data.ref[i, j] <- data[(i - 1), (j - 1)] + } + } + data.ref[1, 2:9] <- data[8, ] + data.ref[10, 2:9] <- data[1, ] + data.ref[2:9, 1] <- data[, 8] + data.ref[2:9, 10] <- data[, 1] + data.ref[1, 1] <- data[8, 8] + data.ref[10, 10] <- data[1, 1] + data.ref[1, 10] <- data[8, 1] + data.ref[10, 1] <- data[1, 8] + return(data.ref) + } > getFilledStatus <- function(data) { + temp1 <- length(which(c(data) == 1)) + temp2 <- length(which(c(data) == 2)) + completed <- max(temp1, temp2)/(temp1 + temp2) + return(completed) + } > for (i in 1:8) { + for (j in 1:8) { + data[i, j] <- sample(1:2, 1) + } + } > counter <- 0 > while (counter < 15000) { + temp <- getGrid(data) + cell <- sample(1:8, 2, replace = T) + cell.ref <- cell + 1 + x <- cell.ref[1] + y <- cell.ref[2] + temp.ref <- temp[(x - 1):(x + 1), (y - 1):(y + 1)] + temp.col <- sample(c(temp.ref)[-5], 1) + x.orig <- cell[1] + y.orig <- cell[2] + data[x.orig, y.orig] <- temp.col + counter <- counter + 1 + status <- getFilledStatus(data) + if (status == 1) + break + } > print(counter) [1] 2541 |
8 by 8 layout with 3 colors
> library(grid) > library(RColorBrewer) > colors <- c("white", "sienna", "black") > data <- matrix(data = NA, nrow = 8, ncol = 8) > getGrid <- function(data) { + data.ref <- matrix(data = NA, nrow = 10, ncol = 10) + for (i in 2:9) { + for (j in 2:9) { + data.ref[i, j] <- data[(i - 1), (j - 1)] + } + } + data.ref[1, 2:9] <- data[8, ] + data.ref[10, 2:9] <- data[1, ] + data.ref[2:9, 1] <- data[, 8] + data.ref[2:9, 10] <- data[, 1] + data.ref[1, 1] <- data[8, 8] + data.ref[10, 10] <- data[1, 1] + data.ref[1, 10] <- data[8, 1] + data.ref[10, 1] <- data[1, 8] + return(data.ref) + } > getFilledStatus <- function(data) { + temp1 <- length(which(c(data) == 1)) + temp2 <- length(which(c(data) == 2)) + temp3 <- length(which(c(data) == 3)) + completed <- max(temp1, temp2, temp3)/(temp1 + temp2 + temp3) + return(completed) + } > for (i in 1:8) { + for (j in 1:8) { + data[i, j] <- sample(1:3, 1) + } + } > counter <- 0 > while (counter < 15000) { + temp <- getGrid(data) + cell <- sample(1:8, 2, replace = T) + cell.ref <- cell + 1 + x <- cell.ref[1] + y <- cell.ref[2] + temp.ref <- temp[(x - 1):(x + 1), (y - 1):(y + 1)] + temp.col <- sample(c(temp.ref)[-5], 1) + x.orig <- cell[1] + y.orig <- cell[2] + data[x.orig, y.orig] <- temp.col + counter <- counter + 1 + status <- getFilledStatus(data) + if (status == 1) + break + } > print(counter) [1] 7237 |