(m1, m2, m3, eps, N)
| 24 | |
| 25 | |
| 26 | def run_experiment(m1, m2, m3, eps, N): |
| 27 | bandits = [Bandit(m1), Bandit(m2), Bandit(m3)] |
| 28 | |
| 29 | data = np.empty(N) |
| 30 | |
| 31 | for i in range(N): |
| 32 | # epsilon greedy |
| 33 | p = np.random.random() |
| 34 | if p < eps: |
| 35 | j = np.random.choice(3) |
| 36 | else: |
| 37 | j = np.argmax([b.mean for b in bandits]) |
| 38 | x = bandits[j].pull() |
| 39 | bandits[j].update(x) |
| 40 | |
| 41 | # for the plot |
| 42 | data[i] = x |
| 43 | cumulative_average = np.cumsum(data) / (np.arange(N) + 1) |
| 44 | |
| 45 | # plot moving average ctr |
| 46 | plt.plot(cumulative_average) |
| 47 | plt.plot(np.ones(N)*m1) |
| 48 | plt.plot(np.ones(N)*m2) |
| 49 | plt.plot(np.ones(N)*m3) |
| 50 | plt.xscale('log') |
| 51 | plt.show() |
| 52 | |
| 53 | for b in bandits: |
| 54 | print(b.mean) |
| 55 | |
| 56 | return cumulative_average |
| 57 | |
| 58 | if __name__ == '__main__': |
| 59 | c_1 = run_experiment(1.0, 2.0, 3.0, 0.1, 100000) |
no test coverage detected