-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim.py
57 lines (47 loc) · 1.08 KB
/
sim.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import numpy as np
import random
def tire(proba):
r = random.random()
s = 0
for i in range(len(proba)):
p = proba[i]
s += p
if r < s:
return i
raise ValueError("Not a proba")
def generateTrans(n):
""" n : taille couloir """
M = []
L = [0 for i in range(n) ]
first = L[::]
first[0] = 1/2
first[1] = 1/2
M.append(first)
for pos in range(1, n-1):
p = L[::]
p[pos-1] = 1/2
p[pos+1] = 1/2
M.append(p)
last = L[::]
last[n-1] = 1
M.append(last)
return np.array(M)
# trans = np.array([[1/2,1/2,0,0,0], [1/2, 0, 1/2, 0, 0], [0, 1/2, 0, 1/2, 0], [0, 0, 1/2, 0 , 1/2], [0,0,0,0,1]])
# print('trans :', trans)
TC = 5
trans = generateTrans(TC)
print('t :', trans)
state = 0
n = 10
nbIt = 1000
nbState = trans.shape[1]
resTable = np.zeros(nbState)
for k in range(nbIt):
state = 0
for i in range(n):
proba = trans[state]
state = tire(proba)
# print('final state :', state)
resTable[state] += 1
resTable /= nbIt
print('resTable :', resTable)