-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_final_validation.py
More file actions
389 lines (303 loc) · 13.7 KB
/
test_final_validation.py
File metadata and controls
389 lines (303 loc) · 13.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
"""
Final validation test for metaheuristic optimization implementation.
This test covers all key functionality and provides a comprehensive report.
"""
import sys
import os
import numpy as np
import pandas as pd
import time
# Add project root to path
project_root = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(project_root, 'src'))
def test_complete_functionality():
"""Comprehensive test of all optimization functionality."""
print("COMPREHENSIVE METAHEURISTIC OPTIMIZATION TEST")
print("="*60)
success_count = 0
total_tests = 0
# Test 1: Basic Algorithm Functionality
print("\n1. Testing Individual Algorithms")
print("-" * 40)
try:
from weight_optimizer import OptimizationConfig, GeneticAlgorithm, ParticleSwarmOptimization, SimulatedAnnealing, ObjectiveFunction
# Create test data
np.random.seed(42)
test_df = pd.DataFrame({
'energy_norm': np.random.random(25),
'flood_norm': np.random.random(25),
'social_norm': np.random.random(25),
'green_norm': np.random.random(25)
})
objective_fn = ObjectiveFunction(test_df, ['energy_norm', 'flood_norm', 'social_norm', 'green_norm'])
bounds = [(0.01, 1.0) for _ in range(4)]
algorithms = [
("Genetic Algorithm", GeneticAlgorithm),
("Particle Swarm Optimization", ParticleSwarmOptimization),
("Simulated Annealing", SimulatedAnnealing)
]
algorithm_results = {}
for alg_name, AlgorithmClass in algorithms:
total_tests += 1
print(f" Testing {alg_name}...")
config = OptimizationConfig(
algorithm=alg_name.lower().replace(" ", "_"),
population_size=8,
max_iterations=5,
max_time_seconds=20,
seed=42
)
algorithm = AlgorithmClass(config)
start_time = time.time()
weights, fitness, stats = algorithm.optimize(objective_fn, 4, bounds)
elapsed_time = time.time() - start_time
# Validate results
assert len(weights) == 4, f"Expected 4 weights, got {len(weights)}"
assert isinstance(fitness, (int, float)), f"Fitness should be numeric"
assert elapsed_time < 25, f"Algorithm took too long: {elapsed_time}s"
assert 'algorithm' in stats, "Missing algorithm in stats"
algorithm_results[alg_name] = {
'fitness': fitness,
'time': elapsed_time,
'weights': weights,
'stats': stats
}
print(f"{alg_name}: fitness={fitness:.4f}, time={elapsed_time:.2f}s")
success_count += 1
except Exception as e:
print(f"Individual algorithms test failed: {e}")
# Test 2: Multi-Algorithm Optimization
print("\n2. Testing Multi-Algorithm Optimization")
print("-" * 40)
try:
from weight_optimizer import MultiAlgorithmOptimizer
total_tests += 1
config = OptimizationConfig(
algorithm="multi",
population_size=6,
max_iterations=4,
max_time_seconds=60,
execution_order=["genetic", "pso", "simulated_annealing"],
algorithm_time_limits={"genetic": 15, "pso": 15, "simulated_annealing": 15},
comparison_metric="fitness",
seed=42
)
multi_optimizer = MultiAlgorithmOptimizer(config)
start_time = time.time()
results = multi_optimizer.run_multi_algorithm_optimization(objective_fn, 4, bounds)
elapsed_time = time.time() - start_time
# Validate multi-algorithm results
assert 'all_results' in results, "Missing all_results"
assert 'selected_algorithm' in results, "Missing selected_algorithm"
assert 'selected_weights' in results, "Missing selected_weights"
assert 'comparison_analysis' in results, "Missing comparison_analysis"
all_results = results['all_results']
assert len(all_results) == 3, f"Expected 3 algorithm results, got {len(all_results)}"
selected_algorithm = results['selected_algorithm']
selected_weights = results['selected_weights']
print(f"Multi-algorithm completed in {elapsed_time:.2f}s")
print(f"Selected algorithm: {selected_algorithm}")
print(f"Optimized weights: energy={selected_weights[0]:.3f}, flood={selected_weights[1]:.3f}")
success_count += 1
except Exception as e:
print(f"Multi-algorithm test failed: {e}")
import traceback
traceback.print_exc()
# Test 3: Different Comparison Metrics
print("\n3. Testing Comparison Metrics")
print("-" * 40)
for metric in ["fitness", "stability", "efficiency"]:
try:
total_tests += 1
config = OptimizationConfig(
algorithm="multi",
population_size=5,
max_iterations=3,
max_time_seconds=45,
execution_order=["pso", "simulated_annealing"],
algorithm_time_limits={"pso": 15, "simulated_annealing": 15},
comparison_metric=metric,
seed=42
)
multi_optimizer = MultiAlgorithmOptimizer(config)
results = multi_optimizer.run_multi_algorithm_optimization(objective_fn, 4, bounds)
selected_algorithm = results['selected_algorithm']
print(f"{metric.capitalize()} metric: selected {selected_algorithm}")
success_count += 1
except Exception as e:
print(f"{metric} metric test failed: {e}")
# Test 4: Time Bounds Enforcement
print("\n4. Testing Time Bounds")
print("-" * 40)
try:
total_tests += 1
config = OptimizationConfig(
algorithm="genetic",
population_size=15,
max_iterations=1000, # High iteration count
max_time_seconds=2.0, # But very low time limit
seed=42
)
algorithm = GeneticAlgorithm(config)
start_time = time.time()
weights, fitness, stats = algorithm.optimize(objective_fn, 4, bounds)
elapsed_time = time.time() - start_time
# Should terminate due to time limit
assert elapsed_time < 5.0, f"Time bound not enforced: {elapsed_time}s > 5.0s"
print(f"Time bounds enforced: {elapsed_time:.2f}s (limit: 2.0s)")
success_count += 1
except Exception as e:
print(f"Time bounds test failed: {e}")
# Test 5: Integration with optimize_weights function
print("\n5. Testing Main Integration Function")
print("-" * 40)
try:
from weight_optimizer import optimize_weights
total_tests += 1
config = OptimizationConfig(
algorithm="genetic",
population_size=8,
max_iterations=5,
max_time_seconds=20,
seed=42
)
weights, stats = optimize_weights(test_df, config=config)
# Validate integration results
assert isinstance(weights, dict), f"Expected dict weights, got {type(weights)}"
assert 'energy' in weights, "Missing energy weight"
assert 'flood' in weights, "Missing flood weight"
assert 'social' in weights, "Missing social weight"
assert 'green' in weights, "Missing green weight"
# Check weight reasonableness
total_weight = sum(weights.values())
assert 0.9 <= total_weight <= 1.1, f"Unreasonable total weight: {total_weight}"
print(f"Integration function: {weights}")
print(f"Algorithm: {stats.get('algorithm', 'unknown')}")
success_count += 1
except Exception as e:
print(f"Integration test failed: {e}")
# Test 6: Ensemble Weights
print("\n6. Testing Ensemble Weights")
print("-" * 40)
try:
total_tests += 1
config = OptimizationConfig(
algorithm="multi",
population_size=5,
max_iterations=3,
max_time_seconds=45,
execution_order=["genetic", "pso"],
algorithm_time_limits={"genetic": 15, "pso": 15},
comparison_metric="fitness",
ensemble_weights=True,
seed=42
)
multi_optimizer = MultiAlgorithmOptimizer(config)
results = multi_optimizer.run_multi_algorithm_optimization(objective_fn, 4, bounds)
selected_algorithm = results['selected_algorithm']
assert "ensemble" in selected_algorithm.lower(), f"Expected ensemble algorithm, got {selected_algorithm}"
print(f"Ensemble weights: {selected_algorithm}")
success_count += 1
except Exception as e:
print(f"Ensemble weights test failed: {e}")
# Test Summary
print("\n" + "="*60)
print("TEST SUMMARY")
print("="*60)
success_rate = (success_count / total_tests) * 100 if total_tests > 0 else 0
print(f"Tests passed: {success_count}/{total_tests}")
print(f"Success rate: {success_rate:.1f}%")
if success_count == total_tests:
print("\nALL TESTS PASSED!")
print("\nYour metaheuristic optimization implementation is fully functional:")
print("Genetic Algorithm")
print("Particle Swarm Optimization")
print("Simulated Annealing")
print("Multi-algorithm execution")
print("Time bounds enforcement")
print("Multiple comparison metrics")
print("Ensemble weight combination")
print("Main pipeline integration")
print("\nREADY FOR PRODUCTION USE!")
print("\nTo run optimization on your data:")
print("1. Single algorithm:")
print(" python src/run_pipeline.py --optimization-enabled --optimization-algorithm genetic")
print("\n2. Multi-algorithm with custom order:")
print(" python src/run_pipeline.py --optimization-enabled --optimization-algorithm multi \\")
print(" --execution-order genetic pso simulated_annealing")
return True
else:
print(f"\n{total_tests - success_count} tests failed.")
print("Please review the error messages above.")
return False
def test_performance_characteristics():
"""Test performance characteristics."""
print("\nPERFORMANCE CHARACTERISTICS")
print("="*60)
try:
from weight_optimizer import OptimizationConfig, GeneticAlgorithm, ObjectiveFunction
# Create test data
np.random.seed(42)
test_df = pd.DataFrame({
'energy_norm': np.random.random(50),
'flood_norm': np.random.random(50),
'social_norm': np.random.random(50),
'green_norm': np.random.random(50)
})
objective_fn = ObjectiveFunction(test_df, ['energy_norm', 'flood_norm', 'social_norm', 'green_norm'])
bounds = [(0.01, 1.0) for _ in range(4)]
# Test scaling with population size
print("\nPopulation Size Scaling:")
print("-" * 25)
for pop_size in [5, 10, 20]:
config = OptimizationConfig(
algorithm="genetic",
population_size=pop_size,
max_iterations=5,
max_time_seconds=30,
seed=42
)
algorithm = GeneticAlgorithm(config)
start_time = time.time()
weights, fitness, stats = algorithm.optimize(objective_fn, 4, bounds)
elapsed_time = time.time() - start_time
print(f" Pop size {pop_size}: {elapsed_time:.3f}s, fitness={fitness:.4f}")
print("\nPerformance characteristics verified!")
return True
except Exception as e:
print(f"Performance test failed: {e}")
return False
def main():
"""Run all validation tests."""
print("METAHEURISTIC OPTIMIZATION VALIDATION")
print("CodeML Hackathon 2025")
print("Final Implementation Test")
# Run comprehensive functionality test
functionality_passed = test_complete_functionality()
# Run performance characteristics test
performance_passed = test_performance_characteristics()
print("\n" + "="*60)
print("FINAL VALIDATION RESULTS")
print("="*60)
if functionality_passed and performance_passed:
print("VALIDATION SUCCESSFUL!")
print("\nImplementation Status: COMPLETE AND TESTED")
print("Ready for: Production Use")
print("Features: All Implemented")
print("Performance: Verified")
print("\nIMPLEMENTATION SUMMARY:")
print("• Metaheuristic algorithms: Genetic Algorithm, PSO, Simulated Annealing")
print("• Multi-algorithm execution with configurable order")
print("• Time and iteration bounds with early termination")
print("• Multiple comparison metrics: fitness, stability, efficiency")
print("• Ensemble weight combination capability")
print("• Full pipeline integration")
print("• Comprehensive configuration management")
return True
else:
print("VALIDATION FAILED")
print("Some tests did not pass. Please review the detailed output above.")
return False
if __name__ == '__main__':
success = main()
sys.exit(0 if success else 1)