Skip to content

Commit

Permalink
prevent DrawFromLocationValuePlannedActionGenerator from returning il…
Browse files Browse the repository at this point in the history
…legal actions
  • Loading branch information
nicolaspayette committed Apr 10, 2024
1 parent f385504 commit 9f151e9
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
/*
* POSEIDON, an agent-based model of fisheries
* Copyright (c) 2024-2024 CoHESyS Lab [email protected]
*
* 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 <http://www.gnu.org/licenses/>.
*/
package uk.ac.ox.oxfish.fisher.purseseiner.planner;

import com.google.common.base.Preconditions;
import ec.util.MersenneTwisterFast;
import uk.ac.ox.oxfish.biology.GlobalBiology;
import uk.ac.ox.oxfish.biology.LocalBiology;
import uk.ac.ox.oxfish.fisher.Fisher;
import uk.ac.ox.oxfish.fisher.purseseiner.actions.CatchMaker;
import uk.ac.ox.oxfish.fisher.purseseiner.samplers.CatchSampler;
import uk.ac.ox.oxfish.fisher.purseseiner.strategies.fields.LocationValues;
Expand All @@ -26,6 +42,7 @@ public abstract class CatchSamplerPlannedActionGenerator<PA extends PlannedActio
private final Class<B> localBiologyClass;

CatchSamplerPlannedActionGenerator(
final Fisher fisher,
final LocationValues originalLocationValues,
final NauticalMap map,
final MersenneTwisterFast random,
Expand All @@ -35,7 +52,7 @@ public abstract class CatchSamplerPlannedActionGenerator<PA extends PlannedActio
final GlobalBiology biology,
final Class<B> localBiologyClass
) {
super(originalLocationValues, map, random);
super(fisher, originalLocationValues, map, random);
this.additionalWaitTime = additionalWaitTime;
this.howMuchWeCanFishOutGenerator = howMuchWeCanFishOutGenerator;
this.catchMaker = catchMaker;
Expand All @@ -44,7 +61,7 @@ public abstract class CatchSamplerPlannedActionGenerator<PA extends PlannedActio
}

