(m1, m2, m3, eps, N)
| 24 | |
| 25 | |
| 26 | def run_experiment(m1, m2, m3, eps, N): |
| 27 | bandits = [BanditArm(m1), BanditArm(m2), BanditArm(m3)] |
| 28 | |
| 29 | # count number of suboptimal choices |
| 30 | means = np.array([m1, m2, m3]) |
| 31 | true_best = np.argmax(means) |
| 32 | count_suboptimal = 0 |
| 33 | |
| 34 | data = np.empty(N) |
| 35 | |
| 36 | for i in range(N): |
| 37 | # epsilon greedy |
| 38 | p = np.random.random() |
| 39 | if p < eps: |
| 40 | j = np.random.choice(len(bandits)) |
| 41 | else: |
| 42 | j = np.argmax([b.m_estimate for b in bandits]) |
| 43 | x = bandits[j].pull() |
| 44 | bandits[j].update(x) |
| 45 | |
| 46 | if j != true_best: |
| 47 | count_suboptimal += 1 |
| 48 | |
| 49 | # for the plot |
| 50 | data[i] = x |
| 51 | cumulative_average = np.cumsum(data) / (np.arange(N) + 1) |
| 52 | |
| 53 | # plot moving average ctr |
| 54 | plt.plot(cumulative_average) |
| 55 | plt.plot(np.ones(N)*m1) |
| 56 | plt.plot(np.ones(N)*m2) |
| 57 | plt.plot(np.ones(N)*m3) |
| 58 | plt.xscale('log') |
| 59 | plt.show() |
| 60 | |
| 61 | for b in bandits: |
| 62 | print(b.m_estimate) |
| 63 | |
| 64 | print("percent suboptimal for epsilon = %s:" % eps, float(count_suboptimal) / N) |
| 65 | |
| 66 | return cumulative_average |
| 67 | |
| 68 | if __name__ == '__main__': |
| 69 | m1, m2, m3 = 1.5, 2.5, 3.5 |
no test coverage detected