wpeterman / ResistanceGA

Optimize resistance surfaces using Genetic Algorithms
36 stars 15 forks source link

Optimisation and NoData Errors (Julia Circuitscape) #22

Closed cmu002 closed 2 years ago

cmu002 commented 2 years ago

G'day,

I've been trying to optimise a singular surface through the Julia Circuitscape function, but have been running into unexpected results within my optimisation. Whenever I run my optimisation, I'm met with a constant mean and best value of '-9999' for each iteration. As well, my RStudio continuously crashes after 23 iterations of that value. My area of interest is quite irregular in shape - so I expected it to have a fair amount of 'nodata' values in the background mask/matrix. However, after I examined the .asc file I created, I noticed that there were no values of '-9999' but rather all of those values were transformed to '0'. I thought it might be an issue with circuitscape originally, but when I tried it with 'commuteDistance' I got a similar output - Error in { : task 3 failed - "0 (non-NA) cases"

That being said, I still have these questions:

I've supplied the data that I'm using through the link below. Any information on this will be very much appreciated! I feel like this is a simple fix, and has something to do with the raster data that I'm using, but for the life of me I can't figure it out.

Thanks for your time! Chris

Data: https://drive.google.com/drive/folders/17P0zNoBj4x4T1DeGA36U3PpnMcBhDR-i?usp=sharing Code: `

Import Library

library(ResistanceGA)

SINGLE SURFACE OPTIMISATION

Create Directory for Examples

if("ResistanceGA_Examples"%in%dir("D:/ResistanceGA/R/V1_Output")==FALSE) dir.create(file.path("D:/ResistanceGA/R/V1_Output", "V1_Test"))

Create Subdirectory for Single Surface

dir.create(file.path("D:/ResistanceGA/R/V1_Output/V1_Test", "SingleSurface"))

Create Directory Object for .asc Files and results

write.dir <- "D:/ResistanceGA/R/V1_Output/V1_Test/SingleSurface/"

Set working directory

setwd("D:\ResistanceGA\Data\ASC")

Optimise using Julia's Circuitscape

Sample locations

samples <- read.table("D:\ResistanceGA\EGK_SampleLoc.txt", sep="") sp.dat <- SpatialPoints(samples[,c(1,2)]) crs(sp.dat) <- CRS("+proj=utm +zone=56 +south") plot(sp.dat, pch = 19)

Import GeoTIFF and write to .asc in write.dir

msavi <- raster("MSAVI2_Test.tif") plot(sp.dat, pch = 19, col = "blue") plot(msavi) writeRaster(msavi, filename = paste0(write.dir, "MSAVI2_Test.asc"), overwrite = TRUE)

Genetic distance measured between sample locations (chord distance)

gen_dist2 <- read.table('D:\ResistanceGA\R\V1_Output\V1_Test\SingleSurface\fst.txt', sep="", header = FALSE)

Specify bin Directory and connect Julia to R

JULIA_HOME <- "C:/Users/User/AppData/Local/Programs/Julia-1.6.3/bin/" JuliaCall::julia_setup(JULIA_HOME)

Run Prep Functions

GA.inputs <- GA.prep(ASCII.dir = write.dir, Results.dir = "D:\ResistanceGA\R\V1_Output\V1_Test\SingleSurface\", parallel = 4)

jl.inputs <- jl.prep(n.Pops = length(sp.dat), response = lower(as.matrix(gen_dist2)), cholmod = TRUE, precision = FALSE, CS_Point.File = sp.dat, JULIA_HOME = JULIA_HOME)

Run Optimisation

jl.optim <- SS_optim(jl.inputs = jl.inputs, GA.inputs = GA.inputs)`

Output: `

Load Package

library(ResistanceGA) library(rgdal) library(corpcor) write.dir <- "D:/ResistanceGA/R/V1_Output/V1_Test/SingleSurface/"

Set working directory

setwd("D:\ResistanceGA\Data\ASC") samples <- read.table("D:\ResistanceGA\EGK_SampleLoc.txt", sep="") sp.dat <- SpatialPoints(samples[,c(1,2)]) crs(sp.dat) <- CRS("+proj=utm +zone=56 +south") plot(sp.dat, pch = 19) msavi <- raster("MSAVI2_Test.tif") View(msavi) plot(msavi) writeRaster(msavi, filename = paste0(write.dir, "MSAVI2_Test.asc"), overwrite = TRUE)

Genetic distance measured between sample locations (chord distance)

gen_dist2 <- read.table('D:\ResistanceGA\R\V1_Output\V1_Test\SingleSurface\fst.txt', sep="", header = FALSE) JULIA_HOME <- "C:/Users/User/AppData/Local/Programs/Julia-1.6.3/bin/" JuliaCall::julia_setup(JULIA_HOME) Julia version 1.6.3 at location C:\Users\User\AppData\Local\Programs\JULIA-~1.3\bin will be used. proj_as_proj_string: C:\Program Files\PostgreSQL\13\share\contrib\postgis-3.1\proj\proj.db lacks DATABASE.LAYOUT.VERSION.MAJOR / DATABASE.LAYOUT.VERSION.MINOR metadata. It comes from another PROJ installation. proj_as_wkt: C:\Program Files\PostgreSQL\13\share\contrib\postgis-3.1\proj\proj.db lacks DATABASE.LAYOUT.VERSION.MAJOR / DATABASE.LAYOUT.VERSION.MINOR metadata. It comes from another PROJ installation. proj_create: C:\Program Files\PostgreSQL\13\share\contrib\postgis-3.1\proj\proj.db lacks DATABASE.LAYOUT.VERSION.MAJOR / DATABASE.LAYOUT.VERSION.MINOR metadata. It comes from another PROJ installation. proj_as_wkt: C:\Program Files\PostgreSQL\13\share\contrib\postgis-3.1\proj\proj.db lacks DATABASE.LAYOUT.VERSION.MAJOR / DATABASE.LAYOUT.VERSION.MINOR metadata. It comes from another PROJ installation. proj_as_proj_string: C:\Program Files\PostgreSQL\13\share\contrib\postgis-3.1\proj\proj.db lacks DATABASE.LAYOUT.VERSION.MAJOR / DATABASE.LAYOUT.VERSION.MINOR metadata. It comes from another PROJ installation. proj_as_wkt: C:\Program Files\PostgreSQL\13\share\contrib\postgis-3.1\proj\proj.db lacks DATABASE.LAYOUT.VERSION.MAJOR / DATABASE.LAYOUT.VERSION.MINOR metadata. It comes from another PROJ installation. proj_create: C:\Program Files\PostgreSQL\13\share\contrib\postgis-3.1\proj\proj.db lacks DATABASE.LAYOUT.VERSION.MAJOR / DATABASE.LAYOUT.VERSION.MINOR metadata. It comes from another PROJ installation. proj_as_wkt: C:\Program Files\PostgreSQL\13\share\contrib\postgis-3.1\proj\proj.db lacks DATABASE.LAYOUT.VERSION.MAJOR / DATABASE.LAYOUT.VERSION.MINOR metadata. It comes from another PROJ installation. proj_as_proj_string: C:\Program Files\PostgreSQL\13\share\contrib\postgis-3.1\proj\proj.db lacks DATABASE.LAYOUT.VERSION.MAJOR / DATABASE.LAYOUT.VERSION.MINOR metadata. It comes from another PROJ installation. proj_as_wkt: C:\Program Files\PostgreSQL\13\share\contrib\postgis-3.1\proj\proj.db lacks DATABASE.LAYOUT.VERSION.MAJOR / DATABASE.LAYOUT.VERSION.MINOR metadata. It comes from another PROJ installation. proj_create: C:\Program Files\PostgreSQL\13\share\contrib\postgis-3.1\proj\proj.db lacks DATABASE.LAYOUT.VERSION.MAJOR / DATABASE.LAYOUT.VERSION.MINOR metadata. It comes from another PROJ installation. proj_as_wkt: C:\Program Files\PostgreSQL\13\share\contrib\postgis-3.1\proj\proj.db lacks DATABASE.LAYOUT.VERSION.MAJOR / DATABASE.LAYOUT.VERSION.MINOR metadata. It comes from another PROJ installation. Loading setup script for JuliaCall... Finish loading setup script for JuliaCall.

Run Prep Functions

GA.inputs <- GA.prep(ASCII.dir = write.dir,

  • Results.dir = "D:\ResistanceGA\R\V1_Output\V1_Test\SingleSurface\",
  • parallel = 4) jl.inputs <- jl.prep(n.Pops = length(sp.dat),
  • response = lower(as.matrix(gen_dist2)),
  • cholmod = TRUE,
  • precision = FALSE,
  • CS_Point.File = sp.dat,
  • JULIA_HOME = JULIA_HOME) [1] "Test: Run Circuitscape from Julia"

Test Passed

jl.optim <- SS_optim(jl.inputs = jl.inputs,

  • GA.inputs = GA.inputs) GA | iter = 1 | Mean = -99999 | Best = -99999 GA | iter = 2 | Mean = -99999 | Best = -99999 GA | iter = 3 | Mean = -99999 | Best = -99999 GA | iter = 4 | Mean = -99999 | Best = -99999`
julian-wittische commented 2 years ago

The issue was associated with a mistake when setting the CRS of the sample locations. Using the following led to a successful run:

samples <- read.table("EGK_SampleLoc.txt", sep="") sp.dat_Julian <- SpatialPoints(samples[,c(1,2)], proj4string = CRS("+proj=longlat +datum=WGS84")) sp.dat_Julian_reproj <- spTransform(sp.dat, CRS("+proj=utm +zone=56 +south"))

wpeterman commented 2 years ago

Thanks for tracking this down, @julian-wittische!

A good check prior to running ResistanceGA is to plot your points on a raster layer to make sure they align and to extract values across your layer to ensure no points fall on NoData areas. Either of these can lead to the -99999 for fitness (ResistnaceGA error).