]> git.sesse.net Git - nms/blob - tsp/tsp.cpp
06fb7f0f1df7c0e13ca2450c48028e1e62fd2f7a
[nms] / tsp / tsp.cpp
1 #include <stdio.h>
2 #include <limits.h>
3 #include <vector>
4 #include <set>
5 #include <algorithm>
6
7 struct order {
8         unsigned row, num;
9         int side;
10         int cost;
11
12         bool operator< (const order &other) const
13         {
14                 return (cost < other.cost);
15         }
16 };
17
18 static unsigned best_so_far = UINT_MAX;
19 order *best_tour;
20
21 int distance_switch(unsigned from, unsigned to)
22 {
23         /* on the same side of the middle? 9.6m per switch. */
24         if ((from > 3) == (to > 3)) {
25                 return abs(from - to) * 96;
26         }
27
28         /* have to cross the border? 25.8m from sw3->sw4 => 16.2m extra gap. */
29         /* that's _got_ to be wrong. say it's 3m. */
30         return abs(from - to) * 96 + 30;
31 }
32
33 int distance_middle(unsigned sw, unsigned middle)
34 {
35         /* symmetry: 4-5-6 are just mirrored 3-2-1. */
36         if (middle == 2) {
37                 if (sw > 3)
38                         sw = 7 - sw;
39
40                 /* estimate 25.8m/2 = 12.9m from sw3 to the middle */
41                 return 129 + (3 - sw) * 96;
42         }
43         
44         /* more symmetry -- getting from 1-6 to the top is like getting from 6-1 to the bottom. */
45         if (middle == 3) {
46                 middle = 1;
47                 sw = 7 - sw;
48         }
49
50         /* guesstimate 4.8m extra to get to the bottom */
51         if (sw > 3)
52                 return 48 + 162 + (sw - 1) * 96;
53         else
54                 return 48 + (sw - 1) * 96;
55 }
56
57 int distance_row(unsigned from, unsigned to)
58 {
59         /* don't calculate gaps here just yet, just estimate 4.1m per double row */
60         return 41 * abs(from - to);
61 }
62
63 int distance(int row_from, int switch_from, int side_from, int row_to, int switch_to, int side_to)
64 {
65         /* can we just walk directly? */
66         if (row_from == row_to && side_from == side_to) {
67                 return distance_switch(switch_from, switch_to);
68         }
69
70         /* can we just switch sides? */
71         if (row_from + 1 == row_to && side_from == 1 && side_to == -1) {
72                 return distance_switch(switch_from, switch_to);
73         }
74         if (row_from == row_to + 1 && side_from == -1 && side_to == 1) {
75                 return distance_switch(switch_from, switch_to);
76         }
77         
78         /* we'll need to go to one of the three middles */
79         int best1 = distance_middle(switch_from, 1) + distance_middle(switch_to, 1);
80         int best2 = distance_middle(switch_from, 2) + distance_middle(switch_to, 2);
81         int best3 = distance_middle(switch_from, 3) + distance_middle(switch_to, 3);
82         return std::min(std::min(best1, best2), best3) + distance_row(row_from, row_to);
83 }
84
85 int optimistic_distance(int row_from, int switch_from, int row_to, int switch_to)
86 {
87         if (abs(row_from - row_to) == 1)
88                 return distance_switch(switch_from, switch_to);
89         else
90                 return distance(row_from, switch_from, -1, row_to, switch_to, -1);
91 }
92
93 // extremely primitive O(V^2) prim
94 int prim_mst(std::set<std::pair<unsigned, unsigned> > &set1)
95 {
96         std::set<std::pair<unsigned, unsigned> > set2;
97
98         // pick the first one
99         std::set<std::pair<unsigned, unsigned> >::iterator start = set1.begin();
100         set2.insert(*start);
101         set1.erase(start);
102
103         unsigned total_cost = 0;
104         while (set1.size() > 0) {
105                 unsigned best_this_cost = UINT_MAX;
106                 std::set<std::pair<unsigned, unsigned> >::iterator best_set1;
107                 
108                 for (std::set<std::pair<unsigned, unsigned> >::iterator i = set1.begin(); i != set1.end(); ++i) {
109                         for (std::set<std::pair<unsigned, unsigned> >::iterator j = set2.begin(); j != set2.end(); ++j) {
110                                 unsigned d = optimistic_distance(i->first, i->second, j->first, j->second);
111                                 if (d < best_this_cost) {
112                                         best_this_cost = d;
113                                         best_set1 = i;
114                                 }
115                         }
116                 }
117
118                 set2.insert(*best_set1);
119                 set1.erase(best_set1);
120                 total_cost += best_this_cost;
121         }
122
123         return total_cost;
124 }
125
126
127 void print_tour(std::vector<std::pair<unsigned, unsigned> > &points)
128 {
129         std::set<std::pair<unsigned, unsigned> > points_left;
130         for (unsigned i = 0; i < points.size(); ++i) {
131                 points_left.insert(points[i]);
132         }
133         
134         for (unsigned i = 0; i < points.size(); ++i) {
135                 if (best_tour[i].side == -1)
136                         printf("%2u-%u (left side)  ", best_tour[i].row, best_tour[i].num);
137                 else
138                         printf("%2u-%u (right side) ", best_tour[i].row, best_tour[i].num);
139                 if (i == 0) {
140                         printf("           ");
141                 } else {
142                         printf("cost=%4u  ", best_tour[i].cost);
143                 }
144
145                 // let's see how good the MST heuristics are
146                 if (i != points.size() - 1) {
147                         std::set<std::pair<unsigned, unsigned> > mst_tree = points_left;
148                         printf("mst_bound=%5u, ", prim_mst(mst_tree));
149
150                         unsigned rest_cost = 0;
151                         for (unsigned j = i + 1; j < points.size(); ++j) {
152                                 rest_cost += best_tour[j].cost;
153                         }
154                         
155                         printf("rest_cost=%5u", rest_cost);
156                 }
157
158                 printf("\n");
159                 
160                 std::set<std::pair<unsigned, unsigned> >::iterator j = points_left.find(std::make_pair(best_tour[i].row, best_tour[i].num));
161                 points_left.erase(j);
162         }
163 }
164
165 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)
166 {
167         if (cost_so_far >= best_so_far)
168                 return UINT_MAX;
169         if (ind == points.size()) {
170                 memcpy(best_tour, ord, sizeof(order) * points.size());
171                 printf("\nNew best tour found! cost=%u\n", cost_so_far);
172                 print_tour(points);
173                 best_so_far = cost_so_far;
174                 return 0;
175         }
176
177         /* 
178          * Simple heuristic: always search for the closest points from this one first; that
179          * will give us a sizable cutoff.
180          */
181         unsigned toi = 0;
182         unsigned last_row = ord[ind-1].row;
183         unsigned last_switch = ord[ind-1].num;
184         unsigned last_side = ord[ind-1].side;
185         
186         std::set<std::pair<unsigned, unsigned> > mst_set = points_left;
187         mst_set.insert(std::make_pair(last_row, last_switch));
188         
189         for (std::set<std::pair<unsigned, unsigned> >::iterator i = points_left.begin(); i != points_left.end(); ++i) {
190                 /* try both sides */
191                 temp[toi].row = i->first;
192                 temp[toi].num = i->second;
193                 temp[toi].side = -1;
194                 temp[toi].cost = distance(last_row, last_switch, last_side, i->first, i->second, -1);
195                 ++toi;
196
197                 temp[toi].row = i->first;
198                 temp[toi].num = i->second;
199                 temp[toi].side = +1;
200                 temp[toi].cost = distance(last_row, last_switch, last_side, i->first, i->second, +1);
201                 ++toi;
202         }
203
204         unsigned min_rest_cost = prim_mst(mst_set);
205         if (cost_so_far + min_rest_cost >= best_so_far) {
206                 return UINT_MAX;
207         }
208         
209         std::sort(temp, temp + toi);
210
211         unsigned best_this_cost = UINT_MAX;
212         for (unsigned i = 0; i < toi; ++i)
213         {
214                 ord[ind] = temp[i];
215                 
216                 std::set<std::pair<unsigned, unsigned> >::iterator j = points_left.find(std::make_pair(temp[i].row, temp[i].num));
217                 points_left.erase(j);
218                 unsigned cost_rest = do_tsp(points, points_left, ord, temp + points.size() * 2, ind + 1, cost_so_far + temp[i].cost);
219                 points_left.insert(std::make_pair(temp[i].row, temp[i].num));
220                 
221                 best_this_cost = std::min(best_this_cost, cost_rest);
222         }
223
224         return best_this_cost;
225 }
226
227 int main()
228 {
229         std::vector<std::pair<unsigned, unsigned> > points;
230         std::set<std::pair<unsigned, unsigned> > points_left;
231
232         for ( ;; ) {
233                 unsigned row, sw;
234                 if (scanf("%u-%u", &row, &sw) != 2)
235                         break;
236
237                 points.push_back(std::make_pair(row, sw));
238                 if (points.size() != 1)
239                         points_left.insert(std::make_pair(row, sw));
240         }
241
242         order *ord = new order[points.size()];
243         best_tour = new order[points.size()];
244         order *temp = new order[points.size() * points.size() * 4];
245         
246         /* always start at the first one, left side (hack) */
247         ord[0].row = points[0].first;
248         ord[0].num = points[0].second;
249         ord[0].side = -1;
250         
251         do_tsp(points, points_left, ord, temp, 1, 0);
252         printf("All done.\n");
253 }
254
255