class SolutionDict(DerivedDictBase):
Known subclasses: cma.evolution_strategy._CMASolutionDict_functional
Constructor: SolutionDict(*args, **kwargs)
dictionary with computation of an hash key.
The hash key is generated from the inserted solution and a stack of previously inserted same solutions is provided. Each entry is meant to store additional information related to the solution.
>>> import cma.utilities.utils as utils, numpy as np >>> d = utils.SolutionDict() >>> x = np.array([1,2,4]) >>> d[x] = {'f': sum(x**2), 'iteration': 1} >>> assert d[x]['iteration'] == 1 >>> assert d.get(x) == (d[x] if d.key(x) in d.keys() else None) >>> y = [1,2,4] >>> d[y] = {'f': sum([n ** 2 for n in y]), 'iteration': 1} >>> assert d[y]['iteration'] == 1 >>> assert d.get(y) == (d[y] if d.key(y) in d.keys() else None) >>> d[2] = 3 >>> assert d[2] == 3
data_with_same_key behaves (in contrast to the order of keys())
like a stack (see setitem and delitem). Thereby the last assignment
self[key] = ... determines, as to be expected, the value of
self[key] even when self.data_with_same_key[key] is not empty.
If the attribute behave_as_queue is set to True, it behaves like a
queue (FIFO).
Technical detail: after a same-key item was removed, self[key] returns the stacked (or queued) previous value, however self._unhashed_keys[key] still returns the last unhashed key which could be a different object than the previous but with the same hash key. However, the object has most likely the correct content because the hash was the same. This happens because only the last unhashed key is kept. As long as the content of self._unhashed_keys[key] is not changed, this should be fine.
TODO: in .truncate, the iteration key is used to clean up without
error management.
| Method | __contains__ |
Undocumented |
| Method | __delitem__ |
remove only most current key-entry of list with same keys |
| Method | __getitem__ |
define access self[key] |
| Method | __init__ |
Undocumented |
| Method | __setitem__ |
define self[key] = value |
| Method | key |
compute key of x |
| Method | truncate |
truncate to max_len/2 when len(self) > max_len. |
| Method | truncate |
truncate to length len_ removing the oldest entries |
| Instance Variable | behave |
Undocumented |
| Instance Variable | data |
Undocumented |
| Instance Variable | last |
Undocumented |
| Instance Variable | _unhashed |
Undocumented |
Inherited from DerivedDictBase:
| Method | __iter__ |
Undocumented |
| Method | __len__ |
Undocumented |
| Instance Variable | data |
Undocumented |
truncate to max_len/2 when len(self) > max_len.
Only truncate entries with 'iteration' key smaller than
min_iter if given.
This looks overdesigned given the dict is ordered and chunk
deletion is not available. See also truncate_to.