Skip to content

Commit

Permalink
Fix saving of optimization results
Browse files Browse the repository at this point in the history
  • Loading branch information
rolfjl committed Jul 9, 2024
1 parent dc32ce4 commit 51b79b6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
12 changes: 9 additions & 3 deletions popt/loop/optimize.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Internal imports
from popt.misc_tools import optim_tools as ot

# External imports
import os
import numpy as np
Expand Down Expand Up @@ -103,7 +106,7 @@ def __set__variable(var_name=None, defalut=None):

# Optimze with external penalty function for constraints, provide r_0 as input
self.epf = __set__variable('epf', {})
self.epf_iteration = 1
self.epf_iteration = 0

# Initialize variables (set in subclasses)
self.options = None
Expand Down Expand Up @@ -194,9 +197,12 @@ def run_loop(self):
self.epf['r'] *= self.epf['r_factor'] # increase penalty factor
self.obj_func_tol *= self.epf['tol_factor'] # decrease tolerance
self.obj_func_values = self.fun(self.mean_state, **self.epf)
self.nfev += 1
self.iteration = 1
self.iteration = 0
self.epf_iteration += 1
optimize_result = ot.get_optimize_result(self)
ot.save_optimize_results(optimize_result)
self.nfev += 1
self.iteration = +1
r = self.epf['r']
logger.info(f' -----> EPF-EnOpt: {self.epf_iteration}, {r} (outer iteration, penalty factor)') # print epf info
else:
Expand Down
2 changes: 1 addition & 1 deletion popt/misc_tools/optim_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ def get_optimize_result(obj):
# Initialize dictionary of variables to save
save_dict = OptimizeResult({'success': True, 'x': obj.mean_state, 'fun': np.mean(obj.obj_func_values),
'nit': obj.iteration, 'nfev': obj.nfev, 'njev': obj.njev})
if hasattr(obj, 'epf'):
if hasattr(obj, 'epf') and obj.epf:
save_dict['epf_iteration'] = obj.epf_iteration

if 'savedata' in obj.options:
Expand Down

0 comments on commit 51b79b6

Please sign in to comment.