/* * #%L * IsisFish data * %% * Copyright (C) 2006 - 2014 Ifremer, CodeLutin * %% * 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 * . * #L% */ package rules; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import scripts.SiMatrix; import fr.ifremer.isisfish.entities.Metier; import fr.ifremer.isisfish.entities.Strategy; import fr.ifremer.isisfish.rule.AbstractRule; import fr.ifremer.isisfish.simulator.SimulationContext; import fr.ifremer.isisfish.types.TimeStep; import fr.ifremer.isisfish.annotations.Doc; import org.nuiton.math.matrix.*; import java.util.regex.Pattern; import static scripts.PTAtoolbox.expandEnvirVar; import java.io.File; import java.io.FileReader; /** * EffortReductionFromFile.java * * Created: 07 march 2024 * modified from EffortReduction.java by Antoine RICOUARD * */ public class EffortReductionFromFile extends AbstractRule { /** to use log facility, just put in your code: log.info("..."); */ static private Log log = LogFactory.getLog(EffortReductionFromFile.class); public String param_path = "${PBS_O_WORKDIR}/RUN"; @Doc("Begin step") public TimeStep param_beginStep = new TimeStep(0); @Doc("End step") public TimeStep param_endStep = new TimeStep(719); private MatrixND matrix = null; private double param_PercentReduction; protected boolean first = true; protected String[] necessaryResult = { // put here all necessary result for this rule // example: // MatrixBiomass.NAME, // MatrixNetValueOfLandingsPerStrategyMet.NAME }; /** * @return the necessaryResult */ @Override public String[] getNecessaryResult() { return this.necessaryResult; } /** * Permet d'afficher a l'utilisateur une aide sur la regle. * @return L'aide ou la description de la regle */ @Override public String getDescription() { return "Reduce monthly effort of each strategy of the percent indicated"; } /** * Appelle au demarrage de la simulation, cette methode permet d'initialiser * des valeurs. * @param context La simulation pour lequel on utilise cette regle */ @Override public void init(SimulationContext context) throws Exception { String simu = context.getSimulationControl().getId(); // String simu = "simu_i0"; System.out.println("nom de la simu en cours : " + simu); Pattern simulNamePattern = Pattern.compile("^(.+)_(i\\d+)$"); String path = simulNamePattern.matcher(simu).replaceAll("_$1/$2/params.csv"); // exemple : "simu_i0" devient "RUN_simu/i0/params.csv" param_path = expandEnvirVar.replace(param_path); matrix = MatrixFactory.getInstance().create(new int[]{1,2});// create a matrix with 1 line and 2 columns matrix.importCSV(new FileReader(new File(param_path + path)), new int[]{0,0}); param_PercentReduction = matrix.getValue(0,0);// (line,column): column 0 of a matrix with 1 line and 2 columns (column 1 is for simulation pattern : used in another rule) } /** * La condition qui doit etre vrai pour faire les actions. * * @param context la simulation pour lequel on utilise cette regle * @param step le pas de temps courant * @param metier le metier concern�� * @return vrai si on souhaite que les actions soit faites */ @Override public boolean condition(SimulationContext context, TimeStep step, Metier metier) throws Exception { boolean result = true; if (step.before(param_beginStep)) { result = false; } else if (step.after(param_endStep)) { result = false; } if (result) { log.info("condition vraie"); } return result; } /** * Si la condition est vrai alors cette action est executee avant le pas * de temps de la simulation. * * @param context la simulation pour lequel on utilise cette regle * @param step le pas de temps courant * @param metier le metier concern�� */ @Override public void preAction(SimulationContext context, TimeStep step, Metier metier) throws Exception { // shinte la boucle metier if (first) { first = false; SiMatrix siMatrix = SiMatrix.getSiMatrix(context); List strs = siMatrix.getStrategies(step); for (Strategy str : strs) { log.info("strategy evaluee : " + str.getName()); /* Dans un premiere temps tant que l inactivit�� est un entier on utilise * la proportion du nombre de bateaux de la strategie pour reduire l effort * ce qu on ferait aussi pour une mesure de reduction du nombre de bateaux mais * comme actuellement on ne tient pas compte de l economie... ca revient au meme * En fait passer par l inactivit�� n est pas la meilleure facon de modifier * l effort le mieux serait d agir sur un autre coeff qui est multipli�� a * l effort (Fstd ou ciblage) car comme ca le code serait generique mais on * ne verrait pas que l effort nominal est modifi��... */ double propOld = str.getProportionSetOfVessels(); double newProp = propOld * (1 - param_PercentReduction); str.setProportionSetOfVessels(newProp); } } } /** * Si la condition est vrai alors cette action est execut��e apres le pas * de temps de la simulation. * * @param context La simulation pour lequel on utilise cette regle * @param step le pas de temps courant * @param metier le metier concern�� */ @Override public void postAction(SimulationContext context, TimeStep step, Metier metier) throws Exception { first = true; } }