diff --git a/cma/evolution_strategy.py b/cma/evolution_strategy.py index 4c9d475..69383ea 100644 --- a/cma/evolution_strategy.py +++ b/cma/evolution_strategy.py @@ -285,6 +285,7 @@ def __init__(self, *args, **kwargs): # TODO: insert takes 30% of the overall CPU time, mostly in def key() # with about 15% of the overall CPU time + # NOTE: Likely improved by reducing calls to def key() on Jan 2026 (PR #336). def insert(self, key, geno=None, iteration=None, fitness=None, value=None, cma_norm=None): """insert an entry with key ``key`` and value @@ -308,10 +309,7 @@ def insert(self, key, geno=None, iteration=None, fitness=None, self.last_solution_index += 1 if value is not None: - try: - iteration = value['iteration'] - except: - pass + iteration = value.get('iteration', iteration) if iteration is not None: if iteration > self.last_iteration: self.last_solution_index = 0 @@ -319,18 +317,18 @@ def insert(self, key, geno=None, iteration=None, fitness=None, else: iteration = self.last_iteration + 0.5 # a hack to get a somewhat reasonable value if value is not None: - self[key] = value + entry = value else: - self[key] = {'pheno': key} + entry = {'pheno': key} if geno is not None: - self[key]['geno'] = geno - if iteration is not None: - self[key]['iteration'] = iteration + entry['geno'] = geno + entry['iteration'] = iteration if fitness is not None: - self[key]['fitness'] = fitness + entry['fitness'] = fitness if cma_norm is not None: - self[key]['cma_norm'] = cma_norm - return self[key] + entry['cma_norm'] = cma_norm + self[key] = entry + return entry class _CMASolutionDict_empty(dict): """a hack to get most code examples running""" diff --git a/cma/transformations.py b/cma/transformations.py index db874e8..55c353b 100644 --- a/cma/transformations.py +++ b/cma/transformations.py @@ -1123,11 +1123,12 @@ def from_bounds(x, copy=False): if archive is not None: try: - x = archive[y]['geno'] + archive_y = archive[y] + x = archive_y['geno'] except (KeyError, TypeError): x = None if x is not None: - if archive[y]['iteration'] < archive.last_iteration: + if archive_y['iteration'] < archive.last_iteration: # no current archived genotype was found!? x = repair_and_flag_change(self, repair, x, copy) # x = repair(x, copy_if_changed=copy)