Author: jcouteau Date: 2009-03-10 16:03:05 +0000 (Tue, 10 Mar 2009) New Revision: 70 Added: trunk/sensitivity/SensitivityCalculatorJavaExpandGrid.java Modified: trunk/sensitivity/SensitivityCalculatorRMorris.java Log: Sensitivity calculators : -Adding Expand Grid using Java for the scenario, R for the analysis on different exports, does not work with Rserve. -Update Morris method, analysis on different exports, does not work with Rserve. Added: trunk/sensitivity/SensitivityCalculatorJavaExpandGrid.java =================================================================== --- trunk/sensitivity/SensitivityCalculatorJavaExpandGrid.java (rev 0) +++ trunk/sensitivity/SensitivityCalculatorJavaExpandGrid.java 2009-03-10 16:03:05 UTC (rev 70) @@ -0,0 +1,233 @@ +/* *##% + * Copyright (C) 2006 - 2009 Code Lutin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +package sensitivity; + +import java.io.File; +import java.io.Serializable; +import java.util.List; + +import org.codelutin.j2r.REngine; +import org.codelutin.j2r.RProxy; +import org.codelutin.util.FileUtil; + +import fr.ifremer.isisfish.datastore.ExportStorage; +import fr.ifremer.isisfish.datastore.SimulationStorage; +import fr.ifremer.isisfish.simulator.sensitivity.DesignPlan; +import fr.ifremer.isisfish.simulator.sensitivity.Factor; +import fr.ifremer.isisfish.simulator.sensitivity.Scenario; +import fr.ifremer.isisfish.simulator.sensitivity.SensitivityCalculator; +import fr.ifremer.isisfish.simulator.sensitivity.SensitivityException; +import fr.ifremer.isisfish.simulator.sensitivity.SensitivityScenarios; +import fr.ifremer.isisfish.simulator.sensitivity.domain.ContinuousDomain; +import fr.ifremer.isisfish.simulator.sensitivity.domain.DiscreteDomain; + +/** + * Implementation of ExpandGrid method using Java + * + * @author jcouteau + * @version $Revision: 1.0 $ + * + */ +public class SensitivityCalculatorJavaExpandGrid implements + SensitivityCalculator { + + public SensitivityScenarios compute(DesignPlan plan) + throws SensitivityException { + + int k = 1; //number of sensitivity scenarios (initialization) + int factorNumber = plan.getFactors().size(); //number of factors + List<Factor<? extends Serializable>> factors = plan.getFactors(); //list of factors + + for (int i = 0; i < factorNumber; i++) { + if (factors.get(i).getDomain() instanceof ContinuousDomain) { + k = k + * ((ContinuousDomain<? extends Serializable>) factors + .get(i).getDomain()).getCardinality(); + } else if (factors.get(i).getDomain() instanceof DiscreteDomain) { + k = k + * ((DiscreteDomain<? extends Serializable>) factors + .get(i).getDomain()).getValues().size(); + } + } + + SensitivityScenarios thisExperiment = new SensitivityScenarios(); + List<Scenario> thisExperimentScenarios = thisExperiment.getScenarios(); + + for (int i = 0; i < k; i++) { + int result = 0; //used for the euclidian division + int rest = k; //used for the euclidian division + Scenario experimentScenario = new Scenario(); + for (int j = (factorNumber - 1); j >= 0; j--) { + Factor<? extends Serializable> factor = factors.get(j); + if (factors.get(j).getDomain() instanceof ContinuousDomain) { + result = (int) Math + .floor(rest + / ((ContinuousDomain<? extends Serializable>) factors + .get(j).getDomain()) + .getCardinality()); + rest = rest + - ((ContinuousDomain<? extends Serializable>) factors + .get(j).getDomain()).getCardinality() + * result; + } + if (factors.get(j).getDomain() instanceof DiscreteDomain) { + result = (int) Math.floor(rest + / ((DiscreteDomain<? extends Serializable>) factors + .get(j).getDomain()).getValues().size()); + rest = rest + - ((DiscreteDomain<? extends Serializable>) factors + .get(j).getDomain()).getValues().size() + * result; + } + + factor.setValueForIdentifier(rest); + experimentScenario.addFactor(factor); + } + thisExperimentScenarios.add(experimentScenario); + thisExperiment.setScenarios(thisExperimentScenarios); + } + return thisExperiment; + } + + public void analyzeResult(SensitivityScenarios sensitivityScenarios, + List<SimulationStorage> simulationStorages) + throws SensitivityException { + + int scenariosNumber = sensitivityScenarios.getScenarios().size(); + int factorNumber = sensitivityScenarios.getScenarios().get(0) + .getFactors().size(); + + REngine engine = new RProxy(); + try { + + int sensitivityNumber = simulationStorages.get(0).getParameter() + .getSensitivityExportNames().size(); + + for (int k = 0; k < sensitivityNumber; k++) { + + //Create the params vectors + for (int j = 0; j < factorNumber; j++) { + String vector = "factor" + j + "<-c("; + for (int i = 0; i < scenariosNumber; i++) { + if (i < (scenariosNumber - 1)) { + vector = vector + + sensitivityScenarios.getScenarios() + .get(i).getFactors().get(j) + .getValue() + ","; + } else { + vector = vector + + sensitivityScenarios.getScenarios() + .get(i).getFactors().get(j) + .getValue(); + } + + } + vector = vector + ")"; + engine.voidEval(vector); + } + + //Create the results vectors + String result = "result<-c("; + for (int l = 0; l < simulationStorages.size(); l++) { + File importFile = new File( + simulationStorages.get(l).getDirectory().toString(), + ExportStorage + .getExport( + simulationStorages + .get(l) + .getParameter() + .getSensitivityExportNames() + .get(k)) + .getNewExportInstance().getExportFilename() + + ExportStorage + .getExport( + simulationStorages + .get(l) + .getParameter() + .getSensitivityExportNames() + .get(k)) + .getNewExportInstance() + .getExtensionFilename()); + String simulResult = FileUtil.readAsString(importFile); + double simulationResult = Double.valueOf(simulResult); + if (l < simulationStorages.size() - 1) { + result = result + simulationResult + ","; + } else { + result = result + simulationResult; + } + } + result = result + ")"; + engine.voidEval(result); + + //Create the dataforaov data.frame + String dataframe = "dataforaov<-data.frame("; + for (int j = 0; j < factorNumber; j++) { + dataframe = dataframe + "factor" + j + "=factor(factor" + j + + "),"; + + } + dataframe = dataframe + "result=result)"; + engine.voidEval(dataframe); + + //Call aov() + String aovCall = "aovresult<-aov(result~"; + for (int j = 0; j < factorNumber; j++) { + if (j < (factorNumber - 1)) { + aovCall = aovCall + "factor" + j + "+"; + } else { + aovCall = aovCall + "factor" + j + ",data=dataforaov)"; + } + } + engine.voidEval(aovCall); + + //Export the results + /* + * SoS<-anova(aovresult)[1:3,2] + * IndSensibilite<-SoS/sum(SoS) + * write.csv(IndSensibilite) + */ + + engine.voidEval("SoS<-anova(aovresult)[1:" + factorNumber + + ",2]"); + engine.voidEval("names(SoS)<-dimnames(anova(aovtest))[[1]][1:" + + factorNumber + "]"); + engine.voidEval("IndSensibilite<-SoS/sum(SoS)"); + engine.voidEval("setwd(\"" + + simulationStorages.get(0).getDirectory().toString() + + "\")"); + engine + .voidEval("write.csv(IndSensibilite,\"" + + simulationStorages.get(0).getParameter() + .getSensitivityExportNames().get(k) + + ".csv\")"); + //FIXME export through java to enable export when using Rserve + } + + } catch (Exception e) { + e.printStackTrace(); + // Error while processing + } + + } + + public String getDescription() { + return "Implementation of Expand Grid method using Java"; + } + +} Modified: trunk/sensitivity/SensitivityCalculatorRMorris.java =================================================================== --- trunk/sensitivity/SensitivityCalculatorRMorris.java 2009-03-09 16:52:38 UTC (rev 69) +++ trunk/sensitivity/SensitivityCalculatorRMorris.java 2009-03-10 16:03:05 UTC (rev 70) @@ -18,11 +18,7 @@ package sensitivity; -import java.io.BufferedWriter; import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; import java.io.Serializable; import java.util.List; @@ -31,8 +27,10 @@ import org.codelutin.j2r.RProxy; import org.codelutin.math.matrix.MatrixFactory; import org.codelutin.math.matrix.MatrixND; +import org.codelutin.util.FileUtil; import scripts.ResultName; +import fr.ifremer.isisfish.datastore.ExportStorage; import fr.ifremer.isisfish.datastore.SimulationStorage; import fr.ifremer.isisfish.simulator.sensitivity.DesignPlan; import fr.ifremer.isisfish.simulator.sensitivity.Domain; @@ -56,11 +54,19 @@ protected String[] necessaryResult = { ResultName.MATRIX_BIOMASS }; public SensitivityScenarios compute(DesignPlan plan) { + double[] dataframe = new double[0]; int nbExperiments = 0; int factorNumber = plan.getFactors().size(); List<Factor<? extends Serializable>> factors = plan.getFactors(); + //Test all factors, if one is discrete, return null + for (int i = 0; i < factorNumber; i++) { + if (factors.get(i).getDomain() instanceof DiscreteDomain) { + return null; + } + } + String rInstruction = "a<-morris(model=NULL,factors=c("; // Creating the factors vector. @@ -196,96 +202,75 @@ public void analyzeResult(SensitivityScenarios sensitivityScenarios, List<SimulationStorage> simulationStorages) { - // This is is an analysis example with biomass analysis. + int scenariosNumber = sensitivityScenarios.getScenarios().size(); - // Creates the R expression. - String rInstruction = "tell(x,y=c("; + REngine engine = new RProxy(); + try { - for (int i = 0; i < sensitivityScenarios.getScenarios().size(); i++) { - try { - File biomass; - MatrixND matrixBiomass; - biomass = new File(simulationStorages.get(i) - .getSimulationDirectory().toString(), "Biomasses.csv"); - /* - * biomass = FileUtil.getFile(".*.csv", "fichier csv séparateur - * ';'"); - */ - matrixBiomass = MatrixFactory.getInstance().create( - new int[] { 0 }); + int sensitivityNumber = simulationStorages.get(0).getParameter() + .getSensitivityExportNames().size(); - matrixBiomass.importCSV(new FileReader(biomass), - new int[] { 0 }); + for (int k = 0; k < sensitivityNumber; k++) { - // uses the very last value of the matrix to check sensitivity - - int[] dimensions = matrixBiomass.getDim(); - if (i == sensitivityScenarios.getScenarios().size() - 1) { - rInstruction = rInstruction - + (double) matrixBiomass.getValue(dimensions) - + "))"; - } else { - rInstruction = rInstruction - + (double) matrixBiomass.getValue(dimensions) + ","; + // Creates the R expression. + String rInstruction = "tell(x,y=c("; + for (int l = 0; l < scenariosNumber; l++) { + File importFile = new File( + simulationStorages.get(l).getDirectory().toString(), + ExportStorage + .getExport( + simulationStorages + .get(l) + .getParameter() + .getSensitivityExportNames() + .get(k)) + .getNewExportInstance().getExportFilename() + + ExportStorage + .getExport( + simulationStorages + .get(l) + .getParameter() + .getSensitivityExportNames() + .get(k)) + .getNewExportInstance() + .getExtensionFilename()); + String simulResult = FileUtil.readAsString(importFile); + double simulationResult = Double.valueOf(simulResult); + if (l < simulationStorages.size() - 1) { + rInstruction = rInstruction + simulationResult + ","; + } else { + rInstruction = rInstruction + simulationResult; + } } + rInstruction = rInstruction + "))"; - } catch (Exception e) { - e.printStackTrace(); - } - } + // Call R + // Load sensitivity package into R (if package already loaded, + // nothing happens. + engine.voidEval("library(sensitivity)"); + // Send the simulation results + engine.voidEval(rInstruction); + // Get back the sensitivity results, mu, mu star and sigma. + engine.voidEval("mu<-apply(x$ee, 2, mean)"); + engine + .voidEval("mu.star <- apply(x$ee, 2, function(x) mean(abs(x)))"); + engine.voidEval("sigma <- apply(x$ee, 2, sd)"); + engine.voidEval("df=data.frame(mu,mu.star,sigma)"); + engine.voidEval("setwd(\"" + + simulationStorages.get(0).getDirectory().toString() + + "\")"); + engine + .voidEval("write.csv(df,\"" + + simulationStorages.get(0).getParameter() + .getSensitivityExportNames().get(k) + + ".csv\")"); + //FIXME export through java to enable export when using Rserve - double[] mu = { 0 }; - double[] mustar = { 0 }; - double[] sigma = { 0 }; - - // Call R - REngine engine = new RProxy(); - try { - // Load sensitivity package into R (if package already loaded, - // nothing happens. - engine.voidEval("library(sensitivity)"); - // Send the simulation results - engine.voidEval(rInstruction); - // Get back the sensitivity results, mu, mu star and sigma. - engine.voidEval("mu<-apply(x$ee, 2, mean)"); - mu = (double[]) engine.eval("mu"); - engine - .voidEval("mu.star <- apply(x$ee, 2, function(x) mean(abs(x)))"); - mustar = (double[]) engine.eval("mu.star"); - engine.voidEval("sigma <- apply(x$ee, 2, sd)"); - sigma = (double[]) engine.eval("sigma"); - - } catch (RException e) { + } + } catch (Exception e) { e.printStackTrace(); - // Error while retrieving the results } - // Treat the sensitivity results as wished. - // Create the content for export - String exportContent = ""; - for (int i = 0; i < sigma.length; i++) { - exportContent = exportContent - + "Factor : " - + sensitivityScenarios.getScenarios().get(0).getFactors() - .get(i).getName() + "\n mu = " + mu[i] - + "\n mu.star = " + mustar[i] + "\n sigma = " + sigma[i] - + "\n\n\n"; - } - - // export in the first simulation storage - try { - File export; - export = new File(simulationStorages.get(0) - .getSimulationDirectory().toString(), "sensitivity.txt"); - - FileWriter fstream = new FileWriter(export); - BufferedWriter out = new BufferedWriter(fstream); - out.write(exportContent); - out.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } public String getDescription() {