-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainMonteCarlo.py
100 lines (79 loc) · 2.14 KB
/
mainMonteCarlo.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
import gameloop
import pygame
import direction
import random
import debug
#seed = random.randint(0, 1<<32 -1)
seedInt = 12
debug.say("seed : " + str(seedInt))
random.seed(seedInt)
DEPTH = 6
WIDTH = 100
class Node:
def __init__(self, value, board):
self.value = float(value)
self.board = board
self.children = {}
def _investigate(self, decision):
if decision not in self.children :
value, board = self.board.potentialEndturn(decision)
child = Node(value, board)
self.children[decision] = child
return child # a new Node was created
return self.children[decision] # found an existing Node
def getValue(self):
# mean of the children's value
if(self.children):
#debug.say(str(len(self.children)))
value = 0.
for child in self.children.values():
value += child.getValue()
value /= len(self.children) #direction.ALL)
value += self.value
value /=2
return value
return self.value
def investigate(self):
depth = 0
node = self
path = []
while depth < DEPTH:
depth += 1
possibility = random.choice(direction.ALL)
node = node._investigate(possibility)
path.append(possibility)
debug.say(" -> ".join(map(str, path)) + " : " + str(node.value))
return node
class MCTSPlayer:
# constructor : receives the gamestate and turn time
def __init__(self, timeStep):
self.timeStep = timeStep
self.decision = None
# the AI computations should go here
def think(self, board):
decision = None
root = Node(0, board)
debug.say("")
for _ in xrange(WIDTH):
root.investigate()
if root.children:
paths = {}
for possibility, node in root.children.items():
value = node.getValue()
paths[possibility] = value
debug.say(str(possibility) + " : " + str(value))
list = sorted(paths, key = paths.get)
decision = list[-1]
debug.say("chosen : " + str(decision))
self.decision = decision
# to handle keyStrokes
def handleKey(self, event):
pass
# returns the player's decision
def getDecision(self):
return self.decision
def getPlayer(timeStep):
return MCTSPlayer(timeStep)
gameloop.getPlayer = getPlayer
application = gameloop.Game()
application.run()