-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
keep going even if the evaluation crashes
(this is a temporary fix, not a long-term solution...)
- Loading branch information
1 parent
c2264d1
commit 9a1c4d5
Showing
4 changed files
with
134 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,18 @@ | ||
/* | ||
* 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.maximization; | ||
|
||
import com.beust.jcommander.JCommander; | ||
|
@@ -32,6 +47,47 @@ public static void main(final String[] args) { | |
boundsWriter.run(); | ||
} | ||
|
||
public void run() { | ||
writeBounds( | ||
getCalibrationFolder().resolve(getCalibrationFileName()), | ||
getCalibrationFolder().resolve(getLogFileName()), | ||
getCalibrationFolder().resolve(getOutputFileName()) | ||
); | ||
} | ||
|
||
public static void writeBounds( | ||
final Path calibrationFile, | ||
final Path logFile, | ||
final Path outputFile | ||
) { | ||
writeBounds( | ||
GenericOptimization.fromFile(calibrationFile), | ||
new SolutionExtractor(logFile).bestSolution().getKey(), | ||
outputFile | ||
); | ||
} | ||
|
||
public Path getCalibrationFolder() { | ||
return calibrationFolder; | ||
} | ||
|
||
public String getCalibrationFileName() { | ||
return calibrationFileName; | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public void setCalibrationFileName(final String calibrationFileName) { | ||
this.calibrationFileName = calibrationFileName; | ||
} | ||
|
||
public String getLogFileName() { | ||
return logFileName; | ||
} | ||
|
||
public String getOutputFileName() { | ||
return outputFileName; | ||
} | ||
|
||
public static void writeBounds( | ||
final GenericOptimization genericOptimization, | ||
final double[] solution, | ||
|
@@ -65,57 +121,16 @@ public static void writeBounds( | |
); | ||
} | ||
|
||
public void run() { | ||
writeBounds( | ||
getCalibrationFolder().resolve(getCalibrationFileName()), | ||
getCalibrationFolder().resolve(getLogFileName()), | ||
getCalibrationFolder().resolve(getOutputFileName()) | ||
); | ||
} | ||
|
||
public void writeBounds( | ||
final Path calibrationFile, | ||
final Path logFile, | ||
final Path outputFile | ||
) { | ||
writeBounds( | ||
GenericOptimization.fromFile(calibrationFile), | ||
new SolutionExtractor(logFile).bestSolution().getKey(), | ||
outputFile | ||
); | ||
} | ||
|
||
public String getOutputFileName() { | ||
return outputFileName; | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public void setOutputFileName(final String outputFileName) { | ||
this.outputFileName = outputFileName; | ||
} | ||
|
||
public String getCalibrationFileName() { | ||
return calibrationFileName; | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public void setCalibrationFileName(final String calibrationFileName) { | ||
this.calibrationFileName = calibrationFileName; | ||
} | ||
|
||
public String getLogFileName() { | ||
return logFileName; | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public void setLogFileName(final String logFileName) { | ||
this.logFileName = logFileName; | ||
} | ||
|
||
public Path getCalibrationFolder() { | ||
return calibrationFolder; | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public void setCalibrationFolder(final Path calibrationFolder) { | ||
this.calibrationFolder = calibrationFolder; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/* | ||
* POSEIDON, an agent-based model of fisheries | ||
* Copyright (C) 2024 CoHESyS Lab [email protected] | ||
* 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 | ||
|
@@ -24,6 +24,7 @@ | |
import uk.ac.ox.oxfish.experiments.tuna.Runner; | ||
import uk.ac.ox.oxfish.fisher.purseseiner.fads.AbundanceFadAttractionEvent; | ||
import uk.ac.ox.oxfish.fisher.purseseiner.fads.FadManager; | ||
import uk.ac.ox.oxfish.maximization.BoundsWriter; | ||
import uk.ac.ox.oxfish.maximization.YearlyResultsRowProvider; | ||
import uk.ac.ox.oxfish.model.FishState; | ||
import uk.ac.ox.oxfish.model.data.monitors.loggers.AbundanceFadAttractionEventObserver; | ||
|
@@ -107,10 +108,16 @@ public void setCalibrationFolder(final Path calibrationFolder) { | |
@Override | ||
public void run() { | ||
|
||
final Scenario scenario = | ||
scenarioFile == null | ||
? new ScenarioExtractor(calibrationFolder).getAndWriteToFile("calibrated_scenario.yaml") | ||
: loadScenario(); | ||
final Scenario scenario; | ||
if (scenarioFile == null) { | ||
scenario = new ScenarioExtractor(calibrationFolder) | ||
.getAndWriteToFile("calibrated_scenario.yaml"); | ||
final BoundsWriter boundsWriter = new BoundsWriter(); | ||
boundsWriter.setCalibrationFolder(calibrationFolder); | ||
boundsWriter.run(); | ||
} else { | ||
scenario = loadScenario(); | ||
} | ||
|
||
final Runner<Scenario> runner = | ||
new Runner<>(() -> scenario, calibrationFolder) | ||
|
2 changes: 1 addition & 1 deletion
2
epo/src/main/java/uk/ac/ox/poseidon/epo/calibration/ScenarioExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/* | ||
* POSEIDON, an agent-based model of fisheries | ||
* Copyright (C) 2024 CoHESyS Lab [email protected] | ||
* 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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,17 @@ | ||
/* | ||
* POSEIDON, an agent-based model of fisheries | ||
* Copyright (C) 2024 CoHESyS Lab [email protected] | ||
* 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 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.calibration; | ||
|
@@ -128,60 +126,24 @@ private static ImmutableIntArray getNumericArgs(final String[] args) { | |
})); | ||
} | ||
|
||
private static Path copyToFolder( | ||
final Path sourceFile, | ||
final Path targetFolder | ||
) { | ||
try { | ||
return Files.copy(sourceFile, targetFolder.resolve(sourceFile.getFileName())); | ||
} catch (final IOException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
||
static void evaluateSolutionAndPrintOutErrors( | ||
final Path calibrationFilePath, | ||
final double[] solution | ||
) { | ||
saveCalibratedScenario(solution, calibrationFilePath); | ||
final Evaluator evaluator = new Evaluator(); | ||
evaluator.setCalibrationFolder(calibrationFilePath.getParent()); | ||
evaluator.run(); | ||
} | ||
|
||
private static void saveCalibratedScenario( | ||
final double[] optimalParameters, | ||
final Path calibrationFilePath | ||
) { | ||
|
||
final Path calibratedScenarioPath = | ||
calibrationFilePath.getParent().resolve(CALIBRATED_SCENARIO_FILE_NAME); | ||
|
||
try (final FileWriter fileWriter = new FileWriter(calibratedScenarioPath.toFile())) { | ||
final GenericOptimization optimization = | ||
GenericOptimization.fromFile(calibrationFilePath); | ||
final Scenario scenario = GenericOptimization.buildScenario( | ||
optimalParameters, | ||
Paths.get(optimization.getScenarioFile()).toFile(), | ||
optimization.getParameters() | ||
); | ||
new FishYAML().dump(scenario, fileWriter); | ||
} catch (final IOException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
||
public double[] run() { | ||
final Path outputFolder = makeOutputFolder(); | ||
final Path calibrationFilePath = | ||
copyToFolder(this.originalCalibrationFilePath, outputFolder); | ||
final double[] solution = calibrate(calibrationFilePath); | ||
evaluateSolutionAndPrintOutErrors(calibrationFilePath, solution); | ||
writeBounds( | ||
GenericOptimization.fromFile(calibrationFilePath), | ||
solution, | ||
outputFolder.resolve("bounds.csv") | ||
); | ||
try { | ||
evaluateSolutionAndPrintOutErrors(calibrationFilePath, solution); | ||
writeBounds( | ||
GenericOptimization.fromFile(calibrationFilePath), | ||
solution, | ||
outputFolder.resolve("bounds.csv") | ||
); | ||
} catch (final IllegalStateException e) { | ||
// Don't crash if something goes wrong in the evaluation. | ||
// This shouldn't be left in; but I need to get a new | ||
// calibration going now and don't have another solution yet. | ||
e.printStackTrace(); | ||
} | ||
return solution; | ||
} | ||
|
||
|
@@ -205,6 +167,17 @@ private Path makeOutputFolder() { | |
return outputFolderPath; | ||
} | ||
|
||
private static Path copyToFolder( | ||
final Path sourceFile, | ||
final Path targetFolder | ||
) { | ||
try { | ||
return Files.copy(sourceFile, targetFolder.resolve(sourceFile.getFileName())); | ||
} catch (final IOException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
||
private double[] calibrate(final Path calibrationFilePath) { | ||
|
||
final Path logFilePath = calibrationFilePath.getParent().resolve(CALIBRATION_LOG_FILE_NAME); | ||
|
@@ -307,6 +280,38 @@ public void initializePopulation(final Population population) { | |
|
||
} | ||
|
||
static void evaluateSolutionAndPrintOutErrors( | ||
final Path calibrationFilePath, | ||
final double[] solution | ||
) { | ||
saveCalibratedScenario(solution, calibrationFilePath); | ||
final Evaluator evaluator = new Evaluator(); | ||
evaluator.setCalibrationFolder(calibrationFilePath.getParent()); | ||
evaluator.run(); | ||
} | ||
|
||
private static void saveCalibratedScenario( | ||
final double[] optimalParameters, | ||
final Path calibrationFilePath | ||
) { | ||
|
||
final Path calibratedScenarioPath = | ||
calibrationFilePath.getParent().resolve(CALIBRATED_SCENARIO_FILE_NAME); | ||
|
||
try (final FileWriter fileWriter = new FileWriter(calibratedScenarioPath.toFile())) { | ||
final GenericOptimization optimization = | ||
GenericOptimization.fromFile(calibrationFilePath); | ||
final Scenario scenario = GenericOptimization.buildScenario( | ||
optimalParameters, | ||
Paths.get(optimization.getScenarioFile()).toFile(), | ||
optimization.getParameters() | ||
); | ||
new FishYAML().dump(scenario, fileWriter); | ||
} catch (final IOException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public boolean isVerbose() { | ||
return verbose; | ||
|