-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.R
64 lines (52 loc) · 2.64 KB
/
main.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
source("src/src.R")
# Parameters for the genetic algorithm. We will be using 10 terrain classes,
# a population of 500, 250 parents to be transmitted to the next generation,
# 50 elite (preserved without mutation), 2% chance of mutation and 20 generations.
numGenes <- 10
numGenomes <- 500
numParents <- 250
numElite <- 50
mutationRate <- 0.2
numIter <- 20
# Testing the sites of Mureybet and Dhra as potential origins
for (originSite in c("Mureybet", "Dhra")) {
# See src for documentation of genetic algorithm
res <- GA(numGenes, numGenomes, numParents, numElite, mutationRate, numIter, originSite, cores=10)
# Get the values from the best model and generate a friction surface
best <- as.numeric(res$genomes[1,])
reclassMatrix <- cbind(1:numGenes, best[1:numGenes])
costRaster <- reclassify(BIOMES, reclassMatrix)
costSPDF <- as(costRaster, "SpatialPixelsDataFrame")
names(costSPDF@data) <- c("val")
# Load cost surface in GRASS with a resolution of 25 km. See src for
# documentation of the GRASS interface functions.
setGRASS(costSPDF, 25000)
# Calculate the speeds (inverse of the cost)
speedRaster <- 1 / costRaster
speedRaster.wgs <- projectRaster(speedRaster, res=0.25, crs=WGS, method="ngb")
speedRaster.cropped <- crop(speedRaster.wgs, extent(DATES))
# Coordinates and start yr BP of the chosen origin site
ORIGIN <- as.numeric(DATES.m[DATES.m$Site==originSite,]@coords)
START <- DATES.m[DATES.m$Site==originSite,]$bp
# Simulate arrival times. See src for function documentation
simDates <- simulateDispersal(ORIGIN, START)
simDates.wgs <- projectRaster(simDates, res=0.25, crs=WGS)
simDates.r <- crop(simDates.wgs, extent(DATES))
cat(paste("Best score:", best[numGenes+1], "\n"))
# Write files
# Rasters used in QGIS to produce the maps in the paper
speeds <- data.frame("region"=1:numGenes, "speed"=1/best[1:numGenes])
writeRaster(speedRaster.cropped, paste("results/speed_", originSite, ".tif", sep=""), overwrite=T)
write.csv(speeds, file=paste("results/res_", originSite, ".csv", sep=""))
save(res, simDates, file=paste("results/ga_", originSite, ".RData", sep=""))
writeRaster(simDates.r, paste("results/sim_", originSite, ".tif", sep=""), overwrite=T)
# Fig 3 and S1 Fig
print(plotDates(simDates, DATES.m, ORIGIN, best[11]))
try(dev.print(tiff, paste("results/plot_", originSite, ".tiff", sep=""), res=300, width=1280))
try(dev.off())
# S3-S4 Fig
par(bg="white")
plotGA(res, paste("Origin:", originSite))
try(dev.print(tiff, paste("results/scores_", originSite, ".tiff", sep=""), res=300, width=2000))
try(dev.off())
}