Skip to content

Commit

Permalink
use action override in EPO policy runs
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaspayette committed Mar 14, 2024
1 parent b8b671b commit 0a69cf6
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ public static double cheapestInsert(

}

public List<ActionType> getActionPreferenceOverrides() {
return actionPreferenceOverrides;
public Map<ActionType, PlanningModule> getPlanModules() {
return planModules;
}

public void setActionPreferenceOverrides(final Collection<ActionType> actionPreferenceOverrides) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
/*
* POSEIDON, an agent-based model of fisheries
* Copyright (C) 2022 CoHESyS Lab [email protected]
* POSEIDON, an agent-based model of fisheries
* Copyright (C) 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/>.
* 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;
Expand Down Expand Up @@ -70,7 +66,6 @@ public class PlannedStrategy implements DestinationStrategy, FishingStrategy {
// these get activated when we are at location and are performing whatever we said we were going to perform
private Action[] actionQueueInProgress;
private int actionQueueIndex = -1;

public PlannedStrategy(
final DrawThenCheapestInsertionPlanner planner,
final double planningHorizonInHours,
Expand All @@ -81,6 +76,10 @@ public PlannedStrategy(
this.minimumValueOfSetOnOwnFad = minimumValueOfSetOnOwnFad;
}

public DrawThenCheapestInsertionPlanner getPlanner() {
return planner;
}

@Override
public void start(
final FishState model,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* POSEIDON, an agent-based model of fisheries
* Copyright (C) 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;
Expand Down Expand Up @@ -195,6 +210,10 @@ public PlannedStrategyProxy(
this.noaSetsRangeInSeaTiles = noaSetsRangeInSeaTiles;
}

public PlannedStrategy getDelegate() {
return delegate;
}

@SuppressWarnings("rawtypes")
@Override
public void start(
Expand Down
2 changes: 1 addition & 1 deletion epo/epo_inputs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,30 @@
* POSEIDON, an agent-based model of fisheries
* Copyright (C) 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 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.
* 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/>.
* 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.poseidon.epo.policies;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import uk.ac.ox.oxfish.experiments.tuna.Policy;
import uk.ac.ox.oxfish.fisher.Fisher;
import uk.ac.ox.oxfish.fisher.purseseiner.planner.ActionType;
import uk.ac.ox.oxfish.fisher.purseseiner.planner.PlannedStrategy;
import uk.ac.ox.oxfish.fisher.purseseiner.planner.PlannedStrategyProxy;
import uk.ac.ox.oxfish.fisher.purseseiner.regulations.ActiveFadLimitsFactory;
import uk.ac.ox.oxfish.model.FishState;
import uk.ac.ox.oxfish.model.StepOrder;
import uk.ac.ox.poseidon.epo.scenarios.EpoScenario;
import uk.ac.ox.poseidon.regulations.core.NamedRegulationsFactory;

Expand All @@ -37,14 +42,18 @@ public class ActiveFadLimitsPolicies extends PolicySupplier {
private final int referenceYear;
private final List<Double> proportionsOfCurrentLimits;

private final boolean addActionOverride;

ActiveFadLimitsPolicies(
final List<Integer> yearsActive,
final int referenceYear,
final List<Double> proportionsOfCurrentLimits
final List<Double> proportionsOfCurrentLimits,
final boolean addActionOverride
) {
super(yearsActive);
this.referenceYear = referenceYear;
this.proportionsOfCurrentLimits = proportionsOfCurrentLimits;
this.addActionOverride = addActionOverride;
}

@Override
Expand All @@ -57,27 +66,71 @@ public List<Policy<EpoScenario<?>>> get() {
"%02d%% of regular active FAD limits",
(int) (proportion * 100)
),
scenario ->
((NamedRegulationsFactory) scenario.getRegulations()).modify(
"Active-FAD limits",
() -> {
final ImmutableMap<String, Integer> newLimits = ACTIVE_FAD_LIMITS
.get(referenceYear)
.entrySet()
.stream()
.collect(toImmutableMap(
Entry::getKey,
entry -> (int) (entry.getValue() * proportion)
));
final ImmutableMap.Builder<Integer, Map<String, Integer>> builder =
ImmutableMap.<Integer, Map<String, Integer>>builder()
.putAll(ACTIVE_FAD_LIMITS);
getYearsActive().forEach(year -> builder.put(year, newLimits));
return new ActiveFadLimitsFactory(builder.buildKeepingLast());
}
)
scenario -> {
modifyActiveFadsLimit(proportion, scenario);
if (addActionOverride) {
addActionOverride(scenario);
}
}

)
)
.collect(toImmutableList());
}

private void modifyActiveFadsLimit(
final Double proportion,
final EpoScenario<?> scenario
) {
((NamedRegulationsFactory) scenario.getRegulations()).modify(
"Active-FAD limits",
() -> {
final ImmutableMap<String, Integer> newLimits = ACTIVE_FAD_LIMITS
.get(referenceYear)
.entrySet()
.stream()
.collect(toImmutableMap(
Entry::getKey,
entry -> (int) (entry.getValue() * proportion)
));
final ImmutableMap.Builder<Integer, Map<String, Integer>> builder =
ImmutableMap.<Integer, Map<String, Integer>>builder()
.putAll(ACTIVE_FAD_LIMITS);
getYearsActive().forEach(year -> builder.put(year, newLimits));
return new ActiveFadLimitsFactory(builder.buildKeepingLast());
}
);
}

private void addActionOverride(
final EpoScenario<?> scenario
) {
scenario.getAdditionalStartables().put(
"FAD deployment action override",
// Those lambdas are crazy, but we:
fishState1 -> // create an algorithm factory
fishState2 -> // that creates a startable
fishState2.scheduleOnceInXDays(
simState -> { // that schedules a steppable...
final ImmutableList<ActionType> overrides =
ImmutableList.of(ActionType.DeploymentAction);
((FishState) simState)
.getFishers()
.stream()
.map(Fisher::getDestinationStrategy)
.filter(PlannedStrategyProxy.class::isInstance)
.map(PlannedStrategyProxy.class::cast)
.map(PlannedStrategyProxy::getDelegate)
.map(PlannedStrategy::getPlanner)
// some vessels (mostly dolphin-setters) have zero empirical deployments
// and thus no "plan module" with deployment location preferences, so
// we exclude those from the "deploy as much as possible" variation
.filter(planner -> planner.getPlanModules().containsKey(ActionType.DeploymentAction))
.forEach(planner -> planner.setActionPreferenceOverrides(overrides));
},
StepOrder.DAWN,
(365 * 2) + 1 // Jan. 1st of the third year
)
);
}
}
28 changes: 16 additions & 12 deletions epo/src/main/java/uk/ac/ox/poseidon/epo/policies/PolicyRuns.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableMap.toImmutableMap;
import static java.util.stream.Collectors.toList;

public class PolicyRuns {

Expand All @@ -47,30 +48,34 @@ public static void main(final String[] args) {
"2024-02-13", "cenv0729", "2024-02-17_06.26.53_local",
"calibrated_scenario_updated.yaml"
));

final Path baseOutputFolder = Paths.get(
System.getProperty("user.home"), "workspace", "epo_policy_runs", "runs"
);
final List<Integer> yearsActive = ImmutableList.of(2023);
final ImmutableList<Double> proportions = ImmutableList.of(0.75, 0.50, 0.25, 0.10, 0.0);
final ImmutableList<Double> fineProportions =
IntStream.rangeClosed(1, 19)
.mapToObj(i -> i * 0.05)
.collect(toImmutableList());
final ImmutableMap<String, List<Policy<EpoScenario<?>>>> policies = ImmutableMap.of(
/*
"global_object_set_limits", new GlobalObjectSetLimit(
yearsActive,
// 8729 FAD + 4003 OFS in 2022:
proportions.stream().map(p -> (int) (p * (8729 + 4003))).collect(toList())
),
"fad_limits", new ActiveFadLimitsPolicies(
"fad_limits_fine", new ActiveFadLimitsPolicies(
yearsActive,
2022,
proportions
2023,
fineProportions,
false
),
*/
"fad_limits_fine", new ActiveFadLimitsPolicies(
"fad_limits_fine_with_override", new ActiveFadLimitsPolicies(
yearsActive,
2023,
IntStream.rangeClosed(1, 20).mapToObj(i -> i * 0.05).collect(toImmutableList())
)
/*
fineProportions,
true
),
"extended_closures", new ExtendedClosurePolicies(
yearsActive,
ImmutableList.of(5, 15, 30)
Expand All @@ -85,7 +90,6 @@ public static void main(final String[] args) {
-120,
ImmutableList.of(5, 15, 30)
)
*/
)
.entrySet()
.stream()
Expand All @@ -94,7 +98,7 @@ public static void main(final String[] args) {
entry -> entry.getValue().getWithDefault()
));

final int numberOfRunsPerPolicy = 3;
final int numberOfRunsPerPolicy = 1;
final int numberOfPolicies = policies.values().stream().mapToInt(List::size).sum();
logger.info(String.format(
"About to run %d policies %d times (%d total runs)",
Expand All @@ -118,7 +122,7 @@ public static void main(final String[] args) {
.requestFisherDailyData(columnName -> columnName.equals("Number of active FADs"))
.requestFisherYearlyData()
.registerRowProvider("yearly_results.csv", YearlyResultsRowProvider::new);
if (!policyName.equals("fad_limits_fine")) {
if (!policyName.startsWith("fad_limits_fine")) {
runner
.registerRowProvider("spatial_closures.csv", RectangularAreaExtractor::new)
.registerRowProvider("sim_action_events.csv", PurseSeineActionsLogger::new);
Expand Down

0 comments on commit 0a69cf6

Please sign in to comment.