From 3fb82fe0fccc8c90c64d370a6afa231c503c32d9 Mon Sep 17 00:00:00 2001
From: "patrick.lambert" <patrick.mh.lambert@inrae.fr>
Date: Wed, 17 Feb 2021 19:02:53 +0100
Subject: [PATCH] possibility to export several outputs

---
 exploration/R_AS_GR3D/tryFromRougier.R   | 70 +++++-------------------
 src/main/java/miscellaneous/EasyRun.java | 33 +++++++++--
 2 files changed, 42 insertions(+), 61 deletions(-)

diff --git a/exploration/R_AS_GR3D/tryFromRougier.R b/exploration/R_AS_GR3D/tryFromRougier.R
index e793ccb..fc903b0 100644
--- a/exploration/R_AS_GR3D/tryFromRougier.R
+++ b/exploration/R_AS_GR3D/tryFromRougier.R
@@ -1,11 +1,9 @@
 library(rJava)
-library(yaml)
+#library(yaml)
 library(dplyr)
 
 rm(list=ls())
 
-set.seed(1)
-
 # # parameters and prios=rs of parameters to be fitted
 # inputData = yaml.load_file("ABC_seq_2param_3stats_NewAfterSA.yaml")
 # # load prior
@@ -21,23 +19,25 @@ set.seed(1)
 # }
 # parametersNames = unlist(parametersNames)
 
-parametersNames = c("processes.processesEachStep.8.tempMinRep",
-                "processes.processesEachStep.8.ratioS95_S50",
-                "processes.processesEachStep.6.pHomingAfterEquil")
-thetas = c(10, 2, 0.7)
 
+## to have the same working diretory as GR3D
+setwd("../..")
 # path to outputs
 outputDir = "simus/"
 
-jarfile = "../../target/GR3D-3.2-SNAPSHOT.jar"
+jarfile = "target/GR3D-3.2-SNAPSHOT.jar"
 simDuration = 48
 simBegin = 1
 timeStepDuration =1
 
 seed = 1
 
