MCPcopy Create free account
hub / github.com/lazyprogrammer/machine_learning_examples / run_experiment

Function run_experiment

rl/comparing_epsilons.py:26–56  ·  view source on GitHub ↗
(m1, m2, m3, eps, N)

Source from the content-addressed store, hash-verified

24
25
26def 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
58if __name__ == '__main__':
59 c_1 = run_experiment(1.0, 2.0, 3.0, 0.1, 100000)

Callers 1

Calls 3

BanditClass · 0.70
pullMethod · 0.45
updateMethod · 0.45

Tested by

no test coverage detected