Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip on action override
Browse files Browse the repository at this point in the history
nicolaspayette committed Mar 14, 2024
1 parent b8b671b commit ea6c1f7
Showing 4 changed files with 125 additions and 48 deletions.
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 cohesys.lab@gmail.com
* POSEIDON, an agent-based model of fisheries
* Copyright (C) 2024 CoHESyS Lab cohesys.lab@gmail.com
*
* 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;
@@ -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,
@@ -81,6 +76,10 @@ public PlannedStrategy(
this.minimumValueOfSetOnOwnFad = minimumValueOfSetOnOwnFad;
}

public DrawThenCheapestInsertionPlanner getPlanner() {
return planner;
}

@Override
public void start(
final FishState model,
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 cohesys.lab@gmail.com
*
* 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;
@@ -195,6 +210,10 @@ public PlannedStrategyProxy(
this.noaSetsRangeInSeaTiles = noaSetsRangeInSeaTiles;
}

public PlannedStrategy getDelegate() {
return delegate;
}

@SuppressWarnings("rawtypes")
@Override
public void start(
Original file line number Diff line number Diff line change
@@ -2,25 +2,30 @@
* POSEIDON, an agent-based model of fisheries
* Copyright (C) 2024 CoHESyS Lab cohesys.lab@gmail.com
*
* 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;

@@ -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
@@ -57,27 +66,70 @@ 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)
.forEach(planner -> {
System.out.println("setting override for " + planner);
planner.setActionPreferenceOverrides(overrides);
});
},
StepOrder.DAWN,
(365 * 2) + 1 // Jan. 1st of the third year
)
);
}
}
13 changes: 10 additions & 3 deletions epo/src/main/java/uk/ac/ox/poseidon/epo/policies/PolicyRuns.java
Original file line number Diff line number Diff line change
@@ -65,10 +65,17 @@ public static void main(final String[] args) {
proportions
),
*/
"fad_limits_fine", new ActiveFadLimitsPolicies(
// "fad_limits_fine", new ActiveFadLimitsPolicies(
// yearsActive,
// 2023,
// IntStream.rangeClosed(1, 20).mapToObj(i -> i * 0.05).collect(toImmutableList()),
// false
// ),
"fad_limits_fine_with_override", new ActiveFadLimitsPolicies(
yearsActive,
2023,
IntStream.rangeClosed(1, 20).mapToObj(i -> i * 0.05).collect(toImmutableList())
IntStream.rangeClosed(1, 1).mapToObj(i -> i * 0.05).collect(toImmutableList()),
true
)
/*
"extended_closures", new ExtendedClosurePolicies(
@@ -94,7 +101,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)",

0 comments on commit ea6c1f7

Please sign in to comment.