Skip to content

Commit 26bfb33

Browse files
committed
Handle exception and make sure cwd is restored
1 parent 1dd3e5d commit 26bfb33

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

optimas/explorations/base.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -208,26 +208,32 @@ def run(self, n_evals: Optional[int] = None) -> None:
208208
# Save exploration parameters to json file.
209209
self._save_exploration_parameters()
210210

211-
# Launch exploration with libEnsemble.
212-
history, persis_info, flag = libE(
213-
sim_specs,
214-
gen_specs,
215-
exit_criteria,
216-
persis_info,
217-
self.alloc_specs,
218-
self.libE_specs,
219-
H0=self._libe_history.H,
220-
)
211+
try:
212+
# Launch exploration with libEnsemble.
213+
history, persis_info, flag = libE(
214+
sim_specs,
215+
gen_specs,
216+
exit_criteria,
217+
persis_info,
218+
self.alloc_specs,
219+
self.libE_specs,
220+
H0=self._libe_history.H,
221+
)
221222

222-
# Update history.
223-
self._libe_history.H = history
223+
# Update history.
224+
self._libe_history.H = history
224225

225-
# Update number of evaluation in this exploration.
226-
n_evals_final = self.generator.n_evaluated_trials
227-
self._n_evals += n_evals_final - n_evals_initial
226+
# Update number of evaluation in this exploration.
227+
n_evals_final = self.generator.n_evaluated_trials
228+
self._n_evals += n_evals_final - n_evals_initial
228229

229-
# Reset `cwd` to initial value before `libE` was called.
230-
os.chdir(cwd)
230+
except Exception as e:
231+
logger.error(
232+
"Exploration stopped due to an exception: {}".format(e)
233+
)
234+
finally:
235+
# Reset `cwd` to initial value before `libE` was called.
236+
os.chdir(cwd)
231237

232238
def attach_trials(
233239
self,

0 commit comments

Comments
 (0)