Created
May 4, 2014 22:29
-
-
Save maksverver/013261076082f2165b67 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
#!/usr/bin/env python3 | |
import numpy as np | |
N = 22 # number of prisoners | |
M = 2 # number of switches | |
def state(n,m): | |
# n is the number of uncounted prisoners | |
# m is the value of the counter | |
return (n << M) + m | |
P = np.identity(N<<M) | |
for n in range(N): | |
for m in range(1<<M): | |
s = state(n,m) | |
if m < n and (m + 1) < (1<<M): | |
t = state(n,m+1) | |
p = (n - m)/N | |
P[s,t] += p | |
P[s,s] -= p | |
t = state(n-m,0) | |
p = 1/N | |
P[s,t] += p | |
P[s,s] -= p | |
# https://en.wikipedia.org/wiki/Absorbing_Markov_chain | |
R = np.matrix(np.identity(N<<M) - P)[1:, 1:,].getI() | |
print(R[state(N-1,0) - 1].sum()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment