-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
97 lines (76 loc) · 3.18 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
import os
import random
import numpy as np
import tqdm
from game import Game,MonteCarloPlayer_classic,MonteCarloPNSPlayer, Move, Player
from board import Board
from tree import MonteCarloTreeSearchNode
from time import time
class RandomPlayer(Player):
def __init__(self) -> None:
super().__init__()
def make_move(self, game: 'Game') -> tuple[tuple[int, int], Move]:
from_pos = (random.randint(0, 4), random.randint(0, 4))
move = random.choice([Move.TOP, Move.BOTTOM, Move.LEFT, Move.RIGHT])
return from_pos, move
if __name__ == '__main__':
losing_board = []
results = {}
my_player_id = 0
players = np.empty(2, dtype=Player)
tot = 1000
# cross validation backbone to find best hyperparameters
# this below is the best configuration found if we consider a performance/execution_time tradeoff
duration = 0.5 #in terms of seconds
# for ns in [1000]:
# for cp in [0.1]:
#wins and matches for accuracy
wins = 0
matches = 0
ns = 1
cp = 0.5
#play tot games
for i in tqdm.tqdm(range(tot)):
my_player_id = random.randint(0, 1)
opposer = 1 - my_player_id
print(f"\nmy_player_id: {my_player_id}")
g = Game()
#g.print()
# player initialization -> our player is players[my_player_id]
minmax_depth = 1
MR_hybrid = False # if True, the player will use the Minimax hybrid algorithm for the rollout
players[my_player_id] = MonteCarloPNSPlayer(player_id=my_player_id,duration=duration, c_param=0.5, pn_param=0.5,MR_hybrid = MR_hybrid,minimax_depth=minmax_depth)
root_classic = MonteCarloTreeSearchNode(state=Board(), player_id=opposer, d=0, id=0,root_player=opposer, num_simulations=ns,c_param=cp)
#players[opposer] = MonteCarloPlayer_classic(root=root_classic, player_id=opposer,num_simulations=ns, c_param=cp,duration = duration)
players[opposer] = RandomPlayer()
# play the game
winner = g.play(players[0], players[1])
g.print()
print(f"Winner: Player {winner}")
matches += 1
#update accuracy
if winner == my_player_id:
print("My player WON!")
wins += 1
else:
#g.print()
print("My player LOST!")
losing_board.append(g.get_board())
acc = 100*float(wins)/float(matches)
print(f"Winning rate: {acc}% with {wins} wins on {matches} matches" )
#print accuracy
acc = 100*float(wins)/float(matches)
print("accuracy: ",acc )
#save results
results[str(ns) + "-"+ str(cp)] = acc
# with open('results.txt', 'a' if os.path.isfile('results.txt') else 'w') as file:
# file.write("\nConfig: "+ str(str(ns) + "-"+ str(cp)))
# file.write("\n")
# file.write(str(results))
# print(results)
# with open('lost.txt', 'a' if os.path.isfile('lost.txt') else 'w') as file:
# file.write("\nConfig: "+ str(str(ns) + "-"+ str(cp)))
# file.write("\n")
# file.write(str(losing_board))
# print(losing_board)
# MonteCarloTreeSearchNode.main()