-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipeline.py
More file actions
249 lines (218 loc) · 7.55 KB
/
pipeline.py
File metadata and controls
249 lines (218 loc) · 7.55 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
from constructive_solver import construct_initial_solution, post_merge_routes
#from mainscript_constructive import battery_routes
from utils import make_routes_battery_feasible
from local_search import apply_local_search, route_cost
from route_utils import sanitize_routes
from evrp_utils import sanitize_routes, filter_overloaded_routes
import time
from heuristics import heuristic_population_initialization
from validation import validate_and_finalize_routes
from ga_operators import genetic_algorithm
from fitness import fitness_function
from local_search import route_cost
from local_search import plot_routes
from ga_operators import remove_trivial_routes
from constructive_solver import construct_initial_solution
from utils import make_routes_battery_feasible
from local_search import apply_local_search, route_cost, plot_routes
from heuristics import heuristic_population_initialization
from validation import validate_and_finalize_routes
import time
def run_pipeline(instance_data, penalty_weights, method="CWS", visualize=False):
(
nodes, charging_stations, depot, customers, cost_matrix, travel_time_matrix,
E_max, _, vehicle_capacity, max_travel_time, requests
) = instance_data
print(f"\n🛠️ Running {method} pipeline...")
# === Step 1: Construct Initial Routes ===
initial_routes = construct_initial_solution(
nodes=nodes,
depot=depot,
customers=customers,
cost_matrix=cost_matrix,
vehicle_capacity=vehicle_capacity,
E_max=E_max,
requests=requests,
charging_stations=charging_stations
)
print(f"[INFO] Initial constructed routes: {initial_routes}")
# === Step 2: Make Battery Feasible ===
battery_routes = make_routes_battery_feasible(
initial_routes,
cost_matrix,
E_max,
charging_stations,
depot
)
print(f"[INFO] After battery repair: {battery_routes}")
# === Step 3: Local Search Optimization ===
optimized_routes = apply_local_search(
battery_routes,
cost_matrix=cost_matrix,
travel_time_matrix=travel_time_matrix,
E_max=E_max,
charging_stations=charging_stations,
recharge_amount=E_max,
penalty_weights=penalty_weights,
depot=depot,
nodes=nodes,
vehicle_capacity=vehicle_capacity,
max_travel_time=max_travel_time,
requests=requests,
customers=customers
)
print(f"[INFO] After local search: {optimized_routes}")
# === Step 4: Final Battery Repair ===
battery_routes = make_routes_battery_feasible(
optimized_routes,
cost_matrix,
E_max,
charging_stations,
depot
)
print(f"[INFO] Final battery-feasible routes: {battery_routes}")
# === Step 5: Evaluation ===
fitness, battery_feasible = fitness_function(
battery_routes,
cost_matrix,
travel_time_matrix,
E_max,
charging_stations,
E_max,
penalty_weights,
depot,
nodes,
vehicle_capacity,
max_travel_time,
requests,
customers
)
print(f"\n📊 Final Fitness: {fitness:.2f}")
print(f"🔋 Battery Feasible: {'✅ Yes' if battery_feasible else '❌ No'}")
return battery_routes, {
"fitness": fitness,
"battery_feasible": battery_feasible
}
def run_ga_pipeline(instance_data, penalty_weights, ga_config, visualize=False, instance_id=""):
"""
Wrapper to run the GA-based EVRP solver and return comparable output for evaluation.
"""
# === Unpack instance ===
(nodes, charging_stations, depot, customers, cost_matrix, travel_time_matrix,
E_max, _, vehicle_capacity, max_travel_time, requests) = instance_data
try:
warm_routes, _ = run_pipeline(
instance_data,
penalty_weights,
method="CWS",
visualize=False,
)
except Exception as e:
print(f"[WARNING] Failed to generate warm-start CWS route: {e}")
warm_routes = []
DEPOT = depot
recharge_amount = E_max
start_time = time.time()
# === 1. Build initial population ===
population = heuristic_population_initialization(
nodes=nodes,
depot=depot,
vehicle_capacity=vehicle_capacity,
cost_matrix=cost_matrix,
travel_time_matrix=travel_time_matrix,
E_max=E_max,
charging_stations=charging_stations,
recharge_amount=recharge_amount,
requests=requests,
num_vehicles=ga_config.get("num_vehicles", 3),
population_size=ga_config.get("population_size", 30),
max_travel_time=max_travel_time,
initial_routes=warm_routes
)
# === 2. Run Genetic Algorithm ===
best_solution = genetic_algorithm(
population=population,
cost_matrix=cost_matrix,
travel_time_matrix=travel_time_matrix,
E_max=E_max,
charging_stations=charging_stations,
recharge_amount=recharge_amount,
penalty_weights=penalty_weights,
depot=depot,
nodes=nodes,
vehicle_capacity=vehicle_capacity,
max_travel_time=max_travel_time,
requests=requests,
customers=customers,
num_generations=ga_config.get("num_generations", 50),
population_size=ga_config.get("population_size", 30),
mutation_rate=ga_config.get("mutation_rate", 0.2),
crossover_rate=ga_config.get("crossover_rate", 0.8),
elite_fraction=ga_config.get("elite_fraction", 0.1),
verbose=ga_config.get("verbose", False)
)
# === 3. Final cleanup of best solution ===
best_routes = validate_and_finalize_routes(
best_solution,
cost_matrix,
E_max,
recharge_amount,
charging_stations,
depot,
nodes
)
best_solution = remove_trivial_routes(best_solution, depot, charging_stations)
# === 4. Fitness Evaluation ===
fitness_score, battery_valid = fitness_function(
battery_routes,
cost_matrix,
travel_time_matrix,
E_max,
charging_stations,
recharge_amount,
penalty_weights,
depot,
nodes,
vehicle_capacity,
max_travel_time,
requests
)
total_distance = sum(route_cost(route, cost_matrix) for route in best_routes if len(route) > 1)
cs_visits = sum(1 for route in best_routes for n in route if n in charging_stations)
runtime = round(time.time() - start_time, 2)
if best_routes and any(len(r) > 1 for r in best_routes):
plot_routes(
routes=best_routes,
nodes=nodes,
depot=depot,
cost_matrix=cost_matrix,
E_max=E_max,
save_plot=True,
method="GA",
instance_id=instance_id,
charging_stations=charging_stations
)
else:
print("[WARNING] No valid GA routes to plot.")
# Optional: visualize best solution
if visualize:
print(f"[DEBUG] Plotting GA final solution for {instance_id}")
plot_routes(
best_routes,
nodes=nodes,
depot=depot,
charging_stations=charging_stations,
method="GA",
save_plot=True,
instance_id=instance_id,
E_max=E_max,
cost_matrix=cost_matrix
)
return best_routes, {
'fitness_score': round(fitness_score, 2),
'is_feasible': battery_valid,
'num_routes': len(best_routes),
'total_distance': round(total_distance, 2),
'num_CS_visits': cs_visits,
'runtime_sec': runtime
}