class documentation

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_to truncate to length len_ removing the oldest entries
Instance Variable behave_as_queue Undocumented
Instance Variable data_with_same_key Undocumented
Instance Variable last_iteration Undocumented
Instance Variable _unhashed_keys Undocumented

Inherited from DerivedDictBase:

Method __iter__ Undocumented
Method __len__ Undocumented
Instance Variable data Undocumented
def __contains__(self, key):
def __delitem__(self, key):

remove only most current key-entry of list with same keys

def __getitem__(self, key):

define access self[key]

def __init__(self, *args, **kwargs):
def __setitem__(self, key, value):

define self[key] = value

def key(self, x):

compute key of x

def truncate(self, max_len, min_iter=None):

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.

def truncate_to(self, len_):

truncate to length len_ removing the oldest entries

behave_as_queue: bool =

Undocumented

data_with_same_key: dict =

Undocumented

last_iteration: int =
_unhashed_keys: dict =

Undocumented