-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.py
48 lines (35 loc) · 1.31 KB
/
memory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import torch
import torch.utils.data
class Memory:
observations = []
def add(self, ob, action, next_ob, reward, game_over):
self.observations.append((ob, action, next_ob, reward, game_over))
def dataset(self):
args = (torch.tensor(x, dtype=torch.float32) for x in zip(*self.observations))
return torch.utils.data.TensorDataset(*args)
class DeterministicMemory:
observations = {}
def hash(self, ob, action):
return ( # hash("{ob}{action}")
'ob' + str(ob) +
'action' + str(action)
)
def add(self, ob, action, next_ob, reward, game_over):
key = self.hash(ob, action)
if key not in self.observations:
self.observations[key] = (ob, action, next_ob, reward, game_over)
def dataset(self):
args = (torch.tensor(x, dtype=torch.float32) for x in zip(*self.observations.values()))
return torch.utils.data.TensorDataset(*args)
def get(ob, ac):
key = self.hash(ob, action)
return self.observations[key] if key in self.observations else None
class MemoryGameWrapper:
def __init__(self, game, memory):
self.game = game
self.memory = memory
def reset():
self.ob = self.game.reset()
return self.ob
def step(ac):
self.game.step(ac)