-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexperiment_mle.py
More file actions
108 lines (71 loc) · 3.05 KB
/
experiment_mle.py
File metadata and controls
108 lines (71 loc) · 3.05 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import cauchy
x0 = 42
gamma = 7
global_maximum = np.array([x0, gamma])
samples = cauchy.rvs(loc=x0, scale=gamma, size=10000)
def log_likelihood(x):
x0, gamma = x[0], x[1]
return -len(samples) * np.log(gamma*np.pi)- np.sum(np.log(1+np.square((samples-x0)/gamma)))
objective = 'max'
objective_fct = log_likelihood
d = 2
n = 50
range_min = (0, 0)
range_max = (100, 100)
T = 100
R = 10
#%%
from metaheuristics.random import RandomSamplingAlgorithm
from metaheuristics.bees import ImprovedBeesAlgorithm
from metaheuristics.bat import BatAlgorithm
from metaheuristics.firefly import FireflyAlgorithm
rand = RandomSamplingAlgorithm(d=d, n=n, range_min=range_min, range_max=range_max)
bees_1 = ImprovedBeesAlgorithm(d=d, n=n, range_min=range_min, range_max=range_max,
nb=10, ne=5, nrb=5, nre=10, sf=0.99, sl=5)
bees_2 = ImprovedBeesAlgorithm(d=d, n=n, range_min=range_min, range_max=range_max,
nb=5, ne=0, nrb=5, nre=0, sf=1.0, sl=T)
bat_1 = BatAlgorithm(d=d, n=n, range_min=range_min, range_max=range_max,
a=1.0, r_min=0.1, r_max=0.2, alpha=0.9, gamma=0.9, f_min=1.0, f_max=3.0)
bat_2 = BatAlgorithm(d=d, n=n, range_min=range_min, range_max=range_max,
a=1.0, r_min=0.5, r_max=1.0, alpha=0.99, gamma=0.9, f_min=1.0, f_max=5.0)
firefly_1 = FireflyAlgorithm(d=d, n=n, range_min=range_min, range_max=range_max,
alpha=0.5, beta_max=1.0, gamma=0.25)
firefly_2 = FireflyAlgorithm(d=d, n=n, range_min=range_min, range_max=range_max,
alpha=2.0, beta_max=2.0, gamma=0.75)
for i, algo in enumerate([rand, bees_1, bees_2, bat_1, bat_2, firefly_1, firefly_2]):
x_list = []
y_list = []
latency_list = []
history_list = []
for _ in range(R):
solution, latency = algo.search(objective, objective_fct, T, visualize=False)
x_list.append(solution[0])
y_list.append(solution[1])
history_list.append(algo.history)
latency_list.append(latency)
print(f"====== ALGORITHM {algo.name} - {i} ======================")
x_list = np.array(x_list)
y_list = np.array(y_list)
non_inf = np.where(y_list != np.inf)
x_list = x_list[non_inf]
y_list = y_list[non_inf]
best = np.nanargmax(y_list, axis=0)
worst = np.nanargmin(y_list, axis=0)
print("best", x_list[best], y_list[best])
print("worst", x_list[worst], y_list[worst])
print("mean", np.nanmean(x_list, axis=0))
print("latency", np.nanmean(latency_list, axis=0))
print("std", np.nanstd(x_list, axis=0))
print("Global optimum", objective_fct(global_maximum))
history_list = np.array(history_list)
plt.figure()
plt.xlabel('iteration')
plt.ylabel('log likelihood')
for h in history_list[non_inf]:
h = [objective_fct(x) for x in h]
plt.plot(h)
plt.hlines(objective_fct(global_maximum), 0, T)
plt.show()
print("======================================")