Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/virtualship/make_realistic/problems/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,6 @@ def execute(
continue

self._log_problem(problem, wp_i, problem_hash, hash_fpath, log_delay)
self._cache_original_expedition(self.expedition)

def _log_problem(
self,
Expand Down Expand Up @@ -352,6 +351,9 @@ def _log_problem(
_save_checkpoint(checkpoint, self.expedition_dir)
self.expedition.to_yaml(self.expedition_dir / CACHE / EXPEDITION_LATEST)

# cache original expedition for user reference
self._cache_original_expedition(self.expedition)

self._tabular_outputter(
problem_str=problem.message,
impact_str=impact_str,
Expand All @@ -360,6 +362,7 @@ def _log_problem(
)

if not has_contingency:
# exit simulation
sys.exit(0)

def _has_contingency(self, problem: ProblemType, problem_wp_i: int | None) -> bool:
Expand Down
40 changes: 39 additions & 1 deletion tests/make_realistic/problems/test_simulator.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import json
import random
import time
from datetime import datetime, timedelta

import pytest

from virtualship.instruments.types import InstrumentType
from virtualship.make_realistic.problems.scenarios import (
GENERAL_PROBLEMS,
Expand All @@ -18,7 +21,7 @@
ShipConfig,
Waypoint,
)
from virtualship.utils import REPORT
from virtualship.utils import CACHE, EXPEDITION_ORIGINAL, REPORT


def _make_simple_expedition(
Expand Down Expand Up @@ -372,3 +375,38 @@ def test_instrument_not_present_doesnt_select_instrument_problem(tmp_path):
# any incompatible waypoint x instrument problem combinations should have been replaced by a general problem
else:
assert isinstance(problem, GeneralProblem)


@pytest.mark.parametrize("halts_simulation", [True, False])
def test_original_expedition_always_cached(tmp_path, monkeypatch, halts_simulation):
"""The original expedition should be cached whenever a problem occurs."""
monkeypatch.setattr(time, "sleep", lambda _: None)

if halts_simulation:
# no contingency for a pre-departure problem, so simulation should halt
expedition = _make_simple_expedition(num_waypoints=2)
problem, problem_wp_i = _get_pre_departure_problem(), 0
else:
# short distance between waypoints, so enough contingency to avoid the problem
expedition = _make_simple_expedition(num_waypoints=2, distance_scale=0.01)
problem = next(
c for c in GENERAL_PROBLEMS if not getattr(c, "pre_departure", False)
)
problem_wp_i = 1

(tmp_path / CACHE).mkdir()
simulator = ProblemSimulator(expedition, str(tmp_path))
log_problem_args = (
problem,
problem_wp_i,
"sample",
tmp_path / "problem_sample.json",
)

if halts_simulation:
with pytest.raises(SystemExit):
simulator._log_problem(*log_problem_args, log_delay=0.0)
else:
simulator._log_problem(*log_problem_args, log_delay=0.0)

assert (tmp_path / CACHE / EXPEDITION_ORIGINAL).exists()
Loading