Passed
Push — master ( fb398d...c64a78 )
by Simon
01:38 queued 11s
created

hyperactive.memory.memory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 66
rs 10
c 0
b 0
f 0
wmc 9

8 Methods

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