-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistory.py
67 lines (58 loc) · 2.26 KB
/
history.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import numpy
class GameHistory:
"""
Store only usefull information of a self-play game.
"""
def __init__(self):
self.observation_history = []
self.action_history = []
self.reward_history = []
self.to_play_history = []
self.child_visits = []
self.root_values = []
self.reanalysed_predicted_root_values = None
# For PER
self.priorities = None
self.game_priority = None
def store_search_statistics(self, mcts, root, action_space):
# Turn visit count from root into a policy
if root is not None:
self.child_visits.append(mcts.sum_child_visit_count(root.idx))
self.root_values.append(mcts.get_node_value(root.idx))
else:
self.root_values.append(None)
def get_stacked_observations(
self, index, num_stacked_observations, action_space_size
):
"""
Generate a new observation with the observation at the index position
and num_stacked_observations past observations and actions stacked.
"""
# Convert to positive index
index = index % len(self.observation_history)
stacked_observations = self.observation_history[index].copy()
for past_observation_index in reversed(
range(index - num_stacked_observations, index)
):
if 0 <= past_observation_index:
previous_observation = numpy.concatenate(
(
self.observation_history[past_observation_index],
[
numpy.ones_like(stacked_observations[0])
* self.action_history[past_observation_index + 1]
/ action_space_size
],
)
)
else:
previous_observation = numpy.concatenate(
(
numpy.zeros_like(self.observation_history[index]),
[numpy.zeros_like(stacked_observations[0])],
)
)
stacked_observations = numpy.concatenate(
(stacked_observations, previous_observation)
)
return stacked_observations