-## to have the same working diretory as GR3D
-setwd("../..")
+
+parametersNames = c("processes.processesEachStep.8.tempMinRep",
+                    "processes.processesEachStep.8.ratioS95_S50",
+                    "processes.processesEachStep.6.pHomingAfterEquil")
+thetas = c(10, 2, 0.7)
+
 
 arguments = c('','-simDuration',simDuration, '-simBegin',simBegin,
             '-timeStepDuration',timeStepDuration, 
@@ -46,54 +46,12 @@ arguments = c('','-simDuration',simDuration, '-simBegin',simBegin,
             '-env',"data/input/northeastamerica/RIOBNneaBasins_Rjava.xml",
             '-observers',"data/input/northeastamerica/RIO_obs_empty.xml")
 
-outputFile = paste0(outputDir,"output",seed)
-
+outputFile = paste0("Avirer",seed)
+.jinit(classpath = "", force.init = TRUE)
 .jinit(classpath = jarfile, force.init = TRUE)
 .jcall("miscellaneous.EasyRun","V","runSimulation",arguments, outputFile,.jarray(parametersNames),.jarray(thetas))
+.jcall("miscellaneous.EasyRun","[D","getValuesFromEnvironement","getMeanLastPercOfAut")
+.jcall("miscellaneous.EasyRun","[D","getValuesFromAquanismGroup","getRangeDistribution")
 
 
 
-
-
-
-
-# return a function to run the model with parameters as arguments
-salrun <- function(jarfile, simDuration, simBegin, timeStepDuration = 1) {
-  assign("inputData",inputData)
-  assign("outputDir",outputDir)
-  function(parameters) {
-    # loading the user's data
-    parametersNames = list()
-    for (i in 1:length(inputData$parameters)) {
-      parametersNames = c(parametersNames, inputData$parameters[[i]]$javapath)
-    }
-    # extracting the seed and the model parameters
-    seed=parameters[1]
-    thetas=parameters[2:length(parameters)]
-    outputFile = paste(outputDir,"output",seed,sep="")
-    if (!file.exists(paste(outputFile,".csv",sep=""))) {
-      # run
-      arguments=c('-q','-simDuration',simDuration, '-simBegin',simBegin,
-                  '-timeStepDuration',timeStepDuration, '-groups',"data/input/fishTryRealBV_CC.xml",
-                  '-env',"data/input/BNtryRealBasins.xml",'-observers',"data/input/obsTryRealABC.xml", '-RNGStatusIndex', format(seed,scientific=FALSE))
-    
-      .jinit(classpath=jarfile)#, force.init=TRUE)
-      .jcall("miscellaneous.EasyABC","V","runSimulation",arguments, outputFile,.jarray(unlist(parametersNames)),.jarray(thetas))
-    }
-    # output
-    likelihood=as.double(as.matrix(read.csv(paste(outputFile,".csv",sep=""), skip=10, h=F, sep=";"))[1,2])
-    agestat=as.double(as.matrix(read.csv(paste(outputFile,".csv",sep=""), skip=11, h=F, sep=";"))[1,2])
-    maxlat=as.double(as.matrix(read.csv(paste(outputFile,".csv",sep=""), skip=12, h=F, sep=";"))[1,2])
-    if (maxlat>0)
-      c(likelihood,agestat,maxlat)
-    else
-      c(NA,NA,NA)
-  }
-}
-
-model = salrun(
-  jarfile = "GR3D-1.0-SNAPSHOT.jar",
-  simDuration = 400,
-  simBegin = 2)
-
-
diff --git a/src/main/java/miscellaneous/EasyRun.java b/src/main/java/miscellaneous/EasyRun.java
index 9333804..1beb5fd 100644
--- a/src/main/java/miscellaneous/EasyRun.java
+++ b/src/main/java/miscellaneous/EasyRun.java
@@ -16,26 +16,36 @@
  */
 package miscellaneous;
 
+import java.util.Arrays;
+
 import fr.cemagref.simaqualife.extensions.pilot.BatchRunner;
 import fr.cemagref.simaqualife.pilot.Pilot;
 
 public class EasyRun {
 
+	static Pilot pilot;
+
+
 	public static void runSimulation(String[] batchArgs, String outputfilename, String[] paramNames, double[] paramValues)
 			throws Exception {
+
 		try {
-			Pilot pilot = new Pilot();
+			pilot = new Pilot();
 			BatchRunner runner = new BatchRunner(pilot);
 			pilot.init();
 			runner.parseArgs(batchArgs, false, true, false);
 			pilot.load();
+
+			// update the simulation parameters
 			ReflectUtils.setFieldValueFromPath(pilot.getAquaticWorld().getAquaNismsGroupsList().get(0),
 					"processes.processesAtEnd.0.fileNameOutput", outputfilename);
 			for (int i = 0; i < paramNames.length; i++) {
 				ReflectUtils.setFieldValueFromPath(pilot.getAquaticWorld().getAquaNismsGroupsList().get(0), paramNames[i],
 						paramValues[i]);
 			}
+			// run the simulation
 			pilot.run();
+
 			// For forcing resources releasing (like shp files)
 			System.gc();
 		} catch (Throwable e) {
@@ -45,6 +55,16 @@ public class EasyRun {
 	}
 
 
+	public static double[] getValuesFromEnvironement(String outputName) throws Exception {
+		return (double[]) ReflectUtils.getValueFromPath(pilot.getAquaticWorld().getEnvironment(), outputName);
+	}
+
+
+	public static double[] getValuesFromAquanismGroup(String outputName) throws Exception {
+		return (double[]) ReflectUtils.getValueFromPath(pilot.getAquaticWorld().getAquaNismsGroupsList().get(0), outputName);
+	}
+
+
 	public static void main(String[] args) throws Exception {
 		String[] batchArguments = ("-simDuration 48 -simBegin 1 -timeStepDuration 1 -RNGStatusIndex 1 "
 				+ "-groups data/input/northeastamerica/fishRIOBasin_Sapidissima_Rjava.xml "
@@ -55,12 +75,15 @@ public class EasyRun {
 				"processes.processesEachStep.8.ratioS95_S50", "processes.processesEachStep.6.pHomingAfterEquil" };
 
 		double[] parameterValues = new double[] { 10, 2, 0.7 };
+
 		runSimulation(batchArguments, "tsointsoin", parameterNames, parameterValues);
 
-		// runSimulation(
-		// batchArguments, "data/output/tsointsoin", new String[] { " = c("processes.processesEachStep.8.tempMinRep"",
-		// "processes.processesEachStep.8.ratioS95_S50", "processes.processesEachStep.6.pHomingAfterEquil" },
-		// new double[] { 10, 2, 0.7 });
+		System.out.println(Arrays.toString(getValuesFromEnvironement("getMeanLastPercOfAut")));
+
+		System.out.println(Arrays.toString(getValuesFromAquanismGroup("getRangeDistribution")));
+
+		// double[] basinRecruitments = runSimulation(batchArguments, "tsointsoin", parameterNames, parameterValues);
+		// System.out.println(Arrays.toString(basinRecruitments));
 
 	}
 }
-- 
GitLab