Skip to content

Commit 429128d

Browse files
committed
Improve performance by reducing redundant dictionary fetches.
1 parent f90bb5c commit 429128d

File tree

3 files changed

+19
-21
lines changed

3 files changed

+19
-21
lines changed

cma/evolution_strategy.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,6 @@ def __init__(self, *args, **kwargs):
283283
super(_CMASolutionDict_functional, self).__init__(*args, **kwargs)
284284
self.last_solution_index = 0
285285

286-
# TODO: insert takes 30% of the overall CPU time, mostly in def key()
287-
# with about 15% of the overall CPU time
288286
def insert(self, key, geno=None, iteration=None, fitness=None,
289287
value=None, cma_norm=None):
290288
"""insert an entry with key ``key`` and value
@@ -301,36 +299,35 @@ def insert(self, key, geno=None, iteration=None, fitness=None,
301299
# archive returned solutions, first clean up archive
302300
if iteration is not None and iteration > self.last_iteration and (iteration % 10) < 1:
303301
self.truncate(300, iteration - 3)
304-
elif value is not None and value.get('iteration'):
305-
iteration = value['iteration']
306-
if (iteration % 10) < 1:
307-
self.truncate(300, iteration - 3)
302+
elif value is not None:
303+
iteration_tmp = value.get('iteration')
304+
if iteration_tmp:
305+
iteration = iteration_tmp
306+
if (iteration % 10) < 1:
307+
self.truncate(300, iteration - 3)
308308

309309
self.last_solution_index += 1
310310
if value is not None:
311-
try:
312-
iteration = value['iteration']
313-
except:
314-
pass
311+
iteration = value.get('iteration', iteration)
315312
if iteration is not None:
316313
if iteration > self.last_iteration:
317314
self.last_solution_index = 0
318315
self.last_iteration = iteration
319316
else:
320317
iteration = self.last_iteration + 0.5 # a hack to get a somewhat reasonable value
321318
if value is not None:
322-
self[key] = value
319+
entry = value
323320
else:
324-
self[key] = {'pheno': key}
321+
entry = {'pheno': key}
325322
if geno is not None:
326-
self[key]['geno'] = geno
327-
if iteration is not None:
328-
self[key]['iteration'] = iteration
323+
entry['geno'] = geno
324+
entry['iteration'] = iteration
329325
if fitness is not None:
330-
self[key]['fitness'] = fitness
326+
entry['fitness'] = fitness
331327
if cma_norm is not None:
332-
self[key]['cma_norm'] = cma_norm
333-
return self[key]
328+
entry['cma_norm'] = cma_norm
329+
self[key] = entry
330+
return entry
334331

335332
class _CMASolutionDict_empty(dict):
336333
"""a hack to get most code examples running"""

cma/transformations.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,12 +1122,13 @@ def from_bounds(x, copy=False):
11221122
return x # not change, no copy
11231123

11241124
if archive is not None:
1125+
archive_y = archive[y]
11251126
try:
1126-
x = archive[y]['geno']
1127+
x = archive_y['geno']
11271128
except (KeyError, TypeError):
11281129
x = None
11291130
if x is not None:
1130-
if archive[y]['iteration'] < archive.last_iteration:
1131+
if archive_y['iteration'] < archive.last_iteration:
11311132
# no current archived genotype was found!?
11321133
x = repair_and_flag_change(self, repair, x, copy)
11331134
# x = repair(x, copy_if_changed=copy)

cma/utilities/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ def __setitem__(self, key, value):
675675
self.data_with_same_key[key].insert(0, value)
676676
else:
677677
if key in self.data_with_same_key:
678-
self.data_with_same_key[key] += [self.data[key]]
678+
self.data_with_same_key[key].append(self.data[key])
679679
elif key in self.data:
680680
self.data_with_same_key[key] = [self.data[key]]
681681
self.data[key] = value

0 commit comments

Comments
 (0)