Skip to content

Commit 3bfe855

Browse files
move the deactivation strategy from comparator- to evaluator-based
because we need to ensure a stable sort and our FAD evaluation might be noisy (e.g. in the case of the non-reliable fish-value calculator)
1 parent 81f888d commit 3bfe855

15 files changed

+301
-279
lines changed

POSEIDON/src/main/java/uk/ac/ox/oxfish/fisher/purseseiner/fads/ComparatorBasedFadDeactivationStrategy.java

Lines changed: 0 additions & 42 deletions
This file was deleted.

POSEIDON/src/main/java/uk/ac/ox/oxfish/fisher/purseseiner/fads/ComparatorBasedFadDeactivationStrategyFactory.java

Lines changed: 0 additions & 52 deletions
This file was deleted.

POSEIDON/src/main/java/uk/ac/ox/oxfish/fisher/purseseiner/fads/ComparatorBasedFadDeactivationStrategyFactorySupplier.java

Lines changed: 0 additions & 34 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* POSEIDON, an agent-based model of fisheries
3+
* Copyright (C) 2024 CoHESyS Lab [email protected]
4+
*
5+
* This program is free software: you can redistribute it and/or modify it under the terms of the
6+
* GNU General Public License as published by the Free Software Foundation, either version 3
7+
* of the License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10+
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11+
* See the GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License along with this program.
14+
* If not, see <http://www.gnu.org/licenses/>.
15+
*/
16+
17+
package uk.ac.ox.oxfish.fisher.purseseiner.fads;
18+
19+
import java.util.Map.Entry;
20+
import java.util.function.ToDoubleFunction;
21+
22+
import static java.util.Comparator.comparingDouble;
23+
import static java.util.stream.Collectors.toList;
24+
import static uk.ac.ox.poseidon.common.core.Entry.entry;
25+
26+
public class EvaluatorBasedFadDeactivationStrategy extends FadDeactivationStrategy {
27+
private static final long serialVersionUID = -2818076865902570581L;
28+
private final ToDoubleFunction<? super Fad> fadEvaluator;
29+
30+
@SuppressWarnings("WeakerAccess")
31+
public EvaluatorBasedFadDeactivationStrategy(final ToDoubleFunction<? super Fad> fadEvaluator) {
32+
this.fadEvaluator = fadEvaluator;
33+
}
34+
35+
@Override
36+
protected void deactivate(final int numberOfFadsToDeactivate) {
37+
getFadManager()
38+
.getDeployedFads()
39+
.stream()
40+
// it's important to evaluate the FADs before sorted because the value might be noisy and
41+
// then something like `comparingDouble(fadEvaluator)` wouldn't be a stable comparison
42+
.map(fad -> entry(fad, fadEvaluator.applyAsDouble(fad)))
43+
.sorted(comparingDouble(Entry::getValue))
44+
.map(Entry::getKey)
45+
.limit(numberOfFadsToDeactivate)
46+
.collect(toList())
47+
.forEach(fad -> getFadManager().loseFad(fad));
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* POSEIDON, an agent-based model of fisheries
3+
* Copyright (C) 2024 CoHESyS Lab [email protected]
4+
*
5+
* This program is free software: you can redistribute it and/or modify it under the terms of the
6+
* GNU General Public License as published by the Free Software Foundation, either version 3
7+
* of the License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10+
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11+
* See the GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License along with this program.
14+
* If not, see <http://www.gnu.org/licenses/>.
15+
*/
16+
17+
package uk.ac.ox.oxfish.fisher.purseseiner.fads;
18+
19+
import uk.ac.ox.poseidon.common.api.ComponentFactory;
20+
import uk.ac.ox.poseidon.common.api.ModelState;
21+
22+
import java.util.function.ToDoubleFunction;
23+
24+
public class EvaluatorBasedFadDeactivationStrategyFactory implements ComponentFactory<FadDeactivationStrategy> {
25+
26+
private ComponentFactory<ToDoubleFunction<Fad>> fadEvaluator;
27+
28+
@SuppressWarnings("unused")
29+
public EvaluatorBasedFadDeactivationStrategyFactory() {
30+
}
31+
32+
public EvaluatorBasedFadDeactivationStrategyFactory(final ComponentFactory<ToDoubleFunction<Fad>> fadEvaluator) {
33+
this.fadEvaluator = fadEvaluator;
34+
}
35+
36+
@SuppressWarnings("unused")
37+
public ComponentFactory<ToDoubleFunction<Fad>> getFadEvaluator() {
38+
return fadEvaluator;
39+
}
40+
41+
@SuppressWarnings("unused")
42+
public void setFadEvaluator(final ComponentFactory<ToDoubleFunction<Fad>> fadEvaluator) {
43+
this.fadEvaluator = fadEvaluator;
44+
}
45+
46+
@Override
47+
public FadDeactivationStrategy apply(final ModelState modelState) {
48+
return new EvaluatorBasedFadDeactivationStrategy(fadEvaluator.apply(modelState));
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* POSEIDON, an agent-based model of fisheries
3+
* Copyright (C) 2024 CoHESyS Lab [email protected]
4+
*
5+
* This program is free software: you can redistribute it and/or modify it under the terms of the
6+
* GNU General Public License as published by the Free Software Foundation, either version 3
7+
* of the License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10+
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11+
* See the GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License along with this program.
14+
* If not, see <http://www.gnu.org/licenses/>.
15+
*/
16+
17+
package uk.ac.ox.oxfish.fisher.purseseiner.fads;
18+
19+
import com.google.auto.service.AutoService;
20+
import uk.ac.ox.poseidon.common.api.FactorySupplier;
21+
import uk.ac.ox.poseidon.common.core.BasicFactorySupplier;
22+
23+
@AutoService(FactorySupplier.class)
24+
public class EvaluatorBasedFadDeactivationStrategyFactorySupplier
25+
extends BasicFactorySupplier<EvaluatorBasedFadDeactivationStrategyFactory> {
26+
public EvaluatorBasedFadDeactivationStrategyFactorySupplier() {
27+
super(
28+
EvaluatorBasedFadDeactivationStrategyFactory.class,
29+
"Evaluator-based FAD deactivation strategy"
30+
);
31+
}
32+
}

POSEIDON/src/main/java/uk/ac/ox/oxfish/fisher/purseseiner/fads/FadManager.java

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
/*
2-
* POSEIDON, an agent-based model of fisheries
3-
* Copyright (C) 2020 CoHESyS Lab [email protected]
2+
* POSEIDON, an agent-based model of fisheries
3+
* Copyright (C) 2024 CoHESyS Lab [email protected]
44
*
5-
* This program is free software: you can redistribute it and/or modify
6-
* it under the terms of the GNU General Public License as published by
7-
* the Free Software Foundation, either version 3 of the License, or
8-
* (at your option) any later version.
5+
* This program is free software: you can redistribute it and/or modify it under the terms of the
6+
* GNU General Public License as published by the Free Software Foundation, either version 3
7+
* of the License, or (at your option) any later version.
98
*
10-
* This program is distributed in the hope that it will be useful,
11-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13-
* GNU General Public License for more details.
14-
*
15-
* You should have received a copy of the GNU General Public License
16-
* along with this program. If not, see <http://www.gnu.org/licenses/>.
9+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10+
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11+
* See the GNU General Public License for more details.
1712
*
13+
* You should have received a copy of the GNU General Public License along with this program.
14+
* If not, see <http://www.gnu.org/licenses/>.
1815
*/
1916

2017
package uk.ac.ox.oxfish.fisher.purseseiner.fads;
@@ -144,6 +141,13 @@ public FadManager(
144141
biomassLostMonitor.ifPresent(observer -> registerObserver(BiomassLostEvent.class, observer));
145142
}
146143

144+
public <T> void registerObserver(
145+
final Class<T> observedClass,
146+
final Observer<? super T> observer
147+
) {
148+
observers.register(observedClass, observer);
149+
}
150+
147151
public static FadManager getFadManager(
148152
final Fisher fisher
149153
) {
@@ -158,6 +162,21 @@ public static Optional<FadManager> maybeGetFadManager(
158162
return maybeGetPurseSeineGear(fisher).map(PurseSeineGear::getFadManager);
159163
}
160164

165+
public int numberOfPermissibleActions(
166+
final ActionClass actionClass,
167+
final int maximumToCheckFor,
168+
final Regulations regulations
169+
) {
170+
return numberOfPermissibleActions(
171+
getFisher(),
172+
regulations,
173+
getYearlyActionCounter(),
174+
getNumberOfActiveFads(),
175+
actionClass,
176+
maximumToCheckFor
177+
);
178+
}
179+
161180
public static int numberOfPermissibleActions(
162181
final Fisher fisher,
163182
final Regulations regulations,
@@ -213,28 +232,6 @@ public long getCount(
213232

214233
}
215234

216-
public <T> void registerObserver(
217-
final Class<T> observedClass,
218-
final Observer<? super T> observer
219-
) {
220-
observers.register(observedClass, observer);
221-
}
222-
223-
public int numberOfPermissibleActions(
224-
final ActionClass actionClass,
225-
final int maximumToCheckFor,
226-
final Regulations regulations
227-
) {
228-
return numberOfPermissibleActions(
229-
getFisher(),
230-
regulations,
231-
getYearlyActionCounter(),
232-
getNumberOfActiveFads(),
233-
actionClass,
234-
maximumToCheckFor
235-
);
236-
}
237-
238235
public Fisher getFisher() {
239236
return fisher;
240237
}
@@ -264,7 +261,9 @@ public Stream<Fad> getFadsAt(final SeaTile location) {
264261
}
265262

266263
public void loseFad(final Fad fad) {
267-
checkArgument(deployedFads.contains(fad));
264+
// remove the FAD from deployed FADs if it is there
265+
// (it won't be if the FAD was, e.g., manually deactivated
266+
// and is now getting zapped because it drifted out)
268267
deployedFads.remove(fad);
269268
fad.lose();
270269
}

0 commit comments

Comments
 (0)