-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
102 lines (75 loc) · 3.19 KB
/
main.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
from games import *
import time
import random
import math
import hashlib
import logging
import argparse
from mcts import *
initial_state = {'P': True, 'Q': False, 'R': True, 'S': False, 'T': True, 'U': False, 'V': True, 'W': False, 'X': True, 'Y': False,
'P11': True, 'Q11': False,'R11': True, 'S11': False, 'T11': True, 'U11': False,'V11': True, 'W11': False,'X11': True, 'Y11': False,
'P22': True, 'Q22': False, 'R22': True, 'S22': False}
start_time = time.time()
#minimax_decision(initial_state,gameofgoals)
#expectiminimax(initial_state,gameofgoals)
#alphabeta_cutoff_search(initial_state,gameofgoals,30)
#print("--- %s seconds ---" % (time.time() - start_time))
#===========================================================
#==================MCTS==========================
#===========================================================
NUM_TURNS = 5
class AntasState():
def __init__(self, current=[0] * 2 * NUM_TURNS, turn=0):
self.current = current
self.turn = turn
self.num_moves = (114 - self.turn) * (114 - self.turn - 1)
def next_state(self):
availableActions = [x for x in range(1, 115)]
for c in self.current:
if c in availableActions:
availableActions.remove(c)
player1action = random.choice(availableActions)
availableActions.remove(player1action)
nextcurrent = self.current[:]
nextcurrent[self.turn] = player1action
player2action = random.choice(availableActions)
availableActions.remove(player2action)
nextcurrent[self.turn + NUM_TURNS] = player2action
next = AntasState(current=nextcurrent, turn=self.turn + 1)
return next
def terminal(self):
if self.turn == NUM_TURNS:
return True
return False
def reward(self):
r = random.uniform(0, 1) # ANTAS, put your own function here
return r
def __hash__(self):
return int(hashlib.md5(str(self.current).encode('utf-8')).hexdigest(), 16)
def __eq__(self, other):
if hash(self) == hash(other):
return True
return False
def __repr__(self):
s = "CurrentState: %s; turn %d" % (self.current, self.turn)
return s
if __name__ == "__main__":
num_sims = 13000
#parser = argparse.ArgumentParser(description='MCTS research code')
#parser.add_argument('--num_sims', action="store", required=True, type=int,
# help="Number of simulations to run, should be more than 114*113")
#args = parser.parse_args()
current_node = Node(AntasState())
start_time = time.time()
# minimax_decision(initial_state,gameofgoals)
# expectiminimax(initial_state,gameofgoals)
# alphabeta_cutoff_search(initial_state,gameofgoals,30)
for l in range(NUM_TURNS):
current_node = UCTSEARCH(num_sims / (l + 1), current_node)
print("level %d" % l)
print("Num Children: %d" % len(current_node.children))
for i, c in enumerate(current_node.children):
print(i, c)
print("Best Child: %s" % current_node.state)
print("--------------------------------")
print("--- %s seconds ---" % (time.time() - start_time))