/* * Copyright (C) 2010 * * 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 2 * 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, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ package rules; import static org.nuiton.i18n.I18n._; import org.nuiton.j2r.REngine; import org.nuiton.j2r.RException; import org.nuiton.j2r.RProxy; import org.nuiton.j2r.types.RDataFrame; import scripts.ResultName; import scripts.SiMatrix; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import fr.ifremer.isisfish.types.TimeStep; import fr.ifremer.isisfish.types.Month; import scripts.ResultName; import scripts.Fmin; import scripts.Fmsy; import scripts.Fsq; import scripts.Fmgt; import scripts.FsqPartial; import java.io.Writer; import fr.ifremer.isisfish.export.Export; import java.io.File; import java.util.Arrays; import java.util.ArrayList; import java.util.List; import org.nuiton.util.FileUtil; import org.nuiton.math.matrix.*; import fr.ifremer.isisfish.entities.Population; import fr.ifremer.isisfish.util.Doc; import fr.ifremer.isisfish.simulator.SimulationContext; import fr.ifremer.isisfish.types.TimeStep; import fr.ifremer.isisfish.entities.*; import fr.ifremer.isisfish.rule.AbstractRule; import fr.ifremer.isisfish.datastore.SimulationStorage; import fr.ifremer.isisfish.datastore.ResultStorage; import org.nuiton.math.matrix.MatrixIterator; import org.nuiton.topia.TopiaContext; import fr.ifremer.isisfish.simulator.SimulationControl; import fr.ifremer.isisfish.simulator.SimulationProperties; /** * XSA.java * * Created: 11 mars 2010 * * @author ben * @version $Revision: 1545 $ * Last update: $Date: 2 * by : $Author: Paul et Youen $ */ public class XSA extends AbstractRule { /** to use log facility, just put in your code: log.info("..."); */ private static Log log = LogFactory.getLog(XSA.class); protected TopiaContext db = null; private MatrixND catchYear0; private MatrixND effortS1Met0; private MatrixND catchS1Met0; private MatrixND catchAll; public MatrixND matrixCpue; public static MatrixND matrixFcurFra; public static MatrixND matrixFcurOth; public static MatrixND matrixFcur; public static MatrixND matrixCcurFra; public static MatrixND matrixCcurOth; public static MatrixND matrixNcur; public MatrixND matrixweightAA; public MatrixND mortNatAct; public MatrixND matrixNoiseAssPokAA; public MatrixND matrixCvAssPokAA; public static MatrixND matrixNageExport; public static MatrixND matrixFageExport; public static double ssbExport, fbarExport; public double raisingFactor; public int optimNumber; public int optimType; // Variables to be changed through the simulations plan; public double param_noiseAssPok = 0.0; Fsq optimFsq; Fmsy optimFmsy; Fmgt optimFmgt; FsqPartial optimFsqPartial; RDataFrame FauxAges; RDataFrame NauxAges; // Configuration @Doc("Mois de l'evaluation") public Month param_month = Month.JANUARY; // Matrices necessaires public String [] necessaryResult = { ResultName.MATRIX_ABUNDANCE, ResultName.MATRIX_BIOMASS, ResultName.MATRIX_CATCH_PER_STRATEGY_MET_PER_ZONE_POP, ResultName.MATRIX_DISCARDS_PER_STR_MET_PER_ZONE_POP, ResultName.MATRIX_DISCARDS_WEIGHT_PER_STR_MET_PER_ZONE_POP }; public String[] getNecessaryResult() { return this.necessaryResult; } public String getDescription() throws Exception { return _("Fait une XSA"); } public void init(SimulationContext context) throws Exception { TimeStep date = new TimeStep(0); List strs = SiMatrix.getSiMatrix(context).getStrategies(date); List metiers = SiMatrix.getSiMatrix(context).getMetiers(date); List months = Arrays.asList(Month.MONTH); List populations = SiMatrix.getSiMatrix(context).getPopulations(date); List agegroupsall = new ArrayList(); this.db = context.getDB(); for (Population pop : populations) { for (PopulationGroup age : pop.getPopulationGroup()) { PopulationGroup tmp = (PopulationGroup) db.findByTopiaId(age.getTopiaId()); agegroupsall.add(tmp); } } catchYear0 = MatrixFactory.getInstance().create( "catchYear0", new List[]{agegroupsall}, new String[]{"AgeGroupsAll"}); matrixCcurFra = MatrixFactory.getInstance().create( "matrixCcurFra", new List[]{agegroupsall}, new String[]{"AgeGroupsAll"}); matrixCcurOth = MatrixFactory.getInstance().create( "matrixCcurOth", new List[]{agegroupsall}, new String[]{"AgeGroupsAll"}); catchAll = MatrixFactory.getInstance().create( "catchAll", new List[]{agegroupsall}, new String[]{"AgeGroupsAll"}); catchS1Met0 = MatrixFactory.getInstance().create( "catchS1MetYear0", new List[]{agegroupsall,metiers}, new String[]{"AgeGroupsAll","Metiers"}); effortS1Met0 = MatrixFactory.getInstance().create( "effortS1MetYear0", new List[]{metiers}, new String[]{"Metiers"}); matrixCpue = MatrixFactory.getInstance().create( "matrixCpue", new List[]{agegroupsall}, new String[]{"AgeGroupsAll"}); matrixNcur = MatrixFactory.getInstance().create( "matrixNcur", new List[]{agegroupsall}, new String[]{"AgeGroupsAll"}); matrixFcur = MatrixFactory.getInstance().create( "matrixFcur", new List[]{agegroupsall}, new String[]{"AgeGroupsAll"}); matrixFcurFra = MatrixFactory.getInstance().create( "matrixFcurFra", new List[]{agegroupsall}, new String[]{"AgeGroupsAll"}); matrixFcurOth = MatrixFactory.getInstance().create( "matrixFcurOth", new List[]{agegroupsall}, new String[]{"AgeGroupsAll"}); matrixweightAA = MatrixFactory.getInstance().create( "matrixweightAA", new List[]{agegroupsall}, new String[]{"AgeGroupsAll"}); matrixNageExport = MatrixFactory.getInstance().create( "matrixNageExport", new List[]{agegroupsall}, new String[]{"AgeGroupsAll"}); matrixFageExport = MatrixFactory.getInstance().create( "matrixFageExport", new List[]{agegroupsall}, new String[]{"AgeGroupsAll"}); mortNatAct = MatrixFactory.getInstance().create( "mortNatAct", new List[]{agegroupsall}, new String[]{"AgeGroupsAll"}); matrixNoiseAssPokAA = MatrixFactory.getInstance().create( "matrixNoiseAssPokAA", new List[]{agegroupsall}, new String[]{"AgeGroupsAll"}); matrixCvAssPokAA = MatrixFactory.getInstance().create( "matrixCvAssPokAA", new List[]{agegroupsall}, new String[]{"AgeGroupsAll"}); optimFsq = null; optimFmsy = null; optimFmgt = null; optimFsqPartial = null; optimNumber = 0; optimType = 0; for (Population pop : populations){ if (pop.getName().compareTo("Pok3a46") == 0){ for (int iage=3; iage <= pop.sizePopulationGroup()-1; iage++){ PopulationGroup age = pop.getPopulationGroup().get(iage); matrixCvAssPokAA.setValue(age,0.2); matrixNoiseAssPokAA.setValue(age,param_noiseAssPok); } // end of iage loop; } // end of pop=pok if-test; } // end of pop loop; REngine engine = new RProxy(); engine.voidEval("library(FLXSA)"); engine.voidEval("stock<-readFLStock(\"C:/Paul/2005-2011/DPCP/P6/DEEPFISHMAN/Science/WP5/VPA/bli567ind.txt\")"); engine.voidEval("indices<-readFLIndices(\"C:/Paul/2005-2011/DPCP/P6/DEEPFISHMAN/Science/WP5/VPA/bli567fl.txt\")"); // Parametres XSA engine.voidEval("tol<-1.e-09"); engine.voidEval("maxit<-1000"); engine.voidEval("min.nse<-0.3"); engine.voidEval("fse<-1.0"); engine.voidEval("rage<-0"); engine.voidEval("qage<-14"); engine.voidEval("shk.n<-TRUE"); engine.voidEval("shk.f<-TRUE"); engine.voidEval("shk.yrs<-5"); engine.voidEval("shk.ages<-5"); engine.voidEval("window<-100"); engine.voidEval("tsrange<-20"); engine.voidEval("tspower<-0"); // stocke dans FLXSA les parametres engine.voidEval("xsa.control<-FLXSA.control(tol=tol,maxit=maxit,min.nse=min.nse,fse=fse,rage=rage,qage=qage,shk.n=shk.n,shk.f=shk.f,shk.yrs=shk.yrs,shk.ages=shk.ages,window=window,tsrange=tsrange,tspower=tspower,vpa=FALSE)"); engine.voidEval("stock@catch.n <- stock@landings.n"); // recupere les capt aux ages de l annee precedente engine.voidEval("save.image(file=\"C:/Paul/2005-2011/DPCP/P6/DEEPFISHMAN/Science/WP5/VPA/xsaRes.RData\")"); // sauver l'objet pour pouvoir le rouvrir ensuite pour utilisation //engine.voidEval("save.image(file=\"F:/D/Projets/deepfishman/Blue ling/xsaRes.RData\")"); // sauver l'objet pour pouvoir le rouvrir ensuite pour utilisation engine.voidEval("stock<-setPlusGroup(stock,17)");//yv 200712 } /** * La condition qui doit etre vrai pour faire les actions * @param simulation La simulation pour lequel on utilise cette regle * @return vrai si on souhaite que les actions soit faites */ public boolean condition(SimulationContext context, TimeStep date, Metier metier) throws Exception { Month currentMonth = date.getMonth(); if (currentMonth.equals(param_month) && (date.getYear() > 0)) { return true; } else return false; } // Booleen permettant que ne boucler que sur un seul metier dans la preaction : boolean first = true; /** * @param simulation La simulation pour lequel on utilise cette regle */ public void preAction(SimulationContext context, TimeStep date, Metier metier) throws Exception { if (first){ System.out.println("Je suis ici"); List strs = SiMatrix.getSiMatrix(context).getStrategies(date); List metiers = SiMatrix.getSiMatrix(context).getMetiers(date); List months = Arrays.asList(Month.MONTH); List populations = SiMatrix.getSiMatrix(context).getPopulations(date); ResultStorage resultmanager = context.getSimulationStorage().getResultStorage(); double Fcur,FcurFra; for (Population pop : populations) { MatrixND AbundancePop = resultmanager.getMatrix(date.previousYear(),pop,ResultName.MATRIX_ABUNDANCE_BEGIN_MONTH); MatrixND Prov1 = AbundancePop.sumOverDim(1); // sum over zones; List agegroups = pop.getPopulationGroup(); double countNcur = 0.0; double countCcurFra = 0.0; double countCcurOth = 0.0; for (PopulationGroup age : agegroups){ catchYear0.setValue(age,0.0); matrixweightAA.setValue(age,age.getMeanWeight()); matrixFcurOth.setValue(age,CalcTAC.matrixFmgtOth.getValue(age)); for (Strategy str : strs){ if (str.getName().compareTo("S1") == 0){ for (Metier met : metiers){ catchS1Met0.setValue(age,met,0.0); effortS1Met0.setValue(met,0.0); } } } } // end of age loop; for (TimeStep dat = date.previousYear(); dat.before(date); dat = dat.next()) { MatrixND CatchPop = resultmanager.getMatrix(dat,pop,ResultName.MATRIX_CATCH_PER_STRATEGY_MET_PER_ZONE_POP); MatrixND EffortS1Met = resultmanager.getMatrix(dat,ResultName.MATRIX_EFFORT_NOMINAL_PER_STRATEGY_MET); MatrixND Prov2 = CatchPop.sumOverDim(0); // sum over strategies; Prov2 = Prov2.sumOverDim(1); // sum over metiers; Prov2 = Prov2.sumOverDim(3); // sum over zones; MatrixND Prov3 = CatchPop.sumOverDim(3); // sum over zones; for (PopulationGroup age : agegroups){ catchYear0.setValue(age,catchYear0.getValue(age)+Prov2.getValue(0,0,age.getId(),0)); } //end of age loop; for (Strategy str : strs){ if ((pop.getName().compareTo("BlueLingSouth") == 0) && (str.getName().compareTo("S1") == 0)){ for (Metier met : metiers){ for (PopulationGroup age : agegroups){ catchS1Met0.setValue(age,met,catchS1Met0.getValue(age,met)+ Prov3.reduce().getValue(str,met,age)); } // end of age loop; effortS1Met0.setValue(met,effortS1Met0.getValue(met)+ EffortS1Met.getValue(str,met)); } // end of metier loop; } // end of str = S1 if-test; } // end of str loop; } // end of dat loop; if (pop.getName().compareTo("BlueLingSouth") == 0){ for (PopulationGroup age : agegroups){ double dummyCpue = 0.0; int dummyMet = 0; for (Metier met : metiers){ if (effortS1Met0.getValue(met) > 0.0){ dummyCpue += catchS1Met0.getValue(age,met)/effortS1Met0.getValue(met); dummyMet++; } } // end of metier loop; if (dummyMet > 0){ matrixCpue.setValue(age,dummyCpue/dummyMet);//YV } else{ matrixCpue.setValue(age,0.00); } mortNatAct.setValue(age,(Double) context.getValue("MBli")); countNcur = Prov1.reduce().getValue(age); countCcurFra = catchYear0.getValue(age); optimType = 0; optimFsq = new Fsq(countCcurFra,countNcur,mortNatAct.getValue(age),matrixFcurOth.getValue(age)); FcurFra = Fmin.fmin(0.0,2.0,1.0e-10,optimFsq,optimFmsy,optimFmgt,optimFsqPartial, optimNumber,optimType); matrixFcurFra.setValue(age,FcurFra); matrixFcur.setValue(age,matrixFcurFra.getValue(age) + matrixFcurOth.getValue(age)); countCcurOth = (matrixFcurOth.getValue(age)/(matrixFcur.getValue(age) + mortNatAct.getValue(age)))* (1 - Math.exp(-matrixFcur.getValue(age)-mortNatAct.getValue(age)))*countNcur; matrixCcurFra.setValue(age,countCcurFra); matrixCcurOth.setValue(age,countCcurOth); matrixNcur.setValue(age,countNcur); optimNumber++; catchAll.setValue(age,matrixCcurFra.getValue(age)+matrixCcurOth.getValue(age)); } // Si on est au mois de Janvier, avant le pas de temps: // Recuperer les captures aux ages etc du pas de temps precedent // realiser la xsa REngine engine = new RProxy(); engine.voidEval("load(file=\"C:/Paul/2005-2011/DPCP/P6/DEEPFISHMAN/Science/WP5/VPA/xsaRes.RData\")"); // etendre l'objet FLR // Objet Stock engine.voidEval("stock<-window(stock,end=range(stock)[5]+1)"); // On agrandit la fenetre temporelle du stock de 1 YV MatrixND caaAexprter = catchAll.getSubMatrix(0,7,11); MatrixND cawAexprter = matrixweightAA.getSubMatrix(0,7,11); int j=0; String essaimatrix = "test <- c("; for(MatrixIterator i = caaAexprter.iterator();i.hasNext();){ i.next(); j = j+1; if (j < 11) { essaimatrix += i.getValue() + ","; } else { essaimatrix += i.getValue(); } } essaimatrix = essaimatrix + ")"; engine.voidEval(essaimatrix); j=0; String cawmatrix = "caw <- c("; for(MatrixIterator i = cawAexprter.iterator();i.hasNext();){ i.next(); j = j+1; if (j < 11) { cawmatrix += i.getValue() + ","; } else { cawmatrix += i.getValue(); } } cawmatrix = cawmatrix + ")"; engine.voidEval(cawmatrix); engine.voidEval("stock@catch.n[,dim(stock@catch.n)[2],,,] <-test/1000" ); // recupere les capt aux ages de l annee precedente engine.voidEval("stock@catch.wt[,dim(stock@catch.wt)[2],,,]<-caw"); // recupere les poids aux ages de la base ISIS engine.voidEval("stock@landings.n[,dim(stock@landings.n)[2],,,]<-stock@catch.n[,dim(stock@catch.n)[2],,,] "); // pas de rejets les capt==debarquements engine.voidEval("stock@landings.wt[,dim(stock@landings.wt)[2],,,]<-caw"); // recupere les poids aux ages de la base ISIS engine.voidEval("stock@stock.wt[,dim(stock@stock.wt)[2],,,]<-caw"); // recupere les poids aux ages de la base ISIS engine.voidEval("stock@m[,dim(stock@m)[2],,,]<-stock@m[,dim(stock@m)[2] - 1,,,] "); // copie la M de l'anne precedente engine.voidEval("stock@mat[,dim(stock@mat)[2],,,]<-stock@mat[,dim(stock@mat)[2] - 1,,,] "); // copie la Maturite de l'anne precedente engine.voidEval("stock@harvest.spwn[,dim(stock@harvest.spwn)[2],,,]<-stock@harvest.spwn[,dim(stock@harvest.spwn)[2] - 1,,,] "); // engine.voidEval("stock@m.spwn[,dim(stock@m.spwn)[2],,,]<-stock@m.spwn[,dim(stock@m.spwn)[2] - 1,,,] "); // engine.voidEval("stock@landings[,dim(stock@landings)[2],,,]<-sum(stock@landings.n[,dim(stock@landings.n)[2],,,]*stock@landings.wt[,dim(stock@landings.n)[2],,,])"); // calcul landings tot // Objet fleet engine.voidEval("indices<-window(indices,end=range(indices)[4]+1)"); // On agrandit la fenetre temporelle de fleet de 1 YV MatrixND iaAexprter = matrixCpue.getSubMatrix(0,7,11) ; int j2=0; String essaimatrix2 = "test <- c("; for(MatrixIterator i = iaAexprter.iterator();i.hasNext();){ i.next(); j2 = j2+1; if (j2 < 11) { essaimatrix2 += i.getValue() + ","; } else { essaimatrix2 += i.getValue(); } } essaimatrix2 = essaimatrix2 + ")"; engine.voidEval(essaimatrix2); engine.voidEval("indices@.Data[[1]]@catch.n[,dim(indices@.Data[[1]]@catch.n)[2],,,]<-test/1000");// recupere les indices aux ages de l annee precedente engine.voidEval("indices@.Data[[1]]@effort[,dim(indices@.Data[[1]]@effort)[2],,,]<-1");// recupere les indices aux ages de l annee precedente log.info("Avant XSA"); // Faire la xsa engine.voidEval("xsa<-FLXSA(stock,indices,xsa.control)"); log.info("Apres XSA"); // retourner ssb et f de la derniere annee //engine.voidEval("range(stock)[3]<-range(xsa)[3]"); //engine.voidEval("stock<-stock+xsa"); engine.voidEval("stock@harvest<-xsa@harvest"); // yv 200712 engine.voidEval("stock@stock.n<-xsa@stock.n");// yv 200712 Double ssb = ((Double) engine.eval("ssb<-as.numeric(as.data.frame(ssb(stock)[1,ncol(ssb(stock))]))[7]")).doubleValue(); Double fbar = ((Double) engine.eval("fbar<-as.numeric(as.data.frame(fbar(stock)[1,ncol(fbar(stock))]))[7]")).doubleValue(); FauxAges = (RDataFrame) engine.eval("FauxAges <- as.data.frame(as.data.frame(harvest(stock)[,ncol(harvest(stock))])$data)"); NauxAges = (RDataFrame) engine.eval("NauxAges<- as.data.frame(as.data.frame(stock.n(stock)[,ncol(stock.n(stock))])$data)"); FauxAges.setVariable("FauxAges"); NauxAges.setVariable("NauxAges"); ssbExport = ssb; fbarExport = fbar; engine.voidEval("save.image(file=\"C:/Paul/2005-2011/DPCP/P6/DEEPFISHMAN/Science/WP5/VPA/xsaRes.RData\")"); for (int iage=0; iage <= pop.sizePopulationGroup()-1; iage++){ PopulationGroup age = pop.getPopulationGroup().get(iage); if (iage < 7){ matrixNageExport.setValue(age,0.0); matrixFageExport.setValue(age,0.0); } // end of iage if-test; else{ matrixNageExport.setValue(age,(Double) NauxAges.get(0,iage-7)); matrixFageExport.setValue(age,(Double) FauxAges.get(0,iage-7)); } // end of iage else-test; } // end of age for loop; engine.voidEval("save.image(file=\"C:/Paul/2005-2011/DPCP/P6/DEEPFISHMAN/Science/WP5/VPA/xsaRes.RData\")"); } // end of pop=bli if-test; else if (pop.getName().compareTo("Pok3a46") == 0){ // Choosing saithe; for (PopulationGroup age : agegroups){ mortNatAct.setValue(age,(Double) context.getValue("MPok")); countNcur = Prov1.reduce().getValue(age); countCcurFra = catchYear0.getValue(age); optimType = 0; optimFsq = new Fsq(countCcurFra,countNcur,mortNatAct.getValue(age),matrixFcurOth.getValue(age)); FcurFra = Fmin.fmin(0.0,2.0,1.0e-10,optimFsq,optimFmsy,optimFmgt,optimFsqPartial, optimNumber,optimType); matrixFcurFra.setValue(age,FcurFra); matrixFcur.setValue(age,matrixFcurFra.getValue(age) + matrixFcurOth.getValue(age)); countCcurOth = (matrixFcurOth.getValue(age)/(matrixFcur.getValue(age) + mortNatAct.getValue(age)))* (1 - Math.exp(-matrixFcur.getValue(age)-mortNatAct.getValue(age)))*countNcur; matrixCcurFra.setValue(age,countCcurFra); matrixCcurOth.setValue(age,countCcurOth); matrixNcur.setValue(age,countNcur); catchAll.setValue(age,matrixCcurFra.getValue(age)+matrixCcurOth.getValue(age)); optimNumber++; if (age.getId() < 3){ matrixNageExport.setValue(age,0.0); matrixFageExport.setValue(age,0.0); } // end of age if-test; else{ matrixNageExport.setValue(age,matrixNcur.getValue(age)* (1+matrixNoiseAssPokAA.getValue(age)*matrixCvAssPokAA.getValue(age))); if (matrixNageExport.getValue(age) <= 0){matrixNageExport.setValue(age,1.0e-3);} optimFsq = new Fsq(catchAll.getValue(age),matrixNageExport.getValue(age), mortNatAct.getValue(age),0.0); matrixFageExport.setValue(age,Fmin.fmin(0.0,2.0,1.0e-10,optimFsq,optimFmsy,optimFmgt, optimFsqPartial,optimNumber,optimType)); matrixNageExport.setValue(age,matrixNageExport.getValue(age)); optimNumber++; } // end of age else-test; } // end of age-for loop; } // end of pop=pok else if-test; else{ // Choosing non age-structured stocks; countNcur = Prov1.getValue(pop.getPopulationGroup().get(0).getId(),0); countCcurFra = catchYear0.getValue(pop.getPopulationGroup().get(0)); FcurFra = countCcurFra/countNcur; matrixFcurFra.setValue(pop.getPopulationGroup().get(0),FcurFra); matrixFcur.setValue(pop.getPopulationGroup().get(0),FcurFra + matrixFcurOth.getValue(pop.getPopulationGroup().get(0))); countCcurOth = CalcTAC.matrixYmgtOth.getValue(pop); matrixCcurFra.setValue(pop.getPopulationGroup().get(0),countCcurFra); matrixCcurOth.setValue(pop.getPopulationGroup().get(0),countCcurOth); matrixNcur.setValue(pop.getPopulationGroup().get(0),countNcur); } // End of choosing non age-structurd stocks; } // end of pop loop; first = false; } // end of (first) if-test; } /** * @param simulation La simulation pour lequel on utilise cette regle */ public void postAction(SimulationContext context, TimeStep date, Metier metier) throws Exception { first = true; } }