-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNArmedBanditProblem.py
executable file
·64 lines (53 loc) · 2.08 KB
/
NArmedBanditProblem.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import numpy as np
import matplotlib.pyplot as plt
import utils.dataUtils as dataUtils
from banditMachine.banditArm import BanditArm
from banditMachine import nArmedMachine as banditMachine
__author__ = "Ariel Barreiros and Richar Sosa"
__status__ = "Development"
def main():
# Creación de los brazos
cantidad_de_brazos = 10
desviacion_estandar = 1
desviacion_de_la_media = 5
arms = []
for i in range(cantidad_de_brazos):
arms.append(BanditArm("Arm_" + str(i), (i + np.random.uniform(0, desviacion_de_la_media)), np.random.uniform(0, desviacion_estandar)))
# Adición de los brazos a la máquina
bandit_machine = banditMachine.NArmedMachine(arms)
# Órden de aprendizaje
iteraciones = 40000
bandit_machine.find_best_arm(iteraciones, 0.1)
# Gráfica del comportamiento de los q_values de cada brazo
plt.figure(1)
legend_plot_1 = []
for i in range(len(arms)):
data = bandit_machine.get_arm_q_values_evolution(i)
legend_plot_1.append(bandit_machine.get_arm(i).get_arm_id())
plt.plot(data)
plt.legend(legend_plot_1)
plt.show()
# Gráfica de las recompensas entregadas por cada brazo en las oportunidades que fueron seleccionados
plt.figure(2)
legend_plot_2 = []
for i in range(len(arms)):
data = bandit_machine.get_arm(i).get_reward_history()
legend_plot_2.append(bandit_machine.get_arm(i).get_arm_id())
plt.plot(data)
plt.legend(legend_plot_2)
plt.show()
# Gráfica de las frecuencia de las recompensas entregadas por cada brazo en las oportunidades que fueron
# seleccionados, en forma de campana de gauss
plt.figure(3)
legend_plot_3 = []
for i in range(len(arms)):
data = bandit_machine.get_arm(i).get_reward_history()
legend_plot_3.append(bandit_machine.get_arm(i).get_arm_id())
(x, y) = dataUtils.get_data_pair_for_plotting(data, 2)
# Este plot también puede ser de tipo scatter
# plt.scatter(x, y)
plt.plot(x, y)
plt.legend(legend_plot_3)
plt.show()
if __name__ == "__main__":
main()