wpeterman / ResistanceGA

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

Running SS_optim #44

Open WilburAshley opened 1 month ago

WilburAshley commented 1 month ago

Hi there,

I've been trying to run ResistanceGA optimisation for the following categorical and continuous surfaces:

These rasters are in .asc format and are at a resolution of 10km due to the total area being large and the study species dispersal and home range size (red fox). I am running SS_optim to optimise each raster independently as I have found this to run much faster than running as a raster stack. I am also running 2 replicates of each optimisation.

JULIA_HOME <- "C:/Users/User/.julia/juliaup/julia-1.10.0+0.x64.w64.mingw32/bin" # Change to your julia bin location. JuliaCall::julia_setup(JULIA_HOME, useRCall = TRUE)

Load in sample coordinates

sample_points <- read.table("C:/ResistanceGA_input/25-7-2024_inputs/Coords/Ch1_neutral_H_EA_xy_coords_adjusted_10km.txt", header = TRUE, sep = " ", row.names = 1) # Set path to coordinates CSV or you genlight@other@latlong here sp.dat <- SpatialPoints(sample_points[, c("X", "Y")], proj4string = CRS("+proj=aea +lat_0=0 +lon_0=132 +lat_1=-18 +lat_2=-36 +x_0=0 +y_0=0 +ellps=GRS80 +units=m +no_defs"))

Load genetic distance matrix

GD <- read.table("C:/ResistanceGA_input/25-7-2024_inputs/GD/Ch1_neutral_H_EA_GD_GenAlEx.txt", header = TRUE, row.names = 1, sep = " ")

Load single raster file or raster stack (change for each landscape variable)

Raster_layer <- raster("C:/ResistanceGA_input/25-7-2024_inputs/Rasters/H_EA/10km/ASC_files/Cropped/H_EA_Elevation_10km_crop.asc")

Set variables

GA.inputs_1 <- GA.prep(ASCII.dir = Raster_layer, Results.dir = "C:/ResistanceGA_output/9-7-2024/H_EA/10km/Elevation/Test/Run_1/", parallel = 7, seed = 111, cat.levels = 10, min.cat = 0, select.trans = list("A"))

GA.inputs_2 <- GA.prep(ASCII.dir = Raster_layer, Results.dir = "C:/ResistanceGA_output/9-7-2024/H_EA/10km/Elevation/Test/Run_2/", parallel = 7, seed = 112, cat.levels = 10, min.cat = 0, select.trans = list("A"))

Set variables to run Circuitscape in Julia

jl.inputs <- jl.prep(n.Pops = length(sp.dat), response = lower(GD), CS_Point.File = sp.dat, JULIA_HOME = JULIA_HOME, cholmod = T)

Run single surface optimisation for raster layer or all raster layers in a raster stack.

SSoptim_out_1 <- SS_optim(jl.inputs = jl.inputs, GA.inputs = GA.inputs_1) SSoptim_out_2 <- SS_optim(jl.inputs = jl.inputs, GA.inputs = GA.inputs_2)

The transformation for all of my continuous variable uses the "Distance" transformation regardless if select.trans is set to "A" or "M". I'm trying to understand if this is correct or not or if I am doing something wrong?

wpeterman commented 1 month ago

By default, 'Distance' will be assessed as part of the outputs. Do you mean that Distance is identified as the best supported?

WilburAshley commented 1 month ago

Thanks for the reply. No I am referring to the "Distance" transformation used on continuous variables. From what I can make out from the source code 'GA.prep' uses the 'Resistance_transform' function for the transformation of continuous variables. When setting 'select.trans = list()' as "M","A", or "R", it also included the "Distance" transformation ("9"), however even when I set 'select.trans = list(3)' which from my understanding should only the "Monomolecular" transformation, the results in my 'ContinuousResults' output file still appear to show the "Distance" transformation equation is being used, as per below:

Surface | obj.func_LL | k | AIC | AICc | R2m | R2c | LL | Equation | shape | max Elevation | -99999 | 4 | 231209.4 | 231209.6 | 0.001232 | 0.967976 | -115601 | Distance | 9 | 9

I'm happy to share my data if that helps.

wpeterman commented 1 month ago

Whenever you see -99999 as the objective function, it means that a non-target transformation was selected (by chance) by the genetic algorithm. To quickly move on from this, a distance only model is fit and a poor objective function value is assigned. After a few generations, the GA should start to desired transformations more often.

WilburAshley commented 1 month ago

Thanks Bill,

I'll try with more iterations and see if that fixes it.

WilburAshley commented 1 month ago

I've run the optimisation again with the 'run=500' but I'm still having the same issues. The optimisation test was run with an elevation raster and my GA.prep parameters and an example of my output is below. Any suggestions?

GA.inputs <- GA.prep(ASCII.dir = rast_cropped, Results.dir = "C:/ResistanceGA_output/9-7-2024/H_EA/10km/Elevation/Test/", parallel = 7, seed = 111, cat.levels = 10, select.trans = list(c(3,5,7)), max.cont = 500, run = 500)

GA | iter = 490 | Mean = -100692.4 | Best = -99999.0 GA | iter = 491 | Mean = -100692.4 | Best = -99999.0 GA | iter = 492 | Mean = -100345.7 | Best = -99999.0 GA | iter = 493 | Mean = -100345.8 | Best = -99999.0 GA | iter = 494 | Mean = -100692.4 | Best = -99999.0 GA | iter = 495 | Mean = -100345.7 | Best = -99999.0 GA | iter = 496 | Mean = -99999 | Best = -99999 GA | iter = 497 | Mean = -100345.7 | Best = -99999.0 GA | iter = 498 | Mean = -99999 | Best = -99999 GA | iter = 499 | Mean = -100692.4 | Best = -99999.0 GA | iter = 500 | Mean = -100345.7 | Best = -99999.0

wpeterman commented 1 month ago

Can you share the files you're using? I can take a look at things and see if anything jumps out to me. You can send files or a link to peterman.73@osu.edu