-
Notifications
You must be signed in to change notification settings - Fork 0
/
mstc.py
101 lines (85 loc) · 2.97 KB
/
mstc.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
import numpy as np
import chess,chess_AI
import random
class Node():
def __init__(self,gs):
self.parent = None
self.children = []
self.rewards = 0
self.visited = 0
self.gs = gs
self.done = False
self.action = None
def simulation(self,depth):
ans = 0.5
pridictor = chess_AI.Evaluate()
z = 0
if len(self.gs.getValidMoves()) == 0: #if the game is over
ans = 0 if self.gs.white_to_move else 1
for i in range(depth):
z = z + 1
moves = self.gs.getValidMoves()
if len(moves) == 0:
break
move = np.random.choice(moves)
self.gs.makeMove(move)
if len(self.gs.getValidMoves()) == 0: #if the game is over
ans = 0 if self.gs.white_to_move else 1
else:
ans = pridictor.evaluate(self.gs)/(self.visited+1)
for _ in range(z):
self.gs.undoMove()
return ans
def cal(self,c):
next_child = None
max = -np.inf
min = np.inf
self.rewards = 0
if self.gs.white_to_move:
for i in self.children:
if i.visited == 0:
i.rewards+=i.simulation(100)
if max <(i.rewards + c*np.sqrt(np.log(self.visited)/(i.visited+1))):
next_child = i
max = i.rewards + c*np.sqrt(np.log(self.visited)/(i.visited+1))
self.rewards = self.rewards + i.rewards
else :
for i in self.children:
if i.visited == 0:
i.rewards+=i.simulation(100)
if min > (i.rewards + c*np.sqrt(np.log(self.visited)/(i.visited+1))):
next_child = i
min = i.rewards + c*np.sqrt(np.log(self.visited)/(i.visited+1))
self.rewards = self.rewards + i.rewards
return min,next_child
return max,next_child
def add(self):
if self.visited == 0:
self.visited = self.visited + 1
moves = self.gs.getValidMoves()
if len(moves) == 0:
self.done = True
return
for i in moves:
self.gs.makeMove(i)
self.children.append(Node(self.gs))
self.children[len(self.children)-1].action = i
self.children[len(self.children)-1].parent = self
self.gs.undoMove()
else:
self.visited = self.visited + 1
def update(self, reward):
current = self
while current!= None:
current.rewards += (0.01)*reward
current.visited+=1
current = current.parent
def start(self, depth):
next_child = None
reward = None
self.add()
if self.done:
return None
reward,next_child = self.cal(1)
self.update(reward=reward)
return next_child