Guest User

Untitled

a guest
Feb 4th, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.97 KB | None | 0 0
  1. import sys,math
  2.  
  3. BOTTOM = -1
  4.    
  5. def expectation_G_Bach(RED_TOTAL, BLACK_TOTAL):
  6.     # sys.stdout = open("C:\\Users\\user\\Desktop\cardgame.txt", "w")
  7.    
  8.     memo = [[BOTTOM for x in range(RED_TOTAL + 1)] for x in range(BLACK_TOTAL + 1)]
  9.     for r in range(RED_TOTAL + 1):
  10.         memo[r][0] = r
  11.     for b in range(BLACK_TOTAL + 1):
  12.         memo[0][b] = 0
  13.        
  14.     for r in range(1, RED_TOTAL + 1):
  15.         for b in range(1, BLACK_TOTAL + 1):
  16.             memo[r][b] = max(0, b/(b+r)*(memo[r][b-1] - 1) + r/(b+r)*(memo[r-1][b] + 1))
  17.             # print("%10.4f" % memo[r][b], end="")
  18.         # print('')
  19.            
  20.     # print('RED: %d \t BLACK: %d \t\t exp: %f' % (RED_TOTAL, BLACK_TOTAL, memo[r][b]))
  21.     # sys.stdout.close()
  22.     # sys.stdout = sys.__stdout__
  23.     return memo
  24.  
  25. def expectation_user223935(RED_TOTAL, BLACK_TOTAL):
  26.  
  27.     memo = [[BOTTOM for x in range(RED_TOTAL + 1)] for x in range(BLACK_TOTAL + 1)]
  28.     for r in range(RED_TOTAL + 1):
  29.         memo[r][0] = 0
  30.     for b in range(BLACK_TOTAL + 1):
  31.         memo[0][b] = b
  32.        
  33.     for r in range(1, RED_TOTAL + 1):
  34.         for b in range(1, BLACK_TOTAL + 1):
  35.             memo[r][b] = max(b-r, b/(b+r)*memo[r][b-1] + r/(b+r)*memo[r-1][b])
  36.             # print("%10.4f" % memo[r][b], end="")
  37.         # print('')
  38.            
  39.     # print('RED: %d \t BLACK: %d \t\t exp: %f' % (RED_TOTAL, BLACK_TOTAL, memo[r][b]))
  40.     # sys.stdout.close()
  41.     # sys.stdout = sys.__stdout__
  42.     return memo
  43.    
  44. RED = 30
  45. BLACK = 30
  46. memo_G_Bach = expectation_G_Bach(RED,BLACK)
  47. memo_user223935 = expectation_user223935(RED,BLACK)
  48. equal = True
  49. for r in range(RED + 1):
  50.     for b in range(BLACK + 1):
  51.         if not math.isclose(memo_G_Bach[r][b], memo_user223935[b][r]):
  52.             print('(r,b) = (%d,%d)\t\t G_Bach: %f \t\t user223935: %f' % (r,b,memo_G_Bach[r][b], memo_user223935[b][r]))
  53.         equal = equal and math.isclose(memo_G_Bach[r][b], memo_user223935[r][b])
  54. print('tables equal: %s' % equal)
Advertisement
Add Comment
Please, Sign In to add comment