-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.py
More file actions
196 lines (137 loc) · 6.36 KB
/
node.py
File metadata and controls
196 lines (137 loc) · 6.36 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import go_plot
import Goban
import copy
import random
import numpy as np
import matplotlib.pyplot as plt
class node():
def __init__(self, board, mycolor , gen, terminal_node, final_reward= False, parent = False, move_parent= None):
self.gen = gen
self.board = board
self.mycolor = mycolor
self.parent = parent
self.move_parent = move_parent
self.score = 0
self.nbr_visit = 0
self.possible_moves = [] # ACTION posssible for this node, call has_children
self.children = []
self.terminal_node = terminal_node
self.final_reward = final_reward
def print_state(self):
print("\n----------------")
print("self.score", self.score)
print("self.nbr_visit", self.nbr_visit)
print("self.children", self.children)
print("----------------\n")
def print_arbre(self, child_index=0):
print("\n------")
print("my gen:", self.gen)
print("I'am the ", child_index, "child")
print("nbr_victoire",self.score,"self.nbr_visit", self.nbr_visit)
print("I have ", len(self.children), "children")
if len(self.children) >0:
for child_index,child in enumerate(self.children):
child.print_arbre(child_index)
def mean_sucess_node(self):
return self.score/self.nbr_visit
def is_terminal_node(self):
if len(self.possible_moves) <= 1:
return 1
else:
return 0
# Décrit les actions possible dans self.possible_actions
def check_possible_children(self):
possible_moves = self.board.weak_legal_moves() # Dont use weak_legal_moves() here!
self.possible_moves = possible_moves
def best_child(self, c_params):
children_weights = [child.mean_sucess_node()+c_params* np.sqrt(np.log(self.nbr_visit)/child.nbr_visit)
for child in self.children]
i = np.argmax(children_weights)
best_child = self.children[np.argmax(children_weights)]
if not self.board._gameOver:
self.board.push(best_child.move_parent)
else:
with open('readme.txt', 'a') as file:
file.write("\n best child game over")
return best_child
def select_child(self, c_params= .1):
# tant que j'ai des fils je select, si pas de fils j'expand? si pas noeud terminal
if self.children == []:
with open('readme.txt', 'a') as file:
file.write("\n select child je me retorune moi meme, le mvt de mes parent est"+ str(self.move_parent))
return self
else:
return self.best_child(c_params).select_child()
# Creer les fils possible a partir d'un noeud
def expand(self):
self.check_possible_children()
self.terminal_node = self.board._gameOver
with open('readme.txt', 'a') as file:
file.write("\n possible_moves: game over"+ str(self.terminal_node))
for move in self.possible_moves:
file.write(str(move)+ " ")
if self.terminal_node:
with open('readme.txt', 'a') as file:
file.write("\n\n\n ICIIIIICICICIICIC:")
pass
else:
for move in self.possible_moves:
is_valid = self.board.push(move)
if is_valid:
if self.board.is_game_over():
with open('readme.txt', 'a') as file:
file.write("\n Game over expand:")
result = self.board.result()
terminal_node = True
if self.mycolor == "black":
final_reward = 1 if result == "0-1" else 0
else:
final_reward = 1 if result == "1-0" else 0
# Creation of child
child_node = node(self.board, self.mycolor, self.gen+1, terminal_node, final_reward, self, move)
# add child to children list
self.children.append(child_node)
else:
terminal_node = False
final_reward = False
# Creation of child
child_node = node(self.board, self.mycolor, self.gen+1, terminal_node, final_reward, self, move)
# add child to children list
self.children.append(child_node)
self.board.pop()
# Simulation du jeu
def rollout(self):
# faire revenir le board à son état
if self.terminal_node:
return self.final_reward
idx_push = 0
# Playing until end
while not self.board.is_game_over():
all_moves = self.board.weak_legal_moves()
move = random.choice(all_moves)
is_valid = self.board.push(move)
while not is_valid:
self.board.pop()
move = random.choice(all_moves)
is_valid = self.board.push(move)
idx_push +=1
result = self.board.result()
if self.mycolor == "black":
final_reward = 1 if result == "0-1" else 0
else:
final_reward = 1 if result == "1-0" else 0
[self.board.pop() for _ in range(idx_push)]
# with open('readme.txt', 'a') as file:
# file.write("\ndel rollout")
return final_reward
def backpropag(self, result):
#print("gen:",self.gen)
#print("score:", self.score, self.nbr_visit)
#print("result is added", result)
self.score += result # 0 ou 1
self.nbr_visit += 1
# print("here",self.nbr_visit)
# with open('readme.txt', 'a') as file:
# file.write("here"+str(self.nbr_visit))
if self.parent:
self.parent.backpropag(result)