Advertisement
cesarcardinale

M3201 - TP1

Nov 27th, 2018
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.63 KB | None | 0 0
  1. import numpy as np
  2.  
  3. x = var('D1')
  4. y = var('D2')
  5. z = var('D3')
  6.  
  7. d1 = 0.9 * x + 0.7 * y + 0.8 * z - x == 0
  8. d2 = 0.05 * x - y == 0
  9. d3 = 0.05 * x + 0.3 * y + 0.2 * z - z == 0
  10. d4 = x + y + z == 1
  11.  
  12. solution = solve([d1,d2,d3,d4], x,y,z)
  13.  
  14. d = solution[0][0].right()
  15. m = solution[0][1].right()
  16. j = solution[0][2].right()
  17.  
  18. show('D =' + str(round(d,5)) + ', M =' + str(round(m,5)) + ', J =' + str(round(j,5)))
  19.  
  20. dico = {"Dormir": 1,"Manger": 2, "Jouer": 3}
  21.  
  22. def Markov1pas(etat_init):
  23.     r = random()
  24.     if etat_init == dico["Dormir"]:
  25.         if r <= 0.05:
  26.             return dico["Manger"]
  27.         elif r > 0.05 and r <= 0.1:
  28.             return dico["Jouer"]
  29.         else:
  30.             return dico["Dormir"]
  31.     elif etat_init == dico["Manger"]:
  32.         if r <= 0.3:
  33.             return dico["Jouer"]
  34.         else:
  35.             return dico["Dormir"]
  36.     elif etat_init == dico["Jouer"]:
  37.         if r <= 0.8:
  38.             return dico["Dormir"]
  39.         else:
  40.             return dico["Jouer"]
  41.     else:
  42.         return -1
  43.  
  44. def TempsEtats(etat_init, n):
  45.     D = 0
  46.     M = 0
  47.     J = 0
  48.     for i in range(n):
  49.         if etat_init == dico["Dormir"]:
  50.             D += 1
  51.         elif etat_init == dico["Manger"]:
  52.             M += 1
  53.         elif etat_init == dico["Jouer"]:
  54.             J += 1
  55.         etat_init = Markov1pas(etat_init)
  56.     print('Domir : ' + str(D) + ' minutes.')
  57.     print('Manger : ' + str(M) + ' minutes.')
  58.     print('Jouer : ' + str(J) + ' minutes.')
  59.     print('\n')
  60.     print('P(D)= ' + str(round(D/n, 5)))
  61.     print('P(M)= ' + str(round(M/n, 5)))
  62.     print('P(J)= ' + str(round(J/n, 5)))
  63.  
  64. TempsEtats(1,6000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement