In this project, we will build a modified hybrid of AlphaGo and AlphaZero for 9x9 Go from almost scratch (using only computational libraries).
Week | Links |
---|---|
1 | Slides, Play Online Go, Computer Go Rules |
2 | Slides, NN Arch, PyTorch CNN example |
3 | Slides, PyTorch Modules |
4 | Slides, SL Description |
5 | Slides |
For a more detailed list of topics and resources, check the most recent "This Week in Mini-AlphaGo" email (released every Wednesday afternoon).
board.py
contains the Board
class, which is a custom Go board interface written specifically for this project. Board
handles all the game logic of Go, including moves, captures, end detection, and scoring. The file contains example usage and docstrings for each function.
It is important to note that Board
objects contain a .grid
, which is a np.NDArray
with shape (size, size)
containing int
s that are 0
(empty), 1
(black stone), or 2
(white stone).
GameNode
inherits from Board
and implements a tree structure with .prev
(the GameNode
leading to self
), .prev_move
(the move tuple that goes from .prev
to self
), and .nexts
(a possibly incomplete list of child GameNode
s).
Examples
Count the number of black stones on a Board
after playing some moves
board = Board(9) # Init empty 9x9 Board
board.play_stone(5, 6) # Black plays at (5, 6)
board.play_stone(0, 1) # White plays at (0, 1)
board.play_stone(0, 0) # Black plays at (0, 0)
print(board) # Visual board representation
print(f"Black stones: {(board.grid == 1).sum()}") # 2
Print history leading up to a GameNode
node = GameNode(9) # Init empty 9x9 GameNode
node = node.create_child((5, 6)) # Black plays at (5, 6)
node = node.create_child((0, 1)) # White plays at (0, 1)
node = node.create_child((0, 0)) # Black plays at (0, 0)
# Print game history in reverse
while node != None:
print(node)
node = node.prev
elo_calculator.py
, contains the Elo_calculator
class, which allows you to play to Bot
s against each other, calculate their updated elos, and store them.
bot.py
has the basic structure for the Bot
class, which will be an abstraction for the Go bots we will build that provides a consistent interface for evaluation.
In the following code:
go = Board(9)
elo = Elo_calculator(game=go, prev_elo_data = "elo_data.pkl")
random_player1 = Bot()
random_player2 = Bot()
elo.register_bot(name= "bob", bot = random_player1)
elo.register_bot(name = "bobette", bot = random_player2)
elo.play_match("bob", "bobette")
elo.save("elo_data.pkl")
We create an elo_calculator object from past elo data, register 2 agents, and run them against each other, saving the data to 'elo_data.pkl'. If you were to run this code again, then the code below would do the same this as the code snippet above:
go = Board(9)
elo = Elo_calculator(game=go, prev_elo_data = "elo_data.pkl")
elo.play_match("bob", "bobette")
elo.save("elo_data.pkl")