Skip to content
Snippets Groups Projects
Unverified Commit 19ec737b authored by Gabriel Santamaria's avatar Gabriel Santamaria
Browse files

Updated statistics

parent f9e27def
No related branches found
No related tags found
No related merge requests found
......@@ -52,9 +52,10 @@ def index(ply: Player):
# that will make MCTS win most of the time.
ucb = [UCB.ucb, UCB.ucb_tuned]
weights = np.arange(0, 10, 1)
levels = np.arange(3, 10, 1)
def game(ce, fucb, i):
def game(ce, fucb, level, i):
"""
Run a single game with the given parameters
"""
......@@ -64,11 +65,11 @@ def game(ce, fucb, i):
if i % 2 == 1:
player_one = StrategyAlphaBeta(grid, rules, 1, depth=20)
player_two = MCTSStrategy(
grid, rules, 2, minimax_level=10, exploration_weight=ce, ucb=fucb
grid, rules, 2, minimax_level=level, exploration_weight=ce, ucb=fucb
)
else:
player_one = MCTSStrategy(
grid, rules, 1, minimax_level=10, exploration_weight=ce, ucb=fucb
grid, rules, 1, minimax_level=level, exploration_weight=ce, ucb=fucb
)
player_two = StrategyAlphaBeta(grid, rules, 2, depth=20)
......@@ -107,7 +108,7 @@ def game(ce, fucb, i):
return int(rules.has_won(grid, ply_mcts)), ttime, niter
def run(ce, fucb, n=50, pbar=None):
def run(ce, fucb, level, n=NITER_PER_GAME, pbar=None):
"""
Runs n games with the given parameters and returns the
number of wins for each player and the total time spent
......@@ -119,7 +120,7 @@ def run(ce, fucb, n=50, pbar=None):
results = []
for i in range(n):
results.append(game(ce, fucb, i))
results.append(game(ce, fucb, level, i))
if pbar:
pbar.update(1)
......@@ -136,16 +137,17 @@ def iterate():
Gently iterate over the parameters space and gets
the data from the run function
"""
wins = np.zeros((len(ucb), len(weights)), dtype=np.float64)
times = np.zeros((len(ucb), len(weights)), dtype=np.float64)
wins = np.zeros((len(ucb), len(weights), len(levels)), dtype=np.float64)
times = np.zeros((len(ucb), len(weights), len(levels)), dtype=np.float64)
niter = len(ucb) * len(weights)
niter = len(ucb) * len(weights) * len(levels)
with tqdm(total=niter * NITER_PER_GAME, desc="Computing statistics...") as pbar:
for i, u in enumerate(ucb):
for j, w in enumerate(weights):
nwins, t, n, niters = run(w, u, pbar=pbar)
wins[i, j] = nwins / n * 100
times[i, j] = t / niters
for k, l in enumerate(levels):
nwins, t, n, niters = run(w, u, l, pbar=pbar)
wins[i, j, k] = nwins / n * 100
times[i, j, k] = t / niters
return wins, times
......@@ -155,11 +157,12 @@ def write_csv(wins, times):
Write the results to a CSV file
"""
with open("stats.csv", "w", encoding="utf-8") as f:
f.write("UCB,Weight,Wins,Time\n")
f.write("UCB,Weight,Minimax Level,Wins,Time\n")
for i, _ in enumerate(ucb):
for j, _ in enumerate(weights):
f.write(f"{i},{j},{wins[i, j]},{times[i, j]}\n")
for k, _ in enumerate(levels):
f.write(f"{i},{j},{k}, {wins[i, j, k]},{times[i, j, k]}\n")
data_wins, data_times = iterate()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment