Newer
Older
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import numpy as np
import random as rd
class TransitionMatrixCalculator:
def __init__(self):
# Probabilités de transition pour les dés "safe", "normal" et "risky"
self.safe_dice = np.array([1/2, 1/2])
self.normal_dice = np.array([1/3, 1/3, 1/3])
self.risky_dice = np.array([1/4, 1/4, 1/4, 1/4])
def compute_transition_matrix(self, layout: list, circle: bool):
size = len(layout)
matrix_safe = self._compute_matrix(layout, self.safe_dice, size, circle, 'safe')
matrix_normal = self._compute_matrix(layout, self.normal_dice, size, circle, 'normal')
matrix_risky = self._compute_matrix(layout, self.risky_dice, size, circle, 'risky')
return matrix_safe, matrix_normal, matrix_risky
def _compute_matrix(self, layout: list, dice_probs: list, size: int, circle: bool, matrix_type: str):
transition_matrix = np.zeros((size, size))
dice_type = None
if matrix_type == 'safe':
dice_type = self.safe_dice
elif matrix_type == 'normal':
dice_type = self.normal_dice
elif matrix_type == 'risky':
dice_type = self.risky_dice
for k in range(size):
for s, p in enumerate(dice_probs):
k_prime = (k + s) % size if circle else min(size - 1, k + s)
if k == 9 and s == 1 and matrix_type == 'safe':
k_prime = size - 1
elif k == 2 and s > 0 and matrix_type == 'safe':
p /= 2
k_prime = 10 + s - 1
if layout[k_prime] == 1:
k_prime = 0
elif layout[k_prime] == 2:
k_prime = max(0, k_prime - 3)
elif k == 7 and s == 3 and matrix_type == 'risky':
k_prime = size - 1
elif k == 8 and s in [2, 3] and matrix_type == 'risky':
if circle or s == 2:
k_prime = size - 1
else:
k_prime = 0
elif k == 9 and s in [1, 2, 3] and matrix_type == 'risky':
if not circle or s == 1:
k_prime = size - 1
elif circle and s == 2:
k_prime = 0
elif circle and s == 3:
k_prime = 1
if layout[k_prime] in [1, 2]:
k_prime = max(0, k_prime - 3) if layout[k_prime] == 2 else 0
transition_matrix[k, k_prime] += p * dice_type[s]
return transition_matrix
def generate_arrays(self,n):
arrays = []
for _ in range(n):
array = np.zeros(15, dtype=int)
indices = rd.sample(range(1, 14), 3)
array[indices] = 1, 2, 3
arrays.append(array)
return arrays
def tst_transition_matrix(self):
layouts = self.generate_arrays(1000)
for array in layouts:
print(array)
self.compute_transition_matrix(array, False)
self.compute_transition_matrix(array, True)
#tmc = TransitionMatrixCalculator()
#tmc.tst_transition_matrix()