dkl9 commited on 2025-190 03:40:56
Showing 1 changed files, with 13 additions and 5 deletions.
| ... | ... |
@@ -61,15 +61,23 @@ def show_mat(distances: DistTable) -> str: |
| 61 | 61 |
rows.append(row) |
| 62 | 62 |
return "\n".join(rows) |
| 63 | 63 |
|
| 64 |
-# good results, slow |
|
| 64 |
+# this and other quirks in cachey_nearest are legitimately for speed |
|
| 65 |
+def min2(a, b): |
|
| 66 |
+ return a if a < b else b |
|
| 67 |
+ |
|
| 68 |
+# good results, slow: O(n^3) |
|
| 65 | 69 |
def cachey_nearest(distances: DistTable) -> IndSeq: |
| 66 | 70 |
seq = [] |
| 67 | 71 |
options = set(range(len(distances))) |
| 68 | 72 |
nearests = [math.inf for _ in range(len(distances))] |
| 69 | 73 |
while options: |
| 70 |
- reduceds = {o: [min(distances[o][i], nearests[i]) for i in range(len(nearests))] for o in options}
|
|
| 71 |
- best = min(options, key=lambda o: sum(reduceds[o])) |
|
| 72 |
- nearests = reduceds[best] |
|
| 74 |
+ best, brds, score = None, None, None |
|
| 75 |
+ for o in options: |
|
| 76 |
+ reduced = [min2(x, y) for x, y in zip(distances[o], nearests)] |
|
| 77 |
+ sr = sum(reduced) |
|
| 78 |
+ if best is None or sr < score: |
|
| 79 |
+ best, brds, score = o, reduced, sr |
|
| 80 |
+ nearests = brds |
|
| 73 | 81 |
seq.append(best) |
| 74 | 82 |
options.remove(best) |
| 75 | 83 |
return seq |
| ... | ... |
@@ -83,7 +91,7 @@ def cachey_maximin(distances: DistTable) -> IndSeq: |
| 83 | 91 |
options.remove(start) |
| 84 | 92 |
score = math.inf |
| 85 | 93 |
while options: |
| 86 |
- updateds = {o: min(min(distances[o][i] for i in seq), score) for o in options}
|
|
| 94 |
+ updateds = {o: min2(min(distances[o][i] for i in seq), score) for o in options}
|
|
| 87 | 95 |
best = max(options, key=lambda o: updateds[o]) |
| 88 | 96 |
score = updateds[best] |
| 89 | 97 |
seq.append(best) |
| 90 | 98 |