Author: jcouteau Date: 2009-03-18 09:16:37 +0000 (Wed, 18 Mar 2009) New Revision: 79 Modified: trunk/sensitivity/SensitivityCalculatorRMorris.java Log: Working Morris sensitivity calculator with full analysis Modified: trunk/sensitivity/SensitivityCalculatorRMorris.java =================================================================== --- trunk/sensitivity/SensitivityCalculatorRMorris.java 2009-03-17 17:36:59 UTC (rev 78) +++ trunk/sensitivity/SensitivityCalculatorRMorris.java 2009-03-18 09:16:37 UTC (rev 79) @@ -116,7 +116,7 @@ if (domain instanceof DiscreteDomain) { rInstruction = rInstruction + ((DiscreteDomain<? extends Serializable>) domain) - .getValues().size(); + .getValues().size(); } else if (domain instanceof ContinuousDomain) { rInstruction = rInstruction + ((ContinuousDomain<? extends Serializable>) domain) @@ -219,26 +219,8 @@ Scenario experimentScenario = new Scenario(); for (int i = 0; i < factorNumber; i++) { Factor<? extends Serializable> factor = factors.get(i); - if (factor.getDomain() instanceof MatrixContinuousDomain) { - if (((MatrixContinuousDomain<?>) factor.getDomain()) - .getOperator().equals("*")) { - factor - .setValueForIdentifier(((MatrixContinuousDomain<?>) factor - .getDomain()).getMatrix().mults( - morris.getValue(new int[] { i, j }))); - } - if (((MatrixContinuousDomain<?>) factor.getDomain()) - .getOperator().equals("/")) { - factor - .setValueForIdentifier(((MatrixContinuousDomain<?>) factor - .getDomain()).getMatrix().divs( - morris.getValue(new int[] { i, j }))); - } - - } else { - factor.setValueForIdentifier(morris.getValue(new int[] { i, + factor.setValueForIdentifier(morris.getValue(new int[] { i, j })); - } experimentScenario.addFactor(factor); } thisExperimentScenarios.add(experimentScenario); @@ -253,17 +235,27 @@ REngine engine = new RProxy(); try { + + // Call R + // Load sensitivity package into R (if package already loaded, + // nothing happens. + engine.voidEval("library(sensitivity)"); + log.info("Message sent to R : "+"library(sensitivity)"); //Set the working directory (for import and exports) engine.voidEval("setwd(\"" + outputdirectory.getAbsolutePath() + "\")"); + log.info("Message sent to R : "+"setwd(\"" + outputdirectory.getAbsolutePath() + + "\")"); // Export the morris object for the second run in a .morris file engine.voidEval("a<-dget(\".morris\")"); + log.info("Message sent to R : "+"a<-dget(\".morris\")"); /*int scenariosNumber = sensitivityScenarios.getScenarios().size();*/ - int scenariosNumber = (Integer) engine - .eval("length(y$X)/y$factors"); + int scenariosNumber = ((Double) engine + .eval("length(a$X)/length(a$factors)")).intValue(); + log.info("Message sent to R : "+"length(a$X)/length(a$factors)"); int sensitivityNumber = simulationStorages.get(0).getParameter() .getSensitivityExport().size(); @@ -274,7 +266,9 @@ String rInstruction = "results<-c("; for (int l = 0; l < scenariosNumber; l++) { File importFile = new File( - simulationStorages.get(l).getDirectory().toString(), + simulationStorages.get(l).getDirectory().toString() + + File.separator + + SimulationStorage.RESULT_EXPORT_DIRECTORY, simulationStorages .get(l) .getParameter() @@ -293,36 +287,55 @@ rInstruction = rInstruction + simulationResult; } } - rInstruction = rInstruction + "))"; + rInstruction = rInstruction + ")"; - // Call R - // Load sensitivity package into R (if package already loaded, - // nothing happens. - engine.voidEval("library(sensitivity)"); + log.info("Message sent to R : "+rInstruction); // Send the simulation results engine.voidEval(rInstruction); //Compute results - engine.voidEval("tell(x,y=results"); + engine.voidEval("tell(a,y=results)"); + log.info("Message sent to R : "+"tell(a,y=results)"); // Get back the sensitivity results, mu, mu star and sigma. - engine.voidEval("mu<-apply(x$ee, 2, mean)"); + engine.voidEval("mu<-apply(a$ee, 2, mean)"); + log.info("Message sent to R : "+"mu<-apply(a$ee, 2, mean)"); engine - .voidEval("mu.star <- apply(x$ee, 2, function(x) mean(abs(x)))"); - engine.voidEval("sigma <- apply(x$ee, 2, sd)"); + .voidEval("mu.star <- apply(a$ee, 2, function(a) mean(abs(a)))"); + log.info("Message sent to R : "+"mu.star <- apply(a$ee, 2, function(a) mean(abs(a)))"); + engine.voidEval("sigma <- apply(a$ee, 2, sd)"); + log.info("Message sent to R : "+"sigma <- apply(a$ee, 2, sd)"); //Create the data.frame of sensitivity indices for export purpose engine.voidEval("df=data.frame(mu,mu.star,sigma)"); + log.info("Message sent to R : "+"df=data.frame(mu,mu.star,sigma)"); - // - engine.voidEval("setwd(\"" - + simulationStorages.get(0).getDirectory().toString() + //Set working directory + engine.voidEval("setwd(\"" + outputdirectory.getAbsolutePath() + "\")"); + log.info("Message sent to R : setwd(\"" + + outputdirectory.getAbsolutePath() + "\")"); + + //Export sensitivity indices engine.voidEval("write.csv(df,\"" + simulationStorages.get(0).getParameter() .getSensitivityExport().get(k).getExportFilename() + "_SensitivityIndices.csv\")"); + log.info("Message sent to R : "+"write.csv(df,\"" + + simulationStorages.get(0).getParameter() + .getSensitivityExport().get(k) + + "_SensitivityIndices.csv\")"); + + //Export results + engine.voidEval("write.csv(results,\"" + + simulationStorages.get(0).getParameter() + .getSensitivityExport().get(k) + + "_Results.csv\")"); + log.info("Message sent to R : "+"write.csv(results,\"" + + simulationStorages.get(0).getParameter() + .getSensitivityExport().get(k) + + "_Results.csv\")"); //FIXME export through java to enable export when using Rserve }