-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_abc.py
More file actions
81 lines (61 loc) · 2.34 KB
/
main_abc.py
File metadata and controls
81 lines (61 loc) · 2.34 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
from functions import Rastrigin
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
from parameters import iterations_number
from abcolony import ABC
from enum import Enum
mpl.style.use('seaborn')
SIMULATIONS = 30
class POSITION_UPDATE(Enum):
TRADITIONAL = 1
FOOD_SOURCE = 2
PROBABILITY = 3
def plot_boxplot(best_fitness, function_name):
fig1, ax1 = plt.subplots()
ax1.set_title(f'BoxPlot Best Fitness for {function_name}')
ax1.boxplot(best_fitness, patch_artist=True, showfliers=False)
ax1.legend()
plt.savefig(f'ABC Boxplot {function_name}.png')
def plot_graphs(average_best_fitness, function_name):
fig, ax = plt.subplots()
ax.plot(list(range(0, iterations_number)), average_best_fitness, 'b', label=f"Best: {average_best_fitness[-1]:.2f}")
ax.set_title(f"ABC {function_name}: Average {SIMULATIONS} runs")
ax.set_xlabel("Iterations")
ax.set_ylabel("Best Fitness")
ax.legend()
plt.savefig(f'ABC Convergence {function_name}.png')
print('Rastrigin')
best_fitness = []
for j in range(SIMULATIONS):
abc = ABC(POSITION_UPDATE.TRADITIONAL)
results = abc.search()
best_fitness.append(results)
plot_boxplot(best_fitness, "Rastrigin (Traditional)")
average_best_fitness = np.sum(np.array(best_fitness), axis=0) / SIMULATIONS
plot_graphs(average_best_fitness, "Rastrigin (Traditional)")
with open("rastrigin_test_1.txt", "w") as f:
for i in average_best_fitness:
f.write(f"{str(i)}\n")
best_fitness = []
for _ in range(SIMULATIONS):
abc = ABC(POSITION_UPDATE.FOOD_SOURCE)
results = abc.search()
best_fitness.append(results)
plot_boxplot(best_fitness, "Rastrigin (Food Source)")
average_best_fitness = np.sum(np.array(best_fitness), axis=0) / SIMULATIONS
plot_graphs(average_best_fitness, "Rastrigin (Food Source)")
with open("rastrigin_test_2.txt", "w") as f:
for i in average_best_fitness:
f.write(f"{str(i)}\n")
best_fitness = []
for _ in range(SIMULATIONS):
abc = ABC(POSITION_UPDATE.PROBABILITY)
results = abc.search()
best_fitness.append(results)
plot_boxplot(best_fitness, "Rastrigin (Probability)")
average_best_fitness = np.sum(np.array(best_fitness), axis=0) / SIMULATIONS
plot_graphs(average_best_fitness, "Rastrigin (Probability)")
with open("rastrigin_test_3.txt", "w") as f:
for i in average_best_fitness:
f.write(f"{str(i)}\n")