Skip to content
Extraits de code Groupes Projets
Valider 4ea09ad8 rédigé par Adrien Payen's avatar Adrien Payen
Parcourir les fichiers

merge the docs of SNL

parent 65de4de8
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Fichier ajouté
Fichier déplacé
Fichier déplacé
import numpy as np import numpy as np
import random as rd import random as rd
from openpyxl import Workbook
class TransitionMatrixCalculator: class TransitionMatrixCalculator:
def __init__(self): def __init__(self):
...@@ -9,12 +8,9 @@ class TransitionMatrixCalculator: ...@@ -9,12 +8,9 @@ class TransitionMatrixCalculator:
self.matrix_normal = np.zeros((15, 15)) self.matrix_normal = np.zeros((15, 15))
self.matrix_risky = np.zeros((15, 15)) self.matrix_risky = np.zeros((15, 15))
# Probability to go from state k to k' # Probability to go from state k to k'
safe_dice = np.array([1/2, 1/2]) self.safe_dice = np.array([1/2, 1/2])
normal_dice = np.array([1/3, 1/3, 1/3]) self.normal_dice = np.array([1/3, 1/3, 1/3])
risky_dice = np.array([1/4, 1/4, 1/4, 1/4]) self.risky_dice = np.array([1/4, 1/4, 1/4, 1/4])
self.safe_dice = safe_dice
self.normal_dice = normal_dice
self.risky_dice = risky_dice
def compute_transition_matrix(self, layout, circle=False): def compute_transition_matrix(self, layout, circle=False):
self.matrix_safe.fill(0) self.matrix_safe.fill(0)
......
import random
from pprint import pprint
import numpy as np
# Initialisation du board
g = [[0 for j in range(15)]for i in range(15)]
# Liens entre les cases
for i in range(9):
g[i][i+1]=1
for i in range(10,14):
g[i][i+1]=1
g[2][10]=1
g[9][14]=1
pprint(g)
# Initialisation du safetydice
gsd = [[0 for j in range(15)]for i in range(15)]
for i in range(9) :
gsd[i][i+1]= 0.5
gsd[i][i]= 0.5
for i in range(10,14):
gsd[i][i+1]= 0.5
gsd[i][i]= 0.5
gsd[2][3] *= 0.5
gsd[2][10] = gsd[2][3]
gsd[9][9]= 0.5 # le fait de faire 0 et rester en case 10
gsd[9][14] = 0.5
pprint(gsd)
g = np.array([[0, 1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0, 0, 0, 0, 0, 0, 0.33, 0.33, 0.33],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.33, 0.66]])
rd = np.array([[0.25, 0.25, 0.25, 0.25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0.25, 0.25, 0.25, 0.25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0.25, 0.125, 0.125, 0.125, 0, 0, 0, 0, 0.125, 0.125, 0.125, 0, 0],
[0, 0, 0, 0.25, 0.25, 0.25, 0.25, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0.25, 0.25, 0.25, 0.25, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0.25, 0.25, 0.25, 0.25, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0.25, 0.25, 0.25, 0.25, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0.25, 0.25, 0.25, 0, 0, 0, 0, 0.25],
[0, 0, 0, 0, 0, 0, 0, 0, 0.25, 0.25, 0, 0, 0, 0, 0.5],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0.25, 0, 0, 0, 0, 0.75],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.25, 0.25, 0.25, 0.25, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.25, 0.25, 0.25, 0.25],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.25, 0.25, 0.5],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.25, 0.75]])
import random
from pprint import pprint
import numpy as np
# Initialisation du board
g = [[0 for j in range(15)]for i in range(15)]
# pprint(g)
# Liens entre les cases
for i in range(9):
g[i][i+1]=1
for i in range(10,14):
g[i][i+1]=1
g[2][10]=1
g[9][14]=1
pprint(g)
import numpy as np
def markovDecision(layout, circle):
# Paramètres du jeu
N = 15 # Nombre total de cases
dice_probs = {
'security': np.array([0.5, 0.5, 0.0, 0.0]),
'normal': np.array([1/3, 1/3, 1/3, 0.0]),
'risky': np.array([0.25, 0.25, 0.25, 0.25])
}
trap_effects = {
1: -1, # Redémarrer
2: -3, # Pénalité
3: 0 # Prison
}
# Initialisation des coûts et des dés à utiliser
costs = np.inf * np.ones(N-1)
dice_choices = np.zeros(N-1, dtype=int)
costs[-1] = 0 # Le coût de la dernière case est 0 car c'est l'objectif
# Itération de valeur
for _ in range(1000): # Nombre d'itérations suffisant pour la convergence
old_costs = costs.copy()
for i in range(N-1):
min_cost = np.inf
best_dice = 0
for dice_name, probs in dice_probs.items():
expected_cost = 0
for j, prob in enumerate(probs):
next_pos = i + j + 1
if next_pos >= N: # Gestion de la boucle
if circle:
next_pos = next_pos % N
else:
next_pos = N - 1
if layout[i] > 0: # Gestion des pièges
trap_effect = trap_effects[layout[i]]
next_pos = max(0, i + trap_effect)
expected_cost += prob * (1 + old_costs[next_pos-1] if next_pos > 0 else 0)
if expected_cost < min_cost:
min_cost = expected_cost
best_dice = ['security', 'normal', 'risky'].index(dice_name) + 1
costs[i] = min_cost
dice_choices[i] = best_dice
return costs, dice_choices
# Exemple d'utilisation
layout = np.array([0, 1, 0, 0, 2, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0]) # Configuration des pièges
circle = False # Indique si le plateau est circulaire
costs, dice_choices = markovDecision(layout, circle)
print("Coûts espérés:", costs)
print("Dés à utiliser:", dice_choices)
\ No newline at end of file
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter