Sandbox


In [1]:
import sys
import os
sys.path.append("../")  # include the root directory as the main
import pandas as pd
import numpy as np

In [3]:
from aind.isolation import Board
from aind.sample_players import RandomPlayer
from aind.sample_players import GreedyPlayer

In [4]:
# create an isolation board (by default 7x7)
player1 = RandomPlayer()
player2 = GreedyPlayer()
game = Board(player1, player2)

In [6]:
# place player 1 on the board at row 2, column 3, then place player 2 on
# the board at row 0, column 5; display the resulting board state.  Note
# that the .apply_move() method changes the calling object in-place.
game.apply_move((2, 3))
game.apply_move((0, 5))
print(game.to_string())


     0   1   2   3   4   5   6
0  |   |   |   |   |   | 2 |   | 
1  |   |   |   |   |   |   |   | 
2  |   |   |   | 1 |   |   |   | 
3  |   |   |   |   |   |   |   | 
4  |   |   |   |   |   |   |   | 
5  |   |   |   |   |   |   |   | 
6  |   |   |   |   |   |   |   | 


In [7]:
# players take turns moving on the board, so player1 should be next to move
assert(player1 == game.active_player)

# get a list of the legal moves available to the active player
print(game.get_legal_moves())


[(1, 5), (4, 2), (4, 4), (1, 1), (0, 2), (3, 5), (0, 4), (3, 1)]

In [8]:
# get a successor of the current state by making a copy of the board and
# applying a move. Notice that this does NOT change the calling object
# (unlike .apply_move()).
new_game = game.forecast_move((1, 1))
assert(new_game.to_string() != game.to_string())
print("\nOld state:\n{}".format(game.to_string()))
print("\nNew state:\n{}".format(new_game.to_string()))


Old state:
     0   1   2   3   4   5   6
0  |   |   |   |   |   | 2 |   | 
1  |   |   |   |   |   |   |   | 
2  |   |   |   | 1 |   |   |   | 
3  |   |   |   |   |   |   |   | 
4  |   |   |   |   |   |   |   | 
5  |   |   |   |   |   |   |   | 
6  |   |   |   |   |   |   |   | 


New state:
     0   1   2   3   4   5   6
0  |   |   |   |   |   | 2 |   | 
1  |   | 1 |   |   |   |   |   | 
2  |   |   |   | - |   |   |   | 
3  |   |   |   |   |   |   |   | 
4  |   |   |   |   |   |   |   | 
5  |   |   |   |   |   |   |   | 
6  |   |   |   |   |   |   |   | 


In [9]:
# play the remainder of the game automatically -- outcome can be "illegal
# move", "timeout", or "forfeit"
winner, history, outcome = game.play()
print("\nWinner: {}\nOutcome: {}".format(winner, outcome))
print(game.to_string())
print("Move history:\n{!s}".format(history))


Winner: <aind.sample_players.GreedyPlayer object at 0x000001E95C3023C8>
Outcome: illegal move
     0   1   2   3   4   5   6
0  | - |   |   | 2 | - | - |   | 
1  |   |   | - |   |   | - |   | 
2  |   | - | - | - | - |   |   | 
3  |   |   | - | - | - |   | - | 
4  | - |   | - | - | - |   | - | 
5  |   |   | - | - | - | - |   | 
6  |   | - |   | - |   | 1 |   | 

Move history:
[[0, 4], [2, 4], [1, 2], [4, 3], [0, 0], [2, 2], [2, 1], [3, 4], [3, 3], [5, 3], [5, 2], [3, 2], [4, 0], [4, 4], [6, 1], [6, 3], [4, 2], [5, 5], [5, 4], [3, 6], [4, 6], [1, 5], [6, 5], [0, 3]]

Reading the data from the file


In [2]:
import pandas as pd

In [3]:
l_data = []
with open('../data/data_test.txt', 'r') as fr:
    for row in fr:
        if 'Win Rate:' in row:
            l_data.append([float(x.replace('%', '')) for x in row.strip().split()[2:]])

In [4]:
df_tests = pd.DataFrame(l_data, columns=['AB_Improved', 'AB_Custom', 'AB_Custom_2', 'AB_Custom_3'])

In [5]:
df_tests


Out[5]:
AB_Improved AB_Custom AB_Custom_2 AB_Custom_3
0 75.0 67.1 70.0 61.4
1 71.4 73.6 71.4 69.3
2 70.7 70.7 72.9 65.0
3 69.3 70.7 66.4 69.3
4 66.4 73.6 65.7 67.9
5 64.3 72.1 69.3 62.9
6 67.9 69.3 68.6 62.9
7 70.7 65.7 71.4 67.9
8 70.0 71.4 69.3 65.0
9 69.3 72.9 73.6 64.3

In [14]:
import pandas as pd
from scipy.stats import ttest_ind
#performs t-test
b = df_tests.AB_Improved
a = df_tests.AB_Custom
tval, p_value = ttest_ind(a, b, equal_var=False)
print("t-value = {:0.6f}, p-value = {:0.8f}".format(tval, p_value/2))


t-value = 0.969089, p-value = 0.17271971

In [19]:
(a>b).sum()


Out[19]:
5

In [18]:
b = df_tests.AB_Improved
a = df_tests.AB_Custom_2
tval, p_value = ttest_ind(a, b, equal_var=False)
print("t-value = {:0.6f}, p-value = {:0.8f}".format(tval, p_value/2))


t-value = 0.293111, p-value = 0.38642045

In [13]:
b = df_tests.AB_Improved
a = df_tests.AB_Custom_3
tval, p_value = ttest_ind(a, b, equal_var=False)
print("t-value = {:0.6f}, p-value = {:0.8f}".format(tval, p_value/2))


t-value = -3.040156, p-value = 0.00352281

In [ ]: