Author: jcouteau
Date: 2009-03-03 16:10:13 +0000 (Tue, 03 Mar 2009)
New Revision: 68
Modified:
trunk/sensitivity/SensitivityCalculatorRMorris.java
Log:
Simplify the scenario creation.
Modified: trunk/sensitivity/SensitivityCalculatorRMorris.java
===================================================================
--- trunk/sensitivity/SensitivityCalculatorRMorris.java 2009-03-03 10:53:32 UTC (rev 67)
+++ trunk/sensitivity/SensitivityCalculatorRMorris.java 2009-03-03 16:10:13 UTC (rev 68)
@@ -24,7 +24,6 @@
import java.io.FileWriter;
import java.io.IOException;
import java.io.Serializable;
-import java.util.ArrayList;
import java.util.List;
import org.codelutin.j2r.REngine;
@@ -54,250 +53,247 @@
*/
public class SensitivityCalculatorRMorris implements SensitivityCalculator {
- protected String[] necessaryResult = { ResultName.MATRIX_BIOMASS };
+ 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();
+ 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(";
+ 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()
- + "\"";
- }
- }
+ // 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(";
+ // 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";
- }
- }
- }
+ // 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 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 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 + "))";
+ // 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");
+ 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;
+ nbExperiments = dataframe.length / factorNumber;
- } catch (RException e) {
- e.printStackTrace();
- // Error while retrieving scenario
- }
+ } catch (RException e) {
+ e.printStackTrace();
+ // Error while retrieving scenario
+ }
- // Transform the result from R in a matrix
- MatrixND morris = MatrixFactory.getInstance().create(dataframe,
- new int[] { factorNumber, nbExperiments });
+ // Transform the result from R in a matrix
+ MatrixND morris = MatrixFactory.getInstance().create(dataframe,
+ new int[] { factorNumber, nbExperiments });
- // Setting up the scenarios.
- SensitivityScenarios thisExperiment = new SensitivityScenarios();
- List<Scenario> thisExperimentScenarios = thisExperiment.getScenarios();
- for (int j = 0; j < nbExperiments; j++) {
- Scenario experimentScenario = new Scenario();
- List<Factor<? extends Serializable>> factorslist = new ArrayList();
- for (int i = 0; i < factorNumber; i++) {
- Factor<? extends Serializable> factor = (Factor<? extends Serializable>) factors
- .get(i).clone();
- factor.setValueForIdentifier(morris
- .getValue(new int[] { i, j }));
- factorslist.add(factor);
- }
- experimentScenario.setFactors(factorslist);
- thisExperimentScenarios.add(experimentScenario);
- thisExperiment.setScenarios(thisExperimentScenarios);
- }
- return thisExperiment;
+ // 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++) {
+ Factor<? extends Serializable> factor = factors.get(i);
+ factor.setValueForIdentifier(morris
+ .getValue(new int[] { i, j }));
+ experimentScenario.addFactor(factor);
+ }
+ thisExperimentScenarios.add(experimentScenario);
+ thisExperiment.setScenarios(thisExperimentScenarios);
+ }
+ return thisExperiment;
- }
+ }
- public void analyzeResult(SensitivityScenarios sensitivityScenarios,
- List<SimulationStorage> simulationStorages) {
+ public void analyzeResult(SensitivityScenarios sensitivityScenarios,
+ List<SimulationStorage> simulationStorages) {
- // This is is an analysis example with biomass analysis.
+ // This is is an analysis example with biomass analysis.
- // Creates the R expression.
- String rInstruction = "tell(x,y=c(";
+ // Creates the R expression.
+ String rInstruction = "tell(x,y=c(";
- 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 });
+ 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 });
- matrixBiomass.importCSV(new FileReader(biomass),
- new int[] { 0 });
+ matrixBiomass.importCSV(new FileReader(biomass),
+ new int[] { 0 });
- // uses the very last value of the matrix to check sensitivity
+ // 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) + ",";
- }
+ int[] dimensions = matrixBiomass.getDim();
+ if (i == sensitivityScenarios.getScenarios().size() - 1) {
+ rInstruction = rInstruction
+ + (double) matrixBiomass.getValue(dimensions)
+ + "))";
+ } else {
+ rInstruction = rInstruction
+ + (double) matrixBiomass.getValue(dimensions) + ",";
+ }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
- double[] mu = { 0 };
- double[] mustar = { 0 };
- double[] sigma = { 0 };
+ 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");
+ // 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) {
- e.printStackTrace();
- // Error while retrieving the results
- }
+ } catch (RException 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";
- }
+ // 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");
+ // 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();
- }
+ FileWriter fstream = new FileWriter(export);
+ BufferedWriter out = new BufferedWriter(fstream);
+ out.write(exportContent);
+ out.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
- }
+ }
- public String getDescription() {
- return "Implementation of Morris method using R";
- }
+ public String getDescription() {
+ return "Implementation of Morris method using R";
+ }
- public String[] getNecessaryResult() {
- return this.necessaryResult;
- }
+ public String[] getNecessaryResult() {
+ return this.necessaryResult;
+ }
}