@Override
public PA drawNewPlannedAction() {
protected PA locationToPlannedAction(final SeaTile location) {
Preconditions.checkState(isReady(), "Did not start the deploy generator yet!");
return
turnSeaTilePickedIntoAction(
Expand Down Expand Up @@ -72,6 +89,7 @@ public static class DolphinActionGenerator<B extends LocalBiology>
private final int rangeInSeaTiles;

DolphinActionGenerator(
final Fisher fisher,
final LocationValues originalLocationValues,
final NauticalMap map,
final MersenneTwisterFast random,
Expand All @@ -83,6 +101,7 @@ public static class DolphinActionGenerator<B extends LocalBiology>
final int rangeInSeaTiles
) {
super(
fisher,
originalLocationValues,
map,
random,
Expand Down Expand Up @@ -124,6 +143,7 @@ public static class NonAssociatedActionGenerator<B extends LocalBiology>
private final int rangeInSeaTiles;

NonAssociatedActionGenerator(
final Fisher fisher,
final LocationValues originalLocationValues,
final NauticalMap map,
final MersenneTwisterFast random,
Expand All @@ -136,6 +156,7 @@ public static class NonAssociatedActionGenerator<B extends LocalBiology>
final int rangeInSeaTiles
) {
super(
fisher,
originalLocationValues,
map,
random,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class DeploymentFromLocationValuePlanningModule

@SuppressWarnings("unchecked")
public DeploymentFromLocationValuePlanningModule(
final Fisher fisher,
final LocationValues locationValues,
final NauticalMap map,
final MersenneTwisterFast random,
Expand All @@ -41,6 +42,7 @@ public DeploymentFromLocationValuePlanningModule(
super(
locationValues,
new DeploymentPlannedActionGenerator(
fisher,
locationValues,
map,
random,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
/*
* POSEIDON, an agent-based model of fisheries
* Copyright (c) 2024-2024 CoHESyS Lab [email protected]
*
* 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 <http://www.gnu.org/licenses/>.
*/
package uk.ac.ox.oxfish.fisher.purseseiner.planner;

import com.google.common.base.Preconditions;
import ec.util.MersenneTwisterFast;
import uk.ac.ox.oxfish.fisher.Fisher;
import uk.ac.ox.oxfish.fisher.purseseiner.strategies.fields.LocationValues;
import uk.ac.ox.oxfish.geography.NauticalMap;
import uk.ac.ox.oxfish.geography.SeaTile;

/**
* this object exists to generate a new deployment action (probably to add to a plan)
Expand All @@ -17,25 +34,28 @@ public class DeploymentPlannedActionGenerator
private final double delayInHoursAfterADeployment;

public DeploymentPlannedActionGenerator(
final Fisher fisher,
final LocationValues originalLocationValues,
final NauticalMap map,
final MersenneTwisterFast random
) {
this(originalLocationValues, map, random, 0);
this(fisher, originalLocationValues, map, random, 0);
}

DeploymentPlannedActionGenerator(
final Fisher fisher,
final LocationValues originalLocationValues,
final NauticalMap map,
final MersenneTwisterFast random,
final double delayInHoursAfterADeployment
) {
super(originalLocationValues, map, random);
super(fisher, originalLocationValues, map, random);
this.delayInHoursAfterADeployment = delayInHoursAfterADeployment;

}

public PlannedAction.Deploy drawNewPlannedAction() {
@Override
protected PlannedAction.Deploy locationToPlannedAction(final SeaTile location) {
Preconditions.checkState(isReady(), "Did not start the deploy generator yet!");
return new PlannedAction.Deploy(
drawNewLocation(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import ec.util.MersenneTwisterFast;
import uk.ac.ox.oxfish.biology.GlobalBiology;
import uk.ac.ox.oxfish.biology.LocalBiology;
import uk.ac.ox.oxfish.fisher.Fisher;
import uk.ac.ox.oxfish.fisher.purseseiner.actions.ActionClass;
import uk.ac.ox.oxfish.fisher.purseseiner.actions.CatchMaker;
import uk.ac.ox.oxfish.fisher.purseseiner.samplers.CatchSampler;
Expand All @@ -27,9 +28,10 @@
import static uk.ac.ox.oxfish.fisher.purseseiner.actions.ActionClass.DEL;

public class DolphinSetFromLocationValuePlanningModule<B extends LocalBiology>
extends LocationValuePlanningModule<B> {
extends LocationValuePlanningModule {

DolphinSetFromLocationValuePlanningModule(
final Fisher fisher,
final LocationValues locationValues,
final NauticalMap map,
final MersenneTwisterFast random,
Expand All @@ -43,6 +45,7 @@ public class DolphinSetFromLocationValuePlanningModule<B extends LocalBiology>
super(
locationValues,
new CatchSamplerPlannedActionGenerator.DolphinActionGenerator<>(
fisher,
locationValues,
map,
random,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import ec.util.MersenneTwisterFast;
import org.apache.commons.math3.distribution.EnumeratedDistribution;
import org.apache.commons.math3.util.Pair;
import uk.ac.ox.oxfish.fisher.Fisher;
import uk.ac.ox.oxfish.fisher.purseseiner.strategies.fields.LocationValues;
import uk.ac.ox.oxfish.geography.NauticalMap;
import uk.ac.ox.oxfish.geography.SeaTile;
Expand All @@ -26,7 +27,8 @@

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

/**
* this object exists to draw a location given a locationvalues object and then turn that location into a planned action
Expand All @@ -36,6 +38,7 @@ public abstract class DrawFromLocationValuePlannedActionGenerator<PA extends Pla
implements Observer<LocationValues> {

protected final NauticalMap map;
private final Fisher fisher;
/**
* the rng to use (compatible with Apache)
*/
Expand All @@ -48,10 +51,12 @@ public abstract class DrawFromLocationValuePlannedActionGenerator<PA extends Pla
private EnumeratedDistribution<SeaTile> seaTilePicker;

DrawFromLocationValuePlannedActionGenerator(
final Fisher fisher,
final LocationValues originalLocationValues,
final NauticalMap map,
final MersenneTwisterFast random
) {
this.fisher = fisher;
this.originalLocationValues = originalLocationValues;
this.map = map;
this.localRng = new MTFApache(random);
Expand All @@ -71,7 +76,7 @@ private void preparePicker(final LocationValues locationValues) {
.stream()
.map(entry -> new Pair<>(map.getSeaTile(entry.getKey()), entry.getValue()))
// avoid areas where values have turned negative
.filter(seaTileDoublePair -> seaTileDoublePair.getValue() >= 0).collect(Collectors.toList());
.filter(seaTileDoublePair -> seaTileDoublePair.getValue() >= 0).collect(toList());
if (valuePairs.isEmpty())
return;

Expand All @@ -95,7 +100,31 @@ private void preparePicker(final LocationValues locationValues) {
}
}

abstract public PA drawNewPlannedAction();
/**
* Draw location values until we find one that can generate a legal action, or we run out of possibilities (in which
* case we return null).
*/
public PA drawNewPlannedAction() {
PA plannedAction = locationToPlannedAction(drawNewLocation());
while (!plannedAction.isAllowedNow(fisher)) {
final SeaTile location = plannedAction.getLocation();
final List<Pair<SeaTile, Double>> newPmf =
seaTilePicker
.getPmf()
.stream()
.filter(pair -> pair.getKey() != location)
.collect(toList());
if (newPmf.isEmpty()) {
plannedAction = null;
break;
}
seaTilePicker = new EnumeratedDistribution<>(localRng, newPmf);
plannedAction = locationToPlannedAction(drawNewLocation());
}
return plannedAction;
}

protected abstract PA locationToPlannedAction(SeaTile location);

SeaTile drawNewLocation() {
if (originalLocationValues.getValues().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@

import ec.util.MersenneTwisterFast;
import uk.ac.ox.oxfish.biology.LocalBiology;
import uk.ac.ox.oxfish.fisher.Fisher;
import uk.ac.ox.oxfish.fisher.purseseiner.actions.ActionClass;
import uk.ac.ox.oxfish.fisher.purseseiner.strategies.fields.LocationValues;
import uk.ac.ox.oxfish.geography.NauticalMap;

import static uk.ac.ox.oxfish.fisher.purseseiner.actions.ActionClass.OFS;

public class FadStealingFromLocationValuePlanningModule<B extends LocalBiology>
extends LocationValuePlanningModule<B> {
extends LocationValuePlanningModule {

FadStealingFromLocationValuePlanningModule(
final Fisher fisher,
final LocationValues locationValues,
final NauticalMap map,
final MersenneTwisterFast random,
Expand All @@ -38,6 +40,7 @@ public class FadStealingFromLocationValuePlanningModule<B extends LocalBiology>
this(
locationValues,
new FadStealingPlannedActionGenerator(
fisher,
locationValues,
map,
random,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
/*
* POSEIDON, an agent-based model of fisheries
* Copyright (c) 2024-2024 CoHESyS Lab [email protected]
*
* 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 <http://www.gnu.org/licenses/>.
*/
package uk.ac.ox.oxfish.fisher.purseseiner.planner;

import ec.util.MersenneTwisterFast;
import uk.ac.ox.oxfish.fisher.Fisher;
import uk.ac.ox.oxfish.fisher.purseseiner.strategies.fields.LocationValues;
import uk.ac.ox.oxfish.geography.NauticalMap;
import uk.ac.ox.oxfish.geography.SeaTile;

public class FadStealingPlannedActionGenerator extends
DrawFromLocationValuePlannedActionGenerator<PlannedAction.OpportunisticFadSet> {
Expand All @@ -22,6 +39,7 @@ public class FadStealingPlannedActionGenerator extends
private final double probabilityOfFindingOtherFads;

FadStealingPlannedActionGenerator(
final Fisher fisher,
final LocationValues originalLocationValues,
final NauticalMap map,
final MersenneTwisterFast random,
Expand All @@ -30,15 +48,15 @@ public class FadStealingPlannedActionGenerator extends
final double minimumFadValueToSteal,
final double probabilityOfFindingOtherFads
) {
super(originalLocationValues, map, random);
super(fisher, originalLocationValues, map, random);
this.hoursItTakesToSet = hoursItTakesToSet;
this.hoursWastedIfNoFadAround = hoursWastedIfNoFadAround;
this.minimumFadValueToSteal = minimumFadValueToSteal;
this.probabilityOfFindingOtherFads = probabilityOfFindingOtherFads;
}

@Override
public PlannedAction.OpportunisticFadSet drawNewPlannedAction() {
protected PlannedAction.OpportunisticFadSet locationToPlannedAction(final SeaTile location) {
return new PlannedAction.OpportunisticFadSet(
drawNewLocation(),
hoursItTakesToSet,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package uk.ac.ox.oxfish.fisher.purseseiner.planner;

import com.google.common.base.Preconditions;
import uk.ac.ox.oxfish.biology.LocalBiology;
import uk.ac.ox.oxfish.fisher.Fisher;
import uk.ac.ox.oxfish.fisher.purseseiner.strategies.fields.LocationValues;
import uk.ac.ox.oxfish.model.FishState;
Expand All @@ -26,8 +25,7 @@
* DrawFromLocationValuePlannedActionGenerator but need to take care into starting the location values with the right
* fisher
*/
public abstract class LocationValuePlanningModule<B extends LocalBiology>
implements PlanningModule {
public abstract class LocationValuePlanningModule implements PlanningModule {

final private LocationValues locationValues;
final private DrawFromLocationValuePlannedActionGenerator<? extends PlannedAction> generator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import ec.util.MersenneTwisterFast;
import uk.ac.ox.oxfish.biology.GlobalBiology;
import uk.ac.ox.oxfish.biology.LocalBiology;
import uk.ac.ox.oxfish.fisher.Fisher;
import uk.ac.ox.oxfish.fisher.purseseiner.actions.ActionClass;
import uk.ac.ox.oxfish.fisher.purseseiner.actions.CatchMaker;
import uk.ac.ox.oxfish.fisher.purseseiner.samplers.CatchSampler;
Expand All @@ -27,9 +28,10 @@
import static uk.ac.ox.oxfish.fisher.purseseiner.actions.ActionClass.NOA;

public class NonAssociatedSetFromLocationValuePlanningModule<B extends LocalBiology>
extends LocationValuePlanningModule<B> {
extends LocationValuePlanningModule {

NonAssociatedSetFromLocationValuePlanningModule(
final Fisher fisher,
final LocationValues locationValues,
final NauticalMap map,
final MersenneTwisterFast random,
Expand All @@ -44,6 +46,7 @@ public class NonAssociatedSetFromLocationValuePlanningModule<B extends LocalBiol
this(
locationValues,
new CatchSamplerPlannedActionGenerator.NonAssociatedActionGenerator<B>(
fisher,
locationValues,
map,
random,
Expand Down
Loading

0 comments on commit 9f151e9

Please sign in to comment.