-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_experiments.py
More file actions
328 lines (276 loc) · 10.7 KB
/
run_experiments.py
File metadata and controls
328 lines (276 loc) · 10.7 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
"""
Spuštění experimentů pro všechny algoritmy (DE, PSO, SOMA, FA, TLBO)
na všech 9 testovacích funkcích s 30 opakováními a generování Excel souboru s výsledky.
"""
import numpy as np
import openpyxl
from openpyxl import Workbook
from openpyxl.styles import Font, Alignment, PatternFill
from main import (
differential_evolution,
particle_swarm_optimization,
soma_all_to_one,
firefly_algorithm,
teaching_learning_based_optimization,
sphere,
schwefel,
rosenbrock,
rastrigin,
griewank,
levy,
michalewicz,
zakharov,
ackley,
get_default_bounds
)
# Konfigurace experimentů podle zadání
D = 30 # počet dimenzí
NP = 30 # velikost populace
MAX_OFE = 3000 # maximální počet vyhodnocení cílové funkce
NUM_EXPERIMENTS = 30 # počet experimentů
# Seznam testovacích funkcí
TEST_FUNCTIONS = [
("Sphere", sphere),
("Schwefel", schwefel),
("Rosenbrock", rosenbrock),
("Rastrigin", rastrigin),
("Griewank", griewank),
("Levy", levy),
("Michalewicz", michalewicz),
("Zakharov", zakharov),
("Ackley", ackley),
]
def run_single_experiment(algorithm_name, algorithm_func, objective, bounds, seed):
"""
Spustí jeden experiment s daným algoritmem na dané funkci.
Parametry:
algorithm_name: název algoritmu (pro výpis)
algorithm_func: funkce algoritmu
objective: cílová funkce
bounds: hranice pro dimenzi
seed: náhodný seed
Návrat:
best_fitness: nejlepší nalezená hodnota fitness
"""
try:
if algorithm_name == "DE":
# Differential Evolution
_, best_fitness = algorithm_func(
objective=objective,
bounds=bounds,
NP=NP,
F=0.5,
CR=0.5,
max_gen=MAX_OFE // NP, # MAX_OFE = max_gen * NP
seed=seed,
visualize=False
)
elif algorithm_name == "PSO":
# Particle Swarm Optimization
_, best_fitness = algorithm_func(
objective=objective,
bounds=bounds,
num_particles=NP,
w=0.7,
c1=2.0,
c2=2.0,
max_iter=MAX_OFE // NP, # MAX_OFE = max_iter * num_particles
seed=seed,
visualize=False
)
elif algorithm_name == "SOMA":
# Self-Organizing Migrating Algorithm
_, best_fitness = algorithm_func(
objective=objective,
bounds=bounds,
pop_size=NP,
path_length=3.0,
step=0.11,
prt=0.3,
max_migrations=MAX_OFE // (NP * int(3.0 / 0.11)), # Přibližný výpočet
seed=seed,
visualize=False
)
elif algorithm_name == "FA":
# Firefly Algorithm
_, best_fitness = algorithm_func(
objective=objective,
bounds=bounds,
pop_size=NP,
beta_0=1.0,
alpha=0.3,
max_generations=MAX_OFE // (NP * NP), # FA má O(N^2) vyhodnocení na generaci
seed=seed,
visualize=False
)
elif algorithm_name == "TLBO":
# Teaching-Learning Based Optimization
_, best_fitness = algorithm_func(
objective=objective,
bounds=bounds,
NP=NP,
max_OFE=MAX_OFE,
seed=seed,
visualize=False
)
else:
raise ValueError(f"Neznámý algoritmus: {algorithm_name}")
return best_fitness
except Exception as e:
print(f" CHYBA v {algorithm_name}: {e}")
return float('inf')
def run_all_experiments():
"""
Spustí všechny experimenty pro všechny algoritmy a funkce.
Návrat:
results: slovník s výsledky
results[func_name][algorithm_name] = list of 30 best fitness values
"""
# Inicializujeme strukturu pro uložení výsledků
results = {}
# Algoritmy k testování
algorithms = [
("DE", differential_evolution),
("PSO", particle_swarm_optimization),
("SOMA", soma_all_to_one),
("FA", firefly_algorithm),
("TLBO", teaching_learning_based_optimization),
]
# Pro každou testovací funkci
for func_name, func in TEST_FUNCTIONS:
print("=" * 80)
print(f"Spouštím experimenty pro funkci: {func_name}")
print("=" * 80)
# Získáme hranice pro D dimenzí
bounds = get_default_bounds(func_name, D)
# Inicializujeme slovník pro tuto funkci
results[func_name] = {}
# Pro každý algoritmus
for algo_name, algo_func in algorithms:
print(f"\n Algoritmus: {algo_name}")
# Inicializujeme seznam pro uložení výsledků
algo_results = []
# Spustíme NUM_EXPERIMENTS experimentů
for exp_num in range(1, NUM_EXPERIMENTS + 1):
# Seed pro reprodukovatelnost (různý pro každý experiment)
seed = exp_num * 100
# Spustíme experiment
best_fitness = run_single_experiment(
algorithm_name=algo_name,
algorithm_func=algo_func,
objective=func,
bounds=bounds,
seed=seed
)
# Uložíme výsledek
algo_results.append(best_fitness)
# Výpis průběhu
if exp_num % 10 == 0 or exp_num == 1:
print(f" Experiment {exp_num:2d}/30: f = {best_fitness:.6e}")
# Uložíme všechny výsledky pro tento algoritmus
results[func_name][algo_name] = algo_results
# Vypočítáme a vypíšeme statistiky
mean = np.mean(algo_results)
std = np.std(algo_results)
print(f" Mean: {mean:.6e}, Std: {std:.6e}")
print("\n" + "=" * 80)
print("VŠECHNY EXPERIMENTY DOKONČENY")
print("=" * 80)
return results
def create_excel_file(results, filename="results.xlsx"):
"""
Vytvoří Excel soubor s 9 tabulkami (jedna pro každou funkci).
Parametry:
results: slovník s výsledky z run_all_experiments()
filename: název výstupního souboru
"""
print(f"\nVytvářím Excel soubor: {filename}")
# Vytvoříme nový workbook
wb = Workbook()
# Odstraníme defaultní sheet
if "Sheet" in wb.sheetnames:
wb.remove(wb["Sheet"])
# Algoritmy (sloupce v tabulce)
algorithms = ["DE", "PSO", "SOMA", "FA", "TLBO"]
# Pro každou funkci vytvoříme nový sheet
for func_name in results.keys():
print(f" Vytvářím tabulku pro: {func_name}")
# Vytvoříme nový sheet
ws = wb.create_sheet(title=func_name)
# Header (hlavička tabulky)
ws.append([""] + algorithms)
# Styling hlavičky
header_fill = PatternFill(start_color="4472C4", end_color="4472C4", fill_type="solid")
header_font = Font(bold=True, color="FFFFFF")
for col_idx in range(1, len(algorithms) + 2):
cell = ws.cell(row=1, column=col_idx)
cell.fill = header_fill
cell.font = header_font
cell.alignment = Alignment(horizontal="center", vertical="center")
# Data - 30 experimentů
for exp_num in range(1, NUM_EXPERIMENTS + 1):
row_data = [f"Experiment {exp_num}"]
for algo_name in algorithms:
# Získáme výsledek tohoto experimentu
value = results[func_name][algo_name][exp_num - 1]
row_data.append(value)
ws.append(row_data)
# Prázdný řádek před statistikami
ws.append([])
# Mean a Std dev řádek
mean_row = ["Mean"]
std_row = ["Std dev"]
for algo_name in algorithms:
values = results[func_name][algo_name]
mean_val = np.mean(values)
std_val = np.std(values)
mean_row.append(mean_val)
std_row.append(std_val)
ws.append(mean_row)
ws.append(std_row)
# Styling pro Mean/Std řádky
mean_fill = PatternFill(start_color="FFC000", end_color="FFC000", fill_type="solid")
mean_font = Font(bold=True)
for col_idx in range(1, len(algorithms) + 2):
# Mean řádek
cell = ws.cell(row=NUM_EXPERIMENTS + 3, column=col_idx)
cell.fill = mean_fill
cell.font = mean_font
cell.alignment = Alignment(horizontal="center")
# Std řádek
cell = ws.cell(row=NUM_EXPERIMENTS + 4, column=col_idx)
cell.fill = PatternFill(start_color="FFE699", end_color="FFE699", fill_type="solid")
cell.font = mean_font
cell.alignment = Alignment(horizontal="center")
# Nastavíme šířky sloupců
ws.column_dimensions['A'].width = 15
for col_idx in range(2, len(algorithms) + 2):
ws.column_dimensions[chr(64 + col_idx)].width = 25 # Širší sloupce pro čitatelnost
# Formátování čísel (normální desetinná čísla místo vědecké notace)
# Použijeme 12 desetinných míst aby se vešla i velmi malá čísla
for row_idx in range(2, NUM_EXPERIMENTS + 5):
for col_idx in range(2, len(algorithms) + 2):
cell = ws.cell(row=row_idx, column=col_idx)
if cell.value is not None and isinstance(cell.value, (int, float)):
cell.number_format = '0.000000000000' # 12 desetinných míst
# Uložíme soubor
wb.save(filename)
print(f"\n✓ Excel soubor vytvořen: {filename}")
if __name__ == "__main__":
print("=" * 80)
print("SPUŠTĚNÍ EXPERIMENTŮ PRO VŠECHNY ALGORITMY")
print("=" * 80)
print(f"Konfigurace:")
print(f" Dimenze (D): {D}")
print(f" Velikost populace (NP): {NP}")
print(f" Max. vyhodnocení funkce (MAX_OFE): {MAX_OFE}")
print(f" Počet experimentů: {NUM_EXPERIMENTS}")
print(f" Testovací funkce: {len(TEST_FUNCTIONS)}")
print()
# Spustíme všechny experimenty
results = run_all_experiments()
# Vytvoříme Excel soubor
create_excel_file(results, filename="tlbo_comparison_results.xlsx")
print("\n" + "=" * 80)
print("HOTOVO!")
print("=" * 80)