-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
130 lines (109 loc) · 4.66 KB
/
main.py
File metadata and controls
130 lines (109 loc) · 4.66 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
#!/usr/bin/env python3
"""
Quantum-Classical Hybrid Sawmill Optimization — Main Entry Point
Runs a batch benchmark of 50 logs through both solvers and generates
visualization outputs.
Usage:
python main.py [--n-logs N] [--qaoa-depth P]
"""
import argparse
import sys
import time
import numpy as np
from data_generator import generate_batch
from classical_solver import greedy_solve
from quantum_solver import qaoa_solve
from visualize_results import generate_all_plots
def fao_cost_model(log, result):
"""
FAO-based cost estimation.
Log cost: $0.30 per board-foot of raw log volume
Processing cost: $5.00 fixed + $0.50 per board cut
"""
log_volume_bf = log.volume_cubic_feet * 12 # rough board-feet
log_cost = log_volume_bf * 0.30
n_cuts = len(result['selected_boards'])
processing_cost = 5.0 + n_cuts * 0.50
total_cost = log_cost + processing_cost
revenue = result['total_value']
value_recovery_pct = (revenue / max(total_cost, 0.01)) * 100
roi = (revenue - total_cost) / max(total_cost, 0.01) * 100
return {
'log_cost': log_cost,
'processing_cost': processing_cost,
'total_cost': total_cost,
'revenue': revenue,
'value_recovery_pct': value_recovery_pct,
'roi': roi,
}
def main():
parser = argparse.ArgumentParser(description='Quantum Sawmill Optimization')
parser.add_argument('--n-logs', type=int, default=50, help='Number of logs')
parser.add_argument('--qaoa-depth', type=int, default=1, help='QAOA circuit depth')
args = parser.parse_args()
n_logs = args.n_logs
print(f"{'='*60}")
print(f" Quantum-Classical Hybrid Sawmill Optimization")
print(f" Batch size: {n_logs} logs | QAOA depth: {args.qaoa_depth}")
print(f"{'='*60}\n")
# Phase 2: Generate logs
print("[Phase 2] Generating synthetic logs...")
logs = generate_batch(n_logs)
total_candidates = sum(len(l.board_candidates) for l in logs)
total_defects = sum(len(l.defects) for l in logs)
print(f" Generated {n_logs} logs with {total_candidates} board candidates "
f"and {total_defects} defect zones.\n")
# Phase 3: Solve
classical_results = []
quantum_results = []
classical_costs = []
quantum_costs = []
print("[Phase 3] Running solvers...")
for i, log in enumerate(logs):
# Classical
c_result = greedy_solve(log)
classical_results.append(c_result)
classical_costs.append(fao_cost_model(log, c_result))
# Quantum
q_result = qaoa_solve(log, p=args.qaoa_depth)
quantum_results.append(q_result)
quantum_costs.append(fao_cost_model(log, q_result))
if (i + 1) % 10 == 0 or i == 0:
print(f" Log {i+1}/{n_logs}: "
f"Classical=${c_result['total_value']:.2f} "
f"({c_result['solve_time']:.3f}s) | "
f"Quantum=${q_result['total_value']:.2f} "
f"({q_result['solve_time']:.3f}s)")
# Phase 4: Analyze & Visualize
print(f"\n[Phase 4] Generating visualizations...")
plot_paths = generate_all_plots(logs, classical_results, quantum_results)
for p in plot_paths:
print(f" Saved: {p}")
# Summary statistics
c_total = sum(r['total_value'] for r in classical_results)
q_total = sum(r['total_value'] for r in quantum_results)
c_waste_avg = np.mean([r['waste_fraction'] for r in classical_results]) * 100
q_waste_avg = np.mean([r['waste_fraction'] for r in quantum_results]) * 100
c_defects = sum(r['defect_discoveries'] for r in classical_results)
q_defects = sum(r['defect_discoveries'] for r in quantum_results)
c_time = sum(r['solve_time'] for r in classical_results)
q_time = sum(r['solve_time'] for r in quantum_results)
c_roi_avg = np.mean([c['roi'] for c in classical_costs])
q_roi_avg = np.mean([c['roi'] for c in quantum_costs])
print(f"\n{'='*60}")
print(f" BENCHMARK RESULTS ({n_logs} logs)")
print(f"{'='*60}")
print(f" {'Metric':<30} {'Classical':>12} {'Quantum':>12}")
print(f" {'-'*54}")
print(f" {'Total Value ($)':<30} {c_total:>12.2f} {q_total:>12.2f}")
print(f" {'Avg Waste (%)':<30} {c_waste_avg:>12.1f} {q_waste_avg:>12.1f}")
print(f" {'Defect Discoveries':<30} {c_defects:>12d} {q_defects:>12d}")
print(f" {'Total Solve Time (s)':<30} {c_time:>12.3f} {q_time:>12.3f}")
print(f" {'Avg ROI (%)':<30} {c_roi_avg:>12.1f} {q_roi_avg:>12.1f}")
advantage = (q_total - c_total) / max(c_total, 0.01) * 100
print(f" {'Quantum Advantage (%)':<30} {advantage:>12.1f}%")
print(f"{'='*60}\n")
print("Done. All outputs saved to output/")
return 0
if __name__ == '__main__':
sys.exit(main())