Created
November 15, 2017 02:56
-
-
Save CamDavidsonPilon/fa4ebe9a20b87a5abf5ac946772d7191 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from numpy.linalg import matrix_power | |
from matplotlib import pyplot as plt | |
import seaborn as sns | |
SIZE = 100 | |
M = np.zeros((SIZE, SIZE)) | |
# encoding rolls of die | |
for y in xrange(SIZE): | |
if y <= (SIZE - 6 - 1): | |
M[y, np.arange(y+1, y+7)] = 1 | |
elif y == SIZE - 1: # end, absorbing state | |
pass | |
else: | |
# this should be checked more carefully. This is the "bounce-off" behaviour. | |
M[y, (y+1):] = 1 | |
M[y, (SIZE-(6 - (SIZE-y-2))):SIZE-1] += 1 | |
def slip_effect(M, start_node, end_node): | |
""" | |
Ex: if am in position 1, and then roll a 2, I should temporarily be on position 3 before slipping to position 13. Thus everywhere that _should_ have landed me on position 3 should instead be moved to position 13, and all position 3s should be erased. | |
""" | |
ix, = np.where(M[:, start_node]) | |
M[ix, end_node] += 1 | |
M[:, start_node] = 0 | |
return M | |
# encoding of snakes | |
M = slip_effect(M, 16, 6) | |
M = slip_effect(M, 86, 23) | |
M = slip_effect(M, 98, 77) | |
M = slip_effect(M, 63, 59) | |
M = slip_effect(M, 61, 18) | |
M = slip_effect(M, 94, 74) | |
M = slip_effect(M, 92, 72) | |
M = slip_effect(M, 53, 33) | |
# encoding of ladders | |
M = slip_effect(M, 3, 13) | |
M = slip_effect(M, 8, 30) | |
M = slip_effect(M, 19, 37) | |
M = slip_effect(M, 27, 83) | |
M = slip_effect(M, 39, 59) | |
M = slip_effect(M, 50, 66) | |
M = slip_effect(M, 62, 80) | |
M = slip_effect(M, 70, 90) | |
M = M/6.0 | |
def reshape_vector(v): | |
M10 = v.reshape(10, 10) | |
M10[1::2, :] = M10[1::2, ::-1] | |
return np.flip(M10, 0) | |
def plot(M, label): | |
plt.figure() | |
labels = reshape_vector(np.arange(1, 101)) | |
ax = sns.heatmap(M, linewidths=.5, vmin=0, square=True, annot=labels, fmt='d', cbar=False) | |
ax.set_xticklabels("") | |
ax.set_yticklabels("") | |
ax.set_title("Move %s" % label) | |
plt.savefig("/Users/camerondavidson-pilon/code/s_l/images/%s.png" % label, dpi=) | |
for i in xrange(1, 100): | |
m = reshape_vector(matrix_power(M, i)[0,:]) | |
plot(m, i) | |
print i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment