Skip to content

Commit

Permalink
Improve final processing of optimization results
Browse files Browse the repository at this point in the history
  • Loading branch information
schmoelder committed Mar 21, 2024
1 parent 8e4a903 commit f33fded
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
25 changes: 19 additions & 6 deletions CADETProcess/optimization/optimizer.py
Expand Up @@ -249,10 +249,9 @@ def optimize(
plt.switch_backend('agg')

start = time.time()

self.run(self.optimization_problem, x0, *args, **kwargs)

time_elapsed = time.time() - start

self.results.time_elapsed = time_elapsed
self.results.cpu_time = self.n_cores * time_elapsed

Expand All @@ -261,6 +260,8 @@ def optimize(

plt.switch_backend(backend)

self.run_final_processing()

return self.results

@abstractmethod
Expand Down Expand Up @@ -482,12 +483,19 @@ def _create_meta_front(self):

return meta_front

def _evaluate_callbacks(self, current_generation):
def _evaluate_callbacks(self, current_generation, sub_dir=None):
if sub_dir is not None:
callbacks_dir = self.callbacks_dir / sub_dir
callbacks_dir.mkdir(exist_ok=True, parents=True)
else:
callbacks_dir = self.callbacks_dir

for callback in self.optimization_problem.callbacks:
if self.optimization_problem.n_callbacks > 1:
_callbacks_dir = self.callbacks_dir / str(callback)
_callbacks_dir = callbacks_dir / str(callback)
else:
_callbacks_dir = self.callbacks_dir
_callbacks_dir = callbacks_dir

callback.cleanup(_callbacks_dir, current_generation)
callback._callbacks_dir = _callbacks_dir

Expand Down Expand Up @@ -552,14 +560,19 @@ def run_post_processing(

self._evaluate_callbacks(current_generation)

self.results.save_results()
self.results.save_results('checkpoint')

for x in self.results.population_all.x:
if x not in self.results.meta_front.x:
self.optimization_problem.prune_cache(str(np.array(x)))

self._log_results(current_generation)

def run_final_processing(self):
self.results.plot_figures(show=False)
self._evaluate_callbacks(0, 'final')
self.results.save_results('final')

@property
def options(self):
"""dict: Optimizer options."""
Expand Down
8 changes: 6 additions & 2 deletions CADETProcess/optimization/results.py
Expand Up @@ -741,7 +741,7 @@ def plot_convergence(
f'{plot_directory / figname}.png'
)

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

results = H5()
results.root = Dict(self.to_dict())
results.filename = self.results_directory / 'checkpoint.h5'
results.filename = self.results_directory / f'{name}.h5'
results.save()

def to_dict(self):
Expand All @@ -763,6 +763,7 @@ def to_dict(self):
Results as a dictionary with populations stored as list of dictionaries.
"""
data = Dict()
data.system_information = self.system_information
data.optimizer_state = self.optimizer_state
data.population_all_id = str(self.population_all.id)
data.populations = {i: pop.to_dict() for i, pop in enumerate(self.populations)}
Expand All @@ -773,6 +774,9 @@ def to_dict(self):
data.meta_fronts = {
i: front.to_dict() for i, front in enumerate(self.meta_fronts)
}
if self.time_elapsed is not None:
data.time_elapsed = self.time_elapsed
data.cpu_time = self.cpu_time

return data

Expand Down

0 comments on commit f33fded

Please sign in to comment.