-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate_solver.py
More file actions
78 lines (63 loc) · 2.48 KB
/
evaluate_solver.py
File metadata and controls
78 lines (63 loc) · 2.48 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
import os
import time
import csv
from instance_parser import parse_instance
from constructive_solver import construct_initial_solution, post_merge_routes
from local_search import apply_local_search
from ga_operators import fitness_function
from utils import make_routes_battery_feasible
from pipeline import run_pipeline, run_ga_pipeline
from evrp_utils import sanitize_routes,filter_overloaded_routes
# === CONFIGURATION ===
INSTANCE_DIR = "instance_files"
OUTPUT_CSV = "evaluation_results.csv"
penalty_weights = {
'missing_customers': 1e6,
'battery_depletion': 1e4,
'unreachable_node': 1e5,
'unnecessary_recharges': 1000,
'low_battery': 5000,
'capacity_overload': 1e5,
'max_travel_time_exceeded': 1e5,
'invalid_route': 1e5,
'vehicle_count': 1e4,
}
# === EVALUATION LOOP ===
results = []
instance_files = [f for f in os.listdir(INSTANCE_DIR) if f.endswith(".xml")]
for filename in sorted(instance_files):
instance_path = os.path.join(INSTANCE_DIR, filename)
instance_id = filename.replace(".xml", "")
print(f"\n🔍 Evaluating instance: {instance_id}")
# Parse instance
(nodes, cs, depot, customers, cost_matrix, travel_time_matrix,
E_max, _, vehicle_capacity, max_travel_time, requests) = parse_instance(instance_path)
instance_data = (nodes, cs, depot, customers, cost_matrix, travel_time_matrix,
E_max, _, vehicle_capacity, max_travel_time, requests)
# === CWS EVALUATION ===
routes, stats = run_pipeline(instance_data, penalty_weights, method="CWS", visualize=True, instance_id=instance_id)
stats['instance_id'] = instance_id
stats['method'] = "CWS"
stats['num_customers'] = len(customers)
results.append(stats)
# === GA EVALUATION ===
ga_config = {
"num_generations": 50,
"population_size": 30,
"mutation_rate": 0.2,
"crossover_rate": 0.8,
"elite_fraction": 0.1,
"verbose": False
}
ga_routes, ga_stats = run_ga_pipeline(instance_data, penalty_weights, ga_config, visualize=True, instance_id=instance_id)
ga_stats['instance_id'] = instance_id
ga_stats['method'] = "GA"
ga_stats['num_customers'] = len(customers)
results.append(ga_stats)
# === WRITE TO CSV ===
with open(OUTPUT_CSV, "w", newline="") as csvfile:
fieldnames = list(results[0].keys())
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(results)
print("\n✅ Evaluation complete. Results saved to:", OUTPUT_CSV)