Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import matplotlib as mpl
- mpl.use('Agg')
- import matplotlib.pyplot as plt
- import numpy as np
- N = 30
- memo = np.zeros((N, 10, 2))
- done = np.zeros((N, 10, 2))
- class Node:
- win = 0.051
- pityCount = 0
- headGuarantee = False
- winHeadChild = None
- winTailChild = None
- loseChild = None
- def avgPulls(self, n):
- if n == 0:
- return 0
- if done[n - 1, self.pityCount, int(self.headGuarantee)] == 1:
- return memo[n - 1, self.pityCount, int(self.headGuarantee)]
- winHeadAvg = 0
- winTailAvg = 0
- loseAvg = 0
- self.winHeadChild = Node()
- if self.pityCount == 9:
- self.win = 1
- # Win branch
- if self.headGuarantee:
- winHeadAvg = 1/3 + self.winHeadChild.avgPulls(n - 1)
- else:
- self.winTailChild = Node()
- self.winTailChild.headGuarantee = True
- winHeadAvg = 0.5*(1/3 + self.winHeadChild.avgPulls(n - 1))
- winTailAvg = 0.5*self.winTailChild.avgPulls(n - 1)
- del self.winTailChild
- del self.winHeadChild
- # Lose branch
- if self.win != 1:
- self.loseChild = Node()
- self.loseChild.pityCount = self.pityCount + 1
- self.loseChild.headGuarantee = self.headGuarantee
- loseAvg = self.loseChild.avgPulls(n - 1)
- del self.loseChild
- ret = self.win*(winHeadAvg + winTailAvg) + (1 - self.win)*loseAvg
- memo[n - 1, self.pityCount, int(self.headGuarantee)] = ret
- done[n - 1, self.pityCount, int(self.headGuarantee)] = 1
- return ret
- print(Node().avgPulls(N))
- x = np.arange(1, N + 1)
- y = memo[:, 0, 0]
- plt.plot(x, y, ".")
- plt.savefig('graph.png')
Add Comment
Please, Sign In to add comment