## loading python libraries
# necessary to display plots inline:
%matplotlib inline
# load the libraries
import matplotlib.pyplot as plt # 2D plotting library
import numpy as np # package for scientific computing
from math import * # package for mathematics (pi, arctan, sqrt, factorial ...)
AdjacencyMatrix=np.matrix([[0,1,0,0],[1,0,1,1],[1,0,0,0],[1,0,0,0]])
n=20
B=AdjacencyMatrix**n
print(str(B[3,3])+' paths of length '+str(n))
#print(B)
488 paths of length 20

# Matrices of b-short words
Adjacency_abc=np.matrix([[1,1,1],[1,1,0],[1,1,1]])
#print('Adjacency matrix : ')
#print(Adjacency_abc)
List=[]
for n in range(1,21):
Mat=Adjacency_abc**(n-1)
List.append(np.sum(Mat))
print('Number of words of size n=',n,': ',np.sum(Mat))
Number of words of size n= 1 : 3 Number of words of size n= 2 : 8 Number of words of size n= 3 : 21 Number of words of size n= 4 : 55 Number of words of size n= 5 : 144 Number of words of size n= 6 : 377 Number of words of size n= 7 : 987 Number of words of size n= 8 : 2584 Number of words of size n= 9 : 6765 Number of words of size n= 10 : 17711 Number of words of size n= 11 : 46368 Number of words of size n= 12 : 121393 Number of words of size n= 13 : 317811 Number of words of size n= 14 : 832040 Number of words of size n= 15 : 2178309 Number of words of size n= 16 : 5702887 Number of words of size n= 17 : 14930352 Number of words of size n= 18 : 39088169 Number of words of size n= 19 : 102334155 Number of words of size n= 20 : 267914296
# Matrices of b-short words
Adjacency_bShort=np.matrix([[1,1,0,0],[1,0,1,0],[1,0,0,1],[1,0,0,0]])
#print('Adjacency matrix : ')
#print(Adjacency_bShort)
for n in range(1,21):
Mat=Adjacency_bShort**(n-1)
MatBis=Mat[0:2,]
print('For n = ',n,' there are ', np.sum(MatBis),' b-short words')
For n = 1 there are 2 b-short words For n = 2 there are 4 b-short words For n = 3 there are 8 b-short words For n = 4 there are 15 b-short words For n = 5 there are 29 b-short words For n = 6 there are 56 b-short words For n = 7 there are 108 b-short words For n = 8 there are 208 b-short words For n = 9 there are 401 b-short words For n = 10 there are 773 b-short words For n = 11 there are 1490 b-short words For n = 12 there are 2872 b-short words For n = 13 there are 5536 b-short words For n = 14 there are 10671 b-short words For n = 15 there are 20569 b-short words For n = 16 there are 39648 b-short words For n = 17 there are 76424 b-short words For n = 18 there are 147312 b-short words For n = 19 there are 283953 b-short words For n = 20 there are 547337 b-short words
We consider a random robot in the following labyrinth:
A=np.matrix([
[0,0.5,0,0.5,0,0,0],
[1/3.0,0,1/3.0,0,1/3.0,0,0],
[0,0.5,0,0,0,0.5,0],
[1/2.0,0,0,0,1/2.0,0,0],
[0,1/3.0,0,1/3.0,0,0,1/3.0],
[0,0,0,0,0,1,0],
[0,0,0,0,0,0,1],
])
#print(np.round(A**80,3))
def ProbaDistribution(n):
Power=np.linalg.matrix_power(A,n)
return Power[1,:]
print('------- Question 1 -----')
print('Approximation of probability distributions:')
for n in [5,100]:
print('p(',n,') = ',str(np.round(ProbaDistribution(n),3)))
print('------- Question 2 -----')
for n in [100]:
Distribution=ProbaDistribution(n)
print('At time n=',n,', the robot is at Exit 1 with probab. '+str(np.round(Distribution[0,5],6)))
------- Question 1 ----- Approximation of probability distributions: p( 5 ) = [[0.224 0. 0.104 0. 0.224 0.241 0.207]] p( 100 ) = [[0. 0. 0. 0. 0. 0.467 0.533]] ------- Question 2 ----- At time n= 100 , the robot is at Exit 1 with probab. 0.466666
def ProbaNoEscape(n):
Distribution=ProbaDistribution(n)
return 1-Distribution[0,5]-Distribution[0,6] # returns 1-Proba(Exit 1)-Proba(Exit 2)
N=30
Proba=[ProbaNoEscape(n) for n in range(N)]
plt.plot(Proba,'o-')
plt.show()
A=np.array([[1,-1/2,0,-1/2,0],
[-1/3,1,-1/3,0,-1/3],
[0,-1/2,1,0,0],
[-1/2,0,0,1,-1/2],
[0,-1/3,0,-1/3,1]])
B=np.array([0,0,1/2,0,0])
print(np.linalg.solve(A, B))
[0.4 0.46666667 0.73333333 0.33333333 0.26666667]
A player plays the following game:
Here is an example with $T=12$: $$ 0 \stackrel{\text{dice = }3}{\longrightarrow} 3 \stackrel{\text{dice = }5}{\longrightarrow} 8\stackrel{\text{dice = }1}{\longrightarrow} 9\stackrel{\text{dice = }4}{\longrightarrow} 13\ \text{(Lost)} $$
T=9 # target
# We consider a transition matrix
Matrix_target=np.zeros([T+6,T+6])
for i in range(T):
for j in range(i+1,i+7):
Matrix_target[i,j]=1/6
for l in range(T,T+6):
Matrix_target[l,l]=1
#print(np.round(Matrix_target,2))
# Transition matrix raised to the power T:
Power=np.linalg.matrix_power(Matrix_target,T)
# Test: should return 0.2803689
print('Test: should be equal to 0.2803689:')
print(Power[0,T])
Test: should be equal to 0.2803689: 0.2803689454414977
def WinningProbability(t,p):
# t = target
# p = number of faces of the dice
WinningProbabilities=np.zeros([1,t+p]) # i-th coordinate = winning probability starting from i
WinningProbabilities[0,t]=1
for k in reversed(range(t)): # first computes the probability for k=n-1, then k=n-2,...
NextProbabilities=WinningProbabilities[0,k+1:k+p+1] # extracts the p next probabilities
WinningProbabilities[0,k]=np.mean(NextProbabilities) # computes the mean
return WinningProbabilities[0,0]
n=30
XX=range(1,n+1)
plt.plot(XX,[WinningProbability(n,6) for n in XX],'o-')
plt.show()