Commit 25e55392 authored by patrick.lambert's avatar patrick.lambert
Browse files

run GR3D from R

parent 3f02d766
......@@ -58,7 +58,7 @@ simulationList = list()
for (simul in 1:nrow(factorModalities)) {
# simulation names
nameSimul = tibble(name = paste(names(factorModalities), factorModalities[i,], sep = ':')) %>%
nameSimul = tibble(name = paste(names(factorModalities), factorModalities[simul,], sep = ':')) %>%
summarise(name = paste(name, collapse = '_')) %>% unlist(use.names = FALSE)
#cat(nameSimul, '\n')
......@@ -69,7 +69,7 @@ for (simul in 1:nrow(factorModalities)) {
# parameters to be modified in class AquaNismGroup
parametersValueANG = c()
for (factor in names(factorModalities %>% select(-replicate))) {
modality = factorModalities[i, factor] %>% unlist(use.names = FALSE)
modality = factorModalities[simul, factor] %>% unlist(use.names = FALSE)
#cat('\t',factor , ':', modality, '\n')
parametersValueANG = parametersValueANG %>%
......@@ -88,3 +88,4 @@ for (simul in 1:nrow(factorModalities)) {
parametersValueENV = parametersValueENV)
}
simulationList
unlist(map(simulationList, 'longName'))
library(tidyverse)
library(rJava) # see J4R
#library(yaml)
library(tidyverse)
library(jdx)
library(tictoc)
library(XML)
library(doParallel)
rm(list = ls())
## to have the same working directory as GR3D
setwd("~/Documents/workspace/GR3D/exploration/NEA_sensitivity_analysis")
setwd("../..")
rm(list = ls())
# ========================================================
# load the function to calculate the metapopulations
source("exploration/GR3D_Rdescription/GR3Dmetapopulation.R")
......@@ -50,25 +52,37 @@ convertFromSALtoR = function(java_out){
}
runEasyRun = function(arguments, parametersNamesANG,thetasANG, parametersNamesENV, thetasENV) {
# run the GR3D with updated parameter values in processes --------------------------------------------
.jcall("analysis.EasyRun", "V", "runSimulation", arguments,
.jarray(parametersNamesANG), .jarray(thetasANG),
.jarray(parametersNamesENV), .jarray(thetasENV))
}
# runEasyRun = function(arguments, parametersNamesANG,thetasANG, parametersNamesENV, thetasENV) {
# # run the GR3D with updated parameter values in processes --------------------------------------------
# .jcall("analysis.EasyRun", "V", "runSimulation", arguments,
# .jarray(parametersNamesANG), .jarray(thetasANG),
# .jarray(parametersNamesENV), .jarray(thetasENV))
# }
exploreEasyRun = function(){
exploreEasyRun = function(simulationName){
# call GR3D method to get model outputs ----------------------------------------------
# 00. simulation name
# NE MARCHE PAS
# simulationName = .jcall("analysis.EasyRun","[Ljava/lang/String;", "getStringsFromEnvironment",
# "simulationName")
# 0. abundance (caution spawner abundance of the last reproduction (no averaging) )
lastSpawnerAdundance = .jcall("analysis.EasyRun","D", "getSingleValueFromAquanismGroupProcess",
"processes.processesEachStep.10.getSpawnerEffective")
# 1. northernmost populated basin_id
# 2. centroid latitude
out_range = .jcall("analysis.EasyRun","[[Ljava/lang/String;","getValuesFromAquanismGroupProcess","processes.processesEachStep.10.exportToR")
out_range = .jcall("analysis.EasyRun","[[Ljava/lang/String;", "getValuesFromAquanismGroupProcess",
"processes.processesEachStep.10.exportToR")
range = suppressMessages(convertFromSALtoR(out_range))
# 3. log-likelihood ----
# the proportion of reproductions leading to a recruitment > 50 over the 1920-1950 period was used
# as a proxy of predicted probability of presence in a basin.
# Then a log-likelihood of a binomial distribution is computed
logL = .jcall("analysis.EasyRun", "D", "getSingleValueFromAquanismGroupProcess", "processes.processesAtEnd.0.getLogLikelihood");
logL = .jcall("analysis.EasyRun", "D", "getSingleValueFromAquanismGroupProcess",
"processes.processesAtEnd.0.getLogLikelihood");
# 4. proportion of first-time spawners
# annual % of first-time spawners according to latitude (or basin_id)
......@@ -78,35 +92,40 @@ exploreEasyRun = function(){
# 5. mean age of female and age
# annual mean age over the period (1920 and 1950) in each basin,
# and average over basin
out_spawners_features = .jcall("analysis.EasyRun","[[Ljava/lang/String;","getValuesFromAquanismGroupProcess","processes.processesEachStep.9.exportToR")
spawners_features =suppressMessages (convertFromSALtoR(out_spawners_features))
out_spawners_features = .jcall("analysis.EasyRun","[[Ljava/lang/String;","getValuesFromAquanismGroupProcess",
"processes.processesEachStep.9.exportToR")
spawners_features = suppressMessages (convertFromSALtoR(out_spawners_features))
slopePrimiparous = lm(pct_primiparous~latitude, data = spawners_features)$coeff[2]
meanAgeFemale = mean(spawners_features$mean_age_female, na.rm = TRUE)
meanAgeMale = mean(spawners_features$mean_age_male, na.rm = TRUE)
# ------------------------------------------------------------ #
# 5. number of metapopulation based on the exchange matrix ----
# 6. number of metapopulation based on the exchange matrix ----
# mean annual exchange matrix between 1920 and 1950
out_exchanges = .jcall("analysis.EasyRun","[[Ljava/lang/String;","getValuesFromAquanismGroupProcess","processes.processesEachStep.16.exportToR")
out_exchanges = .jcall("analysis.EasyRun","[[Ljava/lang/String;","getValuesFromAquanismGroupProcess",
"processes.processesEachStep.16.exportToR")
exchanges = suppressMessages(convertFromSALtoR(out_exchanges))
# keep only basin with a totalRun (sum over migrationBasin) >0
selectedBasin <- exchanges %>% group_by(basin = migrationBasin) %>%
summarise(totalRun = sum(effective), .groups = 'drop') %>%
selectedBasin <- exchanges %>% group_by(basin = migrationBasin) %>%
summarise(totalRun = sum(effective), .groups = 'drop') %>%
filter(totalRun > 0) %>% select(basin)
metapopulations <- exchanges %>% inner_join(selectedBasin, by=c('migrationBasin' = 'basin')) %>%
inner_join(selectedBasin, by=c('natalBasin' = 'basin')) %>%
mutate(year =0) %>% rename(originBasin = natalBasin) %>%
identyMetapopulation(., threshold = .95, verbose = FALSE)
nbMetapopulation = metapopulations$metapopulation %>%
distinct(metapop) %>%
summarise(n = n()) %>%
unlist(use.names = FALSE)
return(tibble(northern_basin_id = range$northern_basin_id,
if (nrow(selectedBasin > 0)) {
metapopulations <- exchanges %>% inner_join(selectedBasin, by = c('migrationBasin' = 'basin')) %>%
inner_join(selectedBasin, by=c('natalBasin' = 'basin')) %>%
mutate(year =0) %>% rename(originBasin = natalBasin) %>%
identyMetapopulation(., threshold = .95, verbose = FALSE)
nbMetapopulation = metapopulations$metapopulation %>%
distinct(metapop) %>%
summarise(n = n()) %>%
unlist(use.names = FALSE)
} else
nbMetapopulation =NA
return(tibble(simulationName = simulationName,
lastSpawnerAdundance=lastSpawnerAdundance,
northern_basin_id = range$northern_basin_id,
effective_centroid_latitude = range$effective_centroid_latitude,
logL = logL,
slopePrimiparous = slopePrimiparous,
......@@ -116,7 +135,6 @@ exploreEasyRun = function(){
}
# GR3D arguments -----------------------------------------
#' jarfile = "target/GR3D-3.2-SNAPSHOT.jar"
#' simDuration = 150
......@@ -135,10 +153,10 @@ exploreEasyRun = function(){
# -----------------------------------------------------
# initializes the Java Virtual Machine -------------------------------------
jarfile = "target/GR3D-3.2-SNAPSHOT.jar"
.jinit(NULL, force.init = TRUE)
.jinit(classpath = jarfile, force.init = TRUE)
.jclassPath()
# jarfile = "target/GR3D-3.2-SNAPSHOT.jar"
# .jinit(NULL, force.init = TRUE)
# .jinit(classpath = jarfile, force.init = TRUE)
# .jclassPath()
# modification of xlm parameters -----------------------
# parametersNamesANG = c("processes.processesEachStep.11.Soffset")
......@@ -157,87 +175,141 @@ jarfile = "target/GR3D-3.2-SNAPSHOT.jar"
# call GR3D method to get model outputs ----------------------------------------------
#==================== run simulation 1 =====
runSimulation = function(simulationParameter) {
arguments = c('-simDuration', 600, '-simBegin',2,
# #==================== run simulation 1 =====
# runSimulation = function(simulationParameter) {
# arguments = c('-simDuration', 600, '-simBegin',2,
# '-timeStepDuration', 1,
# '-RNGStatusIndex', format(simulationParameter$replicat,scientific = FALSE),
# '-groups',"data/input/northeastamerica/fishRIOBasin_Sapidissima_Rjava.xml",
# '-env',"data/input/northeastamerica/RIOBNneaBasins_Rjava.xml",
# '-observers',"data/input/northeastamerica/RIO_obs_empty.xml")
#
# runEasyRun(arguments,
# parametersNamesANG = simulationParameter$parametersNamesANG,
# thetasANG = simulationParameter$thetasANG,
# parametersNamesENV = simulationParameter$parametersNamesENV,
# thetasENV = simulationParameter$thetasENV )
#
# #tic()
# result = exploreEasyRun()
# #toc()
# return(result)
# }
#
# listeSimulation <- list(list(replicat = 1,
# parametersNamesANG = c("processes.processesEachStep.11.Soffset",
# "processes.processesEachStep.6.pHomingAfterEquil"),
# thetasANG = c(-1, 0.97),
# parametersNamesENV = c("simulationName"),
# thetasENV = c("Allee_highHoming")),
#
# list(replicat = 1,
# parametersNamesANG = c("processes.processesEachStep.11.Soffset",
# "processes.processesEachStep.6.pHomingAfterEquil"),
# thetasANG = c(-1, 0.75),
# parametersNamesENV = c("simulationName"),
# thetasENV = c("Allee_lowHoming")),
#
# list(replicat = 1,
# parametersNamesANG = c("processes.processesEachStep.11.Soffset",
# "processes.processesEachStep.6.pHomingAfterEquil"),
# thetasANG = c(0, 0.97),
# parametersNamesENV = c("simulationName"),
# thetasENV = c("noAllee_highHoming")),
#
# list(replicat = 1,
# parametersNamesANG = c("processes.processesEachStep.11.Soffset",
# "processes.processesEachStep.6.pHomingAfterEquil"),
# thetasANG = c(0, 0.75),
# parametersNamesENV = c("simulationName"),
# thetasENV = c("noAllee_lowHoming"))
# )
#
#
# tic()
# resSimulation = lapply(listeSimulation, runSimulation)
# toc()
# simulationParameter = simulationList[[35]]
# toto = simulationParameter$parametersValueANG[,'value']
# dim(toto)
# titi= c(0, 0.75)
# dim(titi)
#==================== run simulation 2 =====
runSimulation2 = function(simulationParameter, simSDuration = 600) {
cat(simulationParameter$name, ' = ', simulationParameter$longName,'\n')
# ---------------------------------------------------------- start the VM
jarfile = "target/GR3D-3.2-SNAPSHOT.jar"
.jinit(classpath = jarfile, force.init = FALSE)
.jclassPath()
#---------------------------------------------- parameters for simulation
# replicate and default values
arguments = c('-simDuration', simSDuration, '-simBegin',2,
'-timeStepDuration', 1,
'-RNGStatusIndex', format(simulationParameter$replicat,scientific = FALSE),
'-RNGStatusIndex', format(simulationParameter$replicat %>% unlist(use.names = FALSE), scientific = FALSE),
'-groups',"data/input/northeastamerica/fishRIOBasin_Sapidissima_Rjava.xml",
'-env',"data/input/northeastamerica/RIOBNneaBasins_Rjava.xml",
'-observers',"data/input/northeastamerica/RIO_obs_empty.xml")
arguments
# new values in the XLMs
parametersNamesANG = simulationParameter$parametersValueANG[,'target']
thetasANG = simulationParameter$parametersValueANG[,'value']
parametersNamesENV = simulationParameter$parametersValueENV['par']
thetasENV = simulationParameter$parametersValueENV['value']
# cat(parametersNamesANG, '\n')
# cat(thetasANG, '\n')
# cat(parametersNamesENV,'\n')
# cat(thetasENV, '\n')
# --------------------------------------- run the simulation with new parameters
#tic("simulationRun")
.jcall("analysis.EasyRun", "V", "runSimulationANG_ENV", arguments,
.jarray(parametersNamesANG), .jarray(thetasANG),
.jarray(parametersNamesENV), .jarray(thetasENV))
#toc()
runEasyRun(arguments,
parametersNamesANG = simulationParameter$parametersNamesANG,
thetasANG = simulationParameter$thetasANG,
parametersNamesENV = simulationParameter$parametersNamesENV,
thetasENV = simulationParameter$thetasENV )
#tic()
result = exploreEasyRun()
#tic("simulationExploration")
result = exploreEasyRun(simulationParameter$name)
#toc()
return(result)
}
listeSimulation <- list(list(replicat = 1,
parametersNamesANG = c("processes.processesEachStep.11.Soffset",
"processes.processesEachStep.6.pHomingAfterEquil"),
thetasANG = c(-1, 0.97),
parametersNamesENV = c("simulationName"),
thetasENV = c("Allee_highHoming")),
list(replicat = 1,
parametersNamesANG = c("processes.processesEachStep.11.Soffset",
"processes.processesEachStep.6.pHomingAfterEquil"),
thetasANG = c(-1, 0.75),
parametersNamesENV = c("simulationName"),
thetasENV = c("Allee_lowHoming")),
list(replicat = 1,
parametersNamesANG = c("processes.processesEachStep.11.Soffset",
"processes.processesEachStep.6.pHomingAfterEquil"),
thetasANG = c(0, 0.97),
parametersNamesENV = c("simulationName"),
thetasENV = c("noAllee_highHoming")),
list(replicat = 1,
parametersNamesANG = c("processes.processesEachStep.11.Soffset",
"processes.processesEachStep.6.pHomingAfterEquil"),
thetasANG = c(0, 0.75),
parametersNamesENV = c("simulationName"),
thetasENV = c("noAllee_lowHoming"))
)
tic()
resSimulation = lapply(listeSimulation, runSimulation)
runSimulation2(simulationList[[35]])
#resSimulation = lapply(simulationList, runSimulation2)
# registerDoParallel(NULL)
# cl <- detectCores() %>% -1 %>% makeCluster
#registerDoParallel(cores = 1)
stopImplicitCluster()
cl <- makeCluster(3)
registerDoParallel(cl)
rm(resSimulations)
#unlist(map(simulationList, 'longName'))[c(1,7,13,19 )]
tic('parralel')
resSimulations = foreach(simul = c(1,7,13,19),
.combine = rbind, .verbose = FALSE, .multicombine = TRUE,
.packages = c('tidyverse', 'rJava', 'jdx')
.inorder = FALSE) %dopar% {
res = runSimulation2(simulationList[[simul]],
)
#res = runSimulation3(simul)
}
toc()
resSimulationsSequentielle
#==================== run simulation 2 =====
runSimulation2 = function(simulationParameter) {
cat(simulationParameter$name, ' = ', simulationParameter$longName,'\n')
arguments = c('-simDuration', 600, '-simBegin',2,
'-timeStepDuration', 1,
'-RNGStatusIndex', format(simulationParameter$replicat %>% unlist(use.names = FALSE), scientific = FALSE),
'-groups',"data/input/northeastamerica/fishRIOBasin_Sapidissima_Rjava.xml",
'-env',"data/input/northeastamerica/RIOBNneaBasins_Rjava.xml",
'-observers',"data/input/northeastamerica/RIO_obs_empty.xml")
simulationList[[1]]$parametersValueANG$value
simulationList[[17]]$parametersValueANG$value
runEasyRun(arguments,
parametersNamesANG = simulationParameter$parametersValueANG[,'target'],
thetasANG = simulationParameter$parametersValueANG[,'value'],
parametersNamesENV = simulationParameter$parametersValueENV['par'],
thetasENV = simulationParameter$parametersValueENV['value'])
tic()
result = exploreEasyRun()
toc()
return(result)
}
save(resSimulations, file = 'exploration/NEA_sensitivity_analysis/simulationSequentielle.Rdata')
getwd()
source(file = 'exploration/NEA_sensitivity_analysis/prepareSensitivityAnalysis.R')
runSimulation2(simulationList[[1]])
resSimulation = lapply(simulationList, runSimulation2)
save(listeSimulation, resSimulation, file = 'simulation.Rdata')
......@@ -52,8 +52,8 @@ public class EasyRun {
}
public static void runSimulation(String[] batchArgs, String[] paramNameaANG, double[] paramValuesANG, String[] paramNameaENV,
String[] paramValuesENV) throws Exception {
public static void runSimulationANG_ENV(String[] batchArgs, String[] paramNameaANG, double[] paramValuesANG,
String[] paramNameaENV, String[] paramValuesENV) throws Exception {
try {
pilot = new Pilot();
......@@ -64,6 +64,7 @@ public class EasyRun {
// update the simulation parameters in AquaNismGroup
for (int i = 0; i < paramNameaANG.length; i++) {
// System.out.println(paramNameaANG[i] + " = " + paramValuesANG[i]);
ReflectUtils.setFieldValueFromPath(pilot.getAquaticWorld().getAquaNismsGroupsList().get(0), paramNameaANG[i],
paramValuesANG[i]);
}
......@@ -90,6 +91,11 @@ public class EasyRun {
}
public static String[] getStringsFromEnvironment(String targetName) throws Exception {
return (String[]) ReflectUtils.getValueFromPath(pilot.getAquaticWorld().getEnvironment(), targetName);
}
public static double[] getValuesFromAquanismGroup(String targetName) throws Exception {
return (double[]) ReflectUtils.getValueFromPath(pilot.getAquaticWorld().getAquaNismsGroupsList().get(0), targetName);
}
......@@ -142,7 +148,7 @@ public class EasyRun {
String[] parameterNamesENV = new String[0];
String[] parameterValuesENV = new String[0];
runSimulation(batchArguments, parameterNamesANG, parameterValuesANG, parameterNamesENV, parameterValuesENV);
runSimulationANG_ENV(batchArguments, parameterNamesANG, parameterValuesANG, parameterNamesENV, parameterValuesENV);
// System.out.println("\n== AnalyseSpawnerFeatures ==");
// String[][] spawnerRunResults = getValuesFromAquanismGroupProcess("processes.processesEachStep.9.exportToR");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment