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

LongTermMemory.save_memory()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 4
dl 0
loc 2
rs 10
c 0
b 0
f 0
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