Skip to content

Commit c54b77e

Browse files
committed
Improve final processing of optimization results
1 parent be0e1c8 commit c54b77e

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

CADETProcess/optimization/optimizationProblem.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,8 @@ def _evaluate(self, x, func, force=False):
796796
result = step.evaluate(current_request)
797797

798798
key = (str(eval_obj), step.id, x_str)
799-
self.cache.set(key, result, tag=x_str)
799+
if not isinstance(step, Callback):
800+
self.cache.set(key, result, tag=x_str)
800801
current_request = result
801802

802803
if len(result) != func.n_metrics:

CADETProcess/optimization/optimizer.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,14 @@ def optimize(
251251
plt.switch_backend('agg')
252252

253253
start = time.time()
254-
255254
self.run(self.optimization_problem, x0, *args, **kwargs)
256-
257255
time_elapsed = time.time() - start
256+
258257
self.results.time_elapsed = time_elapsed
259258
self.results.cpu_time = self.n_cores * time_elapsed
260259

260+
self.run_final_processing()
261+
261262
if delete_cache:
262263
optimization_problem.delete_cache(reinit=True)
263264
self._current_cache_entries = []
@@ -485,12 +486,19 @@ def _create_meta_front(self):
485486

486487
return meta_front
487488

488-
def _evaluate_callbacks(self, current_generation):
489+
def _evaluate_callbacks(self, current_generation, sub_dir=None):
490+
if sub_dir is not None:
491+
callbacks_dir = self.callbacks_dir / sub_dir
492+
callbacks_dir.mkdir(exist_ok=True, parents=True)
493+
else:
494+
callbacks_dir = self.callbacks_dir
495+
489496
for callback in self.optimization_problem.callbacks:
490497
if self.optimization_problem.n_callbacks > 1:
491-
_callbacks_dir = self.callbacks_dir / str(callback)
498+
_callbacks_dir = callbacks_dir / str(callback)
492499
else:
493-
_callbacks_dir = self.callbacks_dir
500+
_callbacks_dir = callbacks_dir
501+
494502
callback.cleanup(_callbacks_dir, current_generation)
495503
callback._callbacks_dir = _callbacks_dir
496504

@@ -555,7 +563,7 @@ def run_post_processing(
555563

556564
self._evaluate_callbacks(current_generation)
557565

558-
self.results.save_results()
566+
self.results.save_results('checkpoint')
559567

560568
# Remove new entries from cache that didn't make it to the meta front
561569
for x in population.x:
@@ -574,6 +582,12 @@ def run_post_processing(
574582

575583
self._log_results(current_generation)
576584

585+
def run_final_processing(self):
586+
self.results.plot_figures(show=False)
587+
if self.optimization_problem.n_callbacks > 0:
588+
self._evaluate_callbacks(0, 'final')
589+
self.results.save_results('final')
590+
577591
@property
578592
def options(self):
579593
"""dict: Optimizer options."""

CADETProcess/optimization/results.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ def plot_convergence(
741741
f'{plot_directory / figname}.png'
742742
)
743743

744-
def save_results(self):
744+
def save_results(self, name):
745745
if self.results_directory is not None:
746746
self._update_csv(self.population_last, 'results_all', mode='a')
747747
self._update_csv(self.population_last, 'results_last', mode='w')
@@ -751,7 +751,7 @@ def save_results(self):
751751

752752
results = H5()
753753
results.root = Dict(self.to_dict())
754-
results.filename = self.results_directory / 'checkpoint.h5'
754+
results.filename = self.results_directory / f'{name}.h5'
755755
results.save()
756756

757757
def to_dict(self):
@@ -763,6 +763,7 @@ def to_dict(self):
763763
Results as a dictionary with populations stored as list of dictionaries.
764764
"""
765765
data = Dict()
766+
data.system_information = self.system_information
766767
data.optimizer_state = self.optimizer_state
767768
data.population_all_id = str(self.population_all.id)
768769
data.populations = {i: pop.to_dict() for i, pop in enumerate(self.populations)}
@@ -773,6 +774,9 @@ def to_dict(self):
773774
data.meta_fronts = {
774775
i: front.to_dict() for i, front in enumerate(self.meta_fronts)
775776
}
777+
if self.time_elapsed is not None:
778+
data.time_elapsed = self.time_elapsed
779+
data.cpu_time = self.cpu_time
776780

777781
return data
778782

0 commit comments

Comments
 (0)