Author: jcouteau Date: 2009-02-26 09:31:58 +0000 (Thu, 26 Feb 2009) New Revision: 63 Added: trunk/sensitivity/SensitivityCalculatorRMorris.java Modified: trunk/pom.xml Log: Adding sensitivity analysis using Morris method and R Modified: trunk/pom.xml =================================================================== --- trunk/pom.xml 2009-02-25 10:03:40 UTC (rev 62) +++ trunk/pom.xml 2009-02-26 09:31:58 UTC (rev 63) @@ -54,7 +54,7 @@ <dependency> <groupId>ifremer</groupId> <artifactId>isis-fish</artifactId> - <version>3.2.0.4-SNAPSHOT</version> + <version>3.2.0.4-rc2-SNAPSHOT</version> <scope>compile</scope> </dependency> </dependencies> Added: trunk/sensitivity/SensitivityCalculatorRMorris.java =================================================================== --- trunk/sensitivity/SensitivityCalculatorRMorris.java (rev 0) +++ trunk/sensitivity/SensitivityCalculatorRMorris.java 2009-02-26 09:31:58 UTC (rev 63) @@ -0,0 +1,168 @@ +package sensitivity; + +import java.io.Serializable; +import java.util.List; + +import org.codelutin.j2r.REngine; +import org.codelutin.j2r.RException; +import org.codelutin.j2r.RProxy; +import fr.ifremer.isisfish.simulator.factors.SensitivityCalculator; +import fr.ifremer.isisfish.simulator.factors.DesignPlan; +import fr.ifremer.isisfish.simulator.factors.Domain; +import fr.ifremer.isisfish.simulator.factors.DiscreteDomain; +import fr.ifremer.isisfish.simulator.factors.ContinuousDomain; +import fr.ifremer.isisfish.simulator.factors.Factor; +import fr.ifremer.isisfish.simulator.factors.SensitivityScenarios; +import fr.ifremer.isisfish.simulator.factors.Scenario; + +public class SensitivityCalculatorRMorris implements SensitivityCalculator { + + 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(); + + String rInstruction = "a<-morris(model=NULL,factors=c("; + + // Creating the factors vector. + for (int i = 0; i < factorNumber; i++) { + if (i != (factorNumber - 1)) { + rInstruction = rInstruction + "\"" + factors.get(i).getName() + + "\","; + } else { + rInstruction = rInstruction + "\"" + factors.get(i).getName() + "\""; + } + } + + // Adding the number of repetition parameter (r), the morris type + // (type="oat") and the level vector + rInstruction = rInstruction + + "),r=4,design=list(type=\"oat\",levels=c("; + + // Creating the levels vector. + for (int i = 0; i < factorNumber; i++) { + Domain<? extends Serializable> domain = factors.get(i).getDomain(); + if (i != (factorNumber - 1)) { + if (domain instanceof DiscreteDomain) { + rInstruction = rInstruction + + ((DiscreteDomain<? extends Serializable>) domain) + .getValues().size() + ","; + } else if (domain instanceof ContinuousDomain) { + rInstruction = rInstruction + "4" + ","; + } + } else { + if (domain instanceof DiscreteDomain) { + rInstruction = rInstruction + + ((DiscreteDomain<? extends Serializable>) domain) + .getValues().size(); + } else if (domain instanceof ContinuousDomain) { + rInstruction = rInstruction + "4"; + } + } + } + + // Adding the grid.jump parameter + rInstruction = rInstruction + "),grid.jump=1),binf=c("; + + // Adding the binf parameter + for (int i = 0; i < factorNumber; i++) { + Domain<? extends Serializable> domain = factors.get(i).getDomain(); + if (i != (factorNumber - 1)) { + if (domain instanceof DiscreteDomain) { + rInstruction = rInstruction + "0,"; + } else if (domain instanceof ContinuousDomain) { + rInstruction = rInstruction + + ((ContinuousDomain<? extends Serializable>) domain) + .getMinBound() + ","; + } + } else { + if (domain instanceof DiscreteDomain) { + rInstruction = rInstruction + "0"; + } else if (domain instanceof ContinuousDomain) { + rInstruction = rInstruction + + ((ContinuousDomain<? extends Serializable>) domain) + .getMinBound(); + } + } + } + + // Adding the bsup parameter + rInstruction = rInstruction + "),bsup=c("; + for (int i = 0; i < factorNumber; i++) { + Domain<? extends Serializable> domain = factors.get(i).getDomain(); + if (i != (factorNumber - 1)) { + if (domain instanceof DiscreteDomain) { + rInstruction = rInstruction + + ((((DiscreteDomain<? extends Serializable>) domain) + .getValues().size()) - 1) + ","; + } else if (domain instanceof ContinuousDomain) { + rInstruction = rInstruction + + ((ContinuousDomain<? extends Serializable>) domain) + .getMaxBound() + ","; + } + } else { + if (domain instanceof DiscreteDomain) { + rInstruction = rInstruction + + ((((DiscreteDomain<? extends Serializable>) domain) + .getValues().size()) - 1); + } else if (domain instanceof ContinuousDomain) { + rInstruction = rInstruction + + ((ContinuousDomain<? extends Serializable>) domain) + .getMaxBound(); + } + } + } + rInstruction = rInstruction + "))"; + + REngine engine = new RProxy(); + try { + // Load sensitivity package into R (if package already loaded, + // nothing happens. + engine.voidEval("library(sensitivity"); + // Run sensitivity analysis + engine.voidEval(rInstruction); + // Get back experiment plan + dataframe = (double[]) engine.eval("a$X"); + + nbExperiments = dataframe.length / factorNumber; + + } catch (RException e) { + e.printStackTrace(); + // Error while retrieving scenario + } + + // Transform the result from R in a matrix + double[][] morris = new double[factorNumber][nbExperiments]; + for (int i = 0; i < factorNumber; i++) { + for (int j = 0; j < nbExperiments; j++) { + morris[i][j] = dataframe[i * nbExperiments + j]; + } + } + + // Setting up the scenarios. + SensitivityScenarios thisExperiment = new SensitivityScenarios(); + List<Scenario> thisExperimentScenarios = thisExperiment.getScenarios(); + for (int j = 0; j < nbExperiments; j++) { + Scenario experimentScenario = new Scenario(); + for (int i = 0; i < factorNumber; i++) { + factors.get(i).setValueForIdentifier(morris[i][j]); + } + experimentScenario.setFactors(factors); + thisExperimentScenarios.add(experimentScenario); + thisExperiment.setScenarios(thisExperimentScenarios); + } + + return thisExperiment; + + } + + public void analyzeResult(SensitivityScenarios sensitivityScenarios) { + + } + + public String getDescription(){ + return "Implementation of Morris method using R"; + } + +}
participants (1)
-
jcouteau@users.labs.libre-entreprise.org