Skip to content

Commit 5f3a1a2

Browse files
authored
Merge branch 'main' into capture_logs
2 parents d023b76 + 938478c commit 5f3a1a2

File tree

2 files changed

+76
-17
lines changed

2 files changed

+76
-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,
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import os
2+
3+
from optimas.explorations import Exploration
4+
from optimas.generators import RandomSamplingGenerator
5+
from optimas.evaluators import FunctionEvaluator
6+
from optimas.core import VaryingParameter, Objective
7+
8+
9+
def eval_func(input_params, output_params):
10+
raise ValueError("Exception to break exploration")
11+
12+
13+
def test_exception_during_exploration_run():
14+
"""Test that the Exploration handles exceptions during the run correctly.
15+
16+
When using `create_evaluation_dirs=True`, the current working directory
17+
will change during exploration and should be restored when `.run` finishes,
18+
even if an exception occurs.
19+
"""
20+
# Define variables and objectives.
21+
var1 = VaryingParameter("x0", -50.0, 5.0)
22+
var2 = VaryingParameter("x1", -5.0, 15.0)
23+
obj = Objective("f", minimize=False)
24+
25+
# Create generator.
26+
gen = RandomSamplingGenerator(
27+
varying_parameters=[var1, var2],
28+
objectives=[obj],
29+
)
30+
31+
# Create function evaluator.
32+
ev = FunctionEvaluator(function=eval_func, create_evaluation_dirs=True)
33+
34+
# Create exploration.
35+
exploration = Exploration(
36+
generator=gen,
37+
evaluator=ev,
38+
max_evals=10,
39+
sim_workers=2,
40+
exploration_dir_path="./tests_output/test_exception_during_run",
41+
)
42+
43+
cwd = os.getcwd()
44+
45+
# Run exploration without raising an exception.
46+
exploration.run()
47+
48+
# Check that the cwd remains unchanged after a failed run.
49+
assert os.getcwd() == cwd
50+
51+
52+
if __name__ == "__main__":
53+
test_exception_during_exploration_run()

0 commit comments

Comments
 (0)