/* * Copyright (C) 2014 slehuta * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * 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 * 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 * . */ package simulationplans; import fr.ifremer.isisfish.IsisFishDAOHelper; import fr.ifremer.isisfish.datastore.RuleStorage; import fr.ifremer.isisfish.datastore.SimulationStorage; import fr.ifremer.isisfish.entities.*; import fr.ifremer.isisfish.entities.PopulationDAO; import fr.ifremer.isisfish.entities.PopulationGroup; import fr.ifremer.isisfish.entities.PopulationSeasonInfo; import fr.ifremer.isisfish.rule.Rule; import fr.ifremer.isisfish.rule.RuleHelper; import fr.ifremer.isisfish.simulator.SimulationPlanContext; import fr.ifremer.isisfish.simulator.SimulationPlanIndependent; import fr.ifremer.isisfish.types.TimeStep; import fr.ifremer.isisfish.types.Month; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.nuiton.math.matrix.MatrixFactory; import org.nuiton.math.matrix.*; //MatrixND; import org.nuiton.topia.TopiaContext; import rules.HCR_transition_MSY_sardine; import rules.HCR_MSYperfect_sardine; import rules.HCR_MSYuncertain_sardine; import rules.TACseries_Metier_OptionLO_redistr; import rules.TACprop_Metier_OptionLO_redistr; import rules.Effort_forcing_DEFIPEL_scenarios; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Properties; import java.util.*; //import scripts.ResultName; /** * PlanDEFIPEL.java * * Created: 04/04/24 * * @author slehuta * @version $Revision: 1545 $ * Last update: $Date: 2024 $ * by : $Author: slehuta $ */ public class PlanDEFIPELscenarios implements SimulationPlanIndependent { /** to use log facility, just put in your code: log.info("..."); */ private static Log log = LogFactory.getLog(PlanDEFIPELscenarios.class); static private String MATRIX = "PlanExp_DEFIPEL"; private MatrixND matrix = null; // mesures de gestion : nom des colonnes static private final String GESTIONPIL = "gesPIL"; static private final String STRATEGY = "strategie"; static private final String MORATOIRE = "moratoire"; // Public params public int param_first = 0; public int param_simulationNumber = 12; static private final int matrix_size = 57; static private final int parameterNumber = 26; public String param_directory = "InputsDEFIPEL/Scenarios/"; public int param_startYear = 10; //2020 public String param_cheminFichier = "InputsDefipel" ; public String param_InactivityFileName = "/Total.Inactivite.effort.all.csv"; //public String param_nbVessFileName = "/Total.NbBateau.effort.csv"; //public String param_EffortPropFileName = "/Total.Proportion.strategy.csv"; public String param_nbVessFileNameSC = "/Scenario.NbBateau.csv"; public String param_EffortPropFileNameSC = "/Scenario.Proportion.strategy.txt"; protected List allStrategies ; protected File Inactivity; //protected File Effort; //protected File nbVess; protected File EffortSC; protected File nbVessSC; protected MatrixND matInactivity; //protected MatrixND matEffort; //protected MatrixND matNbVess; protected MatrixND matEffortSC; protected MatrixND matNbVessSC; protected String[] necessaryResult = { }; @Override 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 */ @Override public String getDescription() throws Exception { return "Changes DB and add rules for simulate DEFIPEL scenarios"; } /** * Called once before {@code beforeSimulation} call. * * @param context plan context */ @Override public void init(SimulationPlanContext context) throws Exception { context.getParam().addExtraRules("Effort_forcing_DEFIPEL_scenarios", "HCR_transition_MSY_sardine", "TACseries_Metier_OptionLO_redistr","Cantonnement,TACprop_Metier_OptionLO_redistr"); matrix = MatrixFactory.getInstance().create(new int[]{matrix_size, parameterNumber}); matrix.importCSV(new FileReader(new File(param_directory, MATRIX + ".txt")), new int[]{0,0}); List dim0 = new ArrayList(); for (int i=0; i < matrix_size; i++) { dim0.add(i); } matrix.setSemantic(0, dim0); matrix.setSemantic(1, Arrays.asList( new String[]{"simu","scPrincipal","croissPIL","priPIL","croissANE","qPIL","qANE","nbNav", "ajustementNbNav","morPIL","moratoire","tacEsp","gesPIL","strategie","recPIL","priHOM", "priHMM","priMAQ","abMAQ","tacMAQ","abHOM","tacHOMHMM","tacHOM","abALB","abBFT","selPIL"})); System.out.println("plan d'exp :"+matrix); // Creation des objects files à partir des chemins String InactivityPath = param_cheminFichier + param_InactivityFileName; String nbVessPath = param_directory + param_nbVessFileNameSC; String EffortPropPath = param_directory + param_EffortPropFileNameSC; Inactivity = new File(InactivityPath); EffortSC = new File(EffortPropPath); nbVessSC = new File(nbVessPath); // Import des matrices matInactivity = MatrixFactory.getInstance().create(Inactivity); //System.out.println("matrice inactivite " + matInactivity); matEffortSC = MatrixFactory.getInstance().create(EffortSC); //System.out.println("matrice effort " + matEffort); matNbVessSC = MatrixFactory.getInstance().create(nbVessSC); //System.out.println("matrice nb navires " + matNbVess); } /** * @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; }*/ /** * 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 */ @Override public boolean beforeSimulation(SimulationPlanContext context, SimulationStorage nextSimulation) throws Exception { int simNum = nextSimulation.getParameter().getSimulationPlanNumber() + param_first; if (simNum < param_simulationNumber + param_first) { TopiaContext db = nextSimulation.getStorage().beginTransaction(); PopulationDAO popDAO = IsisFishDAOHelper.getPopulationDAO(db) ; Population pil = popDAO.findByName("PIL_pop_GdG"); Population ane = popDAO.findByName("Pop_ANE_GdG"); ////////////////////////////////////////////////////////////////////////////////////////// // modif context nextSimulation.getParameter().getTagValue().put("startingYear", Integer.toString(param_startYear)); nextSimulation.getParameter().getTagValue().put("Scenario_princ", Integer.toString((int)matrix.getValue(simNum, "scPrincipal"))); // (for weight) nextSimulation.getParameter().getTagValue().put("ScenarioDeb", Integer.toString((int)matrix.getValue(simNum, "croissPIL"))); nextSimulation.getParameter().getTagValue().put("Scenario_priceSardine", Integer.toString((int)matrix.getValue(simNum, "priPIL"))); nextSimulation.getParameter().getTagValue().put("Scenario_growthANE", Integer.toString((int)matrix.getValue(simNum, "croissANE"))); nextSimulation.getParameter().getTagValue().put("Scenario_nbNav", Integer.toString((int)matrix.getValue(simNum, "nbNav"))); nextSimulation.getParameter().getTagValue().put("Scenario_mnatSardine", Integer.toString((int)matrix.getValue(simNum, "morPIL"))); nextSimulation.getParameter().getTagValue().put("Scenario_recPIL", Integer.toString((int)matrix.getValue(simNum, "recPIL"))); nextSimulation.getParameter().getTagValue().put("Scenario_priceChinch", Integer.toString((int)matrix.getValue(simNum, "priHOM"))); nextSimulation.getParameter().getTagValue().put("Scenario_priceChinchMed", Integer.toString((int)matrix.getValue(simNum, "priHMM"))); nextSimulation.getParameter().getTagValue().put("Scenario_priceMaq", Integer.toString((int)matrix.getValue(simNum, "priMAQ"))); nextSimulation.getParameter().getTagValue().put("Scenario_abMAQ", Integer.toString((int)matrix.getValue(simNum, "abMAQ"))); nextSimulation.getParameter().getTagValue().put("Scenario_tacMAQ", Integer.toString((int)matrix.getValue(simNum, "tacMAQ"))); nextSimulation.getParameter().getTagValue().put("Scenario_abHOM", Integer.toString((int)matrix.getValue(simNum, "abHOM"))); nextSimulation.getParameter().getTagValue().put("Scenario_tacHOM", Integer.toString((int)matrix.getValue(simNum, "tacHOM"))); nextSimulation.getParameter().getTagValue().put("Scenario_tacHOMHMM", Integer.toString((int)matrix.getValue(simNum, "tacHOMHMM"))); nextSimulation.getParameter().getTagValue().put("Scenario_abALB", Integer.toString((int)matrix.getValue(simNum, "abALB"))); nextSimulation.getParameter().getTagValue().put("Scenario_abBFT", Integer.toString((int)matrix.getValue(simNum, "abBFT"))); nextSimulation.getParameter().getTagValue().put("Scenario_selPIL", Integer.toString((int)matrix.getValue(simNum, "selPIL"))); // Modif rule-------------------------------------------------------------------------------------------- List paramRules = nextSimulation.getParameter().getRules(); double gestionPIL = matrix.getValue(simNum, "gesPIL"); //double TACfr = matrix.getValue(simNum,"tacFr"); double TACespMod = matrix.getValue(simNum, "tacEsp"); int strMod = (int)matrix.getValue(simNum, "strategie"); double nbNavMod = matrix.getValue(simNum, "nbNav"); //double reposBio = matrix.getValue(simNum,"repBio"); double moratoire = matrix.getValue(simNum, "moratoire"); double Scenario_qSardine = matrix.getValue(simNum, "qPIL"); double Scenario_qAnchois = matrix.getValue(simNum, "qANE"); // effort2020 // nb nav per str per year 2021+ et inactivity (constant 2020) // SetContextDEFIPEL double TACesp =0.2; // historic 2016-2020 modalite 0 if(TACespMod == 1) { TACesp = 0.1; //faible 2019 13% }else if(TACespMod == 2) { TACesp = 0; //pas de TAC } // tac faible // impl gestion if (gestionPIL == 1 || gestionPIL == 3) { // HCR normal HCR_transition_MSY_sardine HCRpil = new HCR_transition_MSY_sardine(); HCRpil.param_pop = pil; paramRules.add(HCRpil); }else if(gestionPIL == 5 || gestionPIL == 6) { // HCR modele incertain HCR_MSYuncertain_sardine HCRpil = new HCR_MSYuncertain_sardine(); HCRpil.param_pop = pil; paramRules.add(HCRpil); }else if(gestionPIL == 2) { // HCR modele parfait HCR_MSYperfect_sardine HCRpil = new HCR_MSYperfect_sardine(); HCRpil.param_pop = pil; paramRules.add(HCRpil); } double TACmult = 1; if(gestionPIL == 3 || gestionPIL == 6){ //faible quota : reduit l alloc des 2 pays) TACmult = 0.8;} TACseries_Metier_OptionLO_redistr tacFR = new TACseries_Metier_OptionLO_redistr(); tacFR.param_pop = pil; tacFR.param_metNames= ("(OTMPIL|PS_PIL|PTMPIL).*"); tacFR.param_alloc =(1-TACesp)*TACmult; TACseries_Metier_OptionLO_redistr tacESP = new TACseries_Metier_OptionLO_redistr(); tacESP.param_pop = pil; tacESP.param_metNames = ("PS_ANE_PIL.*"); tacESP.param_alloc=TACesp*TACmult; paramRules.add(tacFR); paramRules.add(tacESP); /*else if (gestionPIL == 4) { //fermetures saisonnieres } else if (gestionPIL == 0 & moratoire == 1) { //interdiction metier ? }*/ // tac other species // MAC /*TACprop_Metier_OptionLO_redistr tacMacPdB = new TACprop_Metier_OptionLO_redistr(); param_pop = "(mac).*"; //"mac" ; "Ger" ; "Thon" param_metNames = "(PS_MAX @ 1_Bretons @|PS_PIL|PTMPIL).*"; // "PS_ANE_PIL" param_alloc = 0.6; param_tacProp = 0.00002; TACprop_Metier_OptionLO_redistr tacMacPdA = new TACprop_Metier_OptionLO_redistr(); param_pop = "(mac).*"; //"mac" ; "Ger" ; "Thon" param_metNames = "(PS_MAX @ 1_Bretons.A|PS_MAX @ 2_B|).*"; // "PS_ANE_PIL" param_alloc = 0.6; param_tacProp = 0.00002; TACprop_Metier_OptionLO_redistr tacMacOPV = new TACprop_Metier_OptionLO_redistr(); param_pop = "(mac).*"; //"mac" ; "Ger" ; "Thon" param_metNames = "(OTMPIL|PS_PIL|PTMPIL).*"; // "PS_ANE_PIL" param_alloc = 0.6; param_tacProp = 0.00002; //---- // BFT TACprop_Metier_OptionLO_redistr tacBftPdB = new TACprop_Metier_OptionLO_redistr(); param_pop = "(hom|Chinch).*"; //"mac" ; "Ger" ; "Thon" param_metNames = "(OTMPIL|PS_PIL|PTMPIL).*"; // "PS_ANE_PIL" param_alloc = 0.6; param_tacProp = 0.00002; TACprop_Metier_OptionLO_redistr tacBftPdA = new TACprop_Metier_OptionLO_redistr(); param_pop = "(hom|Chinch).*"; //"mac" ; "Ger" ; "Thon" param_metNames = "(OTMPIL|PS_PIL|PTMPIL).*"; // "PS_ANE_PIL" param_alloc = 0.6; param_tacProp = 0.00002; TACprop_Metier_OptionLO_redistr tacBftOPV = new TACprop_Metier_OptionLO_redistr(); param_pop = "(hom|Chinch).*"; //"mac" ; "Ger" ; "Thon" param_metNames = "(OTMPIL|PS_PIL|PTMPIL).*"; // "PS_ANE_PIL" param_alloc = 0.6; param_tacProp = 0.00002; //----- // ALB TACprop_Metier_OptionLO_redistr tacAlbPdB = new TACprop_Metier_OptionLO_redistr(); param_pop = "(hom|Chinch).*"; //"mac" ; "Ger" ; "Thon" param_metNames = "(OTMPIL|PS_PIL|PTMPIL).*"; // "PS_ANE_PIL" param_alloc = 0.6; param_tacProp = 0.00002; TACprop_Metier_OptionLO_redistr tacAlbPdA = new TACprop_Metier_OptionLO_redistr(); param_pop = "(hom|Chinch).*"; //"mac" ; "Ger" ; "Thon" param_metNames = "(OTMPIL|PS_PIL|PTMPIL).*"; // "PS_ANE_PIL" param_alloc = 0.6; param_tacProp = 0.00002; TACprop_Metier_OptionLO_redistr tacAlbOPV = new TACprop_Metier_OptionLO_redistr(); param_pop = "(hom|Chinch).*"; //"mac" ; "Ger" ; "Thon" param_metNames = "(OTMPIL|PS_PIL|PTMPIL).*"; // "PS_ANE_PIL" param_alloc = 0.6; param_tacProp = 0.00002; //----- // JAX TACprop_Metier_OptionLO_redistr tacJaxPdB = new TACprop_Metier_OptionLO_redistr(); param_pop = "(hom|Chinch).*"; //"mac" ; "Ger" ; "Thon" param_metNames = "(OTMPIL|PS_PIL|PTMPIL).*"; // "PS_ANE_PIL" param_alloc = 0.6; param_tacProp = 0.00002; TACprop_Metier_OptionLO_redistr tacJaxPdA = new TACprop_Metier_OptionLO_redistr(); param_pop = "(hom|Chinch).*"; //"mac" ; "Ger" ; "Thon" param_metNames = "(OTMPIL|PS_PIL|PTMPIL).*"; // "PS_ANE_PIL" param_alloc = 0.6; param_tacProp = 0.00002; TACprop_Metier_OptionLO_redistr tacJaxOPV = new TACprop_Metier_OptionLO_redistr(); param_pop = "(hom|Chinch).*"; //"mac" ; "Ger" ; "Thon" param_metNames = "(OTMPIL|PS_PIL|PTMPIL).*"; // "PS_ANE_PIL" param_alloc = 0.6; param_tacProp = 0.00002;*/ //------------------------------------- // change effort trough rule Effort_forcing_DEFIPEL_scenarios effortSc = new Effort_forcing_DEFIPEL_scenarios(); effortSc.param_NbNavMod = (int)nbNavMod; paramRules.add(effortSc); //------------------------------------------------------------------------------------------------------------------ // Change DB // strategies // q pil and ane // tar hmm/hom // redistr quota via q? // nb nav, inactivite and strategies (+ add effort defipel pour 2020) //-------------------------------------------------------------------- StrategyDAO strategyDAO = IsisFishDAOHelper.getStrategyDAO(db); List allStrategies = strategyDAO.findAll(); List months = Month.getMonths(Month.JANUARY, Month.DECEMBER); // set inactivity to 2020 level for the all simu List ScStrategies = matEffortSC.copy().getSubMatrix(0,strMod).getSemantic(1); List ScMetiers = matEffortSC.copy().getSubMatrix(0,strMod).getSemantic(2); for (Strategy str : allStrategies) { if (!str.getName().equals("Espagnols @ all") && !str.getName().equals("extra @ all")) { if (ScStrategies.contains(str)) { // Change Effort proportion //-------------------------- double newProp = 0; for (Month mon : months) { StrategyMonthInfo smi = str.getStrategyMonthInfo(mon); // on boucle sur les metiers de la strategie pour initialiser les efforts avec matEffort Collection strMet = str.getSetOfVessels().getPossibleMetiers(); for (EffortDescription ed : strMet) { Metier strMetier = ed.getPossibleMetiers(); if(ScMetiers.contains(strMetier)){ newProp = matEffortSC.getValue(strMod, str, strMetier, mon.getMonthNumber()); }else {newProp = 0;} smi.setProportionMetier(strMetier, newProp); System.out.println("mod : "+ strMod + "strategy :"+str.getName()+ " prop :"+newProp); } } } else { str.setProportionSetOfVessels(0); } } } // change q //--------------------------------------------------------- MatrixND qPil = pil.getCapturability(); MatrixND qAne = ane.getCapturability(); for (MatrixIterator i = qAne.iterator(); i.hasNext();) { i.next(); double q = i.getValue() * 0.9; i.setValue(q); } if (Scenario_qSardine == 1) { for (MatrixIterator i = qPil.iterator(); i.hasNext(); ) { i.next(); Object[] sem = i.getSemanticsCoordinates(); PopulationGroup group = (PopulationGroup) sem[0]; PopulationSeasonInfo sea = (PopulationSeasonInfo) sem[1]; if (sea.getFirstMonth().getMonthNumber() >2 && sea.getFirstMonth().getMonthNumber() < 9){ double q = i.getValue() * 0.9; i.setValue(q); } } }else if (Scenario_qSardine == 2){ for (MatrixIterator i = qPil.iterator(); i.hasNext(); ) { i.next(); double q = i.getValue() * 0.9; i.setValue(q); } } // change qAne, Tar HMM, HOM, MAQ pour refleter la redistribution des quotas... return true; }else return false; } /** * Call after each simulation. * * @param context plan context * @param lastSimulation storage used for simulation * @return true if we must do next simulation, false to stop plan * @throws Exception */ @Override public boolean afterSimulation(SimulationPlanContext context, SimulationStorage lastSimulation) throws Exception { return true; } }