Passed
Push — master ( bf6a06...0a901a )
by Simon
01:26
created

hyperactive.extensions.memory.memory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 63
rs 10
c 0
b 0
f 0
wmc 9

8 Methods

Rating   Name   Duplication   Size   Complexity  
A LongTermMemory._get_func_str() 0 2 1
A LongTermMemory._obj2hash() 0 9 2
A LongTermMemory.save_memory() 0 2 1
A Memory.__init__() 0 13 1
A LongTermMemory.load_memory() 0 2 1
A LongTermMemory.__init__() 0 5 1
A ShortTermMemory.__init__() 0 2 1
A LongTermMemory._get_hash() 0 2 1
1
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
import hashlib
6
import inspect
7
8
import numpy as np
9
10
from .memory_load import MemoryLoad
11
from .memory_dump import MemoryDump
12
13
14
class Memory:
15
    def __init__(self, _space_, _main_args_, _cand_):
16
        self._space_ = _space_
17
        self._main_args_ = _main_args_
18
19
        self.pos_best = None
20
        self.score_best = -np.inf
21
22
        self.memory_type = _main_args_.memory
23
        self.memory_dict = {}
24
25
        self.meta_data_found = False
26
27
        self.n_dims = None
28
29
30
class ShortTermMemory(Memory):
31
    def __init__(self, _space_, _main_args_, _cand_):
32
        super().__init__(_space_, _main_args_, _cand_)
33
34
35
class LongTermMemory(Memory):
36
    def __init__(self, _space_, _main_args_, _cand_):
37
        super().__init__(_space_, _main_args_, _cand_)
38
39
        self._load_ = MemoryLoad(_space_, _main_args_, _cand_, self.memory_dict)
40
        self._dump_ = MemoryDump(_space_, _main_args_, _cand_, self.memory_dict)
41
42
    def load_memory(self, _cand_, _verb_):
43
        self._load_._load_memory(_cand_, _verb_)
44
45
    def save_memory(self, _main_args_, _opt_args_, _cand_):
46
        self._dump_._save_memory(_main_args_, _opt_args_, _cand_)
47
48
    def _get_hash(self, object):
49
        return hashlib.sha1(object).hexdigest()
50
51
    def _get_func_str(self, func):
52
        return inspect.getsource(func)
53
54
    def _obj2hash(self):
55
        obj2hash_dict = {}
56
        para_hash_list = self._get_para_hash_list()
57
58
        for para_hash in para_hash_list:
59
            obj = self._read_dill(para_hash)
60
            obj2hash_dict[para_hash] = obj
61
62
        return obj2hash_dict
63