Skip to content

Commit 854ba49

Browse files
committed
propagate multiple problems across a single expedition
1 parent 25899d1 commit 854ba49

2 files changed

Lines changed: 21 additions & 8 deletions

File tree

src/virtualship/make_realistic/problems/simulator.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ def execute(
8585
# 2 = multiple problems can occur (general and instrument; total determined by the length of the expedition), but only one pre-departure problem allowed
8686

8787
# TODO: N.B. there is not logic currently controlling how many problems can occur in total during an expedition; at the moment it can happen every time the expedition is run if it's a different waypoint / problem combination
88+
#! TODO: may want to ensure duplicate problem types are removed; even if they could theoretically occur at different waypoints, so as not to inundate users...
8889

8990
#! TODO: what happens if students decide to re-run the expedition multiple times with slightly changed set-ups to try to e.g. get more measurements? Maybe it should be that problems are ignored if the exact expedition.yaml has been run before, and if there's any changes to the expedition.yaml
9091
# TODO: for this reason, `problems_encountered` dir should be housed in `results` dir along with a cache of the expedition.yaml used for that run...
@@ -143,9 +144,9 @@ def execute(
143144
if hash_path.exists():
144145
continue # problem * waypoint combination has already occurred; don't repeat
145146
else:
146-
self._hash_to_json(p, problem_hash, problem_waypoint_i, hash_path)
147+
self._hash_to_json(problem, problem_hash, problem_waypoint_i, hash_path)
147148

148-
if issubclass(p, GeneralProblem) and p.pre_departure:
149+
if issubclass(problem, GeneralProblem) and problem.pre_departure:
149150
alert_msg = LOG_MESSAGING["pre_departure"]
150151

151152
else:
@@ -154,13 +155,16 @@ def execute(
154155
)
155156

156157
# log problem occurrence, save to checkpoint, and pause simulation
157-
self._log_problem(p, problem_waypoint_i, alert_msg, log_delay)
158+
self._log_problem(
159+
problem, problem_waypoint_i, alert_msg, hash_path, log_delay
160+
)
158161

159162
def _log_problem(
160163
self,
161164
problem: GeneralProblem | InstrumentProblem,
162165
problem_waypoint_i: int | None,
163166
alert_msg: str,
167+
hash_path: Path,
164168
log_delay: float,
165169
):
166170
"""Log problem occurrence with spinner and delay, save to checkpoint, write hash."""
@@ -214,8 +218,15 @@ def _log_problem(
214218
>= (problem.delay_duration.total_seconds() / 3600.0) + sail_time
215219
):
216220
print(LOG_MESSAGING["problem_avoided"])
217-
# give users time to read message before simulation continues
218-
with yaspin():
221+
222+
# update problem json to resolved = True
223+
with open(hash_path, encoding="utf-8") as f:
224+
problem_json = json.load(f)
225+
problem_json["resolved"] = True
226+
with open(hash_path, "w", encoding="utf-8") as f_out:
227+
json.dump(problem_json, f_out, indent=4)
228+
229+
with yaspin(): # time to read message before simulation continues
219230
time.sleep(7.0)
220231
return
221232

@@ -228,6 +239,8 @@ def _log_problem(
228239
# save checkpoint
229240
checkpoint = self._make_checkpoint(
230241
failed_waypoint_i=problem_waypoint_i + 1
242+
if problem_waypoint_i is not None
243+
else None
231244
) # failed waypoint index then becomes the one after the one where the problem occurred; as this is when scheduling issues would be run into
232245
_save_checkpoint(checkpoint, self.expedition_dir)
233246

@@ -271,7 +284,7 @@ def _hash_to_json(
271284
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
272285
"resolved": False,
273286
}
274-
with open(hash_path, "w") as f:
287+
with open(hash_path, "w", encoding="utf-8") as f:
275288
json.dump(hash_data, f, indent=4)
276289

277290
def _cache_original_schedule(self, schedule: Schedule, path: Path | str):

src/virtualship/models/checkpoint.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def verify(self, schedule: Schedule, expedition_dir: Path) -> None:
8282
]
8383
if len(hash_fpaths) > 0:
8484
for file in hash_fpaths:
85-
with open(file) as f:
85+
with open(file, encoding="utf-8") as f:
8686
problem = json.load(f)
8787
if problem["resolved"]:
8888
continue
@@ -110,7 +110,7 @@ def verify(self, schedule: Schedule, expedition_dir: Path) -> None:
110110

111111
# save back to json file changing the resolved status to True
112112
problem["resolved"] = True
113-
with open(file, "w") as f_out:
113+
with open(file, "w", encoding="utf-8") as f_out:
114114
json.dump(problem, f_out, indent=4)
115115

116116
else:

0 commit comments

Comments
 (0)