/* * 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 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.csv"; 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; 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"); // Creation des objects files à partir des chemins String InactivityPath = param_cheminFichier + param_InactivityFileName; String nbVessPath = param_cheminFichier + param_nbVessFileNameSC; String EffortPropPath = param_cheminFichier + 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(""); Population ane = popDAO.findByName(""); ////////////////////////////////////////////////////////////////////////////////////////// // modif context nextSimulation.getParameter().getTagValue().put("startingYear", Integer.toString(param_startYear)); nextSimulation.getParameter().getTagValue().put("Scenario_princ", Double.toString(matrix.getValue(simNum, "scPrincipal"))); // (for weight) nextSimulation.getParameter().getTagValue().put("ScenarioDeb", Double.toString(matrix.getValue(simNum, "croissPIL"))); nextSimulation.getParameter().getTagValue().put("Scenario_priceSardine", Double.toString(matrix.getValue(simNum, "priPIL"))); nextSimulation.getParameter().getTagValue().put("Scenario_growthANE", Double.toString(matrix.getValue(simNum, "croissANE"))); nextSimulation.getParameter().getTagValue().put("Scenario_nbNav", Double.toString(matrix.getValue(simNum, "nbNav"))); nextSimulation.getParameter().getTagValue().put("Scenario_mnatSardine", Double.toString(matrix.getValue(simNum, "morPIL"))); nextSimulation.getParameter().getTagValue().put("Scenario_recPIL", Double.toString(matrix.getValue(simNum, "recPIL"))); nextSimulation.getParameter().getTagValue().put("Scenario_priceChinch", Double.toString(matrix.getValue(simNum, "priHOM"))); nextSimulation.getParameter().getTagValue().put("Scenario_priceChinchMed", Double.toString(matrix.getValue(simNum, "priHMM"))); nextSimulation.getParameter().getTagValue().put("Scenario_priceMaq", Double.toString(matrix.getValue(simNum, "priMAQ"))); nextSimulation.getParameter().getTagValue().put("Scenario_abMAQ", Double.toString(matrix.getValue(simNum, "abMAQ"))); nextSimulation.getParameter().getTagValue().put("Scenario_tacMAQ", Double.toString(matrix.getValue(simNum, "tacMAQ"))); nextSimulation.getParameter().getTagValue().put("Scenario_abHOM", Double.toString(matrix.getValue(simNum, "abHOM"))); nextSimulation.getParameter().getTagValue().put("Scenario_tacHOM", Double.toString(matrix.getValue(simNum, "tacHOM"))); nextSimulation.getParameter().getTagValue().put("Scenario_tacHOMHMM", Double.toString(matrix.getValue(simNum, "tacHOMHMM"))); nextSimulation.getParameter().getTagValue().put("Scenario_abALB", Double.toString(matrix.getValue(simNum, "abALB"))); nextSimulation.getParameter().getTagValue().put("Scenario_abBFT", Double.toString(matrix.getValue(simNum, "abBFT"))); nextSimulation.getParameter().getTagValue().put("Scenario_selPIL", Double.toString(matrix.getValue(simNum, "selPIL"))); // Modif rule-------------------------------------------------------------------------------------------- List paramRules = nextSimulation.getParameter().getRules(); double gestionPIL = matrix.getValue(simNum, "gesPIL"); //double TACfr = matrix.getValue(simNum,"tacFr"); double TACesp = matrix.getValue(simNum, "tacEsp"); double strMod = 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 // impl gestion if (gestionPIL == 1 || gestionPIL == 3) { // HCR Rule HCRpil = new HCR_transition_MSY_sardine(); HCRpil.setPop(pil); Rule tacFR = new TACseries_Metier_OptionLO_redistr(); tacFR.setpop(pil); tacFR.setMetNames("(OTMPIL|PS_PIL|PTMPIL).*"); tacFR.setAlloc(1-TACesp); Rule tacESP = new TACseries_Metier_OptionLO_redistr(); tacESP.setpop(pil); tacESP.setMetNames("PS_ANE_PIL.*"); tacESP.setAlloc(TACesp); paramRules.add(HCRpil); paramRules.add(tacFR); paramRules.add(tacESP); } else if (gestionPIL == 4) { //fermetures saisonnieres } else if (gestionPIL == 0 & moratoire == 1) { //interdiction metier ? } // change effort trough rule Rule effortSc = new Effort_forcing_DEFIPEL_scenarios(); effortSc.setNbNavMod(param_nbNavMod); paramRules.add(effortSc); //------------------------------------------------------------------------------------------------------------------ // Change DB // 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.getSubMatrix(0,strMod).getSemantic(1); 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(); newProp = matEffortSC.getValue(strMod, str, strMetier, mon.getMonthNumber()); smi.setProportionMetier(strMetier, 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 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; } }