Author: bpoussin Date: 2014-12-18 12:20:31 +0000 (Thu, 18 Dec 2014) New Revision: 350 Url: http://forge.codelutin.com/projects/isis-fish-data/repository/revisions/350 Log: fixes #6322: Change DefaultSimulator, SiMatrix and GravityModel to permit easy overwrite method to create new simulator Added: trunk/scripts/SiMatrixEffortByCell.java Modified: trunk/scripts/SiMatrix.java trunk/simulators/DefaultSimulator.java trunk/simulators/SimulatorEffortByCell.java Modified: trunk/scripts/SiMatrix.java =================================================================== --- trunk/scripts/SiMatrix.java 2014-12-03 15:48:52 UTC (rev 349) +++ trunk/scripts/SiMatrix.java 2014-12-18 12:20:31 UTC (rev 350) @@ -79,9 +79,11 @@ protected SimulationContext context = null; protected TopiaContext db = null; + protected ResultManager resManager; /** - * Method used to get SiMatrix used for simulation + * Method used to get SiMatrix used for simulation, before call this method + * new SiMatrix must called * * @param context * context simulation @@ -91,9 +93,6 @@ public static SiMatrix getSiMatrix(SimulationContext context) throws TopiaException { SiMatrix result = (SiMatrix) context.getValue(SiMatrix.class.getName()); - if (result == null) { - result = new SiMatrix(context); - } return result; } @@ -110,10 +109,23 @@ public SiMatrix(SimulationContext context) throws TopiaException { this.context = context; this.db = context.getDB(); + this.resManager = context.getResultManager(); setSiMatrix(context, this); } /** + * This method permit to add some specific computation for specific Simulator. + * example use in SiMatrixEffortByCell + * + * @param step + * @param pop + * @param N + * @throws IsisFishException + */ + public void computeMonthExtra(TimeStep step, Population pop, MatrixND N) throws TopiaException, IsisFishException { + } + + /** * @return * @throws TopiaException */ @@ -244,107 +256,6 @@ // /////////////////////////////////////////////////////////////////////////// - public MatrixND matrixCatchWeightPerStrategyMetPerZoneMet(TimeStep step, - Population pop, MatrixND matrixCatchPerStrategyMetPerZoneMet) - throws TopiaException, IsisFishException { - - return matrixToWeightMatrix(step, 2, - ResultName.MATRIX_CATCH_WEIGHT_PER_STRATEGY_MET_PER_ZONE_MET, - matrixCatchPerStrategyMetPerZoneMet); - -// List<PopulationGroup> groups = pop.getPopulationGroup(); -// -// MatrixND result = matrixCatchPerStrategyMetPerZoneMet.copy(); -// result.setName(ResultName.MATRIX_CATCH_WEIGHT_PER_STRATEGY_MET_PER_ZONE_MET); -// -// for (PopulationGroup group : groups) { -// MatrixND sub = result.getSubMatrix(2, group, 1); -// double meanWeight = group.getMeanWeight(); -// sub.mults(meanWeight); -// } -// -// return result; - } - - public MatrixND matrixCatchPerStrategyMetPerZoneMet(MatrixND N, - Population pop, TimeStep step) throws TopiaException, IsisFishException { - - MatrixND matrixFishingMortalityPerCell = matrixFishingMortalityPerCell( - step, pop); - MatrixND matrixCatchRatePerStrategyMetPerCell = matrixCatchRatePerStrategyMetPerCell( - pop, step, matrixFishingMortalityPerCell); - MatrixND matrixCatchPerStrategyMetPerCell = matrixCatchPerStrategyMetPerCell( - N, pop, step, matrixCatchRatePerStrategyMetPerCell); - - List<Strategy> strategies = getStrategies(step); - List<Metier> metiers = getMetiers(step); - List<PopulationGroup> groups = (List<PopulationGroup>)matrixCatchPerStrategyMetPerCell - .getSemantic(2); - List<Zone> zones = getZones(step); - - Set<Cell> cellPops = new HashSet(matrixCatchPerStrategyMetPerCell - .getSemantic(4)); - - MatrixND result = MatrixFactory.getInstance().create( - ResultName.MATRIX_CATCH_PER_STRATEGY_MET_PER_ZONE_MET, - new List[] { strategies, metiers, groups, zones }, - new String[] { n("Strategies"), n("Metiers"), n("Groups"), - n("Zones") }); - - // matrice temporaire ou les zones pops sont sommees - MatrixND tmp = matrixCatchPerStrategyMetPerCell.sumOverDim(3); - tmp = tmp.reduceDims(3); - - for (int s = 0; s < strategies.size(); s++) { - Strategy str = strategies.get(s); - for (int g = 0; g < groups.size(); g++) { - PopulationGroup group = groups.get(g); - // on iter que sur les metiers qui ont une proportion non 0 - // car la matrice pour les autres metiers, contient 0. - List<Metier> metiersNot0 = getMetiers(str, step); - for (int m = 0; m < metiersNot0.size(); m++) { - Metier metier = metiersNot0.get(m); - MetierSeasonInfo infoMet = metier.getMetierSeasonInfo(step - .getMonth()); - Collection<Zone> zoneMet = infoMet.getZone(); - for (Zone z : zoneMet) { - double value = 0; - List<Cell> cells = z.getCell(); - for (int c = 0; c < cells.size(); c++) { - Cell cell = cells.get(c); - if (cellPops.contains(cell)) { - // les cells de la matrice sont les cells des - // zones pops, donc seul les intersections avec - // les cells des metiers sont des cells valides - value += tmp.getValue(str, metier, group, cell); - } - } - result.setValue(str, metier, group, z, value); - } - } - } - } - - return result; - } - - public MatrixND matrixCatchPerStrategyMetPerZonePop(MatrixND N, - Population pop, TimeStep step) throws TopiaException, IsisFishException { - MatrixND matrixFishingMortalityPerCell = matrixFishingMortalityPerCell( - step, pop); - MatrixND matrixCatchRatePerStrategyMetPerCell = matrixCatchRatePerStrategyMetPerCell( - pop, step, matrixFishingMortalityPerCell); - MatrixND matrixCatchPerStrategyMetPerCell = matrixCatchPerStrategyMetPerCell( - N, pop, step, matrixCatchRatePerStrategyMetPerCell); - - // on somme sur les cellules - MatrixND result = matrixCatchPerStrategyMetPerCell.sumOverDim(4); - result = result.reduceDims(4); - result.setName(ResultName.MATRIX_CATCH_PER_STRATEGY_MET_PER_ZONE_POP); - - return result; - } - public MatrixND matrixCatchWeightPerStrategyMetPerZonePop(TimeStep step, Population pop, MatrixND matrixCatchPerStrategyMetPerZonePop) throws TopiaException, IsisFishException { @@ -352,19 +263,6 @@ return matrixToWeightMatrix(step, 2, ResultName.MATRIX_CATCH_WEIGHT_PER_STRATEGY_MET_PER_ZONE_POP, matrixCatchPerStrategyMetPerZonePop); - -// List<PopulationGroup> groups = pop.getPopulationGroup(); -// -// MatrixND result = matrixCatchPerStrategyMetPerZonePop.copy(); -// result.setName(ResultName.MATRIX_CATCH_WEIGHT_PER_STRATEGY_MET_PER_ZONE_POP); -// -// for (PopulationGroup group : groups) { -// MatrixND sub = result.getSubMatrix(2, group, 1); -// double meanWeight = group.getMeanWeight(); -// sub.mults(meanWeight); -// } -// -// return result; } /** @@ -381,9 +279,11 @@ * @throws IsisFishException */ public MatrixND matrixCatchPerStrategyMetPerZone(MatrixND N, - Population pop, TimeStep step, MatrixND matrixCatchRatePerStrategyMet) + Population pop, TimeStep step) throws TopiaException, IsisFishException { + MatrixND matrixCatchRatePerStrategyMet = matrixCatchRatePerStrategyMetPerZone(pop, step); + int dimGroup = 2; int dimZone = 3; @@ -398,21 +298,6 @@ } return result; -// List<PopulationGroup> groups = pop.getPopulationGroup(); -// List<Zone> zones = pop.getPopulationZone(); -// -// MatrixND result = matrixCatchRatePerStrategyMet.copy(); -// result.setName(ResultName.MATRIX_CATCH_PER_STRATEGY_MET_PER_ZONE_POP); -// -// for (PopulationGroup group : groups) { -// MatrixND sub = result.getSubMatrix(2, group, 1); -// for (Zone zone : zones) { -// MatrixND subsub = sub.getSubMatrix(3, zone, 1); -// double val = N.getValue(group, zone); -// subsub.mults(val); -// } -// } -// return result; } /** @@ -426,9 +311,11 @@ * @throws TopiaException * @throws IsisFishException */ - public MatrixND matrixCatchRatePerStrategyMetPerZone(Population pop, - TimeStep step, MatrixND matrixFishingMortality) throws TopiaException, + public MatrixND matrixCatchRatePerStrategyMetPerZone(Population pop, TimeStep step) throws TopiaException, IsisFishException { + + MatrixND matrixFishingMortality = matrixFishingMortality(step, pop); + List<Strategy> strategies = getStrategies(step); List<Metier> metiers = getMetiers(step); List<PopulationGroup> groups = pop.getPopulationGroup(); @@ -462,6 +349,7 @@ } } } + resManager.addResult(step, pop, result); return result; } @@ -490,8 +378,44 @@ return result; } + /** + * @param str + * @param metier + * @param step + * @return + */ + protected double effortPerStrategyPerCell(Strategy str, Metier metier, + MetierSeasonInfo infoMet, TimeStep step) { + //StrategyMonthInfo smi = str.getStrategyMonthInfo(month); + Collection<Zone> zones = infoMet.getZone(); + double nbCell = getCells(zones).size(); + + if (nbCell == 0) { + // normalement il devrait y avoir des mailles, mais pour les + // ancienne zone AuPort, il n'y en avait pas + if (log.isWarnEnabled()) { + Month month = step.getMonth(); + log.warn("Calcul d'une distance pour le metier " + metier + + " pour le mois " + month + + " avec une zone sans maille: " + zones); + } + return 0; + } + + double effortPerStrategy = effortPerStrategyMet(str, metier, step); + + if (log.isDebugEnabled()) { + log.debug(" strategy=" + str + " metier=" + metier + " nbCell=" + + nbCell + " effortPerStrategy=" + effortPerStrategy); + } + + double result = effortPerStrategy / nbCell; + + return result; + } + // Optimisation Hilaire - private double catchRatePerStrategyMet(Strategy str, Metier metier, + protected double catchRatePerStrategyMet(Strategy str, Metier metier, TimeStep step, PopulationGroup group, Zone zone, MatrixND matrixFishingMortality) throws TopiaException, IsisFishException { @@ -567,83 +491,6 @@ return result; } -// /** -// * Matrice fishing mortality dim [ Strategy x Metier x Classe x zonePop ] -// * -// * @param pop -// * @param step -// * @return -// * @throws TopiaException -// * @throws IsisFishException -// */ -// public MatrixND matrixFishingMortality(TimeStep step, Population pop) -// throws TopiaException, IsisFishException { -// List<Strategy> strategies = getStrategies(step); -// List<Metier> metiers = getMetiers(step); -// List<PopulationGroup> groups = pop.getPopulationGroup(); -// List<Zone> zones = pop.getPopulationZone(); -// -// // default value in matrix is 0 -// MatrixND result = MatrixFactory.getInstance().create( -// ResultName.MATRIX_FISHING_MORTALITY, -// new List[] { strategies, metiers, groups, zones }, -// new String[] { n("Strategies"), n("Metiers"), n("Groups"), -// n("Zones") }); -// -// Month month = step.getMonth(); -// PopulationSeasonInfo infoPop = pop.getPopulationSeasonInfo(month); -// -// for (int g = 0; g < groups.size(); g++) { -// PopulationGroup group = groups.get(g); -// -// // getCapturability is check matrix validity and create matrix if needed and get one value in -// double capturability = infoPop.getCapturability(group); -// if (capturability != 0) { // check 0, this prevent next call, for default value -// -// for (int m = 0; m < metiers.size(); m++) { -// Metier metier = metiers.get(m); -// -// MetierSeasonInfo infoMet = metier -// .getMetierSeasonInfo(month); -// // getTargetFactor seem to be simple -// double ciblage = infoMet.getTargetFactor(group); -// -// if (ciblage != 0) { // check 0, this prevent next call, for default value -// -// Gear gear = metier.getGear(); -// Selectivity selectivity = gear -// .getPopulationSelectivity(pop); -// if (selectivity != null) { -// -// // getCoefficient is equation evaluation -// double coeff = selectivity.getCoefficient(pop, -// group, metier); -// if (coeff != 0) { // check 0, this prevent next call, for default value -// -// for (int s = 0; s < strategies.size(); s++) { -// Strategy str = strategies.get(s); -// for (int z = 0; z < zones.size(); z++) { -// Zone zone = zones.get(z); -// double effort = effortPerZonePop(str, -// metier, infoMet, step, zone); -// if (effort > 0) { // put value only if <> 0 -// double value = coeff -// * capturability * ciblage -// * effort; -// result.setValue(str, metier, group, -// zone, value); -// } -// } -// } -// } -// } -// } -// } -// } -// } -// return result; -// } - /** * Matrice fishing mortality dim [ Strategy x Metier x Classe x zonePop ] * @@ -730,519 +577,16 @@ } } } + resManager.addResult(step, pop, result); return result; } - /** - * Matrice des captures en nombre dim [ Strategy x Metier x Classe x zonePop ] - * - * @param N - * l'abondance sous forme de matrice 2D [class x zone] - * @param pop - * @param step - * @return - * @throws TopiaException - * @throws IsisFishException - */ - private MatrixND matrixCatchPerStrategyMetPerCell(MatrixND N, - Population pop, TimeStep step, - MatrixND matrixCatchRatePerStrategyMetPerCell) - throws TopiaException, IsisFishException { - int groupDim = 2; - int zoneDim = 3; - - int NGroupDim = 0; - int NZoneDim = 1; - - MatrixND result = matrixCatchRatePerStrategyMetPerCell.copy(); - result.setName("matrixCatchPerStrategyMetPerCell"); - - List<PopulationGroup> groups = (List<PopulationGroup>)result.getSemantic(groupDim); - List<Zone> zones = (List<Zone>)result.getSemantic(zoneDim); - - double[] sizeCell = new double[zones.size()]; - int cpt = 0; - for (Zone z : zones) { - sizeCell[cpt++] = z.sizeCell(); - } - - // on suppose que les semantics de N et matrixCatchRatePerStrategyMetPerCell - // pour les PopulationGroup et Zone sont les memes - // mais on verifie tout de meme - if (groups.equals(N.getSemantic(NGroupDim)) && - zones.equals(N.getSemantic(NZoneDim))) { - - // on place les effectifs dans un tableau pour un acces rapide - // on divise directement par le nombre de cell de la zone pour eviter - // de le faire beaucoup plus souvent dans l'autre boucle - double[][] NArray = new double[groups.size()][zones.size()]; - for (MatrixIterator i=N.iteratorNotZero(); i.next();) { - int[] pos = i.getCoordinates(); - double val = i.getValue(); - - NArray[pos[NGroupDim]][pos[NZoneDim]] = val / sizeCell[pos[NZoneDim]]; - } - - // calcul de la matrice resultat - for (MatrixIterator i=result.iteratorNotZero(); i.next();) { - int[] pos = i.getCoordinates(); - double val = i.getValue(); - val *= NArray[pos[groupDim]][pos[zoneDim]]; - i.setValue(val); - } - - } else { - // N et matrixCatchRatePerStrategyMetPerCell non pas les meme semantics - // on fait moins rapide - for (MatrixIterator i=result.iteratorNotZero(); i.next();) { - int[] pos = i.getCoordinates(); - Object[] sems = i.getSemanticsCoordinates(); - PopulationGroup group = (PopulationGroup)sems[groupDim]; - Zone zone = (Zone)sems[zoneDim]; - double val = i.getValue(); - val *= N.getValue(group, zone) / sizeCell[pos[zoneDim]]; - i.setValue(val); - } - } - - return result; - -// List<PopulationGroup> groups = pop.getPopulationGroup(); -// List<Zone> zones = pop.getPopulationZone(); -// -// MatrixND result = matrixCatchRatePerStrategyMetPerCell.copy(); -// result.setName("matrixCatchPerStrategyMetPerCell"); -// -// for (PopulationGroup group : groups) { -// MatrixND sub = result.getSubMatrix(2, group, 1); -// for (Zone zone : zones) { -// MatrixND subsub = sub.getSubMatrix(3, zone, 1); -// double val = N.getValue(group, zone) / (double) zone.sizeCell(); -// subsub.mults(val); -// } -// } -// return result; - } - - /** - * Matrice des captures en poids dim [ Strategy x Metier x Classe x zonePop ] - * - * @param pop - * @param step - * @return - * @throws TopiaException - * @throws IsisFishException - */ - private MatrixND matrixCatchRatePerStrategyMetPerCell(Population pop, - TimeStep step, MatrixND matrixFishingMortalityPerCell) - throws TopiaException, IsisFishException { - - MatrixND totalFishingMortalityPerCell = totalFishingMortalityPerCell( - step, matrixFishingMortalityPerCell); - - MatrixND result = matrixFishingMortalityPerCell.copy(); - result.setName("matrixCatchRatePerStrategyMetPerCell"); - - for (MatrixIterator i=result.iteratorNotZero(); i.next();) { - Object[] sems = i.getSemanticsCoordinates(); - - PopulationGroup group = (PopulationGroup)sems[2]; - Zone zone = (Zone)sems[3]; - Cell cell = (Cell)sems[4]; - - double totalFishingMortality = totalFishingMortalityPerCell.getValue( - group, zone, cell); - - if (totalFishingMortality != 0) { - - double fishingMortalityPerCell = i.getValue(); - double totalCatchRatePerCell = totalCatchRatePerCell(step, group, zone, - totalFishingMortality); - - double value = fishingMortalityPerCell / totalFishingMortality - * totalCatchRatePerCell; - - i.setValue(value); - } - } - - return result; - - -// List<Strategy> strategies = getStrategies(step); -// List<Metier> metiers = getMetiers(step); -// List<PopulationGroup> groups = pop.getPopulationGroup(); -// List<Zone> zones = pop.getPopulationZone(); -// List<Cell> cells = getCells(zones); -// -// MatrixND result = MatrixFactory.getInstance().create( -// "matrixCatchRatePerStrategyMetPerCell", -// new List[] { strategies, metiers, groups, zones, cells }, -// new String[] { n("Strategies"), n("Metiers"), n("Groups"), -// n("Zones"), n("Cells") }); -// -// // Optimisation Hilaire -// MatrixND matrixFishingMortalityPerCellSumOverGroup = matrixFishingMortalityPerCell -// .sumOverDim(2); -// matrixFishingMortalityPerCellSumOverGroup = matrixFishingMortalityPerCellSumOverGroup -// .reduceDims(2); -// -// for (int s = 0; s < strategies.size(); s++) { -// Strategy str = strategies.get(s); -// metiers = getMetiers(str, step); -// for (int m = 0; m < metiers.size(); m++) { -// Metier metier = metiers.get(m); -// for (int z = 0; z < zones.size(); z++) { -// Zone zone = zones.get(z); -// List<Cell> cellZones = zone.getCell(); -// for (int c = 0; c < cellZones.size(); c++) { -// Cell cell = cellZones.get(c); -// double effort = matrixFishingMortalityPerCellSumOverGroup -// .getValue(str, metier, zone, cell); -// if (effort > 0) { -// for (int g = 0; g < groups.size(); g++) { -// PopulationGroup group = groups.get(g); -// double value = catchRatePerStrategyMetPerCell( -// str, metier, step, group, zone, cell, -// matrixFishingMortalityPerCell); -// result.setValue(new Object[] { str, metier, -// group, zone, cell }, value); -// } -// } -// } -// } -// } -// } -// -// return result; - } -// directement dans matrixCatchRatePerStrategyMetPerCell pour pouvoir mieux optimiser -// /** -// * catchRatePerStrategyMetPerCell. -// * -// * Optimisation Hilaire -// * -// * @param str -// * @param metier -// * @param step -// * @param group -// * @param zone -// * @param cell -// * @param matrixFishingMortalityPerCell -// * @return -// * @throws TopiaException -// * @throws IsisFishException -// */ -// private double catchRatePerStrategyMetPerCell(Strategy str, Metier metier, -// TimeStep step, PopulationGroup group, Zone zone, Cell cell, -// MatrixND matrixFishingMortalityPerCell) throws TopiaException, -// IsisFishException { -// -// MatrixND totalFishingMortalityPerCell = totalFishingMortalityPerCell( -// step, matrixFishingMortalityPerCell); -// double totalFishingMortality = totalFishingMortalityPerCell.getValue( -// group, zone, cell); -// -// if (totalFishingMortality == 0) { -// if (log.isDebugEnabled()) { -// log.debug("pas de totalFishingMortality pour (" + group + ", " -// + zone + ")"); -// } -// return 0; -// } -// -// double fishingMortalityPerCell = matrixFishingMortalityPerCell -// .getValue(new Object[] { str, metier, group, zone, cell }); -// double totalCatchRatePerCell = totalCatchRatePerCell(step, group, zone, -// totalFishingMortality); -// -// if (log.isDebugEnabled()) { -// log.debug(" totalFishingMortality=" + totalFishingMortality -// + " fishingMortality=" + fishingMortalityPerCell -// + " totalCatchRate=" + totalCatchRatePerCell); -// } -// double result = fishingMortalityPerCell / totalFishingMortality -// * totalCatchRatePerCell; -// -// return result; -// } - - /** - * @param step - * @param group - * @param zone - * @param totalFishingMortality - * @return - * @throws TopiaException - */ - private double totalCatchRatePerCell(TimeStep step, PopulationGroup group, - Zone zone, double totalFishingMortalityPerCell) - throws TopiaException { - double M = group.getNaturalDeathRate(zone) / Month.NUMBER_OF_MONTH; - if (M == 0) { - // normalement il devrait y avoir de la mortalite naturelle - if (log.isWarnEnabled()) { - log.warn("Pas de mortalite naturelle pour: " + group); - } - } - double F = totalFishingMortalityPerCell; - - double result = 0; - if (M != 0 || F != 0) { - result = F / (F + M) * (1 - Math.exp(-(F + M))); - } - - return result; - } - - /** - * Returne une matrice de mortalite group x zone, donc somme sur str et - * metier - * - * @param step - * @param matrixFishingMortality - * @param group - * @param zone - * @return - */ - private MatrixND totalFishingMortalityPerCell(TimeStep step, - MatrixND matrixFishingMortalityPerCell) { - MatrixND result = matrixFishingMortalityPerCell.sumOverDim(0); - result = result.sumOverDim(1); - result = result.reduceDims(0, 1); - return result; - } - -// /** -// * Matrice fishing mortality dim [ Strategy x Metier x Classe x zonePop x -// * cellPop] -// * -// * Nouvelle implantation suite a demande Steph et Sigrid: 20080208 (NouvellesEquationsfev2008ParCelluleAvecAlgo.odt) -// * -// * @param pop -// * @param step -// * @return -// * @throws TopiaException -// * @throws IsisFishException -// */ -// public MatrixND matrixFishingMortalityPerCell(TimeStep step, Population pop) -// throws TopiaException, IsisFishException { -// List<Strategy> strategies = getStrategies(step); -// List<Metier> metiers = getMetiers(step); -// List<PopulationGroup> groups = pop.getPopulationGroup(); -// List<Zone> zones = pop.getPopulationZone(); -// List<Cell> cells = getCells(zones); -// -// // default value in matrix is 0 -// MatrixND result = MatrixFactory.getInstance().create( -// "matrixFishingMortalityPerCell", -// new List[] { strategies, metiers, groups, zones, cells }, -// new String[] { n("Strategies"), n("Metiers"), n("Groups"), -// n("Zones"), n("Cells") }); -// -// Month month = step.getMonth(); -// PopulationSeasonInfo infoPop = pop.getPopulationSeasonInfo(month); -// -// for (int g = 0; g < groups.size(); g++) { -// PopulationGroup group = groups.get(g); -// -// // getCapturability is check matrix validity and create matrix -// // if needed and get one value in -// double capturability = infoPop.getCapturability(group); -// if (capturability != 0) { // check 0, this prevent next call, for default value -// for (int m = 0; m < metiers.size(); m++) { -// Metier metier = metiers.get(m); -// -// MetierSeasonInfo infoMet = metier -// .getMetierSeasonInfo(month); -// // getTargetFactor seem to be simple -// double ciblage = infoMet.getTargetFactor(group); -// -// if (ciblage != 0) { // check 0, this prevent next call, for default value -// -// Gear gear = metier.getGear(); -// Selectivity selectivity = gear.getPopulationSelectivity(pop); -// if (selectivity != null) { -// -// // getCoefficient is equation evaluation -// double coeff = selectivity.getCoefficient(pop,group, metier); -// if (coeff != 0) { // check 0, this prevent next call, for default value -// -// for (int s = 0; s < strategies.size(); s++) { -// Strategy str = strategies.get(s); -// // l'effort d'une cellule du metier -// double effort = effortPerStrategyPerCell( -// str, metier, infoMet, step); -// if (effort > 0) { // put value only if <> 0 -// for (int z = 0; z < zones.size(); z++) { -// Zone zone = zones.get(z); -// Set<Cell> cellPops = new HashSet<Cell>(zone.getCell()); -// for (Cell cellMet : infoMet.getCells()) { -// if (cellPops.contains(cellMet)) { -// double value = coeff * capturability * ciblage * effort; -// result.setValue( -// new Object[] { str, -// metier, -// group, -// zone, -// cellMet }, -// value); -// } -// } -// } -// } -// } -// } -// } -// } -// } -// } -// } -// return result; -// } -// - - /** - * Matrice fishing mortality dim [ Strategy x Metier x Classe x zonePop x - * cellPop] - * - * Nouvelle implantation suite a demande Steph et Sigrid: 20080208 (NouvellesEquationsfev2008ParCelluleAvecAlgo.odt) - * - * @param pop - * @param step - * @return - * @throws TopiaException - * @throws IsisFishException - */ - public MatrixND matrixFishingMortalityPerCell(TimeStep step, Population pop) - throws TopiaException, IsisFishException { - List<Strategy> strategies = getStrategies(step); - List<Metier> metiers = getMetiers(step); - List<PopulationGroup> groups = pop.getPopulationGroup(); - List<Zone> zones = pop.getPopulationZone(); - List<Cell> cells = getCells(zones); - - // default value in matrix is 0 - MatrixND result = MatrixFactory.getInstance().create( - "matrixFishingMortalityPerCell", - new List[] { strategies, metiers, groups, zones, cells }, - new String[] { n("Strategies"), n("Metiers"), n("Groups"), - n("Zones"), n("Cells") }); - - Month month = step.getMonth(); - PopulationSeasonInfo infoPop = pop.getPopulationSeasonInfo(month); - - for (Strategy str : strategies) { - StrategyMonthInfo smi = str.getStrategyMonthInfo(month); - double nbTrip = smi.getNumberOfTrips(); - double propSetOfVessels = str.getProportionSetOfVessels(); - int nbOfVessels = str.getSetOfVessels().getNumberOfVessels(); - - // dans le calcul de l'effort si une de ces donnees est 0 - // alors le result dans la matrice est 0 - // donc autant ne pas faire tous les autres calculs couteux - if (nbTrip * propSetOfVessels * nbOfVessels != 0) { - for (PopulationGroup group : groups) { - - // getCapturability is check matrix validity and create matrix if needed and get one value in - double capturability = infoPop.getCapturability(group); - if (capturability != 0) { // check 0, this prevent next call, for default value - - // et de la meme facon si la proportion est 0 alors - // le resultat dans la matrice sera 0, donc on boucle que - // sur ceux qui ont une proportion - List<Metier> metiersNot0 = getMetiers(str, step); - for (Metier metier : metiersNot0) { - - MetierSeasonInfo infoMet = metier - .getMetierSeasonInfo(month); - // getTargetFactor seem to be simple - double ciblage = infoMet.getTargetFactor(group); - - if (ciblage != 0) { // check 0, this prevent next call, for default value - - Gear gear = metier.getGear(); - Selectivity selectivity = gear.getPopulationSelectivity(pop); - if (selectivity != null) { - - // getCoefficient is equation evaluation - double coeff = selectivity.getCoefficient(pop,group, metier); - if (coeff != 0) { // check 0, this prevent next call, for default value - - // l'effort d'une cellule du metier - double effort = effortPerStrategyPerCell( - str, metier, infoMet, step); - if (effort > 0) { // put value only if <> 0 - for (Zone zone : zones) { - Set<Cell> cellPops = new HashSet<Cell>(zone.getCell()); - for (Cell cellMet : infoMet.getCells()) { - if (cellPops.contains(cellMet)) { - double value = coeff * capturability * ciblage * effort; - result.setValue( - new Object[] { str, - metier, - group, - zone, - cellMet }, - value); - } - } - } - } - } - } - } - } - } - } - } - } - return result; - } - - /** * @param str * @param metier * @param step * @return */ - private double effortPerStrategyPerCell(Strategy str, Metier metier, - MetierSeasonInfo infoMet, TimeStep step) { - //StrategyMonthInfo smi = str.getStrategyMonthInfo(month); - Collection<Zone> zones = infoMet.getZone(); - double nbCell = getCells(zones).size(); - - if (nbCell == 0) { - // normalement il devrait y avoir des mailles, mais pour les - // ancienne zone AuPort, il n'y en avait pas - if (log.isWarnEnabled()) { - Month month = step.getMonth(); - log.warn("Calcul d'une distance pour le metier " + metier - + " pour le mois " + month - + " avec une zone sans maille: " + zones); - } - return 0; - } - - double effortPerStrategy = effortPerStrategyMet(str, metier, step); - - if (log.isDebugEnabled()) { - log.debug(" strategy=" + str + " metier=" + metier + " nbCell=" - + nbCell + " effortPerStrategy=" + effortPerStrategy); - } - - double result = effortPerStrategy / nbCell; - - return result; - } - - /** - * @param str - * @param metier - * @param step - * @return - */ private double effortNominalPerStrategyMet(Strategy str, Metier metier, TimeStep step) { Month month = step.getMonth(); StrategyMonthInfo smi = str.getStrategyMonthInfo(month); @@ -1303,7 +647,7 @@ * @param step * @return */ - private double effortPerStrategyMet(Strategy str, Metier metier, TimeStep step) { + protected double effortPerStrategyMet(Strategy str, Metier metier, TimeStep step) { Month month = step.getMonth(); StrategyMonthInfo smi = str.getStrategyMonthInfo(month); @@ -1545,71 +889,6 @@ /////////////////////////////////////////////////////////////////////////// /** - * @param N - * @param pop - * @param step - * @return - * @throws IsisFishException - * @throws TopiaException - */ - private MatrixND matrixAbundancePerCell(MatrixND N, Population pop, - TimeStep step, MatrixND matrixFishingMortality) throws TopiaException, - IsisFishException { - - List<PopulationGroup> groups = pop.getPopulationGroup(); - List<Zone> zones = pop.getPopulationZone(); - List<Cell> allCells = getCells(zones); - - MatrixND result = MatrixFactory.getInstance().create( - ResultName.MATRIX_ABUNDANCE + "_PER_CELL", - new List[] { groups, zones, allCells }, - new String[] { n("Groups"), n("Zones"), n("Cells") }); - - for (int g = 0; g < groups.size(); g++) { - PopulationGroup group = groups.get(g); - for (int z = 0; z < zones.size(); z++) { - Zone zone = zones.get(z); - List<Cell> cells = zone.getCell(); - for (int c = 0; c < cells.size(); c++) { - Cell cell = cells.get(c); - double value = survivalRatePerCell(step, group, zone, cell, - matrixFishingMortality); - double n = N.getValue(g, z) / zone.sizeCell(); - value *= n; - result.setValue(g, z, c, value); - } - } - } - - return result; - } - - /** - * Utilise pour le calcule en Cell. - * - * @param N - * @param pop - * @param step - * @return - * @throws IsisFishException - * @throws TopiaException - */ - public MatrixND matrixAbundance(MatrixND N, Population pop, TimeStep step) - throws TopiaException, IsisFishException { - - MatrixND matrixFishingMortalityPerCell = matrixFishingMortalityPerCell( - step, pop); - - MatrixND result = matrixAbundancePerCell(N, pop, step, - matrixFishingMortalityPerCell); - result = result.sumOverDim(2); - result = result.reduceDims(2); - result.setName(ResultName.MATRIX_ABUNDANCE); - - return result; - } - - /** * Utilise pour le calcule en Zone. * * @param N @@ -1620,9 +899,11 @@ * @throws IsisFishException * @throws TopiaException */ - public MatrixND matrixAbundance(MatrixND N, Population pop, TimeStep step, - MatrixND matrixFishingMortality) throws TopiaException, + public MatrixND matrixAbundance(MatrixND N, Population pop, TimeStep step) throws TopiaException, IsisFishException { + + MatrixND matrixFishingMortality = matrixFishingMortality(step, pop); + List<PopulationGroup> groups = pop.getPopulationGroup(); List<Zone> zones = pop.getPopulationZone(); @@ -1690,7 +971,7 @@ * @throws IsisFishException * @throws TopiaException */ - private double survivalRatePerZone(TimeStep step, PopulationGroup group, + protected double survivalRatePerZone(TimeStep step, PopulationGroup group, Zone zone, MatrixND matrixFishingMortality) throws TopiaException, IsisFishException { double F = totalFishingMortality(step, matrixFishingMortality) @@ -1702,27 +983,6 @@ return result; } - /** - * @param step - * @param group - * @param zone - * @return - * @throws IsisFishException - * @throws TopiaException - */ - private double survivalRatePerCell(TimeStep step, PopulationGroup group, - Zone zone, Cell cell, MatrixND matrixFishingMortalityPerCell) - throws TopiaException, IsisFishException { - - double F = totalFishingMortalityPerCell(step, - matrixFishingMortalityPerCell).getValue(group, zone, cell); //totalFishingMortality(step, group, zone); // rem perf: totalFishingMortality a deja ete calcule - double M = group.getNaturalDeathRate(zone) - / (double) Month.NUMBER_OF_MONTH; - double result = (double) Math.exp(-(F + M)); - - return result; - } - /////////////////////////////////////////////////////////////////////////// // // Added: trunk/scripts/SiMatrixEffortByCell.java =================================================================== --- trunk/scripts/SiMatrixEffortByCell.java (rev 0) +++ trunk/scripts/SiMatrixEffortByCell.java 2014-12-18 12:20:31 UTC (rev 350) @@ -0,0 +1,514 @@ +package scripts; + +import static org.nuiton.i18n.I18n.n; + +import fr.ifremer.isisfish.IsisFishException; +import fr.ifremer.isisfish.entities.Cell; +import fr.ifremer.isisfish.entities.Gear; +import fr.ifremer.isisfish.entities.Metier; +import fr.ifremer.isisfish.entities.MetierSeasonInfo; +import fr.ifremer.isisfish.entities.Population; +import fr.ifremer.isisfish.entities.PopulationGroup; +import fr.ifremer.isisfish.entities.PopulationSeasonInfo; +import fr.ifremer.isisfish.entities.Selectivity; +import fr.ifremer.isisfish.entities.Strategy; +import fr.ifremer.isisfish.entities.StrategyMonthInfo; +import fr.ifremer.isisfish.entities.Zone; +import fr.ifremer.isisfish.simulator.SimulationContext; +import fr.ifremer.isisfish.types.Month; +import fr.ifremer.isisfish.types.TimeStep; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.math.matrix.MatrixFactory; +import org.nuiton.math.matrix.MatrixIterator; +import org.nuiton.math.matrix.MatrixND; +import org.nuiton.topia.TopiaException; + +import static org.nuiton.i18n.I18n.n; + +/** + * + * @author poussin + * @version $Revision$ + * + * Last update: $Date$ + * by : $Author$ + */ +public class SiMatrixEffortByCell extends SiMatrix { + + /** to use log facility, just put in your code: log.info(\"...\"); */ + static private Log log = LogFactory.getLog(SiMatrixEffortByCell.class); + + public SiMatrixEffortByCell(SimulationContext context) throws TopiaException { + super(context); + } + + @Override + public void computeMonthExtra(TimeStep step, Population pop, MatrixND N) throws TopiaException, IsisFishException { + MatrixND catchPerStrategyMetPerZoneMet = + matrixCatchPerStrategyMetPerZoneMet(N, pop, step); + resManager.addResult(step, pop, catchPerStrategyMetPerZoneMet); + + if (resManager + .isEnabled(ResultName.MATRIX_CATCH_WEIGHT_PER_STRATEGY_MET_PER_ZONE_MET)) { + MatrixND catchWeightPerStrategyMet = + matrixCatchWeightPerStrategyMetPerZoneMet(step, + pop, catchPerStrategyMetPerZoneMet); + resManager.addResult(step, pop, catchWeightPerStrategyMet); + } + } + + protected MatrixND matrixCatchWeightPerStrategyMetPerZoneMet(TimeStep step, + Population pop, MatrixND matrixCatchPerStrategyMetPerZoneMet) + throws TopiaException, IsisFishException { + + return matrixToWeightMatrix(step, 2, + ResultName.MATRIX_CATCH_WEIGHT_PER_STRATEGY_MET_PER_ZONE_MET, + matrixCatchPerStrategyMetPerZoneMet); + } + + protected MatrixND matrixCatchPerStrategyMetPerZoneMet(MatrixND N, + Population pop, TimeStep step) throws TopiaException, IsisFishException { + + MatrixND matrixFishingMortalityPerCell = matrixFishingMortalityPerCell( + step, pop); + MatrixND matrixCatchRatePerStrategyMetPerCell = matrixCatchRatePerStrategyMetPerCell( + pop, step, matrixFishingMortalityPerCell); + MatrixND matrixCatchPerStrategyMetPerCell = matrixCatchPerStrategyMetPerCell( + N, pop, step, matrixCatchRatePerStrategyMetPerCell); + + List<Strategy> strategies = getStrategies(step); + List<Metier> metiers = getMetiers(step); + List<PopulationGroup> groups = (List<PopulationGroup>)matrixCatchPerStrategyMetPerCell + .getSemantic(2); + List<Zone> zones = getZones(step); + + Set<Cell> cellPops = new HashSet(matrixCatchPerStrategyMetPerCell + .getSemantic(4)); + + MatrixND result = MatrixFactory.getInstance().create( + ResultName.MATRIX_CATCH_PER_STRATEGY_MET_PER_ZONE_MET, + new List[] { strategies, metiers, groups, zones }, + new String[] { n("Strategies"), n("Metiers"), n("Groups"), + n("Zones") }); + + // matrice temporaire ou les zones pops sont sommees + MatrixND tmp = matrixCatchPerStrategyMetPerCell.sumOverDim(3); + tmp = tmp.reduceDims(3); + + for (int s = 0; s < strategies.size(); s++) { + Strategy str = strategies.get(s); + for (int g = 0; g < groups.size(); g++) { + PopulationGroup group = groups.get(g); + // on iter que sur les metiers qui ont une proportion non 0 + // car la matrice pour les autres metiers, contient 0. + List<Metier> metiersNot0 = getMetiers(str, step); + for (int m = 0; m < metiersNot0.size(); m++) { + Metier metier = metiersNot0.get(m); + MetierSeasonInfo infoMet = metier.getMetierSeasonInfo(step + .getMonth()); + Collection<Zone> zoneMet = infoMet.getZone(); + for (Zone z : zoneMet) { + double value = 0; + List<Cell> cells = z.getCell(); + for (int c = 0; c < cells.size(); c++) { + Cell cell = cells.get(c); + if (cellPops.contains(cell)) { + // les cells de la matrice sont les cells des + // zones pops, donc seul les intersections avec + // les cells des metiers sont des cells valides + value += tmp.getValue(str, metier, group, cell); + } + } + result.setValue(str, metier, group, z, value); + } + } + } + } + + return result; + } + + + @Override + public MatrixND matrixCatchPerStrategyMetPerZone(MatrixND N, + Population pop, TimeStep step) throws TopiaException, IsisFishException { + MatrixND matrixFishingMortalityPerCell = matrixFishingMortalityPerCell( + step, pop); + MatrixND matrixCatchRatePerStrategyMetPerCell = matrixCatchRatePerStrategyMetPerCell( + pop, step, matrixFishingMortalityPerCell); + MatrixND matrixCatchPerStrategyMetPerCell = matrixCatchPerStrategyMetPerCell( + N, pop, step, matrixCatchRatePerStrategyMetPerCell); + + // on somme sur les cellules + MatrixND result = matrixCatchPerStrategyMetPerCell.sumOverDim(4); + result = result.reduceDims(4); + result.setName(ResultName.MATRIX_CATCH_PER_STRATEGY_MET_PER_ZONE_POP); + + return result; + } + + + /** + * Utilise pour le calcule en Cell. + * + * @param N + * @param pop + * @param step + * @return + * @throws IsisFishException + * @throws TopiaException + */ + @Override + public MatrixND matrixAbundance(MatrixND N, Population pop, TimeStep step) + throws TopiaException, IsisFishException { + + MatrixND matrixFishingMortalityPerCell = matrixFishingMortalityPerCell( + step, pop); + + MatrixND result = matrixAbundancePerCell(N, pop, step, + matrixFishingMortalityPerCell); + result = result.sumOverDim(2); + result = result.reduceDims(2); + result.setName(ResultName.MATRIX_ABUNDANCE); + + return result; + } + + /** + * @param N + * @param pop + * @param step + * @return + * @throws IsisFishException + * @throws TopiaException + */ + protected MatrixND matrixAbundancePerCell(MatrixND N, Population pop, + TimeStep step, MatrixND matrixFishingMortality) throws TopiaException, + IsisFishException { + + List<PopulationGroup> groups = pop.getPopulationGroup(); + List<Zone> zones = pop.getPopulationZone(); + List<Cell> allCells = getCells(zones); + + MatrixND result = MatrixFactory.getInstance().create( + ResultName.MATRIX_ABUNDANCE + "_PER_CELL", + new List[] { groups, zones, allCells }, + new String[] { n("Groups"), n("Zones"), n("Cells") }); + + for (int g = 0; g < groups.size(); g++) { + PopulationGroup group = groups.get(g); + for (int z = 0; z < zones.size(); z++) { + Zone zone = zones.get(z); + List<Cell> cells = zone.getCell(); + for (int c = 0; c < cells.size(); c++) { + Cell cell = cells.get(c); + double value = survivalRatePerCell(step, group, zone, cell, + matrixFishingMortality); + double n = N.getValue(g, z) / zone.sizeCell(); + value *= n; + result.setValue(g, z, c, value); + } + } + } + + return result; + } + + /** + * @param step + * @param group + * @param zone + * @return + * @throws IsisFishException + * @throws TopiaException + */ + protected double survivalRatePerCell(TimeStep step, PopulationGroup group, + Zone zone, Cell cell, MatrixND matrixFishingMortalityPerCell) + throws TopiaException, IsisFishException { + + double F = totalFishingMortalityPerCell(step, + matrixFishingMortalityPerCell).getValue(group, zone, cell); //totalFishingMortality(step, group, zone); // rem perf: totalFishingMortality a deja ete calcule + double M = group.getNaturalDeathRate(zone) + / (double) Month.NUMBER_OF_MONTH; + double result = (double) Math.exp(-(F + M)); + + return result; + } + + /** + * Matrice fishing mortality dim [ Strategy x Metier x Classe x zonePop x + * cellPop] + * + * Nouvelle implantation suite a demande Steph et Sigrid: 20080208 (NouvellesEquationsfev2008ParCelluleAvecAlgo.odt) + * + * @param pop + * @param step + * @return + * @throws TopiaException + * @throws IsisFishException + */ + protected MatrixND matrixFishingMortalityPerCell(TimeStep step, Population pop) + throws TopiaException, IsisFishException { + List<Strategy> strategies = getStrategies(step); + List<Metier> metiers = getMetiers(step); + List<PopulationGroup> groups = pop.getPopulationGroup(); + List<Zone> zones = pop.getPopulationZone(); + List<Cell> cells = getCells(zones); + + // default value in matrix is 0 + MatrixND result = MatrixFactory.getInstance().create( + "matrixFishingMortalityPerCell", + new List[] { strategies, metiers, groups, zones, cells }, + new String[] { n("Strategies"), n("Metiers"), n("Groups"), + n("Zones"), n("Cells") }); + + Month month = step.getMonth(); + PopulationSeasonInfo infoPop = pop.getPopulationSeasonInfo(month); + + for (Strategy str : strategies) { + StrategyMonthInfo smi = str.getStrategyMonthInfo(month); + double nbTrip = smi.getNumberOfTrips(); + double propSetOfVessels = str.getProportionSetOfVessels(); + int nbOfVessels = str.getSetOfVessels().getNumberOfVessels(); + + // dans le calcul de l'effort si une de ces donnees est 0 + // alors le result dans la matrice est 0 + // donc autant ne pas faire tous les autres calculs couteux + if (nbTrip * propSetOfVessels * nbOfVessels != 0) { + for (PopulationGroup group : groups) { + + // getCapturability is check matrix validity and create matrix if needed and get one value in + double capturability = infoPop.getCapturability(group); + if (capturability != 0) { // check 0, this prevent next call, for default value + + // et de la meme facon si la proportion est 0 alors + // le resultat dans la matrice sera 0, donc on boucle que + // sur ceux qui ont une proportion + List<Metier> metiersNot0 = getMetiers(str, step); + for (Metier metier : metiersNot0) { + + MetierSeasonInfo infoMet = metier + .getMetierSeasonInfo(month); + // getTargetFactor seem to be simple + double ciblage = infoMet.getTargetFactor(group); + + if (ciblage != 0) { // check 0, this prevent next call, for default value + + Gear gear = metier.getGear(); + Selectivity selectivity = gear.getPopulationSelectivity(pop); + if (selectivity != null) { + + // getCoefficient is equation evaluation + double coeff = selectivity.getCoefficient(pop,group, metier); + if (coeff != 0) { // check 0, this prevent next call, for default value + + // l'effort d'une cellule du metier + double effort = effortPerStrategyPerCell( + str, metier, infoMet, step); + if (effort > 0) { // put value only if <> 0 + for (Zone zone : zones) { + Set<Cell> cellPops = new HashSet<Cell>(zone.getCell()); + for (Cell cellMet : infoMet.getCells()) { + if (cellPops.contains(cellMet)) { + // Reduction d'effort (generic): + double effortReduction = getEffortReduction(step, gear, cellMet); + + double value = (1.0 - effortReduction) * coeff * capturability * ciblage * effort; + result.setValue( + new Object[] { str, + metier, + group, + zone, + cellMet }, + value); + } + } + } + } + } + } + } + } + } + } + } + } + return result; + } + + protected double getEffortReduction(TimeStep step, Gear gear, Cell cellMet) { + return 0.0; + } + + /** + * Matrice des captures en nombre dim [ Strategy x Metier x Classe x zonePop ] + * + * @param N + * l'abondance sous forme de matrice 2D [class x zone] + * @param pop + * @param step + * @return + * @throws TopiaException + * @throws IsisFishException + */ + protected MatrixND matrixCatchPerStrategyMetPerCell(MatrixND N, + Population pop, TimeStep step, + MatrixND matrixCatchRatePerStrategyMetPerCell) + throws TopiaException, IsisFishException { + int groupDim = 2; + int zoneDim = 3; + + int NGroupDim = 0; + int NZoneDim = 1; + + MatrixND result = matrixCatchRatePerStrategyMetPerCell.copy(); + result.setName("matrixCatchPerStrategyMetPerCell"); + + List<PopulationGroup> groups = (List<PopulationGroup>)result.getSemantic(groupDim); + List<Zone> zones = (List<Zone>)result.getSemantic(zoneDim); + + double[] sizeCell = new double[zones.size()]; + int cpt = 0; + for (Zone z : zones) { + sizeCell[cpt++] = z.sizeCell(); + } + + // on suppose que les semantics de N et matrixCatchRatePerStrategyMetPerCell + // pour les PopulationGroup et Zone sont les memes + // mais on verifie tout de meme + if (groups.equals(N.getSemantic(NGroupDim)) && + zones.equals(N.getSemantic(NZoneDim))) { + + // on place les effectifs dans un tableau pour un acces rapide + // on divise directement par le nombre de cell de la zone pour eviter + // de le faire beaucoup plus souvent dans l'autre boucle + double[][] NArray = new double[groups.size()][zones.size()]; + for (MatrixIterator i=N.iteratorNotZero(); i.next();) { + int[] pos = i.getCoordinates(); + double val = i.getValue(); + + NArray[pos[NGroupDim]][pos[NZoneDim]] = val / sizeCell[pos[NZoneDim]]; + } + + // calcul de la matrice resultat + for (MatrixIterator i=result.iteratorNotZero(); i.next();) { + int[] pos = i.getCoordinates(); + double val = i.getValue(); + val *= NArray[pos[groupDim]][pos[zoneDim]]; + i.setValue(val); + } + + } else { + // N et matrixCatchRatePerStrategyMetPerCell non pas les meme semantics + // on fait moins rapide + for (MatrixIterator i=result.iteratorNotZero(); i.next();) { + int[] pos = i.getCoordinates(); + Object[] sems = i.getSemanticsCoordinates(); + PopulationGroup group = (PopulationGroup)sems[groupDim]; + Zone zone = (Zone)sems[zoneDim]; + double val = i.getValue(); + val *= N.getValue(group, zone) / sizeCell[pos[zoneDim]]; + i.setValue(val); + } + } + + return result; + } + + /** + * Matrice des captures en poids dim [ Strategy x Metier x Classe x zonePop ] + * + * @param pop + * @param step + * @return + * @throws TopiaException + * @throws IsisFishException + */ + protected MatrixND matrixCatchRatePerStrategyMetPerCell(Population pop, + TimeStep step, MatrixND matrixFishingMortalityPerCell) + throws TopiaException, IsisFishException { + + MatrixND totalFishingMortalityPerCell = totalFishingMortalityPerCell( + step, matrixFishingMortalityPerCell); + + MatrixND result = matrixFishingMortalityPerCell.copy(); + result.setName("matrixCatchRatePerStrategyMetPerCell"); + + for (MatrixIterator i=result.iteratorNotZero(); i.next();) { + Object[] sems = i.getSemanticsCoordinates(); + + PopulationGroup group = (PopulationGroup)sems[2]; + Zone zone = (Zone)sems[3]; + Cell cell = (Cell)sems[4]; + + double totalFishingMortality = totalFishingMortalityPerCell.getValue( + group, zone, cell); + + if (totalFishingMortality != 0) { + + double fishingMortalityPerCell = i.getValue(); + double totalCatchRatePerCell = totalCatchRatePerCell(step, group, zone, + totalFishingMortality); + + double value = fishingMortalityPerCell / totalFishingMortality + * totalCatchRatePerCell; + + i.setValue(value); + } + } + + return result; + } + + /** + * @param step + * @param group + * @param zone + * @param totalFishingMortality + * @return + * @throws TopiaException + */ + private double totalCatchRatePerCell(TimeStep step, PopulationGroup group, + Zone zone, double totalFishingMortalityPerCell) + throws TopiaException { + double M = group.getNaturalDeathRate(zone) / Month.NUMBER_OF_MONTH; + if (M == 0) { + // normalement il devrait y avoir de la mortalite naturelle + if (log.isWarnEnabled()) { + log.warn("Pas de mortalite naturelle pour: " + group); + } + } + double F = totalFishingMortalityPerCell; + + double result = 0; + if (M != 0 || F != 0) { + result = F / (F + M) * (1 - Math.exp(-(F + M))); + } + + return result; + } + + /** + * Returne une matrice de mortalite group x zone, donc somme sur str et + * metier + * + * @param step + * @param matrixFishingMortality + * @param group + * @param zone + * @return + */ + protected MatrixND totalFishingMortalityPerCell(TimeStep step, + MatrixND matrixFishingMortalityPerCell) { + MatrixND result = matrixFishingMortalityPerCell.sumOverDim(0); + result = result.sumOverDim(1); + result = result.reduceDims(0, 1); + return result; + } + +} Property changes on: trunk/scripts/SiMatrixEffortByCell.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Modified: trunk/simulators/DefaultSimulator.java =================================================================== --- trunk/simulators/DefaultSimulator.java 2014-12-03 15:48:52 UTC (rev 349) +++ trunk/simulators/DefaultSimulator.java 2014-12-18 12:20:31 UTC (rev 350) @@ -22,8 +22,8 @@ */ package simulators; -import fr.ifremer.isisfish.IsisFish; import fr.ifremer.isisfish.IsisFishException; +import fr.ifremer.isisfish.datastore.StorageException; import fr.ifremer.isisfish.entities.Metier; import fr.ifremer.isisfish.entities.Population; import fr.ifremer.isisfish.entities.PopulationSeasonInfo; @@ -70,6 +70,27 @@ static private Log log = LogFactory.getLog(DefaultSimulator.class); /** + * When you create new simulator with specific SiMatrix you must + * overwrite this method to return you SiMatrix version + * @param context + * @return + */ + protected SiMatrix newSiMatrix(SimulationContext context) throws TopiaException { + return new SiMatrix(context); + } + + /** + * When you create new simulator with specific GravityModel you must + * overwrite this method to return you GravityModel version + * @param context + * @param siMatrix + * @return + */ + protected GravityModel newGravityModel(SimulationContext context, SiMatrix siMatrix) throws TopiaException, StorageException { + return new GravityModel(context, siMatrix); + } + + /** * Called by isis don't modify signature */ public void simulate(SimulationContext context) throws Exception { @@ -83,8 +104,8 @@ ResultManager resManager = context.getResultManager(); TopiaContext db = context.getDB(); - SiMatrix siMatrix = SiMatrix.getSiMatrix(context); - GravityModel gravityModel = new GravityModel(context, siMatrix); + SiMatrix siMatrix = newSiMatrix(context); + GravityModel gravityModel = newGravityModel(context, siMatrix); PopulationMonitor populationMonitor = context.getPopulationMonitor(); MetierMonitor metierMonitor = context.getMetierMonitor(); @@ -449,30 +470,10 @@ // this matrix is necessary for PopulationMonitor.holdCatch (reused in rule) MatrixND catchPerStrategyMetPerZonePop; - if (isEffortByCell(context)) { - abundance = siMatrix.matrixAbundance(N, pop, step); - catchPerStrategyMetPerZonePop = siMatrix - .matrixCatchPerStrategyMetPerZonePop(N, pop, step); - } else { // en zone - MatrixND matrixFishingMortality = siMatrix - .matrixFishingMortality(step, pop); - resManager.addResult(step, pop, matrixFishingMortality); + abundance = siMatrix.matrixAbundance(N, pop, step); + catchPerStrategyMetPerZonePop = siMatrix + .matrixCatchPerStrategyMetPerZone(N, pop, step); - abundance = siMatrix.matrixAbundance(N, pop, step, - matrixFishingMortality); - - // this matrix is necessary for matrixCatchPerStrategyMet - MatrixND catchRatePerStrategyMet = siMatrix - .matrixCatchRatePerStrategyMetPerZone(pop, step, - matrixFishingMortality); - resManager.addResult(step, pop, catchRatePerStrategyMet); - - catchPerStrategyMetPerZonePop = siMatrix - .matrixCatchPerStrategyMetPerZone(N, pop, step, - catchRatePerStrategyMet); - - } - popMon.holdCatch(pop, catchPerStrategyMetPerZonePop); resManager.addResult(step, pop, catchPerStrategyMetPerZonePop); @@ -484,35 +485,24 @@ resManager.addResult(step, pop, catchWeightPerStrategyMet); } - if (isEffortByCell(context)) { - MatrixND catchPerStrategyMetPerZoneMet = siMatrix - .matrixCatchPerStrategyMetPerZoneMet(N, pop, step); - resManager.addResult(step, pop, catchPerStrategyMetPerZoneMet); - - if (resManager - .isEnabled(ResultName.MATRIX_CATCH_WEIGHT_PER_STRATEGY_MET_PER_ZONE_MET)) { - MatrixND catchWeightPerStrategyMet = siMatrix - .matrixCatchWeightPerStrategyMetPerZoneMet(step, - pop, catchPerStrategyMetPerZoneMet); - resManager.addResult(step, pop, catchWeightPerStrategyMet); - } - } - if (resManager.isEnabled(ResultName.MATRIX_FISHING_MORTALITY_PER_GROUP) || resManager.isEnabled(ResultName.MATRIX_TOTAL_FISHING_MORTALITY)) { - MatrixND fishingMortalityPerGroup = siMatrix.fishingMortalityPerGroup(step, - pop, context.getResultManager()); + MatrixND fishingMortalityPerGroup = siMatrix.fishingMortalityPerGroup( + step, pop, context.getResultManager()); if (resManager.isEnabled(ResultName.MATRIX_FISHING_MORTALITY_PER_GROUP)) { resManager.addResult(step, pop, fishingMortalityPerGroup); } if (resManager.isEnabled(ResultName.MATRIX_TOTAL_FISHING_MORTALITY)) { - MatrixND totalFishingMortality = siMatrix.totalFishingMortality(step, pop, fishingMortalityPerGroup); + MatrixND totalFishingMortality = siMatrix.totalFishingMortality( + step, pop, fishingMortalityPerGroup); resManager.addResult(step, pop, totalFishingMortality); } } + siMatrix.computeMonthExtra(step, pop, N); + } else { // no strategies // compute only if fishing mortality =0 to apply Natural Mortality abundance = siMatrix.matrixAbundanceSsF(N, pop, step); Modified: trunk/simulators/SimulatorEffortByCell.java =================================================================== --- trunk/simulators/SimulatorEffortByCell.java 2014-12-03 15:48:52 UTC (rev 349) +++ trunk/simulators/SimulatorEffortByCell.java 2014-12-18 12:20:31 UTC (rev 350) @@ -26,29 +26,25 @@ import org.apache.commons.logging.LogFactory; import fr.ifremer.isisfish.simulator.SimulationContext; -import fr.ifremer.isisfish.simulator.SimulationParameter; -import fr.ifremer.isisfish.simulator.Simulator; +import scripts.SiMatrix; +import scripts.SiMatrixEffortByCell; +import org.nuiton.topia.TopiaException; + /** * Simulateur qui fixe la valeur de effortByCell a true pour force * l'utilisation des calculs par cellule plutot que par zone. * * @author poussin */ -public class SimulatorEffortByCell implements Simulator { +public class SimulatorEffortByCell extends DefaultSimulator { /** to use log facility, just put in your code: log.info("..."); */ static private Log log = LogFactory.getLog(SimulatorEffortByCell.class); - /** - * Called by isis don't modify signature - */ - public void simulate(SimulationContext context) throws Exception { - SimulationParameter param = context.getSimulationStorage().getParameter(); - param.getTagValue().put("effortByCell", "true"); - - DefaultSimulator simulator = new DefaultSimulator(); - simulator.simulate(context); + @Override + protected SiMatrix newSiMatrix(SimulationContext context) throws TopiaException { + return new SiMatrixEffortByCell(context); } }
participants (1)
-
bpoussin@users.forge.codelutin.com