-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameSimulator.py
109 lines (48 loc) · 2.6 KB
/
GameSimulator.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from Node import Node
import json
import random
class GameSimulator:
def __init__(self, M, game_manager):
self.M = M
self.game_manager = game_manager
def run_simulations(self, root):
for i in range(self.M):
leaf = self.tree_search(root)
self.node_expansion(leaf)
new_leaf = self.choose_rollout_root(leaf)
winner = self.rollout(new_leaf)
self.backpropagate(new_leaf, winner)
def tree_search(self, root):
current_node = root
while not current_node.is_leaf():
current_node = current_node.best_traverse()
return current_node
def node_expansion(self, leaf):
game_state = leaf.game_state
next_states = self.game_manager.get_all_next_states(game_state)
if(next_states == None):
return leaf
for state in next_states:
new_node = Node(state, leaf)
leaf.add_child(new_node)
def choose_rollout_root(self, node):
return random.choice(list(node.children.keys())) if not node.is_leaf() else node
def rollout(self, node):
current_game_state = node.game_state
while not self.game_manager.is_finished(current_game_state):
current_game_state = self.random_move(current_game_state)
return self.game_manager.get_winner(current_game_state)
def random_move(self, game_state):
next_states = self.game_manager.get_all_next_states(game_state)
return random.choice(next_states)
def backpropagate(self, leaf, winner):
current_node = leaf
while(current_node.parent != None):
current_node.visited += 1
current_node.parent.children[current_node]["n(s, a)"] += 1
if(winner == 1):
current_node.parent.children[current_node]["q(s, a)"] += 1
else:
current_node.parent.children[current_node]["q(s, a)"] += -1
current_node = current_node.parent
current_node.visited += 1