-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.py
More file actions
76 lines (67 loc) · 3.08 KB
/
Copy pathnode.py
File metadata and controls
76 lines (67 loc) · 3.08 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
from math import sqrt, log
class Node:
'''
children visits are specific to nodes
'''
def __init__(self,next_turn,last_turn,terminal_bool,terminal_result,terminal_move,
legal_children_moves,hash_value,hash_table,possible_moves,anti_terminal,anti_terminal_move):
self.possible_moves = possible_moves
self.hash_value = (hash_value[0],hash_value[1])
self.terminal_bool = terminal_bool
self.terminal_result = terminal_result
self.terminal_move = terminal_move
self.anti_terminal = anti_terminal
self.anti_terminal_move = anti_terminal_move
self.next_turn = next_turn
self.last_turn = last_turn
self.wins = 0
#adding this so it can go for tie worst comes to worst
self.losses = 0
self.ties = 0
self.visits = 0
self.legal_children_moves = legal_children_moves
self.children_hashes = []
self.children = []
self.children_moves = []
self.add_self_to_dic(hash_table)
def add_self_to_dic(self,hash_table):
hash_table[self.hash_value] = self
def back_propugate(self,rollout_value,node_branch):
'''
node branch is the trail of nodes taken, have to be specific because multiple parents
'''
# node_branch.append(self)
for node in node_branch:
if node.last_turn == rollout_value:
node.wins += 1
elif rollout_value == 0:
node.wins += .1
node.visits +=1
def ucb_select(self, exploration = 1, if_print = False):
'''
Need to update child visited in this step because child_node.visits don't correlate to child_visited per node
I might just try to rig this for now by applying the right value, so it will be redundant informartion
'''
ucb_ratios = []
for n in range(len(self.children)):
child = self.children[n]
if child.terminal_bool == True:
ratio = 0
else:
ratio = (child.wins/child.visits) + (exploration * (sqrt(log(self.visits) / child.visits)))
ucb_ratios.append(ratio)
return ucb_ratios.index(max(ucb_ratios))
def child_most_visited(self):
s = max(self.children,key=lambda c: c.visits)
return s
def turn_most_visited(self):
s = max(self.children,key=lambda c: c.visits)
i = self.children_moves[self.children.index(s)]
return i
def examine_node(self):
print(
f'self:: win_ratio:{self.visits-self.wins}/{self.visits} | legal_children:{self.legal_children_moves} | total children: {len(self.children)} | possible_moves:{self.possible_moves} | hash:{self.hash_value}')
for i,child in enumerate(self.children):
print("***")
print(f'child:: move:{self.children_moves[i]+1} | win_ratio:{child.wins}/{child.visits} | legal_children:{child.legal_children_moves} | possible_moves:{child.possible_moves} | hash:{child.hash_value}')
print('-----------------------------------------------------')