-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqiasa_test.py
More file actions
147 lines (121 loc) · 4.92 KB
/
Copy pathqiasa_test.py
File metadata and controls
147 lines (121 loc) · 4.92 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
import unittest
import numpy as np
from typing import List, Dict, Any
import time
import qiasa_advanced
from qiasa_advanced import load_city_data, QIASAParams, TSP, load_knapsack_data, KnapsackProblem, QIASA, load_job_data, JobScheduling
class TestQIASA(unittest.TestCase):
def setUp(self):
self.params = QIASAParams(
population_size=50,
max_iterations=100,
amplitude_decay=0.9,
amplitude_gain=1.1,
mutation_rate=0.1,
crossover_rate=0.8
)
def test_tsp_optimization(self):
# Test TSP with different sizes
sizes = [10, 20, 30]
results = []
for size in sizes:
cities = n_cities=size
tsp = TSP(cities)
optimizer = QIASA(tsp, self.params)
start_time = time.time()
solution, fitness, history = optimizer.optimize()
end_time = time.time()
results.append({
'size': size,
'fitness': fitness,
'time': end_time - start_time,
'convergence_rate': (history[0] - history[-1]) / len(history)
})
# Verify solution validity
self.assertTrue(tsp.validate(solution))
return results
def test_knapsack_optimization(self):
# Test Knapsack with different sizes
sizes = [20, 40, 60]
results = []
for size in sizes:
items, capacity = load_knapsack_data(n_items=size)
knapsack = KnapsackProblem(items, capacity)
optimizer = QIASA(knapsack, self.params)
start_time = time.time()
solution, fitness, history = optimizer.optimize()
end_time = time.time()
results.append({
'size': size,
'fitness': fitness,
'time': end_time - start_time,
'convergence_rate': (history[-1] - history[0]) / len(history)
})
# Verify solution validity
self.assertTrue(knapsack.validate(solution))
return results
def test_job_scheduling_optimization(self):
# Test Job Scheduling with different sizes
configs = [
{'jobs': 15, 'machines': 3},
{'jobs': 30, 'machines': 5},
{'jobs': 45, 'machines': 7}
]
results = []
for config in configs:
jobs, n_machines = load_job_data(
n_jobs=config['jobs'],
n_machines=config['machines']
)
scheduling = JobScheduling(jobs, n_machines)
optimizer = QIASA(scheduling, self.params)
start_time = time.time()
solution, fitness, history = optimizer.optimize()
end_time = time.time()
results.append({
'config': config,
'fitness': -fitness, # Convert back to makespan
'time': end_time - start_time,
'convergence_rate': (history[0] - history[-1]) / len(history)
})
# Verify solution validity
self.assertTrue(scheduling.validate(solution))
return results
def run_benchmark():
"""
Run comprehensive benchmarks and generate performance report
"""
test_suite = TestQIASA()
# Run all tests
tsp_results = test_suite.test_tsp_optimization()
knapsack_results = test_suite.test_knapsack_optimization()
scheduling_results = test_suite.test_job_scheduling_optimization()
# Print results
print("\nQIASA Performance Benchmark Results")
print("==================================")
print("\nTraveling Salesman Problem:")
print("--------------------------")
for result in tsp_results:
print(f"Size: {result['size']} cities")
print(f"Best Distance: {result['fitness']:.2f}")
print(f"Time: {result['time']:.2f} seconds")
print(f"Convergence Rate: {result['convergence_rate']:.4f}")
print()
print("\nKnapsack Problem:")
print("----------------")
for result in knapsack_results:
print(f"Size: {result['size']} items")
print(f"Best Value: {result['fitness']:.2f}")
print(f"Time: {result['time']:.2f} seconds")
print(f"Convergence Rate: {result['convergence_rate']:.4f}")
print()
print("\nJob Scheduling Problem:")
print("----------------------")
for result in scheduling_results:
print(f"Config: {result['config']['jobs']} jobs, {result['config']['machines']} machines")
print(f"Best Makespan: {result['fitness']:.2f}")
print(f"Time: {result['time']:.2f} seconds")
print(f"Convergence Rate: {result['convergence_rate']:.4f}")
print()
if __name__ == "__main__":
run_benchmark()