-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
87 lines (66 loc) · 3.51 KB
/
main.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from datetime import datetime
from core import pso, topologies, fitness_functions
def run_experiments_and_plot_graphs(fitness_function_name, fitness_function, topology):
mpl.style.use('seaborn')
list_global_best_values, runs = main(1, topology, fitness_function)
description = f"Constant Inertia - {fitness_function_name} - {topology}"
plot_boxplot(runs, fitness_function_name, description)
list_global_best_values, runs = main(2, topology, fitness_function)
description = f"Linear Inertia - {fitness_function_name} - {topology}"
plot_boxplot(runs, fitness_function_name, description)
list_global_best_values, runs = main(3, topology, fitness_function)
description = f"Clerc - {fitness_function_name} - {topology}"
plot_boxplot(runs, fitness_function_name, description)
plot_covergence_graphs(fitness_function_name, fitness_function, topology)
def plot_covergence_graphs(fitness_function_name, fitness_function, topology):
mpl.style.use('seaborn')
fig, ax = plt.subplots()
list_global_best_values, runs = main(1, topology, fitness_function)
ax.plot(list(range(0, 10000)), list_global_best_values, 'b', label=f"Constant - Best: {list_global_best_values[-1]:.2f}")
ax.set_title("PSO Local Topology")
ax.set_ylabel("Best Fitness")
ax.set_xlabel("Iterations")
list_global_best_values, runs = main(2, topology, fitness_function)
ax.plot(list(range(0, 10000)), list_global_best_values, 'r', label=f"Linear - Best: {list_global_best_values[-1]:.2f}")
list_global_best_values, runs = main(3, topology, fitness_function)
ax.plot(list(range(0, 10000)), list_global_best_values, 'g', label=f"Clerc - Best: {list_global_best_values[-1]:.2f}")
ax.legend()
plt.savefig(f'PSO Convergence {fitness_function_name} {topology} Average 30 runs_2.png')
def plot_boxplot(best_fitness, function_name, description):
fig1, ax1 = plt.subplots()
ax1.set_title(f'BoxPlot Best Fitness for {function_name}: {description}')
ax1.boxplot(best_fitness, patch_artist=True, showfliers=False)
ax1.legend()
plt.savefig(f'PSO {description} Boxplot {function_name}_2.png')
def save_global_best_values(inertia_type, pso_algorithm):
now = datetime.now().strftime("%d-%m-%Y%H:%M:%S")
filepath = f"results/{inertia_type}-{now}.txt"
with open(filepath, "w") as f:
f.write(f"[\n")
for i in pso_algorithm.list_global_best_values:
f.write(f"{str(i)},\n")
f.write(f"]")
def main(inertia_type, topology, fitness_function):
runs = []
for _ in range(30):
pso_algorithm = pso.PSOAlgorithm(
topology=topology,
bound= [-32,32],
dimensions=30,
num_particles=30,
num_iterations=10000,
max_speed=[-1, 1],
fitness_function=fitness_function,
)
pso_algorithm.search(inertia_type)
runs.append(pso_algorithm.list_global_best_values)
return np.average(runs, axis=0), runs
topology_l = topologies.Local(max_speed=[-1, 1])
topology_g = topologies.Global(max_speed=[-1, 1])
# run_experiments_and_plot_graphs("Rastrigin", fitness_functions.rastrigin_function, topology_l)
# run_experiments_and_plot_graphs("Rastrigin", fitness_functions.rastrigin_function, topology_g)
# run_experiments_and_plot_graphs("Rosenbrocks", fitness_functions.rosenbrocks_function, topology_l)
run_experiments_and_plot_graphs("Rosenbrocks", fitness_functions.rosenbrocks_function, topology_g)