package analyseplans; import static org.nuiton.i18n.I18n._; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.io.*; import java.io.File; import java.io.FileReader; import java.util.Arrays; import java.util.ArrayList; 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.entities.*; import fr.ifremer.isisfish.simulator.SimulationPlanIndependent; import fr.ifremer.isisfish.simulator.SimulationPlanContext; import fr.ifremer.isisfish.datastore.RuleStorage; import fr.ifremer.isisfish.datastore.SimulationStorage; /* * SAIndicateursEco_Anchois.java * * Created: 10/09/09 * * @author slehuta * @version $Revision: version 3.2. * * Last update: $Date: $ * by : $Author:sigrid $ */ public class SAIndicateursEco_MultiSp implements SimulationPlanIndependent { /** to use log facility, just put in your code: log.info("..."); */ static private Log log = LogFactory.getLog(SAIndicateursEco_MultiSp.class); static private final String MATRIX = "matrix"; private MatrixND matrix = null; // pour l anchois static private final String MORTALITY = "Mortality"; static private final String MIG = "DistrSpat"; static private final String GROWTH = "growth"; // pour la sardine static private final String REPROpil = "reproPil"; // pour le bar static private final String CIBbssM = "cibBarM"; static private final String CIBbssG = "cibBarG"; // pour le thon static private final String ACCESSalb = "qAlb"; // pour toutes les esp static private final String PRIX = "prix"; // flottilles static private final String COEFVPUEpel1 = "coefvpuePel1"; static private final String COEFVPUEpel2 = "coefvpuePel2"; static private final String COEFVPUEbol1 = "coefvpueBol1"; static private final String COEFVPUEbol2 = "coefvpueBol2"; static private final String COEFEFFpel1 = "coefeffPel1"; static private final String COEFEFFpel2 = "coefeffPel2"; static private final String COEFEFFbol1 = "coefeffBol1"; static private final String COEFEFFbol2 = "coefeffBol2"; static private final String COEFRUMpel1 = "coefRumPel1"; static private final String COEFRUMpel2 = "coefRumPel2"; static private final String COEFRUMbol1 = "coefRumBol1"; static private final String COEFRUMbol2 = "coefRumBol2"; static private final String EFFORTesp = "effortEsp"; static private final String EFFORTpel1 = "effortPel1"; static private final String EFFORTpel2 = "effortPel2"; static private final String EFFORTbol1 = "effortBol1"; static private final String EFFORTbol2 = "effortBol2"; static private final String FUELprice = "fuel"; // mesures de gestion static private final String TACMPARUM = "TacMpaRum"; // pour les francais static private final String MPAESP = "mpaEsp"; // pour les esp static private final String TACESP = "tacEsp"; // pour les esp static private final String PAITA = "paita"; static private final String TAE = "tae"; int param_parameterNumber = 23; public int param_first = 0; public int param_simulationNumber = 8704; public String param_directory = "Inputs_Anchois/PlanIIFET/"; public String [] necessaryResult = { }; 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 { // TODO return _("fait experiences d'un D-opt pour 17 parametres"); } /** * Appelmarrage de la simulation, cette methode permet d'initialiser * des valeurs * @param simulation La simulation pour lequel on utilise cette regle */ public void init(SimulationPlanContext context) throws Exception { File dir = new File(param_directory); matrix = MatrixFactory.getInstance().create(new int[]{8704, param_parameterNumber}); matrix.importCSV(new FileReader(new File(dir, MATRIX + ".txt")), new int[]{0,0}); matrix.setSemantic(1, Arrays.asList(new String[]{MORTALITY,MIG,REPROpil,CIBbssM,CIBbssG,ACCESSalb,FUELprice,GROWTH,PAITA,EFFORTpel1,EFFORTpel2, EFFORTbol1,EFFORTbol2,EFFORTesp,COEFRUMpel1,COEFRUMpel2,COEFRUMbol1,COEFRUMbol2,PRIX,TACMPARUM,MPAESP,TACESP,TAE})); System.out.println(matrix); context.getParam().addExtraRules("Paita5_Anchois","Cantonnement","MPARuleBRUM_conditionnel_MultiSp","Acc_Arcachon","RuleE_Es_Anchois","EffortReduction"); } /** * @param name le nom de l'element a recuperer * @param simulation le numero de la simulation * @return */ private double getDouble(String name,String colname, 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, colname); //System.out.println("nom "+name+"mod :"+mod); double result = Double.parseDouble(prop.getProperty(""+mod)); return result; } /** * @param name le nom de l'element a recuperer * @param simulation le numero de la simulation * @return */ private double [] getList(String name,String colname ,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")))); System.out.println("getList simulation ="+simulation+" colname :"+colname +"first ="+param_first); int ligne = simulation + param_first; int mod = (int)matrix.getValue(ligne, colname); 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, String colname ,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 , colname); 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 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); System.out.println("ruleName : "+ ruleName); System.out.println("properties : "+ prop.getProperty(""+mod,"beginDate")); 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 { System.out.println("begin before"); int simNum = nextSimulation.getParameter().getSimulationPlanNumber(); System.out.println("simNum ="+simNum); //int simNum = context.getNumber(); if (simNum < param_simulationNumber) { double[] croissance = getList (GROWTH,GROWTH, simNum); String eqMBeg = getString (MORTALITY,MORTALITY, simNum); String mig = getString (MIG,MIG,simNum); double reproSar = getDouble (REPROpil,REPROpil,simNum); double qThon = getDouble (ACCESSalb,ACCESSalb,simNum); double[] cibBarM = getList (CIBbssM,CIBbssM,simNum); double[] cibBarG = getList (CIBbssG,CIBbssG,simNum); double price = getDouble (PRIX,PRIX,simNum); String fuelprice = getString(FUELprice,FUELprice,simNum); nextSimulation.getParameter().getTagValue().put("fuelPrice",fuelprice); double coefvpuepel1 = getDouble(COEFVPUEpel1,COEFRUMpel1,simNum); double coefvpuepel2 = getDouble(COEFVPUEpel2,COEFRUMpel2,simNum); double coefvpuebol1 = getDouble(COEFVPUEbol1,COEFRUMbol1,simNum); double coefvpuebol2 = getDouble(COEFVPUEbol2,COEFRUMbol2,simNum); double coefeffpel1 = getDouble(COEFEFFpel1,COEFRUMpel1,simNum); double coefeffpel2 = getDouble(COEFEFFpel2,COEFRUMpel2,simNum); double coefeffbol1 = getDouble(COEFEFFbol1,COEFRUMbol1,simNum); double coefeffbol2 = getDouble(COEFEFFbol2,COEFRUMbol2,simNum); nextSimulation.getParameter().getTagValue().put("coefvpuepel1",String.valueOf(coefvpuepel1)); nextSimulation.getParameter().getTagValue().put("coefvpuepel2",String.valueOf(coefvpuepel2)); nextSimulation.getParameter().getTagValue().put("coefvpuebol1",String.valueOf(coefvpuebol1)); nextSimulation.getParameter().getTagValue().put("coefvpuebol2",String.valueOf(coefvpuebol2)); nextSimulation.getParameter().getTagValue().put("coefeffpel1",String.valueOf(coefeffpel1)); nextSimulation.getParameter().getTagValue().put("coefeffpel2",String.valueOf(coefeffpel2)); nextSimulation.getParameter().getTagValue().put("coefeffbol1",String.valueOf(coefeffbol1)); nextSimulation.getParameter().getTagValue().put("coefeffbol2",String.valueOf(coefeffbol2)); List strat = nextSimulation.getParameter().getStrategies(); Map inactivity = new HashMap(); for(Strategy str : strat){ if("Espagnols".equals(str.getName())){ inactivity.put(str,getList (EFFORTesp,EFFORTesp, simNum)); }else if("PelProfil1".equals(str.getName())){ inactivity.put(str,getList (EFFORTpel1,EFFORTpel1, simNum)); }else if("PelProfil2".equals(str.getName())){ inactivity.put(str,getList (EFFORTpel2,EFFORTpel2, simNum)); }else if("BolBasques".equals(str.getName())){ inactivity.put(str,getList (EFFORTbol1,EFFORTbol1, simNum)); }else if("BolBretons".equals(str.getName())){ inactivity.put(str,getList (EFFORTbol2,EFFORTbol2, simNum));} } ////////////////////////////////////////////////////////////////////////////////////////// // Modif rules Rule mpaEsp = getRule(nextSimulation.getStorage(), MPAESP, simNum); Rule tacEsp = getRule(nextSimulation.getStorage(), TACESP, simNum); Rule paita = getRule(nextSimulation.getStorage(), PAITA, simNum); Rule tacmparum= getRule(nextSimulation.getStorage(), TACMPARUM, simNum); Rule tae = getRule(nextSimulation.getStorage(), TAE, simNum); String ruleName = "Acc_Arcachon" ; RuleStorage ruleStorage = RuleStorage.getRule(ruleName); Rule ChalPel = ruleStorage.getNewRuleInstance(); List paramRules = nextSimulation.getParameter().getRules(); // on ajoute les regles paramRules.add(ChalPel); paramRules.add(mpaEsp); paramRules.add(tacEsp); paramRules.add(paita); paramRules.add(tacmparum); paramRules.add(tae); /////////////////////////////////////////////////////////////////////////////////////////// // Modif database TopiaContext tx = nextSimulation.getStorage().beginTransaction(); PopulationDAO popDAO = IsisFishDAOHelper.getPopulationDAO(tx); Population popAnc = popDAO.findByName("Anchois_long"); Population popSar = popDAO.findByName("Sardine"); Population popTho = popDAO.findByName("GermonATN"); Population popBarM = popDAO.findByName("Bar_Manche"); Population popBarG = popDAO.findByName("Bar_GdG"); List popsDAO = new ArrayList(); popsDAO.add(popAnc); popsDAO.add(popSar); popsDAO.add(popTho); popsDAO.add(popBarM); popsDAO.add(popBarG); List groups = popAnc.getPopulationGroup(); List pops = nextSimulation.getParameter().getPopulations(); // modif l equation de mortalite String table = "double[] Gir = { 1.00737487695202,0.972648073966282,1.16229475831665,0.794988571511489,0.808250288313429,0.881062633486975,0.849344654548444,0.90357337536964,0.908535727008596 ,0.8990557};"+ "double[] Lc = { 1.67373248632490,1.53053371538169,1000,1.07727363674847,1.10318771751015,1.26204292324141,1.18890931612246,1.31872769186814,1.33186057132054, 1.3069833};"+ "double[] Ll = { 1.07221635413322,1.03210337573559,1.25854131697145,0.833418705224535,0.847939051045617,0.928476724392764,0.893215285970529,0.953684876521156,0.959263423358487,0.9486131 };"+ "double[] Ro = { 1.07970427304884,1.03892018182404,1.27013659327899,0.837704083608148,0.852372107241012,0.933826857375023,0.89814248852123,0.959359581372648,0.965012593366373,0.9542209 };"+ "double[] No = { 1.06332801427602,1.02399798732765,1.24491807757672,0.828290561939393,0.842636173122938,0.922091667612195,0.887328676130483,0.946918015884907,0.952409055318547,0.9419248};"; String eqMEnd = "double Mlarves = 104.566936588408;if (group == null){ return 0; } else if (group.getId() == 0 ){if (\"gironde\".equals(zone.getName())){return Gir[0]*Mlarves;}else if (\"Rochebonne\".equals(zone.getName())) {return Ro[3]*Mlarves;}else if (\"LandesCote\".equals(zone.getName())){return Lc[1]*Mlarves;}else if (\"LandesLarge\".equals(zone.getName())){return Ll[2]*Mlarves;}else if (\"MigHiver\".equals(zone.getName())){return No[4]*Mlarves;}else return 0;} else if (group.getId() == 1 ){ 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 = table +eqMBeg + eqMEnd; Equation eqm = popAnc.getNaturalDeathRate(); eqm.setContent(mortality); // modif migration List saison = popAnc.getPopulationSeasonInfo(); String tabJuv = "double [] gir = {0.6209,0.2424,0.5422,0.2863,0.4440,0.7715,0.2687,0.3792,0.7217,0.475};"+ "double [] lc = {0.1818,0.0474,0.1026,0.0513,0.2610,0.0000,0.1791,0.1073,0.0237,0.106};"+ "double [] ll = {0.0455,0.0338,0.3113,0.0513,0.0430,0.1520,0.0012,0.0761,0.0299,0.083};"+ "double [] ro = {0.1348,0.2847,0.0369,0.4792,0.2357,0.0756,0.5509,0.3910,0.2222,0.268};"+ "double [] no = {0.0170,0.3917,0.0071,0.1318,0.0163,0.001,0.00023,0.0464,0.0025,0.068};"; String eqJuv = "if(group.getId() < 15 && \"Recru\".equals(departureZone.getName())){if(\"gironde\".equals(arrivalZone.getName()))return gir[y];"+ "else if(\"Rochebonne\".equals(arrivalZone.getName())) return ro[y];else if(\"LandesCote\".equals(arrivalZone.getName())) return lc[y];"+ "else if(\"LandesLarge\".equals(arrivalZone.getName())) return ll[y];else if(\"MigHiver\".equals(arrivalZone.getName())) return no[y];}return 0;"; String tabAd = "double [] gir = {0.2523304,0.2086467,0.5008767,0.1155539,0.1063631,0.5764831,0.2469257,0.2556178,0.3414639,0.289};"+ "double [] lc = {0.05400881,0.16173449,0.04129422,0.14009017,0.29670246,0.00000000,0.30205351,0.14407688,0.19059324,0.148};"+ "double [] ll = {0.138540813,0.184829055,0.252067222,0.108165542,0.363177244,0.299495371,0.002653738,0.112834679,0.151165424,0.179};"+ "double [] ro = {0.4937572,0.3623039,0.1978877,0.4360312,0.2149993,0.1233157,0.4476018,0.4413241,0.3155026,0.337};"+ "double [] no = { 0.0613627737,0.0824858520,0.0078741776,0.2001591791,0.0187579836,0.0007059103,0.0007652638,0.0461465156,0.0012747868,0.047};"; String eqAd = " if(group.getId() > 14 && \"MigHiver\".equals(departureZone.getName())){if(\"gironde\".equals(arrivalZone.getName()))return gir[y];"+ "else if(\"Rochebonne\".equals(arrivalZone.getName())) return ro[y]; else if(\"LandesCote\".equals(arrivalZone.getName())) return lc[y];"+ "else if(\"LandesLarge\".equals(arrivalZone.getName())) return ll[y]; }return 0;"; for(PopulationSeasonInfo sea : saison){ if (sea.getFirstMonth().equals(Month.JANUARY)){ String eq = tabJuv +mig+ eqJuv ; Equation eqmig = sea.getMigrationEquation() ; eqmig.setContent(eq); } else if (sea.getFirstMonth().equals(Month.APRIL)){ String eq = tabAd + mig + eqAd ; Equation eqmig = sea.getMigrationEquation() ; eqmig.setContent(eq); } } // croissance for (PopulationGroup group : groups) { group.setMinLength(croissance[group.getId()]) ; group.setMaxLength(croissance[group.getId()+1]) ; } // modif la capturabilite du thon MatrixND c = popTho.getCapturability(); for (MatrixIterator i = c.iterator(); i.hasNext();){ i.next() ; Object [] sem = i.getSemanticsCoordinates(); PopulationSeasonInfo season = (PopulationSeasonInfo)sem[1]; if (season.getFirstMonth().after(Month.AUGUST) & season.getFirstMonth().before(Month.DECEMBER)){// anciennement q2 avant cl 12 i.setValue(qThon); } } // modif le ciblage du bar MetierDAO metierDAO = IsisFishDAOHelper.getMetierDAO(tx); List metiers = metierDAO.findAll(); for(Metier metier : metiers){ if ( "BarPel1_GdG".equals(metier.getName())){ List seasons = metier.getMetierSeasonInfo(); for(MetierSeasonInfo season : seasons){ if (Month.APRIL.equals(season.getFirstMonth()) || Month.MAY.equals(season.getFirstMonth())){ Equation target = season.getSpeciesTargetSpecies(popBarG.getSpecies()).getTargetFactorEquation(); int nb = season.getFirstMonth().getMonthNumber(); String eqt = "return " + cibBarG[nb-3] + ";"; target.setContent(eqt); } } }else if("BarPel2_GdG".equals(metier.getName())){ List seasons = metier.getMetierSeasonInfo(); for(MetierSeasonInfo season : seasons){ if (Month.APRIL.equals(season.getFirstMonth()) || Month.MAY.equals(season.getFirstMonth())){ Equation target = season.getSpeciesTargetSpecies(popBarG.getSpecies()).getTargetFactorEquation(); int nb = season.getFirstMonth().getMonthNumber(); String eqt = "return " + cibBarG[nb-3+2] + ";"; target.setContent(eqt); } } }else if("BarPel1_Manche".equals(metier.getName())){ List seasons = metier.getMetierSeasonInfo(); for(MetierSeasonInfo season : seasons){ if (Month.DECEMBER.equals(season.getFirstMonth())){ Equation target = season.getSpeciesTargetSpecies(popBarG.getSpecies()).getTargetFactorEquation(); String eqt = "return " + cibBarM[0] + ";"; target.setContent(eqt); }else if(Month.JANUARY.equals(season.getFirstMonth())){ Equation target = season.getSpeciesTargetSpecies(popBarG.getSpecies()).getTargetFactorEquation(); String eqt = "return " + cibBarM[1] + ";"; target.setContent(eqt); } } }else if("BarPel2_Manche".equals(metier.getName())){ List seasons = metier.getMetierSeasonInfo(); for(MetierSeasonInfo season : seasons){ if (Month.NOVEMBER.equals(season.getFirstMonth()) || Month.DECEMBER.equals(season.getFirstMonth())){ Equation target = season.getSpeciesTargetSpecies(popBarG.getSpecies()).getTargetFactorEquation(); int nb = season.getFirstMonth().getMonthNumber(); String eqt = "return " + cibBarM[nb-8] + ";"; target.setContent(eqt); }else if(Month.JANUARY.equals(season.getFirstMonth())){ Equation target = season.getSpeciesTargetSpecies(popBarG.getSpecies()).getTargetFactorEquation(); String eqt = "return " + cibBarM[4] + ";"; target.setContent(eqt); } } } } // modif recrutement de la sardine Equation reproSardine = popSar.getReproductionEquation() ; String [] eqSar = reproSardine.getContent().split("prod = 2.489614660e9"); String nouvelleEqSar = eqSar[0] + "prod = " + reproSar + eqSar[1] ; reproSardine.setContent(nouvelleEqSar); // modif price for(Population p : popsDAO){ Equation eqp = p.getPrice(); String [] eqpp = eqp.getContent().split("double sign = 0"); String nouvelleEq = "double sign =" + price + eqpp[1] ; eqp.setContent(nouvelleEq); //System.out.println("equation de prix :"+nouvelleEq); } // modif le nb de jours d inactivite StrategyDAO strategyDAO = IsisFishDAOHelper.getStrategyDAO(tx) ; List strategies = strategyDAO.findAll(); for (Strategy strategy : strategies) { List monthInfos = strategy.getStrategyMonthInfo(); double [] in = inactivity.get(strategy); for (StrategyMonthInfo month : monthInfos) { double days = in[month.getMonth().getMonthNumber()]; 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; } }