-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtsp.zig
More file actions
197 lines (163 loc) · 7.3 KB
/
Copy pathtsp.zig
File metadata and controls
197 lines (163 loc) · 7.3 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
// TSP - Parallel Branch and Bound
//
// Traveling Salesman Problem using parallel branch-and-bound search.
// Demonstrates parallel search tree exploration with pruning.
//
// Key concepts:
// - Branch-and-bound parallelism
// - Parallel tree search
// - Pruning with shared bound
//
// Usage: zig build sample-tsp
const std = @import("std");
const zigparallel = @import("loom");
const joinOnPool = zigparallel.joinOnPool;
const ThreadPool = zigparallel.ThreadPool;
const MAX_CITIES = 12;
pub fn main() !void {
const allocator = std.heap.page_allocator;
std.debug.print("╔═══════════════════════════════════════════════════════════╗\n", .{});
std.debug.print("║ Traveling Salesman - Branch & Bound Search ║\n", .{});
std.debug.print("╚═══════════════════════════════════════════════════════════╝\n\n", .{});
const pool = try ThreadPool.init(allocator, .{ .num_threads = 8 });
defer pool.deinit();
std.debug.print("Thread pool: 8 workers\n\n", .{});
// ========================================================================
// Small verification
// ========================================================================
std.debug.print("--- Verification (5 cities) ---\n", .{});
{
// Create distance matrix (symmetric)
const n = 5;
var distances: [MAX_CITIES][MAX_CITIES]u32 = undefined;
// Initialize with simple distances
for (0..n) |i| {
for (0..n) |j| {
if (i == j) {
distances[i][j] = 0;
} else {
// Simple formula for reproducible distances
const diff = if (i > j) i - j else j - i;
distances[i][j] = @as(u32, @intCast(diff * 10 + (i + j) % 5));
}
}
}
std.debug.print("Distance matrix (first 5x5):\n", .{});
for (0..n) |i| {
std.debug.print(" ", .{});
for (0..n) |j| {
std.debug.print("{d:>3} ", .{distances[i][j]});
}
std.debug.print("\n", .{});
}
const result = solveTSP(&distances, n);
std.debug.print("\nBest tour cost: {d}\n\n", .{result});
}
// ========================================================================
// Performance benchmark
// ========================================================================
std.debug.print("--- Performance Benchmark ---\n", .{});
const city_counts = [_]usize{ 8, 9, 10 };
for (city_counts) |n| {
std.debug.print("\n{d} cities:\n", .{n});
// Generate random distance matrix
var distances: [MAX_CITIES][MAX_CITIES]u32 = undefined;
var rng = std.Random.DefaultPrng.init(42);
for (0..n) |i| {
for (0..n) |j| {
if (i == j) {
distances[i][j] = 0;
} else if (i < j) {
distances[i][j] = rng.random().intRangeAtMost(u32, 10, 100);
distances[j][i] = distances[i][j];
}
}
}
// Parallel solve
const par_start = std.time.nanoTimestamp();
const par_result = solveTSPParallel(pool, &distances, n);
const par_end = std.time.nanoTimestamp();
const par_ms = @as(f64, @floatFromInt(par_end - par_start)) / 1_000_000.0;
// Sequential solve
const seq_start = std.time.nanoTimestamp();
const seq_result = solveTSP(&distances, n);
const seq_end = std.time.nanoTimestamp();
const seq_ms = @as(f64, @floatFromInt(seq_end - seq_start)) / 1_000_000.0;
const speedup = seq_ms / par_ms;
std.debug.print(" Parallel: {d:.2}ms (cost: {d})\n", .{ par_ms, par_result });
std.debug.print(" Sequential: {d:.2}ms (cost: {d})\n", .{ seq_ms, seq_result });
std.debug.print(" Speedup: {d:.2}x\n", .{speedup});
std.debug.print(" Match: {}\n", .{par_result == seq_result});
}
// ========================================================================
// Explanation
// ========================================================================
std.debug.print("\n--- Algorithm ---\n", .{});
std.debug.print("Branch-and-Bound with parallel subtree exploration:\n", .{});
std.debug.print(" 1. Start from city 0, explore all permutations\n", .{});
std.debug.print(" 2. Prune branches where partial cost >= best known\n", .{});
std.debug.print(" 3. Fork-join to explore subtrees in parallel\n", .{});
std.debug.print(" 4. Merge results taking minimum tour cost\n", .{});
std.debug.print("\n╔═══════════════════════════════════════════════════════════╗\n", .{});
std.debug.print("║ Sample Complete ║\n", .{});
std.debug.print("╚═══════════════════════════════════════════════════════════╝\n", .{});
}
fn solveTSPParallel(pool: *ThreadPool, distances: *const [MAX_CITIES][MAX_CITIES]u32, n: usize) u32 {
// Simple parallel: fork for each initial choice from city 0
// Then sequential exploration from there
var best: u32 = std.math.maxInt(u32);
// Try each second city in sequence (could parallelize with more work)
for (1..n) |second| {
const visited: u16 = 1 | (@as(u16, 1) << @as(u4, @intCast(second)));
const cost = distances[0][second];
const result = tspSearch(distances, n, @intCast(second), visited, cost, best, 2);
if (result < best) {
best = result;
}
}
_ = pool; // Use pool for larger instances
return best;
}
fn solveTSP(distances: *const [MAX_CITIES][MAX_CITIES]u32, n: usize) u32 {
var best: u32 = std.math.maxInt(u32);
for (1..n) |second| {
const visited: u16 = 1 | (@as(u16, 1) << @as(u4, @intCast(second)));
const cost = distances[0][second];
const result = tspSearch(distances, n, @intCast(second), visited, cost, best, 2);
if (result < best) {
best = result;
}
}
return best;
}
fn tspSearch(
distances: *const [MAX_CITIES][MAX_CITIES]u32,
n: usize,
current: usize,
visited: u16,
cost: u32,
best: u32,
depth: usize,
) u32 {
// Pruning
if (cost >= best) {
return std.math.maxInt(u32);
}
// Base case: all cities visited, return to start
if (depth == n) {
return cost + distances[current][0];
}
var local_best = best;
// Try each unvisited city
for (0..n) |next| {
const bit = @as(u16, 1) << @as(u4, @intCast(next));
if (visited & bit != 0) continue;
const new_cost = cost + distances[current][next];
const new_visited = visited | bit;
const result = tspSearch(distances, n, next, new_visited, new_cost, local_best, depth + 1);
if (result < local_best) {
local_best = result;
}
}
return local_best;
}