Skip to content

Commit fd0d2ab

Browse files
committed
Store changes for last runs at detached head
1 parent 89f18fa commit fd0d2ab

File tree

5 files changed

+197
-98
lines changed

5 files changed

+197
-98
lines changed

POSEIDON/src/main/java/uk/ac/ox/oxfish/experiments/tuna/Runner.java

+59-15
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ public final class Runner<S extends Scenario> {
6161
private static final String YEARLY_DATA_FILENAME = "yearly_data.csv";
6262
private static final String FISHER_YEARLY_DATA_FILENAME = "fisher_yearly_data.csv";
6363
private static final String FISHER_DAILY_DATA_FILENAME = "fisher_daily_data.csv";
64+
private static final String FAD_DAILY_DATA_FILENAME = "fad_daily_data.csv";
65+
6466
private static final String RUNS_FILENAME = "runs.csv";
6567
private static final String POLICIES_FILENAME = "policies.csv";
6668
private static final String SCENARIOS_FOLDER = "scenarios";
@@ -69,7 +71,8 @@ public final class Runner<S extends Scenario> {
6971

7072
private final Supplier<? extends S> scenarioSupplier;
7173
private final Path outputPath;
72-
private final Multimap<Path, AlgorithmFactory<Iterable<? extends RowProvider>>> rowProviderFactories =
74+
private final Multimap<Path, AlgorithmFactory<Iterable<? extends RowProvider>>>
75+
rowProviderFactories =
7376
HashMultimap.create();
7477
private boolean parallel = true;
7578
private boolean writeScenarioToFile = false;
@@ -237,23 +240,29 @@ private void writeScenarioToFile(final State runnerState) {
237240
final Path scenariosFolder = outputPath.resolve(SCENARIOS_FOLDER);
238241
Files.createDirectories(scenariosFolder);
239242
final Path scenarioFile =
240-
scenariosFolder.resolve(runnerState.getPolicy().getName().replaceAll("[^a-zA-Z0-9-_.]", "_") + ".yaml");
243+
scenariosFolder.resolve(runnerState
244+
.getPolicy()
245+
.getName()
246+
.replaceAll("[^a-zA-Z0-9-_.]", "_") + ".yaml");
241247
new FishYAML().dump(runnerState.getScenario(), new FileWriter(scenarioFile.toFile()));
242248
} catch (final IOException e) {
243249
throw new RuntimeException(e);
244250
}
245251
}
246252

247253
private Multimap<Path, RowProvider> makeRowProviders(final State state) {
248-
final ImmutableMultimap.Builder<Path, RowProvider> rowProviders = ImmutableMultimap.builder();
254+
final ImmutableMultimap.Builder<Path, RowProvider> rowProviders =
255+
ImmutableMultimap.builder();
249256
rowProviders.put(outputPath.resolve(RUNS_FILENAME), state);
250257
rowProviders.put(outputPath.resolve(POLICIES_FILENAME), state.policy);
251-
rowProviderFactories.forEach((path, factory) -> factory.apply(state.model).forEach(rowProvider -> {
252-
if (rowProvider instanceof Startable) {
253-
state.model.registerStartable((Startable) rowProvider);
254-
}
255-
rowProviders.put(path, rowProvider);
256-
}));
258+
rowProviderFactories.forEach((path, factory) -> factory
259+
.apply(state.model)
260+
.forEach(rowProvider -> {
261+
if (rowProvider instanceof Startable) {
262+
state.model.registerStartable((Startable) rowProvider);
263+
}
264+
rowProviders.put(path, rowProvider);
265+
}));
257266
return rowProviders.build();
258267
}
259268

@@ -271,9 +280,17 @@ private void writeOutputs(
271280
if (!activeProviders.isEmpty()) {
272281
synchronized (overwriteFiles) {
273282
final boolean overwrite =
274-
overwriteFiles.computeIfAbsent(outputPath, __ -> new AtomicBoolean(true)).getAndSet(false);
275-
try (final Writer fileWriter = new FileWriter(outputPath.toFile(), !overwrite)) {
276-
final CsvWriter csvWriter = new CsvWriter(new BufferedWriter(fileWriter), csvWriterSettings);
283+
overwriteFiles
284+
.computeIfAbsent(outputPath, __ -> new AtomicBoolean(true))
285+
.getAndSet(false);
286+
try (final Writer fileWriter = new FileWriter(
287+
outputPath.toFile(),
288+
!overwrite
289+
)) {
290+
final CsvWriter csvWriter = new CsvWriter(
291+
new BufferedWriter(fileWriter),
292+
csvWriterSettings
293+
);
277294
writeRows(csvWriter, activeProviders, runNumber, overwrite);
278295
} catch (final IOException e) {
279296
throw new IllegalStateException("Writing to " + outputPath + " failed.", e);
@@ -326,6 +343,21 @@ public Runner<S> requestFisherDailyData(final Predicate<String> columnNamePredic
326343
);
327344
}
328345

346+
public Runner<S> requestFadDailyData(final Predicate<String> columnNamePredicate) {
347+
return registerRowProviders(
348+
FAD_DAILY_DATA_FILENAME,
349+
fishState -> fishState
350+
.getFishers()
351+
.stream()
352+
.map(fisher -> new TidyFisherDailyData(
353+
fisher.getDailyData(),
354+
columnNamePredicate,
355+
fisher.getTagsList().get(0)
356+
))
357+
.collect(toImmutableList())
358+
);
359+
}
360+
329361
@SuppressWarnings({"WeakerAccess", "unused"})
330362
public Runner<S> requestYearlyData(final Predicate<String> columnNamePredicate) {
331363
return registerRowProvider(YEARLY_DATA_FILENAME, fishState -> new TidyYearlyData(
@@ -338,7 +370,10 @@ public Runner<S> registerRowProvider(
338370
@SuppressWarnings("SameParameterValue") final String fileName,
339371
final AlgorithmFactory<? extends RowProvider> rowProviderFactory
340372
) {
341-
registerRowProviders(fileName, fishState -> ImmutableList.of(rowProviderFactory.apply(fishState)));
373+
registerRowProviders(
374+
fileName,
375+
fishState -> ImmutableList.of(rowProviderFactory.apply(fishState))
376+
);
342377
return this;
343378
}
344379

@@ -409,8 +444,17 @@ public LocalDateTime getStartTime() {
409444

410445
void printStep() {
411446
logger.info(String.format(
412-
"Run %" + runDigits + "d / %" + runDigits + "d, " + "step %5d (year %" + yearDigits + "d / %" +
413-
yearDigits + "d, " + "day %3d), policy: %s",
447+
"Run %" +
448+
runDigits +
449+
"d / %" +
450+
runDigits +
451+
"d, " +
452+
"step %5d (year %" +
453+
yearDigits +
454+
"d / %" +
455+
yearDigits +
456+
"d, " +
457+
"day %3d), policy: %s",
414458
runNumber,
415459
numRuns,
416460
model.getStep(),
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,62 @@
1-
/*
2-
* POSEIDON, an agent-based model of fisheries
3-
* Copyright (C) 2020 CoHESyS Lab [email protected]
4-
*
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.
9-
*
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/>.
17-
*
18-
*/
19-
20-
package uk.ac.ox.oxfish.model.data.heatmaps;
21-
22-
import uk.ac.ox.oxfish.geography.SeaTile;
23-
import uk.ac.ox.oxfish.geography.fads.FadMap;
24-
import uk.ac.ox.oxfish.model.FishState;
25-
import uk.ac.ox.oxfish.model.Startable;
26-
import uk.ac.ox.oxfish.model.data.heatmaps.mergers.IterativeAverageMerger;
27-
28-
import java.util.function.ToDoubleFunction;
29-
30-
public class FadDensityHeatmapGatherer extends HeatmapGatherer {
31-
32-
private static final long serialVersionUID = -5707936919764634160L;
33-
34-
public FadDensityHeatmapGatherer(
35-
final int interval
36-
) {
37-
super(
38-
"FAD density",
39-
"Average number of FADs",
40-
interval,
41-
new Extractor(),
42-
heatmapGatherer -> new IterativeAverageMerger(heatmapGatherer::getNumObservations)
43-
);
44-
}
45-
46-
private static class Extractor implements ToDoubleFunction<SeaTile>, Startable {
47-
48-
private FadMap fadMap = null;
49-
50-
@Override
51-
public void start(final FishState fishState) {
52-
fadMap = fishState.getFadMap();
53-
}
54-
55-
@Override
56-
public double applyAsDouble(final SeaTile seaTile) {
57-
return fadMap.fadsAt(seaTile).numObjs;
58-
}
59-
60-
}
61-
62-
}
1+
/*
2+
* POSEIDON, an agent-based model of fisheries
3+
* Copyright (C) 2020 CoHESyS Lab [email protected]
4+
*
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.
9+
*
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/>.
17+
*
18+
*/
19+
20+
package uk.ac.ox.oxfish.model.data.heatmaps;
21+
22+
import uk.ac.ox.oxfish.geography.SeaTile;
23+
import uk.ac.ox.oxfish.geography.fads.FadMap;
24+
import uk.ac.ox.oxfish.model.FishState;
25+
import uk.ac.ox.oxfish.model.Startable;
26+
import uk.ac.ox.oxfish.model.data.heatmaps.mergers.IterativeAverageMerger;
27+
28+
import java.util.function.ToDoubleFunction;
29+
30+
public class FadDensityHeatmapGatherer extends HeatmapGatherer {
31+
32+
private static final long serialVersionUID = -5707936919764634160L;
33+
34+
public FadDensityHeatmapGatherer(
35+
final int interval
36+
) {
37+
super(
38+
"FAD density",
39+
"Average number of FADs",
40+
interval,
41+
new Extractor(),
42+
heatmapGatherer -> new IterativeAverageMerger(heatmapGatherer::getNumObservations)
43+
);
44+
}
45+
46+
private static class Extractor implements ToDoubleFunction<SeaTile>, Startable {
47+
48+
private FadMap fadMap = null;
49+
50+
@Override
51+
public void start(final FishState fishState) {
52+
fadMap = fishState.getFadMap();
53+
}
54+
55+
@Override
56+
public double applyAsDouble(final SeaTile seaTile) {
57+
return fadMap.fadsAt(seaTile).numObjs;
58+
}
59+
60+
}
61+
62+
}

epo/src/main/java/uk/ac/ox/poseidon/epo/calibration/Evaluator.java

+13-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ public class Evaluator implements JCommanderRunnable {
5858
// group_by(action_type) |>
5959
// slice_max(n, with_ties = FALSE)
6060
@Parameter(names = "--track_fads_of_vessels")
61-
private Set<String> vesselsWhoseFadsToTrack = ImmutableSet.of(); //"1779", "453", "1552");
61+
private Set<String> vesselsWhoseFadsToTrack = ImmutableSet.of("1779", "453", "1552");
62+
//"1779", "453", "1552");
6263
@Parameter(names = {"-r", "--num-runs"})
6364
private int numRuns = Math.min(8, getRuntime().availableProcessors());
6465
@Parameter(names = {"-y", "--years"})
@@ -118,7 +119,10 @@ public void run() {
118119
scenario = loadScenario();
119120
break;
120121
case "md":
121-
scenario = new ScenarioExtractor(calibrationFolder, calibrationFolder.resolve(scenarioSource))
122+
scenario = new ScenarioExtractor(
123+
calibrationFolder,
124+
calibrationFolder.resolve(scenarioSource)
125+
)
122126
.getAndWriteToFile(CALIBRATED_SCENARIO_FILENAME);
123127
break;
124128
default:
@@ -170,6 +174,13 @@ private Scenario loadScenario() {
170174
}
171175

172176
private void registerFadAttractionEventProviders(final Runner<Scenario> runner) {
177+
registerFadAttractionEventProviders(runner, this.vesselsWhoseFadsToTrack);
178+
}
179+
180+
private void registerFadAttractionEventProviders(
181+
final Runner<Scenario> runner,
182+
final Set<String> vesselsWhoseFadsToTrack
183+
) {
173184
runner.setAfterStartConsumer(state -> {
174185
final FishState fishState = state.getModel();
175186
final AbundanceFadAttractionEventObserver observer =

0 commit comments

Comments
 (0)