Skip to content

Commit

Permalink
Add success attribute to OptimizationResults and raise Exepction `i…
Browse files Browse the repository at this point in the history
…f not success`
  • Loading branch information
schmoelder committed Mar 22, 2024
1 parent d59c7fd commit 9478b91
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CADETProcess/optimization/axAdapater.py
Expand Up @@ -455,6 +455,10 @@ def run(self, optimization_problem, x0):

print(exp_to_df(self.ax_experiment))

self.results.success = True
self.results.exit_flag = 0
self.results.exit_message = global_stopping_message


class SingleObjectiveAxInterface(AxInterface):
def _setup_optimization_config(self, objectives, outcome_constraints):
Expand Down
5 changes: 5 additions & 0 deletions CADETProcess/optimization/optimizer.py
Expand Up @@ -265,6 +265,11 @@ def optimize(

plt.switch_backend(backend)

if not self.results.success:
raise CADETProcessError(
f"Optimizaton failed with message: {self.results.exit_message}"
)

return self.results

@abstractmethod
Expand Down
5 changes: 4 additions & 1 deletion CADETProcess/optimization/pymooAdapter.py
Expand Up @@ -169,12 +169,15 @@ def run(self, optimization_problem: OptimizationProblem, x0=None):
self.run_post_processing(X, F, G, CV, algorithm.n_gen-1, X_opt)

if algorithm.n_gen >= n_max_gen:
exit_message = 'Max number of generations exceeded.'
success = True
exit_flag = 1
exit_message = 'Max number of generations exceeded.'
else:
success = False
exit_flag = 0
exit_message = 'success'

self.results.success = success
self.results.exit_flag = exit_flag
self.results.exit_message = exit_message

Expand Down
3 changes: 3 additions & 0 deletions CADETProcess/optimization/results.py
Expand Up @@ -31,6 +31,8 @@ class OptimizationResults(Structure):
Optimization problem.
optimizer : OptimizerBase
Optimizer used to optimize the OptimizationProblem.
success : bool
True if optimization was successfully terminated. False otherwise.
exit_flag : int
Information about the solver termination.
exit_message : str
Expand All @@ -56,6 +58,7 @@ class OptimizationResults(Structure):
functions.
"""
success = Bool(default=False)
exit_flag = UnsignedInteger()
exit_message = String()
time_elapsed = UnsignedFloat()
Expand Down
1 change: 1 addition & 0 deletions CADETProcess/optimization/scipyAdapter.py
Expand Up @@ -136,6 +136,7 @@ def callback_function(x, state=None):
callback=callback_function,
)

self.results.success = bool(scipy_results.success)
self.results.exit_flag = scipy_results.status
self.results.exit_message = scipy_results.message

Expand Down

0 comments on commit 9478b91

Please sign in to comment.