Author: echatellier Date: 2013-12-05 16:00:47 +0100 (Thu, 05 Dec 2013) New Revision: 179 Url: http://forge.codelutin.com/projects/isis-fish-docs/repository/revisions/179 Log: Traduction de la page simulation plan par Sigrid Added: trunk/src/site/en/resources/downloads/CalibrationEspeceq1q2Export_en.java trunk/src/site/en/resources/downloads/Example_SimulationPlan_en.java Modified: trunk/src/site/en/rst/v4/user/tutorials/simulationPlan.rst Added: trunk/src/site/en/resources/downloads/CalibrationEspeceq1q2Export_en.java =================================================================== --- trunk/src/site/en/resources/downloads/CalibrationEspeceq1q2Export_en.java (rev 0) +++ trunk/src/site/en/resources/downloads/CalibrationEspeceq1q2Export_en.java 2013-12-05 15:00:47 UTC (rev 179) @@ -0,0 +1,447 @@ +package analyseplans; +import static org.codelutin.i18n.I18n._; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import scripts.ResultName; +import java.io.*; +import java.util.*; +import org.codelutin.math.matrix.*; +import org.codelutin.topia.*;// to use methode StringUtil.toDouble() +import org.codelutin.util.*;// to use methode StringUtil.toDouble() +import fr.ifremer.isisfish.*; +import fr.ifremer.isisfish.types.*; +import fr.ifremer.isisfish.simulator.SimulationContext; +import fr.ifremer.isisfish.types.Date; +import fr.ifremer.isisfish.entities.*; +import fr.ifremer.isisfish.simulator.AnalysePlan; +import fr.ifremer.isisfish.simulator.AnalysePlanContext; +import fr.ifremer.isisfish.simulator.SimulationParameter; +import fr.ifremer.isisfish.datastore.SimulationStorage; +import fr.ifremer.isisfish.datastore.ResultStorage; + +/** + * /////***File must be copied in isis-database-3/ analyseplans/ + * File name : CalibrationEspeceq1q2Export.java + * /////*** File name could be modified if needed + * /////*** BUT class name must be identical to file name (without the extention ".java") see below + * + * Created: 17 septembre 2007 + * + * @author <> + * @version $Revision$ + * + * Last update: $Date$ + * by : $Author$ + */ + + +/////***You can modify class name if you want +/////***BUT attention : file name and class name must be the same (without the extention ".java"), ie here : "CalibrationEspeceq1q2Export" +public class CalibrationEspeceq1q2Export_en implements AnalysePlan { + + /** to use log facility, just put in your code: log.info("..."); */ + +/////***class name to check here + static private Log log = LogFactory.getLog(CalibrationEspeceq1q2Export_en.class); + + enum State {STATE_INIT, STATE_0, STATE_1, STATE_2, STATE_3, STATE_4}; + +/////***here must appear the path to export the historic file ("Historic.csv") where q1, q2 and criteria computed at each simulation are written +/////***Attention : before beginning a new calibration rename your eventual old file Historic.csv otherwise it will be lost + File exportHistoric = new File ("C:Documents and Settings/Sigrid/work/ISIS-FISH/Calibration/Historic.csv"); + protected String exportHisto = ""; + + +//initial points of the simplex + public Population param_Population = null; + public String param_M1 = "2.42e-5;2.11e-6";// devient un parametre du plan d analyse + public String param_M2 = "2.34e-5;2.59e-6";// devient un parametre du plan d analyse + public String param_M3 = "2.59e-5;2.41e-6";// devient un parametre du plan d analyse + //public String param_pas = "1e-5";// devient un parametre du plan d analyse +///// ***put here the path and name of the file containing the data on which you calibrate your fichery ( here observed catches) + public String param_nomfichier_debarquements = "Q:/work/ISIS-FISH/Simu/Calibration/Observedlandings2001-2003.csv";//in row : time ; in columns : age or length group + protected File debarquementsObserves; + protected MatrixND matrixDebarquement; + + protected State state = State.STATE_INIT; + public Experiences experiences = new Experiences(); + public String [] necessaryResult = { + ResultName.MATRIX_CATCH_WEIGHT_PER_STRATEGY_MET + }; + + public String[] getNecessaryResult() { + return this.necessaryResult; + } + + /** + * Permet d'afficher a l'utilisateur une aide sur le plan. + * @return L'aide ou la description du plan + */ + public String getDescription() throws Exception { + return _("Calibration using variable step Simplex method (Walters): user gives a file of observations (here catches) by time step and group (.csv), output will try to approach oservations by changing the values of catchability"); + } + + /** + * Appele au demarrage de la simulation, cette methode permet d'initialiser + * des valeurs + * @param simulation La simulation pour lequel on utilise cette regle + */ + public void init(AnalysePlanContext context) throws Exception { + if (param_nomfichier_debarquements==null || "".equals(param_nomfichier_debarquements)){ + debarquementsObserves = FileUtil.getFile(".*.csv", "fichier csv s�parateur ';'"); + } else { + debarquementsObserves = new File(param_nomfichier_debarquements); + } + int nbYear = context.getParam().getNumberOfYear(); + + TopiaContext db = context.getParam().getRegion().getStorage().beginTransaction(); + Population pop = (Population)db.findByTopiaId(param_Population.getTopiaId()); + + /////*** specify dimention of the matrix containning observations (observed landings for instance) + /////*** numbers of group/columns : could be equal to your number of classes in ISIS but may also be different if your had only aggregated data + int nbGroup = 4 ; +/////*** enter number of observation per year (if you have observation by quarter put 4) / lines of the observations file + int nbTrim = nbYear * 4; + int [] dimMatrix = {nbTrim,nbGroup}; + matrixDebarquement = MatrixFactory.getInstance().create(dimMatrix); +//matrixDebarquement = MatrixFactory.getInstance().create(new int[]{nbGroup}); +// List<PopulationGroup> groups = pop.getPopulationGroup(); +// matrixDebarquement = MatrixFactory.getInstance().create(new List[]{groups}); + matrixDebarquement.importCSV(new FileReader(debarquementsObserves),new int []{0,0}); + log.info("MatrixDebarquement : " + matrixDebarquement); + db.closeContext(); + } + + /** + * Call before each simulation + * @param context plan context + * @param nextSimulation storage used for next simulation + * @return true if we must do next simulation, false to stop plan + * @throws Exception + */ + double g1; + double g2; + double worst1; + double worst2; + + public boolean beforeSimulation(AnalysePlanContext context, SimulationStorage nextSimulation) throws Exception { + boolean doNext = true; + boolean doBoucle = true; +log.info("before simulation"); + + int number = nextSimulation.getParameter().getAnalysePlanNumber(); + +if (number <3) { +log.info("number<3"); + + String [] M1 = param_M1.split(";"); + String [] M2 = param_M2.split(";"); + String [] M3 = param_M3.split(";"); + + double [] q1 = StringUtil.toArrayDouble(M1[0], M2[0], M3[0]); + double [] q2 = StringUtil.toArrayDouble(M1[1], M2[1], M3[1]); + experiences.getExperience(number).q1 = q1[number]; + experiences.getExperience(number).q2 = q2[number]; + + changeDB(experiences.getExperience(number), nextSimulation); + +} else { +/// Here is the simplex algorithm, nothing to change here + double q1 = 1000; + double q2 = 1000; + double lastCritere = experiences.getExperience(number-1).criteria; + while (doBoucle){ + doBoucle = false; + if (state == State.STATE_INIT) { +doBoucle = false ; +log.info("state init"); + +//ranks the first three experiments according to the value of the objectif function +Collections.sort(experiences.current); +//log.info("SIMPLEXE : current 0 = " + experiences.current.get(0).criteria + "current 1 = " + experiences.current.get(1).criteria + "current 2 = " + experiences.current.get(2).criteria ); +log.info("SIMPLEXE : current 0 = " + experiences.current.get(0).criteria + "current 1 = " + experiences.current.get(1).criteria + "current 2 = " + experiences.current.get(2).criteria ); +log.info("SIMPLEXE : Best q1 = " + experiences.current.get(0).q1 + " q2 = " + experiences.current.get(0).q2); +log.info("SIMPLEXE : NextBest q1 = " + experiences.current.get(1).q1 + " q2 = " + experiences.current.get(1).q2); +log.info("SIMPLEXE : Worst q1 = " + experiences.current.get(2).q1 + " q2 = " + experiences.current.get(2).q2); + + +//computes and evaluates R +double g1 = (experiences.current.get(0).q1 + experiences.current.get(1).q1) / 2.0; +double g2 = (experiences.current.get(0).q2 + experiences.current.get(1).q2) / 2.0; +double worst1 = experiences.current.get(2).q1; +double worst2 = experiences.current.get(2).q2; + +state = State.STATE_0; + + q1 = 2 * g1 - worst1; + q2 = 2 * g2 - worst2; + +log.info ("R : q1 = " + q1 + " q2 = " + q2 ); + + } else if (state == State.STATE_0) { +doBoucle = false; +log.info("state 0"); + + // Run fifth experiments with parameters which depends on the fourth +//log.info("g1 = " + g1 + " " + "g2 = " + g2); +//log.info("worst1 = " + worst1 + " " + "worst2 = " + worst2); + + if (lastCritere > experiences.current.get(2).criteria) { +log.info("State 0 : R : lastCtritere > current2 : R pire de W"); + state = State.STATE_1; + //calcul de Cw + q1 = ((experiences.current.get(0).q1 + experiences.current.get(1).q1) / 2.0) - ( ((experiences.current.get(0).q1 + experiences.current.get(1).q1) / 2.0) - experiences.current.get(2).q1 ) / 2.0; + q2 = ((experiences.current.get(0).q2 + experiences.current.get(1).q2) / 2.0) - ( ((experiences.current.get(0).q2 + experiences.current.get(1).q2) / 2.0) - experiences.current.get(2).q2 ) / 2.0; +log.info("Cw : q1 = " + q1 + " q2 = " + q2); + + + } else if (lastCritere > experiences.current.get(1).criteria) { +log.info("State 0 :R : lastCritere > current 1 : R meilleur que W et moins bon que N"); + state = State.STATE_2; + // calcul de Cr + q1 = ((experiences.current.get(0).q1 + experiences.current.get(1).q1) / 2.0) + ( ((experiences.current.get(0).q1 + experiences.current.get(1).q1) / 2.0) - experiences.current.get(2).q1 ) / 2.0; + q2 = ((experiences.current.get(0).q2 + experiences.current.get(1).q2) / 2.0) + ( ((experiences.current.get(0).q2 + experiences.current.get(1).q2) / 2.0) - experiences.current.get(2).q2 ) / 2.0; +log.info("Cr : q1 = " + q1 + " q2 = " + q2); + + } else if (lastCritere > experiences.current.get(0).criteria) { +log.info("State 0 :R : lastCritere > current0 : R meilleur que N et moins bon que B"); + state = State.STATE_INIT; + experiences.current.remove(2);//remove(3)avant + doBoucle = true; +log.info("remove W, simplex BNR"); + + } else { // dernier cas possible: if (lastCritere < experiences.current.get(0).critere) { +log.info("State 0 :R : lastCritere < current 0 : R meilleur que B, calcul de E"); + state = State.STATE_4; + + q1 = experiences.getExperience(number-1).q1 + (experiences.current.get(0).q1 + experiences.current.get(1).q1) / 2.0 - experiences.current.get(2).q1; + q2 = experiences.getExperience(number-1).q2 + (experiences.current.get(0).q2 + experiences.current.get(1).q2) / 2.0 - experiences.current.get(2).q2; + //q1 = experiences.current.get(3).q1 + (experiences.current.get(0).q1 + experiences.current.get(1).q1) / 2.0 - experiences.current.get(2).q1; + //q2 = experiences.current.get(3).q2 + (experiences.current.get(0).q2 + experiences.current.get(1).q2) / 2.0 - experiences.current.get(2).q2; +log.info("E : q1 = " + q1 + " q2 = " + q2); + } + + } else if (state == State.STATE_1) { +log.info("state 1, simplex BNCw"); + experiences.current.remove(3); + experiences.current.remove(2); + state = State.STATE_INIT; + doBoucle = true; + + + } else if (state == State.STATE_2) { +log.info("state 2, simplex BNCr"); + experiences.current.remove(3); + experiences.current.remove(2); + state = State.STATE_INIT; + doBoucle = true; + + + } else if (state == State.STATE_4) { +log.info("state 4 :comparaison de E a B"); + doBoucle = true; + if (lastCritere < experiences.current.get(0).criteria) { +log.info("E meilleur que B, remove 2 et 3 : simplex BNE"); + experiences.current.remove(3); + experiences.current.remove(2); + } else { +log.info("E moins bon que B, remove 2 et 4, simplex BNR"); + experiences.current.remove(4); + experiences.current.remove(2); + } + + state = State.STATE_INIT; + } + + +}//fin du while +//fill in the table of experiments (keeps track of the simulation runs) + experiences.getExperience(number).q1 = q1; + experiences.getExperience(number).q2 = q2; + +log.info("on change Q dans la DB avec : q1 = " + q1 + " " + "q2 = " + q2); + +// Changes the value of q in the database + changeDB(experiences.getExperience(number), nextSimulation); + }// fin du else (number > 3) +return doNext; + }// fin du before simulation + + + + /** + + * Call after each simulation, compute criteria for last simulation + * @param context plan context + * @param nextSimulation storage used for next simulation + * @return true if we must do next simulation, false to stop plan + * @throws Exception + */ + + public boolean afterSimulation(AnalysePlanContext context, SimulationStorage lastSimulation) throws Exception { + boolean doNext = true; +log.info("after simulation"); + int number = lastSimulation.getParameter().getAnalysePlanNumber(); + ResultStorage result = lastSimulation.getResultStorage(); + +/////*** Simulated catches are cumulated over strategies, areas and metiers + + MatrixND L2 = result.getMatrix(param_Population, ResultName.MATRIX_CATCH_WEIGHT_PER_STRATEGY_MET); +// log.info("dim de L2" + " " + Arrays.toString(L2.getDim())); +/////*** If some strategies, metiers or areas must not be included in the cumulated catches see below else put "//" at the beginning of line 297 and replace "L2" by "L" at line 289 +/////*** the strategies or so to exclude must be at the begining or end of the list you enter in the parameters interface +/////*** fill the line MatrixND L = L2.getSubMatrix(a,b,c).copy(); as follow : +/////*** a = 1 if you want to exclude strategies, 2 if metiers and 4 if areas +/////*** b = indice of the first object considered +/////*** c = number of object to keep after b, b included + MatrixND L = L2.getSubMatrix(1,2,4).copy(); // for instance : keep strategies 2,3,4,5 +//log.info("sous matrice extraite"); + L = L.sumOverDim(1);// sum over strategies + L = L.sumOverDim(2);// sum over metiers + L = L.sumOverDim(4);// sum over zones +log.info("sommes sur les strategies, metiers et zones faites"); + +/////*** if observations are cumulated by groups else put "//" at the beginning of the following line +L = L.sumOverDim(3); +/////*** else if number of groups in your database differs from number of groups in observations ... write me an e-mail + +////*** if observations are cumulated over quarter a = 3, over year a = 12, else put "//" at the beginning of the following line + L = L.sumOverDim(0,a); + + L = L.reduce(); + + +/////////////////// Computation of the objective function ////////////////// +log.info("calcul du critere"); +log.info("dim de L" + " " + Arrays.toString(L.getDim())); +log.info("dim de obs" + " " + Arrays.toString(matrixDebarquement.getDim())); + double crit = 0; + for ( MatrixIterator g = L.iterator(); g.hasNext();){ + g.next(); +//loop over quarters and size classes + + int [] dim = g.getCoordinates(); + + double obs = matrixDebarquement.getValue(dim); + double simules = g.getValue(); + + crit += Math.pow(obs-simules, 2); // crit = crit + (obs-simules)^2 + }// fin du for +log.info("critere " + number + " = " + crit ); + +//add the value of the objective function in the table of experiments + experiences.getExperience(number).criteria = crit; + +// writes the results of the last simulation in a csv file +exportHisto += experiences.getExperience(number).q1 +";"+ experiences.getExperience(number).q2 +";"+ experiences.getExperience(number).criteria + "\n"; +org.codelutin.util.FileUtil.writeString(exportHistoric, exportHisto); + + +return doNext; + +}// fin du after simulation + + /** + * Modify nextSimulation database with q1 and q2 in exp. + * @param exp + * @param nextSimulation + * @throws Exception + */ + + + protected void changeDB(Experience exp, SimulationStorage nextSimulation) throws Exception { +//method called to change the database + TopiaContext db = nextSimulation.getStorage().beginTransaction(); + Population pop = (Population)db.findByTopiaId(param_Population.getTopiaId()); //get the target population + MatrixND c = pop.getCapturability(); // get the catchability matrix + +//log.info("Pour cette simulation : q1 = " + exp.q1 + ";" + "q2 = " + exp.q2 ); + +/////*** that is where you explain how to fill the catchability matrix with q1 and q2 + for (MatrixIterator i = c.iterator(); i.hasNext();){ + i.next(); + Object [] sem = i.getSemanticsCoordinates(); + PopulationGroup group = (PopulationGroup)sem[0]; + PopulationSeasonInfo season = (PopulationSeasonInfo)sem[1]; + +////*** exemple when q2 corresponds to the 12 first groups (groups 0 to 11) + if (group.getId() < 12){ + i.setValue(exp.q2); + }else { + i.setValue(exp.q1); + } + +/////*** exemple when it depends on seasons and groups +/* if (season.getFirstMonth().after(Month.JULY) && group.getId() >=18){ //month >= aout && groupID >= 18 + i.setValue(exp.q2); + }else { + i.setValue(exp.q1); + } +*/ + + }//fin du for + + db.commitTransaction(); + db.closeContext(); +} + + + + + static public class Experiences { +// create the table of experiments where simulations are stored + + /** contains last simplex and potentialy 2 more simulation */ + public List<Experience> current = new ArrayList<Experience>(); + + /** contains all experience done */ + public List<Experience> history = new ArrayList<Experience>(); + /** + * return experience requested, if this experience doesn't exist + * create it. + * + * @param i simulation number + * @return experience with simulation number fixed if new experience + * is returned + */ + public Experience getExperience(int i) { + Experience result; + if (i<history.size()) { + result = history.get(i); + } else { + result = new Experience(); + result.simNumber = i; + history.add(i, result); + current.add(result); + } + return result; + }//fin de la definition de getExperience + + +/** + * @return the history + */ + public List<Experience> getHistory() { + return this.history; + } + }// fin de la creation des listes experiences + + + + static public class Experience implements Comparable { + public int simNumber; + public double criteria; + public double q1; + public double q2; + /** + * Permit to order experience, first is experience with smallest criteria + */ + public int compareTo(Object arg0) { + Experience other = (Experience)arg0; + int result = Double.compare(this.criteria, other.criteria); + return result; + } + } + + +} \ No newline at end of file Property changes on: trunk/src/site/en/resources/downloads/CalibrationEspeceq1q2Export_en.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/src/site/en/resources/downloads/Example_SimulationPlan_en.java =================================================================== --- trunk/src/site/en/resources/downloads/Example_SimulationPlan_en.java (rev 0) +++ trunk/src/site/en/resources/downloads/Example_SimulationPlan_en.java 2013-12-05 15:00:47 UTC (rev 179) @@ -0,0 +1,310 @@ +package analyseplans; + +import static org.codelutin.i18n.I18n._; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import scripts.ResultName; + +import java.io.*; +import java.io.File; +import java.io.FileReader; +import java.io.Writer; +import java.util.*; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +import org.codelutin.math.matrix.*; +import org.codelutin.util.*; +import org.codelutin.topia.*; + +import fr.ifremer.isisfish.*; +import fr.ifremer.isisfish.types.*; +import fr.ifremer.isisfish.rule.Rule; +import fr.ifremer.isisfish.rule.RuleHelper; +import fr.ifremer.isisfish.simulator.SimulationContext; +import fr.ifremer.isisfish.types.Date; +import fr.ifremer.isisfish.entities.*; +import fr.ifremer.isisfish.simulator.AnalysePlan; +import fr.ifremer.isisfish.simulator.AnalysePlanContext; +import fr.ifremer.isisfish.simulator.SimulationParameter; +import fr.ifremer.isisfish.datastore.RegionStorage; +import fr.ifremer.isisfish.datastore.RuleStorage; +import fr.ifremer.isisfish.datastore.SimulationStorage; +import fr.ifremer.isisfish.datastore.ResultStorage; + +/* + * Exemple_PlanAnalyse.java + * + * Created: 6 juin 2007 + * + * @author sl + * @version $Revision$ + * + * Last update: $Date$ + * by : $Author$ + */ + +/** + * The way it works is that you create text files containing the simulation matrix (matrix.txt) and + * one text file per parameter modified by the plan, containing the different values taken by the parameter at each simulation. + * For instance if the TAC is modified the file will be named tac.txt. + * For parameters that are double files should look like this: + * <pre> + * -1=0.8 + * 1=1.2 + * </pre> + * + * For parameters that are String files should look like this: + * <pre> + * -1=equation written on a single line + * 1=another equation written on a single line + * </pre> + * + * For parameters that are management rules files should look like this: + * <pre> +-1=Cantonnement +rule.-1.parameter.gear=fr.ifremer.isisfish.entities.Gear\#11690286646709\#0.5814158398678262 +rule.-1.parameter.zone=fr.ifremer.isisfish.entities.Zone\#11690286645767\#0.37798185123822536 +rule.-1.parameter.beginDate=0 +rule.-1.parameter.endDate=119 +rule.-1.parameter.enginSelectivite=false +rule.-1.parameter.beginMonth=8 +rule.-1.parameter.endMonth=11 +1=Cantonnement +rule.1.parameter.gear=fr.ifremer.isisfish.entities.Gear\#11690286646709\#0.5814158398678262 +rule.1.parameter.zone=fr.ifremer.isisfish.entities.Zone\#11690286645767\#0.37798185123822536 +rule.1.parameter.beginDate=0 +rule.1.parameter.endDate=119 +rule.1.parameter.enginSelectivite=false +rule.1.parameter.beginMonth=3 +rule.1.parameter.endMonth=11 + * </pre> + */ +public class Example_SimulationPlan_en implements AnalysePlan { + + /** to use log facility, just put in your code: log.info("..."); */ + static private Log log = LogFactory.getLog(Example_SimulationPlan_en.class); + + /// Enter here the names of the matrix and parameter files + /// In this example, we change the bounds of size classes (growth), + /// catchability, selectivity and the parameters of the Marine protected area. + + static private final String MATRIX = "matrix"; /// The file is names matrix.txt + static private final String VBGF = "growth"; + static private final String SELECTIVITY = "selectivity"; + static private final String CATCHABILITY = "capturabilite"; + static private final String AMP = "amp"; + + /// Here are the public parameters of the simulation plan (the parameters you can change in the interface) + + public int param_parameterNumber = 4; /// This parameter is normally not modify, it is the number of column in the simulation matrix + public int param_first = 0; /// We use this parameter to indicate at which line of the matrix the simulations should start. + /// It's useful if the plan has been interrupted and you don't want to start all over again. + public int param_simulationNumber = 16;/// this parameter is also not modified in theory, this is the number of lines in the simulation matrix. + public String param_directory = "Exemple_directory/"; /// Here enter the path to th efolder where the text files are stored, + /// by default the same folder where the launch file is. + + + /// Declaration of the matrix of experiments + private MatrixND matrix = null; + + public String [] necessaryResult = { + // put here all necessary result for this rule + // example: + // ResultName.MATRIX_BIOMASS, + // ResultName.MATRIX_NET_VALUE_OF_LANDINGS_PER_STRATEGY_MET, + }; + + public String[] getNecessaryResult() { + return this.necessaryResult; + } + + /** + * Permet d'afficher a l'utilisateur une aide sur le plan. + * @return L'aide ou la description du plan + */ + public String getDescription() throws Exception { + return _("Do the experiments of a full factorial design for 4 parameters with 2 modalities"); + } + + /** + * Appel� au d�marrage de la simulation, cette m�thode permet d'initialiser + * des valeurs + * @param simulation La simulation pour lequel on utilise ce plan + */ + public void init(AnalysePlanContext context) throws Exception { + /// Create the matrix of experiements and load the values from the text file: + File dir = new File(param_directory); + matrix = MatrixFactory.getInstance().create(new int[]{param_simulationNumber, param_parameterNumber}); + matrix.importCSV(new FileReader(new File(dir, MATRIX + ".txt")), new int[]{0,0}); + matrix.setSemantics(1, Arrays.asList(new String[]{VBGF,CATCHABILITY, SELECTIVITY,AMP})); +System.out.println(matrix); +System.out.println("amp 0 : "+ matrix.getValue(0,AMP)); + /// add the management rules modified by the simulation plan + context.getParam().addExtraRules("Cantonnement"); + } + + + /// Here we create a method to read the value of the modality of a parameter in the matrix of experiments for the ongoing simulation + /// The arguments for this method are the name of the text file and the rank of the simulation in the simulation design + /// For a parameter that is a double + /** + * @param name name of the file + * @param simulation rank of the simulation + * @return + */ + private double getDouble(String name, int simulation) throws Exception { + File dir = new File(param_directory); + Properties prop = new Properties(); + prop.load(new BufferedReader(new FileReader(new File(dir, name + ".txt")))); + int ligne = simulation + param_first; + int mod = (int)matrix.getValue(ligne, name); +System.out.println("mod et ligne : "+ mod+" "+ligne); + double result = Double.parseDouble(prop.getProperty(""+mod)); +System.out.println("result : "+ result); + return result; + } + + /// for a list of doubles + /** + * @param name name of the file + * @param simulation rank of the simulation + * @return + */ + private double [] getList(String name, int simulation) throws Exception { + File dir = new File(param_directory); + Properties prop = new Properties(); + prop.load(new BufferedReader(new FileReader(new File(dir, name + ".txt")))); + int ligne = simulation + param_first; + int mod = (int)matrix.getValue(ligne, name); + double [] result = StringUtil.toArrayDouble(prop.getProperty(""+mod).split(";")); // ""+ builds a character string + return result; + } + + /// for a character string (equation) + /** + * @param name name of the file + * @param simulation rank of the simulation + * @return + */ + private String getString(String name, int simulation) throws Exception { + File dir = new File(param_directory); + Properties prop = new Properties(); + prop.load(new BufferedReader(new FileReader(new File(dir, name + ".txt")))); + int ligne = simulation + param_first; + int mod = (int)matrix.getValue(ligne , name); + String result = prop.getProperty(""+mod); + return result; + } + /// for the parameters of a management rule + /** + * @param name name of the file + * @param simulation rank of the simulation + * @return + */ + private Rule getRule(RegionStorage regionStorage, String name, int simulation) throws Exception { + File dir = new File(param_directory); + Properties prop = new Properties(); + prop.load(new BufferedReader(new FileReader(new File(dir, name+".txt")))); + int ligne = simulation + param_first; + int mod = (int)matrix.getValue(ligne , name); + String ruleName = prop.getProperty(""+mod); + RuleStorage ruleStorage = RuleStorage.getRule(ruleName); + Rule rule = ruleStorage.getNewRuleInstance(); + RuleHelper.populateRule(mod, regionStorage, rule, prop); + return rule; + } + + + + /** + * Call before each simulation + * @param context plan context + * @param nextSimulation storage used for next simulation + * @return true if we must do next simulation, false to stop plan + * @throws Exception + */ + public boolean beforeSimulation(AnalysePlanContext context, SimulationStorage nextSimulation) throws Exception { + + int simNum = nextSimulation.getParameter().getAnalysePlanNumber()+ param_first; + + if (simNum < param_simulationNumber+1) { /// check if the end of the plan is reached + + /// Use of the methods to read parameter values in the matrix of experiments + double [] croissance = getList (VBGF , simNum); + double catchability = getDouble (CATCHABILITY, simNum); + String selectivity = getString (SELECTIVITY, simNum); + Rule amp = getRule(nextSimulation.getStorage(), AMP, simNum); + + /// Write information in the log + nextSimulation.getInformation().addInformation("Growth ("+context.getNumber()+")= " + croissance); + nextSimulation.getInformation().addInformation("Selectivity ("+context.getNumber()+")= " + selectivity); + + + /// Here the values of the parameters in the database are modified according to the modality in the matrix of experiments + /// For management rules + + // remove the previous management rule(it is stored under the name "LastAMP") + List<Rule> paramRules = nextSimulation.getParameter().getRules(); + // add the new rule to the simulation + paramRules.add(amp); + + /// Modify parameters in the database: + TopiaContext tx = nextSimulation.getStorage().beginTransaction(); + /// get the objects to modify in the database + PopulationDAO popDAO = IsisFishDAOHelper.getPopulationDAO(tx); + Population pop = popDAO.findByName("test population"); + List<PopulationGroup> groups = pop.getPopulationGroup(); + GearDAO gearDAO = IsisFishDAOHelper.getGearDAO(tx); + List<Gear> gears = gearDAO.findAll(); + + ///Modify bounds of size classes + /// replace with the elements in the list + for (PopulationGroup group : groups) { + group.setMinLength(croissance[group.getId()]) ; + group.setMaxLength(croissance[group.getId()+1]) ; + } + + /// modify catchability + /// the value in the database is changed by multiplying by the value of the modality + MatrixND c = pop.getCapturability(); + for (MatrixIterator i = c.iterator(); i.hasNext();){ + i.next(); + i.setValue(i.getValue()* catchability); + } + + /// modify selectivity + /// replace the existing equation with the new one + for (Gear gear : gears) { + Selectivity sel = gear.getPopulationSelectivity(pop); + Equation eq = sel.getEquation(); + eq.setContent(selectivity); + } + + tx.commitTransaction(); + return true; + } else { + return false; + } + + } + + /** + * Call after each simulation + * @param context plan context + * @param nextSimulation storage used for next simulation + * @return true if we must do next simulation, false to stop plan + * @throws Exception + */ + public boolean afterSimulation(AnalysePlanContext context, SimulationStorage lastSimulation) throws Exception { + return true; + } + +} Property changes on: trunk/src/site/en/resources/downloads/Example_SimulationPlan_en.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: trunk/src/site/en/rst/v4/user/tutorials/simulationPlan.rst =================================================================== --- trunk/src/site/en/rst/v4/user/tutorials/simulationPlan.rst 2013-12-05 14:44:36 UTC (rev 178) +++ trunk/src/site/en/rst/v4/user/tutorials/simulationPlan.rst 2013-12-05 15:00:47 UTC (rev 179) @@ -14,183 +14,175 @@ .. * .. * 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 +.. * 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, see +.. * License along with this program. If not, see .. * <http://www.gnu.org/licenses/gpl-3.0.html>. .. * #L% .. - -Tutorial to use simulation plans -================================ +Tutorial for the use of Simulation plans +======================================== -This tutorial have not been translated yet. Here is the French version +An experimental design is a statistically chosen set of experiments (simulations) which differ by the +values taken by certain parameters of the model with the objective of answering a specific question. -Un plan d'analyse est un ensemble d'expériences (simulations) pour lesquelles on -va modifier la valeur de certains paramètres selon un protocole défini pour -répondre à une question. On distingue des plans de simulations pour lesquels +Two kinds of simulation plans must be distinguished : -* *les expériences sont indépendantes* . C'est à dire que l'ordre des - simulations n'a pas d'importance et que l'on pourrait par exemple les lancer - sur des ordinateurs différents. Les valeurs que les paramètres devront prendre - à chaque nouvelle simulation sont déterminées au préalable. +* *experiments are independent* . That is to say that the order in which simulations are run doesn’t + matter, and they could possibly ber un on different computers. The values taken by the parameters + at each simulation are previously determined. -* *les expériences sont séquentielles* . Les valeurs des paramètres de la - simulation n+1 dépendent des résultats de la simulation n. +* *sequential experiments*. In this case, parameter values in simulation n+1 depend on the results + of simulation n. -Expériences Indépendantes : exemple d'une analyse de sensibilité ----------------------------------------------------------------- +Independent Experiments: the example of sensitivity analysis +------------------------------------------------------------ -Dans le cas d'expériences indépendantes, la première étape est de définir les -paramètres à modifier et les différentes valeurs qu'ils devront prendre -(appelées « modalités »). On les organise ensuite selon un plan d'expérience -adéquat (plan complet, factoriel, optimisé...) qu'on écrit sous forme de matrice. -Des méthodes sont déjà disponibles pour réaliser ce type de plans sous -ISIS-Fish. Ils sont décrits ici mais libre à l'utilisateur d'écrire ses propres -scripts si la méthode actuellement utilisée ne répond pas à ses besoins. Voici -la description et le fonctionnement des scripts existants : ils nécessitent -l'écriture de 3 types de fichiers : un script de plan d'analyse, une matrice -d'expérience et des fichiers paramètre : +In the case of independent simulations, first step is to identy the parameters which are modified +at each simulation and the values they take (“modalities”). An adequate simulation plan is then +designed (full factorial design, Latin hypercube…) which translates into a matrix. -La matrice d'expérience -~~~~~~~~~~~~~~~~~~~~~~~ +Methods are available in ISIS-Fish to design these kinds of simulation plans without any coding +(scripts available and interface sensitivity analysis), however if the proposed methods do not cover +the user’s needs, he is encouraged to adapt the scripts, or write and share his own scripts. -*La matrice d'expérience* est une matrice [expériences x paramètres] : chaque -colonne correspond à l'un des paramètres à modifier et chaque ligne contient -les modalités des paramètres pour une expérience donnée. Les modalités sont -codées par des entiers (-1 ;0 ;1 etc). Cette matrice est copiée dans un -fichier .txt sans en-tête de colonnes ni de lignes. +Here is how a simulation plan works: Three types of files are required: -Les fichiers paramètres -~~~~~~~~~~~~~~~~~~~~~~~ +A script of simulation plan, a matrix of experiment, and parameters files. -*Les fichiers paramètres* : un fichier .txt par paramètre donnant la -correspondance entre la valeur de la modalité (-1 ;0 ;1) dans la matrice et la -valeur du paramètre. Ce paramètre peut être un réel, une liste de réels, une -équation… Si l'on nomme les modalités –1 et 1, le fichier est de la forme +The matrix of experiments +~~~~~~~~~~~~~~~~~~~~~~~~~ -Pour un paramètre qui est une liste +* *The matrix of experiments* is a matrix [experiments x parameters] : each column corresponds to + one of the parameters modified in the analysis with its modalities and each line corresponds to one + simulation. Usually, discrete modalities are coded using integers (-1 ;0 ;1 etc) but the value taken by + the parameter can as well be listed. + +The matrix must be copied in a text file (.txt) without row names nor column names. + +Parameter files +~~~~~~~~~~~~~~~ + +*Parameter files*: If the modalities are coded with integers, a text file (.txt) per parameter (or +column of the matrix) which gives the correspondence between the modality (-1 ;0 ;1) in the matrix +and the value of the parameter to enter in the database. It is mostly useful if the parameter is an +equation, a list, a matrix, or a management rule... + +For instance if the modalities are –1 and 1, the file looks like : + +For a parameter which is a list + ___________________________________ :: - -1=0.5;0.8;0.9;0.11 - 1=0.5;0.11;0.15;0.41 + -1=0.5;0.8;0.9;0.11 -Pour un paramètre qui est une équation + 1=0.5;0.11;0.15;0.41 + +For a parameter which is a list an equation + ______________________________________ :: - -1=if(condition = true) return 5 ; else return 3 ; - 1=if(condition = true) return 7 ; else return 3 ; + -1=if(condition = true) return 5 ; else return 3 ; -Pour un paramètre qui est un réel + 1=if(condition = true) return 7 ; else return 3 ; + +For a parameter which is a real + _________________________________ :: - -1=0.8 - 1=1.2 + -1=0.8 -Autres + 1=1.2 + +Other… + ______ -On peut aussi modifier les paramètres des règles de gestion...(voir -exemple). Il faut alors indiquer dans le .txt tous les paramètres public de -la règle. Attention pour certaines règles de gestion il faut définir la -population, l'engin, la zone... ciblée par la règle dans les paramètres. -Pour ce faire il n'est pas possible d'écrire le nom de la population, de la -zone... il faut déterminer le code correspondant qui est du genre +It’s possible for instance to change the management rules and their parameters… (see example). +In this case, you need to fill the .txt with all the public parameters of the rule. Be aware +that for some rules the public parameters are population or zone, in this case you will need to find +the code corresponding to the pop or zone, which looks like this: :: - fr.ifremer.isisfish.entities.Zone#1169028645767#0.37798185123822536 + fr.ifremer.isisfish.entities.Zone#1169028645767#0.37798185123822536 -Pour cela on peut faire tourner une simulation avec la règle en question -correctement paramétrée et aller voir dans les logs (fichier debug.txt dans -le même dossier que le .bat de lancement), ces codes sont inscrits à coté du -nom du paramètre au moment de l'initialisation des règle de gestion. +The easiest way, is to run a simulation with the rule correctly filed and look in the logs (debug.txt) for +the codes, which appear after the name of the parameter when the rule is initiated. -*Attention !!!!* dans ces fichiers la syntaxe est importante: +*Warning !!!!* in these files the syntax is of importance : -* Pas d'espace -* Pas de « ; » à la fin des lignes -* Pas de « + » devant un chiffre quand il désigne une modalité (+1=NON ; 1=OUI) +* no space +* no « ; » at the end of the lines +* no « + » in front of a number when it refers to a modality (+1=NO ; 1=YES) -Les fichier matrix.txt et les fichiers paramètres.txt doivent être placés *dans -un même dossier* de préférence localisé dans le dossier contenant le .bat de -lancement d'ISIS-Fish (qui est la racine pour le logiciel). +For practical reasons, place the matrix.txt and parameters files *in the same folder*, at best in the +same folder when the lauching file for ISIS is (.bat). This is the root for the software which avoid +problems with paths. -Exemple +Example ~~~~~~~ -On propose ici la matrice et les fichiers paramètres d'une analyse -de sensibilité sur les paramètres de croissance, capturabilité, selectivité et -la période de fermeture d'un cantonnement. +As an example, we want here to carry out a sensitivity analysis on growth, catchability, selectivity +and time period for a spatial closure. -`Fichiers parametres et matrice`_ +`Parameter files and matrix`_ -.. _Fichiers parametres et matrice: ../../downloads/Exemple_directory.zip +.. _Parameter files and matrix: ../../downloads/Exemple_directory.zip -*Le script de plan de simulation* (écrit à partir de l'éditeur de scripts) -permet de récupérer et modifier les valeurs des paramètres en fonction de la -matrice d'expérience pour chaque simulation. (Voir le manuel pour l'explication -de la structure des scripts de plans de simulation.) +*The script for the simulation plan* (written from the editor) allows to read and modify the values of +the parameters depending on the simulation matrix. (Please refer to the manual for a description of +the structure of the scripts for simulation plans). -On joint ici le script commenté correspondant à l'analyse de sensibilité -précédante. Le script contient les méthodes nécessaires à lire les fichiers -matrice.txt et les fichiers contenant les modalités des paramètres. Il contient -aussi le code servant à modifier les valeurs de ces paramètres dans la base de -donnée (pour connaitre les méthodes permettant d'accéder aux différents -paramètres de la base de donnée, se référer aux APIs). +Download here the commented program for this sensitivity analysis -`Script d'analyse de sensibilite`_ +The code contains methods required to read the text files and modify the database (to learn how to +access parameters of the database, please refer to the tutorial on APIs). -.. _Script d'analyse de sensibilite: ../../downloads/Exemple_PlanSimulation.java +`Script sensitivity analysis`_ -Plans sequentiels : exemple de calibration par la méthode du simplexe ---------------------------------------------------------------------- +.. _Script sensitivity analysis: ../../downloads/Example_SimulationPlan_en.java -Dans ce cas, les modalités prises par les paramètres à la simulation suivante -dépendent des résultats des simulations précédentes. Il faut donc écrire -l'algorithme de calcul de ces nouvelles valeurs en fonction des résultats de -simulation dans le script du plan d'analyse. Il faut également écrire le code -permettant de modifier la valeur des paramètres pour la remplacer par la -nouvelle valeur calculée. +Sequential simulation designs: an example of calibration using the simplex method +--------------------------------------------------------------------------------- -Exemple +In the case of sequential designs, le values taken by a parameter in simulation n+1 depend on +the results of simulation n, n-1, n-2... The script of the simulation plan contains the algorithm of +computation of these news values. In addition the script contains the code to modify the parameters +in the database. + +Example ~~~~~~~ -L'exemple reprend un script qui permet de calibrer deux paramètres (ici la -capturabilité) par la méthode du simplexe à pas variable à partir des -débarquements trimestriels. +This example is available in the software when downloaded. It allows for the calibration of two +parameters (usually two catchability parameters)using the simplex method, the objective function +being the mean squared error between catch per quarter simulated and observed. -`Script de calibration`_ +`Calibration script`_ -.. _Script de calibration: ../../downloads/calibration.java +.. _Calibration script: ../../downloads/CalibrationEspeceq1q2Export_en.java -Lancement d'un Plan d'analyse ------------------------------ +Launching a simulation plan +--------------------------- -Quand les scripts sont écrits, c'est tout simple ! +As soon as scripts as written and checked it’s simple ! +In the simulation interface, prepare your simulation as usual, except if your simulation plan modifies +management rules: If a rule is modified it must not be loaded in the interface (the simulation plan +will add it). Check the box “use a simulation plan”. In the corresponding tab, select your simulation +plan in the list, and clic “add”. Fill in the parameters. Go back to the simulation tab and clic simulate. -Dans l'interface de lancement de simulation on prépare sa simulation comme -d'habitude à l'exception des règles de gestion : il ne faut pas charger une -règle si elle doit être modifiée par le plan car elle sera ajoutée par le plan -d'analyse à la suite des règles entrées dans l'interface. On coche la case -« Utiliser le plan d'analyse ». On sélectionne le plan Exemple_PlanAnalyse ou -CalibrationExpeceq1q2 dans la liste déroulante. On remplit les paramètres du -plan et on lance la simulation. +References +---------- -PS : Attention à bien vérifier les différentes étapes listées dans le Pense-Bête -sinon gare aux 500 simulations sans effectifs initiaux... ;-) - -Biblio intéressante -------------------- - * Drouineau, H., Mahévas, S., Pelletier, D. and Beliaeff, B. 2006. Assessing the impact of different management options using ISIS-Fish: the French Hake-Nephrops mixed fishery of the Bay of Biscay. Aquatic living resource, @@ -205,4 +197,5 @@ Simplex optimization: a technique for improving quality and productivity in research, development, and manufacturing (Chemometrics series). B.R. CRC Press LLC. 402 pp. -* Nocedal, J., and Wright, S.J. 2006. Numerical Optimization. Mikosch, T. V., Resnick, S. I. , andRobinson, S. M. (Eds.). 2nd Ed., Springer Series in Operations Research, New York. \ No newline at end of file +* Nocedal, J., and Wright, S.J. 2006. Numerical Optimization. Mikosch, T. V., Resnick, S. I. , + andRobinson, S. M. (Eds.). 2nd Ed., Springer Series in Operations Research, New York.