]> git.sesse.net Git - nms/blob - tsp/tsp.cpp
Add a pessimistic distance to complement the optimistic one.
[nms] / tsp / tsp.cpp
1 #include <stdio.h>
2 #include <limits.h>
3 #include <vector>
4 #include <set>
5 #include <algorithm>
6
7 #define MIN_ROW 1
8 #define MAX_ROW 75
9 #define MIN_SWITCH 1
10 #define MAX_SWITCH 6
11 #define HEAP_MST 0
12
13 static const unsigned num_cache_elem = (MAX_ROW * MAX_SWITCH * 2) * (MAX_ROW * MAX_SWITCH * 2);
14 static unsigned short dist_cache[(MAX_ROW * MAX_SWITCH * 2) * (MAX_ROW * MAX_SWITCH * 2)],
15         opt_dist_cache[MAX_ROW * MAX_SWITCH * MAX_ROW * MAX_SWITCH],
16         pess_dist_cache[MAX_ROW * MAX_SWITCH * MAX_ROW * MAX_SWITCH];
17
18 inline unsigned short &cache(
19         unsigned row_from, unsigned switch_from, unsigned side_from,
20         unsigned row_to, unsigned switch_to, unsigned side_to)
21 {
22         return dist_cache[(row_from * MAX_SWITCH * 2 + switch_from * 2 + side_from) * (MAX_ROW * MAX_SWITCH * 2) +
23                 row_to * MAX_SWITCH * 2 + switch_to * 2 + side_to];
24 }
25
26 inline unsigned short &opt_cache(
27         unsigned row_from, unsigned switch_from,
28         unsigned row_to, unsigned switch_to)
29 {
30         return opt_dist_cache[(row_from * MAX_SWITCH + switch_from) * (MAX_ROW * MAX_SWITCH) +
31                 row_to * MAX_SWITCH + switch_to];
32 }
33
34 inline unsigned short &pess_cache(
35         unsigned row_from, unsigned switch_from,
36         unsigned row_to, unsigned switch_to)
37 {
38         return pess_dist_cache[(row_from * MAX_SWITCH + switch_from) * (MAX_ROW * MAX_SWITCH) +
39                 row_to * MAX_SWITCH + switch_to];
40 }
41
42 struct order {
43         unsigned row, num;
44         int side;
45         int cost;
46
47         bool operator< (const order &other) const
48         {
49                 return (cost < other.cost);
50         }
51 };
52
53 static unsigned best_so_far = UINT_MAX;
54 order *best_tour;
55
56 int distance_switch(unsigned from, unsigned to)
57 {
58         /* on the same side of the middle? 9.6m per switch. */
59         if ((from > 3) == (to > 3)) {
60                 return abs(from - to) * 96;
61         }
62
63         /* have to cross the border? 25.8m from sw3->sw4 => 16.2m extra gap. */
64         /* that's _got_ to be wrong. say it's 3m. */
65         return abs(from - to) * 96 + 30;
66 }
67
68 int distance_middle(unsigned sw, unsigned middle)
69 {
70         /* symmetry: 4-5-6 are just mirrored 3-2-1. */
71         if (middle == 2) {
72                 if (sw > 3)
73                         sw = 7 - sw;
74
75                 /* estimate 25.8m/2 = 12.9m from sw3 to the middle */
76                 return 129 + (3 - sw) * 96;
77         }
78         
79         /* more symmetry -- getting from 1-6 to the top is like getting from 6-1 to the bottom. */
80         if (middle == 3) {
81                 middle = 1;
82                 sw = 7 - sw;
83         }
84
85         /* guesstimate 4.8m extra to get to the bottom */
86         if (sw > 3)
87                 return 48 + 162 + (sw - 1) * 96;
88         else
89                 return 48 + (sw - 1) * 96;
90 }
91
92 int distance_row(unsigned from, unsigned to)
93 {
94         /* don't calculate gaps here just yet, just estimate 4.1m per double row */
95         return 41 * abs(from - to);
96 }
97
98 int pessimistic_distance(int row_from, int switch_from, int row_to, int switch_to)
99 {
100         /* we'll need to go to one of the three middles */
101         int best2 = distance_middle(switch_from, 2) + distance_middle(switch_to, 2);
102         int distrow = distance_row(row_from, row_to);
103         if ((switch_from > 3) != (switch_to > 3))
104                 return best2 + distrow;
105         if (switch_from > 3) {
106                 int best3 = distance_middle(switch_from, 3) + distance_middle(switch_to, 3);
107                 return std::min(best2, best3) + distrow;
108         } else {
109                 int best1 = distance_middle(switch_from, 1) + distance_middle(switch_to, 1);
110                 return std::min(best2, best1) + distrow;
111         }
112 }
113
114 int distance(int row_from, int switch_from, int side_from, int row_to, int switch_to, int side_to)
115 {
116         /* can we just walk directly? */
117         if (row_from == row_to && side_from == side_to) {
118                 return distance_switch(switch_from, switch_to);
119         }
120         
121         /* can we just switch sides? */
122         if (row_from + 1 == row_to && side_from == 1 && side_to == 0) {
123                 return distance_switch(switch_from, switch_to);
124         }
125         if (row_from == row_to + 1 && side_from == 0 && side_to == 1) {
126                 return distance_switch(switch_from, switch_to);
127         }
128
129         return pessimistic_distance(row_from, switch_from, row_to, switch_to);
130 }       
131
132 int optimistic_distance(int row_from, int switch_from, int row_to, int switch_to)
133 {
134         if (abs(row_from - row_to) <= 1)
135                 return distance_switch(switch_from, switch_to);
136         else
137                 return pessimistic_distance(row_from, switch_from, row_to, switch_to);
138 }
139
140 #if HEAP_MST
141 // this is, surprisingly enough, _slower_ than the naive variant below, so it's not enabled
142 struct prim_queue_val {
143         std::pair<unsigned, unsigned> dst;
144         int cost;
145
146         bool operator< (const prim_queue_val &other) const
147         {
148                 return (cost > other.cost);
149         }
150 };
151
152 // standard O(V^2 log v) prim
153 int prim_mst(std::set<std::pair<unsigned, unsigned> > &in)
154 {
155         std::set<std::pair<unsigned, unsigned> > set2;
156         std::priority_queue<prim_queue_val> queue;
157
158         // pick the first one
159         std::set<std::pair<unsigned, unsigned> >::iterator start = in.begin();
160         
161         unsigned row = start->first;
162         unsigned num = start->second;
163
164         set2.insert(*start);
165         
166         // find all the edges out from it
167         for (std::set<std::pair<unsigned, unsigned> >::iterator j = in.begin(); j != in.end(); ++j) {
168                 if (set2.count(*j))
169                         continue;
170                 
171                 unsigned d = opt_cache(row, num, j->first, j->second);
172                 prim_queue_val val = { *j, d };
173                 queue.push(val);
174         }
175
176         unsigned total_cost = 0;
177         while (set2.size() != in.size()) {
178 invalid:
179                 prim_queue_val val = queue.top();
180                 queue.pop();
181                 
182                 // check if dst is already moved
183                 if (set2.count(val.dst))
184                         goto invalid;
185         
186                 unsigned row = val.dst.first;
187                 unsigned num = val.dst.second;
188                 set2.insert(val.dst);
189
190                 total_cost += val.cost;
191
192                 // find all the edges from this new node
193                 for (std::set<std::pair<unsigned, unsigned> >::iterator j = in.begin(); j != in.end(); ++j) {
194                         if (set2.count(*j))
195                                 continue;
196                         
197                         unsigned d = opt_cache(row, num, j->first, j->second);
198                         prim_queue_val val = { *j, d };
199                         queue.push(val);
200                 }
201         }
202
203         return total_cost;
204 }
205 #else
206 // extremely primitive O(V^3) prim
207 int prim_mst(std::set<std::pair<unsigned, unsigned> > &set1)
208 {
209         std::set<std::pair<unsigned, unsigned> > set2;
210
211         // pick the first one
212         std::set<std::pair<unsigned, unsigned> >::iterator start = set1.begin();
213         set2.insert(*start);
214         set1.erase(start);
215
216         unsigned total_cost = 0;
217         while (set1.size() > 0) {
218                 unsigned best_this_cost = UINT_MAX;
219                 std::set<std::pair<unsigned, unsigned> >::iterator best_set1;
220                 
221                 for (std::set<std::pair<unsigned, unsigned> >::iterator i = set1.begin(); i != set1.end(); ++i) {
222                         for (std::set<std::pair<unsigned, unsigned> >::iterator j = set2.begin(); j != set2.end(); ++j) {
223                                 unsigned d = opt_cache(i->first, i->second, j->first, j->second);
224                                 if (d < best_this_cost) {
225                                         best_this_cost = d;
226                                         best_set1 = i;
227                                 }
228                         }
229                 }
230
231                 set2.insert(*best_set1);
232                 set1.erase(best_set1);
233                 total_cost += best_this_cost;
234         }
235
236         return total_cost;
237 }
238 #endif
239
240 void print_tour(std::vector<std::pair<unsigned, unsigned> > &points)
241 {
242         std::set<std::pair<unsigned, unsigned> > points_left;
243         for (unsigned i = 0; i < points.size(); ++i) {
244                 points_left.insert(points[i]);
245         }
246         
247         for (unsigned i = 0; i < points.size(); ++i) {
248                 if (best_tour[i].side == 0)
249                         printf("%2u-%u (left side)  ", best_tour[i].row, best_tour[i].num);
250                 else
251                         printf("%2u-%u (right side) ", best_tour[i].row, best_tour[i].num);
252                 if (i == 0) {
253                         printf("           ");
254                 } else {
255                         printf("cost=%4u  ", best_tour[i].cost);
256                 }
257
258                 // let's see how good the MST heuristics are
259                 if (i != points.size() - 1) {
260                         std::set<std::pair<unsigned, unsigned> > mst_tree = points_left;
261                         printf("mst_bound=%5u, ", prim_mst(mst_tree));
262
263                         unsigned rest_cost = 0;
264                         for (unsigned j = i + 1; j < points.size(); ++j) {
265                                 rest_cost += best_tour[j].cost;
266                         }
267                         
268                         printf("rest_cost=%5u", rest_cost);
269                 }
270
271                 printf("\n");
272                 
273                 std::set<std::pair<unsigned, unsigned> >::iterator j = points_left.find(std::make_pair(best_tour[i].row, best_tour[i].num));
274                 points_left.erase(j);
275         }
276 }
277
278 unsigned do_tsp(std::vector<std::pair<unsigned, unsigned> > &points, std::set<std::pair<unsigned, unsigned> > &points_left, order *ord, order *temp, unsigned ind, unsigned cost_so_far)
279 {
280         if (cost_so_far >= best_so_far)
281                 return UINT_MAX;
282         if (ind == points.size()) {
283                 memcpy(best_tour, ord, sizeof(order) * points.size());
284                 printf("\nNew best tour found! cost=%u\n", cost_so_far);
285                 print_tour(points);
286                 best_so_far = cost_so_far;
287                 return 0;
288         }
289
290         /* 
291          * Simple heuristic: always search for the closest points from this one first; that
292          * will give us a sizable cutoff.
293          */
294         unsigned toi = 0;
295         unsigned last_row = ord[ind-1].row;
296         unsigned last_switch = ord[ind-1].num;
297         unsigned last_side = ord[ind-1].side;
298         
299         std::set<std::pair<unsigned, unsigned> > mst_set = points_left;
300         mst_set.insert(std::make_pair(last_row, last_switch));
301         
302         for (std::set<std::pair<unsigned, unsigned> >::iterator i = points_left.begin(); i != points_left.end(); ++i) {
303                 /* try both sides */
304                 temp[toi].row = i->first;
305                 temp[toi].num = i->second;
306                 temp[toi].side = 0;
307                 temp[toi].cost = cache(last_row, last_switch, last_side, i->first, i->second, 0);
308                 ++toi;
309
310                 temp[toi].row = i->first;
311                 temp[toi].num = i->second;
312                 temp[toi].side = 1;
313                 temp[toi].cost = cache(last_row, last_switch, last_side, i->first, i->second, 1);
314                 ++toi;
315         }
316
317         unsigned min_rest_cost = prim_mst(mst_set);
318         if (cost_so_far + min_rest_cost >= best_so_far) {
319                 return UINT_MAX;
320         }
321         
322         std::sort(temp, temp + toi);
323
324         unsigned best_this_cost = UINT_MAX;
325         for (unsigned i = 0; i < toi; ++i)
326         {
327                 ord[ind] = temp[i];
328                 
329                 std::set<std::pair<unsigned, unsigned> >::iterator j = points_left.find(std::make_pair(temp[i].row, temp[i].num));
330                 points_left.erase(j);
331                 unsigned cost_rest = do_tsp(points, points_left, ord, temp + points.size() * 2, ind + 1, cost_so_far + temp[i].cost);
332                 points_left.insert(std::make_pair(temp[i].row, temp[i].num));
333                 
334                 best_this_cost = std::min(best_this_cost, cost_rest);
335         }
336
337         return best_this_cost;
338 }
339
340 int main()
341 {
342         std::vector<std::pair<unsigned, unsigned> > points;
343         std::set<std::pair<unsigned, unsigned> > points_left;
344
345         for ( ;; ) {
346                 unsigned row, sw;
347                 if (scanf("%u-%u", &row, &sw) != 2)
348                         break;
349
350                 if (row < MIN_ROW || row > MAX_ROW || sw < MIN_SWITCH || sw > MAX_SWITCH) {
351                         fprintf(stderr, "%u-%u is out of bounds!\n", row, sw);
352                         exit(1);
353                 }
354
355                 points.push_back(std::make_pair(row, sw));
356                 if (points.size() != 1)
357                         points_left.insert(std::make_pair(row, sw));
358         }
359
360         // precalculate all distances
361         for (unsigned i = 0; i < points.size(); ++i) {
362                 for (unsigned j = 0; j < points.size(); ++j) {
363                         cache(points[i].first, points[i].second, 0, points[j].first, points[j].second, 0) =
364                                 distance(points[i].first, points[i].second, 0, points[j].first, points[j].second, 0);
365                         
366                         cache(points[i].first, points[i].second, 0, points[j].first, points[j].second, 1) =
367                                 distance(points[i].first, points[i].second, 0, points[j].first, points[j].second, 1);
368                         
369                         cache(points[i].first, points[i].second, 1, points[j].first, points[j].second, 0) =
370                                 distance(points[i].first, points[i].second, 1, points[j].first, points[j].second, 0);
371                         
372                         cache(points[i].first, points[i].second, 1, points[j].first, points[j].second, 1) =
373                                 distance(points[i].first, points[i].second, 1, points[j].first, points[j].second, 1);
374                         
375                         opt_cache(points[i].first, points[i].second, points[j].first, points[j].second) =
376                                 optimistic_distance(points[i].first, points[i].second, points[j].first, points[j].second);
377                         
378                         pess_cache(points[i].first, points[i].second, points[j].first, points[j].second) =
379                                 pessimistic_distance(points[i].first, points[i].second, points[j].first, points[j].second);
380                 }
381         }
382         
383         order *ord = new order[points.size()];
384         best_tour = new order[points.size()];
385         order *temp = new order[points.size() * points.size() * 4];
386         
387         /* always start at the first one, left side (hack) */
388         ord[0].row = points[0].first;
389         ord[0].num = points[0].second;
390         ord[0].side = 0;
391         
392         do_tsp(points, points_left, ord, temp, 1, 0);
393         printf("All done.\n");
394 }
395
396