::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(cache.extra = knitr::rand_seed)
knitrsetwd("~/GitHub/SWF-molder/R")
library(terra)
library(devtools)
library(parallel)
library(igraph)
load_all()
## ℹ Loading SWFmolder
## Warning: Objects listed as exports, but not present in namespace:
## • ClosestDiagonalAgriCell
function(x, p){
extractRandWindow <- sample(seq(length(x) - p + 1), 1)
firstIndex =:(firstIndex + p -1)]}
x[firstIndex c('#ef8a62', '#f7f7f7', '#67a9cf') color_map <-
seq(10,170,20)
dims <- 50
iters = 1
swfCat = 2
agriCat = 2 # 2 px allocated per time
Q = 3 # 3 NN
NNeighbors = 0.9
swfCover = detectCores() np =
lapply(dims, function(dim) {
Eben.res <-message("Dimension: ", dim)
Sys.sleep(1)
matrix(2, ncol=dim,nrow=dim)
null.mt <-set.seed(123)
# 5 % inititial SWF cover
sample((1:dim^2),floor(dim^2*0.05))] <-1
null.mt[# 5 % inititial Forest cover
as.vector(replicate(3,extractRandWindow(1:dim^2,floor(dim^2*0.05))))] <-3
null.mt[# plot(rast(t(null.mt)),col=color_map,cex=10)
GfM(null.mt)
null.gr <-
system.time(matrix.m <- swf.molder(Hmatrix=null.mt, swfCover=swfCover, swfCat=swfCat, agriCat=agriCat, Q=Q, ExpPriority="mixed", ExpDirection="mixed", reduceQTo=0, iterations = iters, NNeighbors=NNeighbors, maxDistance = 1, queensCase=TRUE, maxGDistance=1, np=np))
matrix.time <-
Sys.sleep(1)
system.time(graph.m <- swf_molderN(g=null.gr, swfCat, agriCat, iterations = iters, Q, NNeighbors, swfCover, maxGDistance=1, max_radius=1, np=np))
graph.time <-
lapply(graph.m, MfG, nrow(null.mt), ncol(null.mt))
matrix.m1 <- identical(t(matrix.m[[length(matrix.m)]]), matrix.m1[[length(matrix.m1)]])
check <-return(list(do.call(rbind,list(graph.time,matrix.time)),check,matrix.m,matrix.m1))
})
sapply(1:length(Eben.res), function(x) Eben.res[[x]][[1]][1,3])
timeG <- sapply(1:length(Eben.res), function(x) Eben.res[[x]][[1]][2,3])
timeM <-# sapply(1:length(ben.results), function(x) ben.results[[x]][[2]])
cbind(timeG,timeM,dims) timePlot <-
timePlot[,3]^2
dims2 <-plot(timeM~dims2,timePlot,type="l",col="red",ylim=c(0,max(timeM,timeG)), ylab="Seconds elapsed", xlab="matrix dimension (pixels)")
lines(timeG~dims2,timePlot,type="l",col="blue")
legend("topleft",lty=c(1,1),col=c("red","blue"),legend=c("matrix","graph"))
title("5% initial SWF cover")
4
ncol=par(mfrow = c(ceiling(length(dims)/ncol),ncol) , mai = c(0.1, 0.1, 0.1, 0.1))
for (dim in dims) {
matrix(2, ncol=dim,nrow=dim)
null.mt <-set.seed(123)
sample((1:dim^2),floor(dim^2*0.15))] <-1
null.mt[as.vector(replicate(3,extractRandWindow(1:dim^2,floor(dim^2*0.05))))] <-3
null.mt[ rast(t(null.mt))
smfTemp <-plot(smfTemp, col = color_map, legend=FALSE, axes = FALSE, box = FALSE, main = paste("Dimension ", dim^2," pixels"))
# text(smfTemp, digits=1)
}
seq(10,170,20)
dims <- 50
iters = 1
swfCat = 2
agriCat = 1 # 2 px allocated per time
Q = 3 # 3 NN
NNeighbors = 0.9
swfCover = detectCores() np =
lapply(dims, function(dim) {
Hben.res <-message("Dimension: ", dim)
matrix(2, ncol=dim,nrow=dim)
null.mt <-set.seed(123)
# 15 % inititial SWF cover
sample((1:dim^2),floor(dim^2*0.15))] <-1
null.mt[# 5 % inititial SWF cover
as.vector(replicate(3,extractRandWindow(1:dim^2,floor(dim^2*0.05))))] <-3
null.mt[# plot(rast(t(null.mt)),col=color_map,cex=10)
GfM(null.mt)
null.gr <-Sys.sleep(0.5)
system.time(matrix.m <- swf.molder(Hmatrix=null.mt, swfCover=swfCover, swfCat=swfCat, agriCat=agriCat, Q=Q, ExpPriority="mixed", ExpDirection="mixed", reduceQTo=0, iterations = iters, NNeighbors=NNeighbors, maxDistance = 1, queensCase=TRUE, maxGDistance=1, np=np))
matrix.time <-
Sys.sleep(1)
system.time(graph.m <- swf_molderN(g=null.gr, swfCat, agriCat, iterations = iters, Q, NNeighbors, swfCover, maxGDistance=1, max_radius=1, np=np))
graph.time <-
lapply(graph.m, MfG, nrow(null.mt), ncol(null.mt))
matrix.m1 <- identical(t(matrix.m[[length(matrix.m)]]), matrix.m1[[length(matrix.m1)]])
check <-
return(list(do.call(rbind,list(graph.time,matrix.time)),check,matrix.m,matrix.m1))
})
sapply(1:length(Hben.res), function(x) Hben.res[[x]][[1]][1,3])
timeG <- sapply(1:length(Hben.res), function(x) Hben.res[[x]][[1]][2,3])
timeM <-# sapply(1:length(Hben.res), function(x) Hben.res[[x]][[2]])
cbind(timeG,timeM,dims) timePlot <-
timePlot[,3]^2
dims2 <-plot(timeM~dims2,timePlot,type="l",col="red",ylim=c(0,max(timeM,timeG)), ylab="Seconds elapsed", xlab="matrix dimension (pixels)")
lines(timeG~dims2,timePlot,type="l",col="blue")
legend("topleft",lty=c(1,1),col=c("blue","red"),legend=c("graph","matrix"))
title("15% initial SWF cover")
4
ncol=par(mfrow = c(ceiling(length(dims)/ncol),ncol) , mai = c(0.1, 0.1, 0.1, 0.1))
for (dim in dims) {
matrix(2, ncol=dim,nrow=dim)
null.mt <-set.seed(123)
sample((1:dim^2),floor(dim^2*0.15))] <-1
null.mt[as.vector(replicate(3,extractRandWindow(1:dim^2,floor(dim^2*0.15))))] <-3
null.mt[ rast(t(null.mt))
smfTemp <-plot(smfTemp, col = color_map, legend=FALSE, axes = FALSE, box = FALSE, main = paste("Dimension ", dim^2," pixels"))
# text(smfTemp, digits=1)
}