package analyseplans; import static org.nuiton.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.nuiton.math.matrix.*; import org.nuiton.util.*; import org.nuiton.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.TimeStep; import fr.ifremer.isisfish.entities.*; import fr.ifremer.isisfish.simulator.SimulationPlan; import fr.ifremer.isisfish.simulator.SimulationPlanContext; import fr.ifremer.isisfish.simulator.SimulationPlanIndependent; 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; /* * SAAnchois2tac.java * * Created: 6 juin 2007 * * @author bpoussin * @version $Revision: 1.27 $ * * Last update: $Date: 2007/11/02 $ *ajout des instructions pour modifier la regle Paita * by : $Author:sigrid $ */ public class ScenariosUNCOVER implements SimulationPlanIndependent { /** to use log facility, just put in your code: log.info("..."); */ static private Log log = LogFactory.getLog(ScenariosUNCOVER.class); static private final String MATRIX = "matrix"; static private final String MIGRATIONAd = "migrationAd" ; static private final String MIGRATIONJuv = "migrationJuv" ; static private final String MORTALITY = "Mortality"; static private final String EFF="Effort"; static private final String TACe = "tacEsp"; static private final String TACMPAf = "TacMpaRum"; static private final String AMPe = "mpaEsp"; static private final String RedEff = "redEff"; static private final String PAITA = "paita"; static private final String TOTCLOSURE = "TotalClosure"; int parameterNumber = 10; public int param_first = 0; public int param_simulationNumber = 640; public String param_directory = "Inputs_Anchois/ScenariosUNCOVER/"; 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 _("SI_scenarios pour le projet UNCOVER"); } public void init(SimulationPlanContext context) throws Exception { File dir = new File(param_directory); matrix = MatrixFactory.getInstance().create(new int[]{param_simulationNumber, parameterNumber}); matrix.importCSV(new FileReader(new File(dir, MATRIX + ".txt")), new int[]{0,0}); matrix.setSemantic(1, Arrays.asList(new String[]{ MORTALITY,MIGRATIONAd,MIGRATIONJuv,EFF,TACMPAf,AMPe,TACe,RedEff, PAITA,TOTCLOSURE})); context.getParam().addExtraRules("Paita5_Anchois","Cantonnement","FrTACfixMPARumMultiCost_Anchois","FrRuleBMPARumMultiCost_Anchois","TACfix_Es_Anchois","RuleB_Es_Anchois","EffortReduction","TotalClosure"); } /** * @param name le nom de l'element a recuperer * @param simulation le numero de la simulation * @return */ private double getDouble(String name, int ligne) throws Exception { File dir = new File(param_directory); Properties prop = new Properties(); prop.load(new BufferedReader(new FileReader(new File(dir, name + ".txt")))); int mod = (int)matrix.getValue(ligne, name); double result = Double.parseDouble(prop.getProperty(""+mod)); return result; } private double [] getList(String name, int ligne) throws Exception { File dir = new File(param_directory); Properties prop = new Properties(); prop.load(new BufferedReader(new FileReader(new File(dir, name + ".txt")))); int mod = (int)matrix.getValue(ligne, name); double [] result = StringUtil.toArrayDouble(prop.getProperty(""+mod).split(";")); return result; } /** * @param name le nom de l'element a recuperer * @param simulation le numero de la simulation * @return */ private String getString(String name, int ligne) throws Exception { File dir = new File(param_directory); Properties prop = new Properties(); prop.load(new BufferedReader(new FileReader(new File(dir, name + ".txt")))); int mod = (int)matrix.getValue(ligne , name); String result = prop.getProperty(""+mod); return result; } /** * @param name le nom de l'element a recuperer * @param simulation le numero de la simulation * @return */ private Rule getRule(TopiaContext tc, String name, int ligne) throws Exception { File dir = new File(param_directory); Properties prop = new Properties(); prop.load(new BufferedReader(new FileReader(new File(dir, name+".txt")))); int mod = (int)matrix.getValue(ligne , name); String ruleName = prop.getProperty(""+mod); System.out.println("ruleName : "+ ruleName); RuleStorage ruleStorage = RuleStorage.getRule(ruleName); Rule rule = ruleStorage.getNewRuleInstance(); RuleHelper.populateRule(mod, tc, 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(SimulationPlanContext context, SimulationStorage nextSimulation) throws Exception { int simNum = nextSimulation.getParameter().getSimulationPlanNumber(); int ligne = simNum + param_first ; if (ligne < param_simulationNumber) { String eqMBeg = getString (MORTALITY, ligne); double valEff = getDouble(EFF, ligne); String migValYoungs = getString (MIGRATIONJuv,ligne); String migValAdults = getString (MIGRATIONAd,ligne); Rule tacE = getRule(nextSimulation.getStorage(), TACe, ligne); Rule tacMpaRumF = getRule(nextSimulation.getStorage(), TACMPAf, ligne); Rule ampE = getRule(nextSimulation.getStorage(), AMPe, ligne); Rule redEffort = getRule(nextSimulation.getStorage(),RedEff, ligne); Rule Paita = getRule(nextSimulation.getStorage(), PAITA, ligne); //Rule Paita2 = getRule(nextSimulation.getStorage(), PAITA2, ligne); Rule TotClosure = getRule(nextSimulation.getStorage(),TOTCLOSURE, ligne); // modif les parametres des regles de gestion List paramRules = nextSimulation.getParameter().getRules(); // on ajoute les regles paramRules.add(tacE); paramRules.add(ampE); paramRules.add(tacMpaRumF); paramRules.add(redEffort); paramRules.add(Paita); //paramRules.add(Paita2); paramRules.add(TotClosure); // modif la base de données TopiaContext tx = nextSimulation.getStorage().beginTransaction(); PopulationDAO popDAO = IsisFishDAOHelper.getPopulationDAO(tx); Population pop = popDAO.findByName("Anchois_long"); List groups = pop.getPopulationGroup(); // modif migration date et coeff List saison = pop.getPopulationSeasonInfo(); String migYoungsfix = "if(group.getId() < 15 && \"Recru\".equals(departureZone.getName())){if(\"gironde\".equals(arrivalZone.getName())){return migY[0];}else if(\"Rochebonne\".equals(arrivalZone.getName())){ return migY[3];}else if(\"LandesCote\".equals(arrivalZone.getName())) {return migY[1];}else if(\"LandesLarge\".equals(arrivalZone.getName())) {return migY[2];}else if(\"MigHiver\".equals(arrivalZone.getName())){ return migY[4];}}return 0;"; String migAdultsfix = "if(group.getId() > 14 && \"MigHiver\".equals(departureZone.getName())){if(\"gironde\".equals(arrivalZone.getName())){return migA[0];}else if(\"Rochebonne\".equals(arrivalZone.getName())){ return migA[3];}else if(\"LandesCote\".equals(arrivalZone.getName())){ return migA[1];}else if(\"LandesLarge\".equals(arrivalZone.getName())) {return migA[2];}} return 0;"; String eq = ""; for(PopulationSeasonInfo sea : saison){ if (sea.getFirstMonth().equals(Month.JANUARY)){ eq = migValYoungs + migYoungsfix ; Equation eqm = sea.getMigrationEquation() ; eqm.setContent(eq); } else if (sea.getFirstMonth().equals(Month.APRIL)){ eq = migValAdults + migAdultsfix ; Equation eqm = sea.getMigrationEquation() ; eqm.setContent(eq); } System.out.println("migration "+eq); } // modif l equation de mortalite String eqMEnd = "Date date = context.getSimulationControl().getDate();int y = date.getYear();" + "double Mlarves = 104.566936588408;if (group == null){ return 0;" + " } else if (group.getId() == 0 ){if (\"gironde\".equals(zone.getName())){return Gir[y]*Mlarves;"+ "}else if (\"Rochebonne\".equals(zone.getName())) {return Ro[y]*Mlarves;"+ "}else if (\"LandesCote\".equals(zone.getName())){return Lc[y]*Mlarves;"+ "}else if (\"LandesLarge\".equals(zone.getName())){return Ll[y]*Mlarves;"+ "}else if (\"MigHiver\".equals(zone.getName())){return No[y]*Mlarves;"+ "}else return 0;" + "} else if (group.getId() == 2 ){ return 9.82478558470092 ; } else if (group.getId() == 3 ){ return 7.0649131668528 ; } else if (group.getId() == 4 ){ return 5.5310129122975 ; } else if (group.getId() == 5 ){ return 4.55120471928753 ; } else if (group.getId() == 6 ){ return 3.86997563927112 ; } else if (group.getId() == 7 ){ return 3.36835344779698 ; } else if (group.getId() == 8 ){ return 2.98330373250168 ; } else if (group.getId() == 9 ){ return 2.67826283574381 ; } else if (group.getId() == 10 ){ return 2.43054306588725 ; } else if (group.getId() == 11 ){ return 2.22531163202572 ; } else if (group.getId() == 12 ){ return 2.05245751678875 ; } else if (group.getId() == 13 ){ return 1.90484865411085 ; } else if (group.getId() == 14 ){ return 1.77730869215621 ; } else if (group.getId() == 15 ){ return 1.67 ; } else if (group.getId() == 16 ){ return 1.57 ; } else if (group.getId() == 17 ){ return 1.67 ; }else return 0;"; String [] mortality = new String[]{ eqMBeg + eqMEnd}; System.out.println("mortalite equation : "+mortality[0]); Equation eqm = pop.getNaturalDeathRate(); String mort = (String) mortality[0]; eqm.setContent(mort); // modif le nb de jours d inactivite double[] multEsp = {0.018,0.004,0.176,0.101,0.517,0.187,0.036,0.013,0.027,0.037,0.073,0.024}; double[] multPel1 = {0.031,0.070,0.075,0.056,0.077,0.088,0.082,0.087,0.079,0.053,0.018,0.011}; double[] multPel2 = {0.068,0.066,0.078,0.069,0.098,0.092,0.079,0.075,0.049,0.047,0.047,0.015}; double[] multBol1 = {0.057,0.052,0.051,0.029,0.050,0.058,0.083,0.071,0.073,0.011,0.027,0.023}; double[] multBol2 = {0.015,0.011,0.004,0.005,0.018,0.015,0.025,0.035,0.015,0.007,0.008,0.003}; StrategyDAO strategyDAO = IsisFishDAOHelper.getStrategyDAO(tx) ; List strategies = strategyDAO.findAll(); for (Strategy str : strategies) { double [] mult = multEsp; if("Espagnols".equals(str.getName())){ mult = multEsp; }else if("PelProfil1".equals(str.getName())){ mult = multPel1; }else if("PelProfil2".equals(str.getName())){ mult = multPel2; }else if("BolBasques".equals(str.getName())){ mult = multBol1; }else if("BolBretons".equals(str.getName())){ mult = multBol2; } List monthInfos = str.getStrategyMonthInfo(); for (StrategyMonthInfo month : monthInfos) { double multVal = mult[month.getMonth().getMonthNumber()]; double days = month.getMinInactivityDays()*(1.0+valEff*multVal); month.setMinInactivityDays(days); } } 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(SimulationPlanContext context, SimulationStorage lastSimulation) throws Exception { return true; } }