Advertisement
Guest User

Untitled

a guest
Sep 4th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 0.84 KB | None | 0 0
  1.  
  2. # from the starting state, select the next state according to the
  3. # transition matrix T
  4. function rungame(T)
  5.   # start at state 140
  6.   current = 140
  7.  
  8.   # count turns
  9.   turns = 0
  10.  
  11.   while current != 134
  12.     # get transitions from current state
  13.     A = [(i,j) for (i,j) in enumerate(T[:,current]) if i != 0]
  14.  
  15.     # separate transitions into states and probabilities
  16.     states = [i[1] for i in A]
  17.     probs = [i[2] for i in A]
  18.  
  19.     # select the next state from the transitions distribution
  20.     current = sample(states, Weights(probs))
  21.     turns+=1
  22.   end
  23.  
  24.   return turns
  25.  
  26. end
  27.  
  28. # run function rungame on transition matrix T
  29. # and return the average turns over N runs
  30. function getAverageTurns(T,N)
  31.   result=0
  32.   for i=1:N
  33.     result+=rungame(T)/N
  34.   end
  35.  
  36.   return result
  37. end
  38.  
  39.  
  40. println("turns:\t",getAverageTurns(T,1000),"\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement