Learn AlphaGo & Python (07)

Source: Deep Learning on Medium

Create zobrist hash to improve performance. Rename dlgo/goboard_slow.py to dlgo/goboard.py

🐍 mv dlgo/goboard_slow.py dlgo/goboard.py

Create generate_zobrist_hashes.py to generate zobrist hash and save it in file dlgo/zobrist.py. There are 19 * 19 * 2 = 722 hash entries.

🐍 vim generate_zobrist_hashes.py
🐍 python3 generate_zobrist_hashes.py > dlgo/zobrist.py
🐍 wc -l dlgo/zobrist.py
730 dlgo/zobrist.py

generate_zobrist_hashes.py

import random

from dlgo.gotypes import Player, Point
def to_python(player_state):
if player_state is None:
return 'None'
if player_state == Player.black:
return Player.black
return Player.white
MAX63 = 0x7ffffffffffffffftable = {}
empty_board = 0
for row in range(1, 20):
for col in range(1, 20):
for state in (Player.black, Player.white):
code = random.randint(0, MAX63)
table[Point(row, col), state] = code
print('from .gotypes import Player, Point')
print('')
print("__all__ = ['HASH_CODE', 'EMPTY_BOARD']")
print('')
print('HASH_CODE = {')
for (pt, state), hash_code in table.items():
print(' (%r, %s): %r,' % (pt, to_python(state), hash_code))
print('}')
print('')
print('EMPTY_BOARD = %d' % (empty_board,))

Show first 20 lines of dlgo/zobrist.py:

🐍 head -n 20 dlgo/zobrist.py

First 20 lines of dlgo/zobrist.py:

from .gotypes import Player, Point__all__ = ['HASH_CODE', 'EMPTY_BOARD']HASH_CODE = {
(Point(row=1, col=1), Player.black): 1743318457860286132,
(Point(row=1, col=1), Player.white): 6077592620268640577,
(Point(row=1, col=2), Player.black): 5247304070065423072,
(Point(row=1, col=2), Player.white): 3396283731204976049,
(Point(row=1, col=3), Player.black): 8022707334311970093,
(Point(row=1, col=3), Player.white): 2105848293139671387,
(Point(row=1, col=4), Player.black): 1699706885907749284,
(Point(row=1, col=4), Player.white): 3230500686802259798,
(Point(row=1, col=5), Player.black): 7389518629523105701,
(Point(row=1, col=5), Player.white): 7464805636137369601,
(Point(row=1, col=6), Player.black): 959081238933504817,
(Point(row=1, col=6), Player.white): 6047429569262711179,
(Point(row=1, col=7), Player.black): 6961206778848968522,
(Point(row=1, col=7), Player.white): 4609973287293947835,
(Point(row=1, col=8), Player.black): 7590471621530264626,

Show last 20 lines of dlgo/zobrist.py:

🐍 tail -n 20 dlgo/zobrist.py

Last 20 lines of dlgo/zobrist.py:

 (Point(row=19, col=11), Player.white): 7731002159384008752,
(Point(row=19, col=12), Player.black): 8926271898120833506,
(Point(row=19, col=12), Player.white): 3737940356998393611,
(Point(row=19, col=13), Player.black): 6855143523350797896,
(Point(row=19, col=13), Player.white): 3998144684427552885,
(Point(row=19, col=14), Player.black): 1583619279007959424,
(Point(row=19, col=14), Player.white): 8656479320519039671,
(Point(row=19, col=15), Player.black): 7450373251714384625,
(Point(row=19, col=15), Player.white): 1431220503631063432,
(Point(row=19, col=16), Player.black): 3445681469884545256,
(Point(row=19, col=16), Player.white): 1266150490074752899,
(Point(row=19, col=17), Player.black): 2266936888251877784,
(Point(row=19, col=17), Player.white): 2683496064820486440,
(Point(row=19, col=18), Player.black): 8082726413353721878,
(Point(row=19, col=18), Player.white): 6349636703857523885,
(Point(row=19, col=19), Player.black): 8589908489541376170,
(Point(row=19, col=19), Player.white): 6874551449332124093,
}
EMPTY_BOARD = 0