library(calibrar) library(doSNOW) # tous les variables d’environnement sont définies dans le script PBS setwd(Sys.getenv("PBS_O_WORKDIR")) corresp <- data.table::fread("InputsMed/df_fleet_fdi_isis.csv", data.table = FALSE) matchZoneGSA <- data.table::fread("InputsMed/MatchZonePopGSA.csv", data.table = FALSE) ############################################################################### runModel <- function(par) { #"par" provided by the calibrate function # get back the capturability parameters (par) and write them in the format the model is able to read (csv, semantic) textLines <- readLines("q.csv") for (i in seq_along(par)) { id <- 2 + i tmp <- strsplit(textLines[id], ";")[[1]] textLines[id] <- paste0(tmp[1], ";", par[i]) } write(textLines, file = "q.csv") # launch ISIS from R, cf le script PBS system2("myIsis", stdout = "debugISIS.log", stderr = "debugISIS.log") # read the model outputs and tranform them into a list ========== # catch at age export1 <- Sys.getenv("SIMUL_PATH") %>% file.path(., "resultExports/Captures_AgeStructure_pop_3pop.csv.gz") read.table(file = ., header = TRUE, sep = ";") %>% mutate(names = paste0("gp", group, "_", year + 2018)) # catch per fleet export2 <- Sys.getenv("SIMUL_PATH") %>% file.path(., "resultExports/CatchWeightSpFltYear.csv.gz") %>% read.table(file = ., header = TRUE, sep = ";") %>% filter(valSim != 0) export2d <- export2 %>% left_join(corresp) %>% left_join(matchZoneGSA) %>% mutate(value_gsa = valSim * prop) %>% group_by(yearQuarter, country_code, sub_region, super_length, super_gear) %>% summarise(value = sum(value_gsa)) %>% ungroup() %>% mutate(names = paste0( yearQuarter, country_code, sub_region, super_length, super_gear, collapse = "_" )) # combine ======================================================= export <- rbind( export1 %>% select(names, value), export2d %>% select(names, value) ) output <- as.list(export$value) names(output) <- export$names return(output) } ############################################################################### # residual sum of squares function userFunction <- function(obs, sim) ((obs - sim) / obs) ^ 2 # calibrationInfo calib_set <- calibration_setup( file = "data_calib/calibrationInfo.csv", control = list(col_skip = 1) # pour compatibilité avec ancienne version ) # Observed data calib_obs <- calibration_data(setup = calib_set, path = "data_calib") # creation of the objective function obj_fn <- calibration_objFn( model = runModel, setup = calib_set, observed = calib_obs, aggFn = calibrar:::.weighted.sum ) ############################################################################### cl <- makeCluster() registerDoSNOW(cl) invisible(clusterEvalQ(cl, { # cacher ces messages gênants library(calibrar) library(dplyr, warn.conflicts = FALSE) })) clusterExport(cl, c( "corresp", "matchZoneGSA", "userFunction", "obj_fn", "calib_set", "calib_obs" )) ############################################################################### # control argument of the calibrate function control <- list( maxgen = 500, master = "master", run = paste0("RUN_", Sys.getenv("SIMUL_NAME")), restart.file = paste0("results_", Sys.getenv("SIMUL_NAME")), REPORT = 1, parallel = TRUE, nCores = as.integer(Sys.getenv("NCPUS")), popsize = 50, trace = 3, convergence = 0.001 ) parinit <- c(1e-6, rep(1e-5, 5)) calib <- calibrate( par = parinit, fn = obj_fn, method = "default", lower = 0, upper = 1e-3, control = control ) ############################################################################### stopCluster(cl)