Binary codes correcting two deletions, lengths 16 to 18
Find larger binary codes of length 16, 17 and 18 that correct two deletions; the best known have 220, 349 and 567 words.
Add an incidence-based search and a 320-word n17 construction
Browse files+325 −0 routes/irisbud-codex/n17-local-search/n17.json added View file
| @@ -0,0 +1,325 @@ | ||
| 1 | +{ | |
| 2 | + "n": 17, | |
| 3 | + "words": [ | |
| 4 | + "00000000000000000", | |
| 5 | + "00000000000001101", | |
| 6 | + "00000000000111111", | |
| 7 | + "00000000001110000", | |
| 8 | + "00000000010110011", | |
| 9 | + "00000000101111010", | |
| 10 | + "00000000110010001", | |
| 11 | + "00000000111101111", | |
| 12 | + "00000001010010111", | |
| 13 | + "00000001101000000", | |
| 14 | + "00000001110011100", | |
| 15 | + "00000001111000011", | |
| 16 | + "00000010011000110", | |
| 17 | + "00000010101010100", | |
| 18 | + "00000010110111011", | |
| 19 | + "00000011000011101", | |
| 20 | + "00000011011111000", | |
| 21 | + "00000011111000100", | |
| 22 | + "00000011111111101", | |
| 23 | + "00000100111010101", | |
| 24 | + "00000101000100000", | |
| 25 | + "00000101100000011", | |
| 26 | + "00000110001100111", | |
| 27 | + "00000110101000101", | |
| 28 | + "00000110101011110", | |
| 29 | + "00000111000011000", | |
| 30 | + "00000111011011001", | |
| 31 | + "00000111110001111", | |
| 32 | + "00001000100110101", | |
| 33 | + "00001001100111111", | |
| 34 | + "00001010000011110", | |
| 35 | + "00001010010111000", | |
| 36 | + "00001010110010010", | |
| 37 | + "00001010111101110", | |
| 38 | + "00001011110110000", | |
| 39 | + "00001100000100001", | |
| 40 | + "00001100011111001", | |
| 41 | + "00001100111000001", | |
| 42 | + "00001110001010110", | |
| 43 | + "00001110010000100", | |
| 44 | + "00001110111000110", | |
| 45 | + "00001110111011111", | |
| 46 | + "00001111001110100", | |
| 47 | + "00001111110000000", | |
| 48 | + "00001111111111000", | |
| 49 | + "00010000110110010", | |
| 50 | + "00010001110100111", | |
| 51 | + "00010010000100101", | |
| 52 | + "00010100101000011", | |
| 53 | + "00010101000111011", | |
| 54 | + "00010110001100100", | |
| 55 | + "00010110010010111", | |
| 56 | + "00010111010010001", | |
| 57 | + "00010111011111010", | |
| 58 | + "00010111101011011", | |
| 59 | + "00011000000110100", | |
| 60 | + "00011000010011111", | |
| 61 | + "00011001100101000", | |
| 62 | + "00011001101011101", | |
| 63 | + "00011010000000000", | |
| 64 | + "00011010111100100", | |
| 65 | + "00011011011100111", | |
| 66 | + "00011100000000111", | |
| 67 | + "00011100011111110", | |
| 68 | + "00011101100000101", | |
| 69 | + "00011110000110011", | |
| 70 | + "00011111100111001", | |
| 71 | + "00011111110100001", | |
| 72 | + "00011111111011110", | |
| 73 | + "00100010010000010", | |
| 74 | + "00100010100111001", | |
| 75 | + "00100100011011110", | |
| 76 | + "00100101010101011", | |
| 77 | + "00100110110000111", | |
| 78 | + "00100110111010100", | |
| 79 | + "00101000001001100", | |
| 80 | + "00101000101100000", | |
| 81 | + "00101001001111100", | |
| 82 | + "00101001011111111", | |
| 83 | + "00101001101001010", | |
| 84 | + "00101010111100011", | |
| 85 | + "00101011010000000", | |
| 86 | + "00101100001110001", | |
| 87 | + "00101110011101011", | |
| 88 | + "00101111010000110", | |
| 89 | + "00101111100111110", | |
| 90 | + "00110000100001001", | |
| 91 | + "00110000110010110", | |
| 92 | + "00110001110111000", | |
| 93 | + "00110011000000011", | |
| 94 | + "00110011100011111", | |
| 95 | + "00110011110110110", | |
| 96 | + "00110100000011101", | |
| 97 | + "00110100101101001", | |
| 98 | + "00110101100001100", | |
| 99 | + "00110110111111001", | |
| 100 | + "00110111001100001", | |
| 101 | + "00110111100010101", | |
| 102 | + "00111000001010010", | |
| 103 | + "00111001110100000", | |
| 104 | + "00111010000111111", | |
| 105 | + "00111010010100111", | |
| 106 | + "00111011001111000", | |
| 107 | + "00111011111110111", | |
| 108 | + "00111100000111100", | |
| 109 | + "00111101011001010", | |
| 110 | + "00111110000000001", | |
| 111 | + "00111110110001111", | |
| 112 | + "00111111000000111", | |
| 113 | + "01000010001101100", | |
| 114 | + "01000101101001100", | |
| 115 | + "01000101110001011", | |
| 116 | + "01000110011011011", | |
| 117 | + "01001000110000100", | |
| 118 | + "01001000111101000", | |
| 119 | + "01001001010110001", | |
| 120 | + "01001100011100110", | |
| 121 | + "01001101101100000", | |
| 122 | + "01001110010111100", | |
| 123 | + "01001111000010000", | |
| 124 | + "01010000010111111", | |
| 125 | + "01010000011010011", | |
| 126 | + "01010001000001101", | |
| 127 | + "01010101000101010", | |
| 128 | + "01010101110110010", | |
| 129 | + "01010111111101100", | |
| 130 | + "01011000100111010", | |
| 131 | + "01011001000001000", | |
| 132 | + "01011011101010111", | |
| 133 | + "01011100001011011", | |
| 134 | + "01011100110001110", | |
| 135 | + "01011110011011001", | |
| 136 | + "01100000001000011", | |
| 137 | + "01100000010001000", | |
| 138 | + "01100000010011110", | |
| 139 | + "01100000111011010", | |
| 140 | + "01100001001110000", | |
| 141 | + "01100010110111110", | |
| 142 | + "01100011000110001", | |
| 143 | + "01100011010001110", | |
| 144 | + "01100011111001001", | |
| 145 | + "01100100111101011", | |
| 146 | + "01100111000001010", | |
| 147 | + "01100111111000111", | |
| 148 | + "01101001001100110", | |
| 149 | + "01101010001000001", | |
| 150 | + "01101011010111111", | |
| 151 | + "01101011110011101", | |
| 152 | + "01101011111000000", | |
| 153 | + "01101100111000100", | |
| 154 | + "01101101001001011", | |
| 155 | + "01101110101001000", | |
| 156 | + "01110000110001111", | |
| 157 | + "01110001010010100", | |
| 158 | + "01110010100110111", | |
| 159 | + "01110010101000011", | |
| 160 | + "01110011101011001", | |
| 161 | + "01110011111111000", | |
| 162 | + "01110101000111001", | |
| 163 | + "01110101101110100", | |
| 164 | + "01110110000101110", | |
| 165 | + "01111000000001100", | |
| 166 | + "01111000110111101", | |
| 167 | + "01111000111100011", | |
| 168 | + "01111001000110000", | |
| 169 | + "01111001111101111", | |
| 170 | + "01111011110110000", | |
| 171 | + "01111100101110110", | |
| 172 | + "01111101110000101", | |
| 173 | + "01111110001000010", | |
| 174 | + "01111110110011100", | |
| 175 | + "01111111000011000", | |
| 176 | + "01111111011110011", | |
| 177 | + "10000000000010100", | |
| 178 | + "10000010100010110", | |
| 179 | + "10000011110000010", | |
| 180 | + "10000100000100111", | |
| 181 | + "10000100101110011", | |
| 182 | + "10000101100101001", | |
| 183 | + "10000110101010000", | |
| 184 | + "10000111111101011", | |
| 185 | + "10001000001010000", | |
| 186 | + "10001000011111101", | |
| 187 | + "10001010110011110", | |
| 188 | + "10001100010010010", | |
| 189 | + "10001110000111010", | |
| 190 | + "10001110011110001", | |
| 191 | + "10001111010111111", | |
| 192 | + "10001111100010100", | |
| 193 | + "10010000000110001", | |
| 194 | + "10010000001110110", | |
| 195 | + "10010000111001001", | |
| 196 | + "10010010001000110", | |
| 197 | + "10010010111111100", | |
| 198 | + "10010011000110101", | |
| 199 | + "10010011111001101", | |
| 200 | + "10010101001101000", | |
| 201 | + "10010110101110110", | |
| 202 | + "10010111101011000", | |
| 203 | + "10011000100010111", | |
| 204 | + "10011010011101111", | |
| 205 | + "10011100110010011", | |
| 206 | + "10011110100101110", | |
| 207 | + "10011111111100100", | |
| 208 | + "10100000100000001", | |
| 209 | + "10100001000011111", | |
| 210 | + "10100001011110010", | |
| 211 | + "10100010000100100", | |
| 212 | + "10100011001000011", | |
| 213 | + "10100011101011101", | |
| 214 | + "10100110100011000", | |
| 215 | + "10100111000100111", | |
| 216 | + "10101000110110111", | |
| 217 | + "10101001111110000", | |
| 218 | + "10101010101000111", | |
| 219 | + "10101010111000001", | |
| 220 | + "10101011111101111", | |
| 221 | + "10101101000111101", | |
| 222 | + "10101101110011100", | |
| 223 | + "10101110001000100", | |
| 224 | + "10101110111110011", | |
| 225 | + "10101111110010010", | |
| 226 | + "10110000010010101", | |
| 227 | + "10110000110101000", | |
| 228 | + "10110010000111100", | |
| 229 | + "10110011011111010", | |
| 230 | + "10110100110000000", | |
| 231 | + "10110110000011001", | |
| 232 | + "10111000011111111", | |
| 233 | + "10111010011000101", | |
| 234 | + "10111100000011111", | |
| 235 | + "10111101010000010", | |
| 236 | + "10111110000100011", | |
| 237 | + "10111110011100111", | |
| 238 | + "10111111000011101", | |
| 239 | + "10111111011010101", | |
| 240 | + "11000000001010101", | |
| 241 | + "11000000011110100", | |
| 242 | + "11000001110000111", | |
| 243 | + "11000001111011111", | |
| 244 | + "11000001111110001", | |
| 245 | + "11000010000011100", | |
| 246 | + "11000100101010111", | |
| 247 | + "11000101011000000", | |
| 248 | + "11000101110110011", | |
| 249 | + "11000110010101100", | |
| 250 | + "11000111110111100", | |
| 251 | + "11001000110011000", | |
| 252 | + "11001001000000000", | |
| 253 | + "11001010000110011", | |
| 254 | + "11001100010111110", | |
| 255 | + "11001101101010010", | |
| 256 | + "11001110100000001", | |
| 257 | + "11001110101110000", | |
| 258 | + "11001110110110111", | |
| 259 | + "11010001010100010", | |
| 260 | + "11010001111110110", | |
| 261 | + "11010011110000110", | |
| 262 | + "11010110011100011", | |
| 263 | + "11011000000000111", | |
| 264 | + "11011000001101111", | |
| 265 | + "11011001011100111", | |
| 266 | + "11011001110110001", | |
| 267 | + "11011011100011011", | |
| 268 | + "11011100100001101", | |
| 269 | + "11011101001111100", | |
| 270 | + "11011110001110010", | |
| 271 | + "11011110101010011", | |
| 272 | + "11011111011111101", | |
| 273 | + "11011111110000011", | |
| 274 | + "11100000001100000", | |
| 275 | + "11100000001110111", | |
| 276 | + "11100001001100011", | |
| 277 | + "11100001100001010", | |
| 278 | + "11100001110011100", | |
| 279 | + "11100001111100000", | |
| 280 | + "11100010000000110", | |
| 281 | + "11100011001111111", | |
| 282 | + "11100100101101010", | |
| 283 | + "11100101011110001", | |
| 284 | + "11100110010010000", | |
| 285 | + "11100110101001111", | |
| 286 | + "11100111111111011", | |
| 287 | + "11101000001010100", | |
| 288 | + "11101001000001111", | |
| 289 | + "11101001010111011", | |
| 290 | + "11101001111111001", | |
| 291 | + "11101010110001100", | |
| 292 | + "11101011000100110", | |
| 293 | + "11101100010100101", | |
| 294 | + "11101110110100001", | |
| 295 | + "11101111000000000", | |
| 296 | + "11101111001011111", | |
| 297 | + "11101111101011010", | |
| 298 | + "11110000001110010", | |
| 299 | + "11110001001001110", | |
| 300 | + "11110010111001000", | |
| 301 | + "11110011010010101", | |
| 302 | + "11110011100111110", | |
| 303 | + "11110110110000111", | |
| 304 | + "11110110110110110", | |
| 305 | + "11110111010000100", | |
| 306 | + "11110111111001011", | |
| 307 | + "11111000010000000", | |
| 308 | + "11111000011101001", | |
| 309 | + "11111000110110000", | |
| 310 | + "11111001100000011", | |
| 311 | + "11111010001010111", | |
| 312 | + "11111010110111111", | |
| 313 | + "11111011101111000", | |
| 314 | + "11111011110011001", | |
| 315 | + "11111100000110110", | |
| 316 | + "11111100011111110", | |
| 317 | + "11111100101010100", | |
| 318 | + "11111101111100010", | |
| 319 | + "11111110011100000", | |
| 320 | + "11111111100010000", | |
| 321 | + "11111111100011110", | |
| 322 | + "11111111111011100", | |
| 323 | + "11111111111111111" | |
| 324 | + ] | |
| 325 | +} | |
+69 −0 routes/irisbud-codex/n17-local-search/run_seeds.py added View file
| @@ -0,0 +1,69 @@ | ||
| 1 | +"""Run four independent searches, retain the largest code and record its seed.""" | |
| 2 | + | |
| 3 | +import json | |
| 4 | +import shutil | |
| 5 | +import subprocess | |
| 6 | +import sys | |
| 7 | +import time | |
| 8 | +from pathlib import Path | |
| 9 | + | |
| 10 | + | |
| 11 | +def checkpoint_size(path): | |
| 12 | + if not path.exists(): | |
| 13 | + return 0 | |
| 14 | + return len(json.loads(path.read_text(encoding="utf-8"))["words"]) | |
| 15 | + | |
| 16 | + | |
| 17 | +def main(): | |
| 18 | + executable, work, start, rounds, *args = sys.argv[1:] | |
| 19 | + scratch = Path(work) | |
| 20 | + seeds = [int(seed) for seed in args] | |
| 21 | + if len(set(seeds)) != 4: | |
| 22 | + raise SystemExit("Provide four distinct seeds.") | |
| 23 | + outputs = {seed: scratch / f"code-{seed}.json" for seed in seeds} | |
| 24 | + workers = {} | |
| 25 | + try: | |
| 26 | + for seed in seeds: | |
| 27 | + with (scratch / f"seed-{seed}.log").open("w", encoding="utf-8") as log: | |
| 28 | + workers[seed] = subprocess.Popen( | |
| 29 | + [executable, str(seed), rounds, start, str(outputs[seed])], | |
| 30 | + stdout=log, | |
| 31 | + stderr=subprocess.STDOUT, | |
| 32 | + ) | |
| 33 | + | |
| 34 | + previous = 0 | |
| 35 | + while True: | |
| 36 | + sizes = {seed: checkpoint_size(outputs[seed]) for seed in seeds} | |
| 37 | + completed = sum(worker.poll() is not None for worker in workers.values()) | |
| 38 | + largest = max(sizes.values()) | |
| 39 | + if largest > previous: | |
| 40 | + print(f"metric: code_size {largest}", flush=True) | |
| 41 | + previous = largest | |
| 42 | + print(f"progress: {completed}/{len(seeds)} searches done, best {largest}", flush=True) | |
| 43 | + if completed == len(seeds): | |
| 44 | + break | |
| 45 | + time.sleep(12) | |
| 46 | + finally: | |
| 47 | + for worker in workers.values(): | |
| 48 | + if worker.poll() is None: | |
| 49 | + worker.terminate() | |
| 50 | + worker.wait() | |
| 51 | + | |
| 52 | + failures = [seed for seed in seeds if workers[seed].returncode != 0] | |
| 53 | + if failures: | |
| 54 | + for seed in failures: | |
| 55 | + print((scratch / f"seed-{seed}.log").read_text(encoding="utf-8"), file=sys.stderr) | |
| 56 | + raise SystemExit(f"Search failed for seeds: {failures}") | |
| 57 | + | |
| 58 | + seed = max(seeds, key=lambda item: (sizes[item], -item)) | |
| 59 | + shutil.copyfile(outputs[seed], "n17.json.part") | |
| 60 | + Path("n17.json.part").replace("n17.json") | |
| 61 | + lines = [f"Rounds per seed: {rounds}", f"Start: {Path(start).name}"] | |
| 62 | + lines.extend(f"Seed {item}: {sizes[item]} words" for item in seeds) | |
| 63 | + lines.append(f"Winner: seed {seed}, {sizes[seed]} words") | |
| 64 | + Path("search-report.txt").write_text("\n".join(lines) + "\n", encoding="utf-8") | |
| 65 | + print(lines[-1], flush=True) | |
| 66 | + | |
| 67 | + | |
| 68 | +if __name__ == "__main__": | |
| 69 | + main() | |
+332 −0 routes/irisbud-codex/n17-local-search/search.cpp added View file
| @@ -0,0 +1,332 @@ | ||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | +namespace { | |
| 18 | +constexpr int n = 17; | |
| 19 | +constexpr int word_count = 1 << n; | |
| 20 | +constexpr int descendant_count = 1 << (n - 2); | |
| 21 | + | |
| 22 | +int delete_bit(int word, int position) { | |
| 23 | + const int low = (1 << position) - 1; | |
| 24 | + return (word & low) | ((word >> 1) & ~low); | |
| 25 | +} | |
| 26 | + | |
| 27 | +struct Incidence { | |
| 28 | + std::vector<std::vector<std::uint16_t>> descendants{word_count}; | |
| 29 | + std::vector<std::vector<int>> parents{descendant_count}; | |
| 30 | + | |
| 31 | + Incidence() { | |
| 32 | + std::vector<int> last_seen(descendant_count, -1); | |
| 33 | + for (int word = 0; word < word_count; ++word) { | |
| 34 | + auto& row = descendants[word]; | |
| 35 | + for (int second = 1; second < n; ++second) { | |
| 36 | + const int once = delete_bit(word, second); | |
| 37 | + for (int first = 0; first < second; ++first) { | |
| 38 | + const int child = delete_bit(once, first); | |
| 39 | + if (last_seen[child] == word) continue; | |
| 40 | + last_seen[child] = word; | |
| 41 | + row.push_back(static_cast<std::uint16_t>(child)); | |
| 42 | + parents[child].push_back(word); | |
| 43 | + } | |
| 44 | + } | |
| 45 | + std::sort(row.begin(), row.end()); | |
| 46 | + } | |
| 47 | + } | |
| 48 | + | |
| 49 | + bool disjoint(int a, int b) const { | |
| 50 | + const auto& left = descendants[a]; | |
| 51 | + const auto& right = descendants[b]; | |
| 52 | + auto i = left.begin(), j = right.begin(); | |
| 53 | + while (i != left.end() && j != right.end()) { | |
| 54 | + if (*i == *j) return false; | |
| 55 | + if (*i < *j) ++i; | |
| 56 | + else ++j; | |
| 57 | + } | |
| 58 | + return true; | |
| 59 | + } | |
| 60 | +}; | |
| 61 | + | |
| 62 | +// Dense membership with constant-time insertion, deletion, and random access. | |
| 63 | +class WordSet { | |
| 64 | +public: | |
| 65 | + bool contains(int word) const { return positions[word] >= 0; } | |
| 66 | + int size() const { return static_cast<int>(words.size()); } | |
| 67 | + int operator[](int i) const { return words[i]; } | |
| 68 | + const std::vector<int>& values() const { return words; } | |
| 69 | + | |
| 70 | + void add(int word) { | |
| 71 | + positions[word] = size(); | |
| 72 | + words.push_back(word); | |
| 73 | + } | |
| 74 | + | |
| 75 | + void erase(int word) { | |
| 76 | + const int i = positions[word]; | |
| 77 | + words[i] = words.back(); | |
| 78 | + positions[words[i]] = i; | |
| 79 | + words.pop_back(); | |
| 80 | + positions[word] = -1; | |
| 81 | + } | |
| 82 | + | |
| 83 | +private: | |
| 84 | + std::vector<int> positions = std::vector<int>(word_count, -1); | |
| 85 | + std::vector<int> words; | |
| 86 | +}; | |
| 87 | + | |
| 88 | +struct Move { int word; bool added; }; | |
| 89 | + | |
| 90 | +class Search { | |
| 91 | +public: | |
| 92 | + Search(const Incidence& graph, std::uint32_t seed) : graph_(graph), random_(seed) { | |
| 93 | + for (int word = 0; word < word_count; ++word) free_.add(word); | |
| 94 | + } | |
| 95 | + | |
| 96 | + const WordSet& code() const { return code_; } | |
| 97 | + | |
| 98 | + void reset(const std::vector<int>& words) { | |
| 99 | + while (code_.size() > 0) erase(code_[0]); | |
| 100 | + for (int word : words) insert(word); | |
| 101 | + journal_.clear(); | |
| 102 | + } | |
| 103 | + | |
| 104 | + void step(int round, int best_size) { | |
| 105 | + const int previous = code_.size(); | |
| 106 | + journal_.clear(); | |
| 107 | + const int mode = draw(10); | |
| 108 | + if (mode < 7) { | |
| 109 | + const int insertions = mode < 4 ? 1 : 2 + draw(2); | |
| 110 | + for (int i = 0; i < insertions; ++i) force_candidate(mode < 4 ? 2 : 4); | |
| 111 | + } else { | |
| 112 | + const int removals = 2 + draw(mode == 9 ? 15 : 5); | |
| 113 | + for (int i = 0; i < removals; ++i) { | |
| 114 | + const int a = code_[draw(code_.size())]; | |
| 115 | + const int b = code_[draw(code_.size())]; | |
| 116 | + drop(graph_.descendants[a].size() > graph_.descendants[b].size() ? a : b); | |
| 117 | + } | |
| 118 | + } | |
| 119 | + | |
| 120 | + const int samples = 1 + draw(18); | |
| 121 | + fill(samples); | |
| 122 | + const int probes = mode < 4 ? 6 : 14; | |
| 123 | + for (int i = 0; i < probes; ++i) { | |
| 124 | + if (one_for_two(code_[draw(code_.size())])) fill(samples); | |
| 125 | + } | |
| 126 | + | |
| 127 | + // A small loss can move the search off a plateau. The accepted drop | |
| 128 | + // widens and narrows periodically without losing the saved incumbent. | |
| 129 | + const int phase = round % 1800; | |
| 130 | + const int tolerance = phase < 1200 ? 4 : 8; | |
| 131 | + const int size = code_.size(); | |
| 132 | + if (size >= previous || (size >= best_size - tolerance && size + 2 >= previous && draw(6) == 0)) { | |
| 133 | + return; | |
| 134 | + } | |
| 135 | + for (auto i = journal_.rbegin(); i != journal_.rend(); ++i) { | |
| 136 | + if (i->added) erase(i->word); | |
| 137 | + else insert(i->word); | |
| 138 | + } | |
| 139 | + } | |
| 140 | + | |
| 141 | +private: | |
| 142 | + int draw(int upper) { return static_cast<int>(random_() % static_cast<unsigned>(upper)); } | |
| 143 | + | |
| 144 | + void insert(int word) { | |
| 145 | + if (occupied_[word] != 0 || code_.contains(word)) throw std::logic_error("invalid insertion"); | |
| 146 | + code_.add(word); | |
| 147 | + for (int child : graph_.descendants[word]) { | |
| 148 | + owner_[child] = word; | |
| 149 | + for (int parent : graph_.parents[child]) { | |
| 150 | + if (occupied_[parent]++ == 0) free_.erase(parent); | |
| 151 | + } | |
| 152 | + } | |
| 153 | + } | |
| 154 | + | |
| 155 | + void erase(int word) { | |
| 156 | + if (!code_.contains(word)) throw std::logic_error("invalid removal"); | |
| 157 | + code_.erase(word); | |
| 158 | + for (int child : graph_.descendants[word]) { | |
| 159 | + owner_[child] = -1; | |
| 160 | + for (int parent : graph_.parents[child]) { | |
| 161 | + if (--occupied_[parent] == 0) free_.add(parent); | |
| 162 | + } | |
| 163 | + } | |
| 164 | + } | |
| 165 | + | |
| 166 | + void take(int word) { insert(word); journal_.push_back({word, true}); } | |
| 167 | + void drop(int word) { erase(word); journal_.push_back({word, false}); } | |
| 168 | + | |
| 169 | + void fill(int samples) { | |
| 170 | + while (free_.size() > 0) { | |
| 171 | + int best = free_[draw(free_.size())]; | |
| 172 | + int cost = static_cast<int>(graph_.descendants[best].size()) + draw(9); | |
| 173 | + for (int i = 1; i < std::min(samples, free_.size()); ++i) { | |
| 174 | + const int word = free_[draw(free_.size())]; | |
| 175 | + const int rival = static_cast<int>(graph_.descendants[word].size()) + draw(9); | |
| 176 | + if (rival < cost) { | |
| 177 | + best = word; | |
| 178 | + cost = rival; | |
| 179 | + } | |
| 180 | + } | |
| 181 | + take(best); | |
| 182 | + } | |
| 183 | + } | |
| 184 | + | |
| 185 | + // Choose a new word with few blockers and preferentially trade it for | |
| 186 | + // owners using more descendants, freeing space for the greedy refill. | |
| 187 | + void force_candidate(int max_blockers) { | |
| 188 | + int best = -1; | |
| 189 | + int best_cost = std::numeric_limits<int>::max(); | |
| 190 | + std::array<int, 4> best_owners{}; | |
| 191 | + int best_count = 0; | |
| 192 | + const int pivot = code_[draw(code_.size())]; | |
| 193 | + | |
| 194 | + for (int i = 0; i < 48; ++i) { | |
| 195 | + int word = draw(word_count); | |
| 196 | + if (i < 32) { | |
| 197 | + const auto& children = graph_.descendants[pivot]; | |
| 198 | + const auto& parents = graph_.parents[children[draw(static_cast<int>(children.size()))]]; | |
| 199 | + word = parents[draw(static_cast<int>(parents.size()))]; | |
| 200 | + } | |
| 201 | + if (occupied_[word] == 0 || code_.contains(word)) continue; | |
| 202 | + | |
| 203 | + std::array<int, 4> owners{}; | |
| 204 | + int count = 0; | |
| 205 | + int released = 0; | |
| 206 | + for (int child : graph_.descendants[word]) { | |
| 207 | + const int owner = owner_[child]; | |
| 208 | + if (owner < 0 || std::find(owners.begin(), owners.begin() + count, owner) != owners.begin() + count) continue; | |
| 209 | + if (count == max_blockers) { | |
| 210 | + ++count; | |
| 211 | + break; | |
| 212 | + } | |
| 213 | + owners[count++] = owner; | |
| 214 | + released += static_cast<int>(graph_.descendants[owner].size()); | |
| 215 | + } | |
| 216 | + if (count > max_blockers) continue; | |
| 217 | + | |
| 218 | + const int cost = count * 20 + static_cast<int>(graph_.descendants[word].size()) - released / 2 + draw(30); | |
| 219 | + if (cost < best_cost) { | |
| 220 | + best = word; | |
| 221 | + best_cost = cost; | |
| 222 | + best_owners = owners; | |
| 223 | + best_count = count; | |
| 224 | + } | |
| 225 | + } | |
| 226 | + if (best < 0) return; | |
| 227 | + for (int i = 0; i < best_count; ++i) drop(best_owners[i]); | |
| 228 | + take(best); | |
| 229 | + } | |
| 230 | + | |
| 231 | + // A touched outside word is available after removing owner exactly when | |
| 232 | + // the occupied children it shares with owner account for all blockers. | |
| 233 | + bool one_for_two(int owner) { | |
| 234 | + touched_.clear(); | |
| 235 | + options_.clear(); | |
| 236 | + for (int child : graph_.descendants[owner]) { | |
| 237 | + for (int word : graph_.parents[child]) { | |
| 238 | + if (shared_[word]++ == 0) touched_.push_back(word); | |
| 239 | + } | |
| 240 | + } | |
| 241 | + for (int word : touched_) { | |
| 242 | + if (word != owner && shared_[word] == occupied_[word]) options_.push_back(word); | |
| 243 | + shared_[word] = 0; | |
| 244 | + } | |
| 245 | + const int count = static_cast<int>(options_.size()); | |
| 246 | + if (count < 2) return false; | |
| 247 | + const int offset = draw(count); | |
| 248 | + for (int i = 0; i < count; ++i) { | |
| 249 | + const int a = options_[(i + offset) % count]; | |
| 250 | + for (int j = 0; j < i; ++j) { | |
| 251 | + const int b = options_[(j + offset) % count]; | |
| 252 | + if (!graph_.disjoint(a, b)) continue; | |
| 253 | + drop(owner); | |
| 254 | + take(a); | |
| 255 | + take(b); | |
| 256 | + return true; | |
| 257 | + } | |
| 258 | + } | |
| 259 | + return false; | |
| 260 | + } | |
| 261 | + | |
| 262 | + const Incidence& graph_; | |
| 263 | + std::mt19937 random_; | |
| 264 | + WordSet code_; | |
| 265 | + WordSet free_; | |
| 266 | + std::vector<int> owner_ = std::vector<int>(descendant_count, -1); | |
| 267 | + std::vector<std::uint8_t> occupied_ = std::vector<std::uint8_t>(word_count); | |
| 268 | + std::vector<std::uint8_t> shared_ = std::vector<std::uint8_t>(word_count); | |
| 269 | + std::vector<int> touched_; | |
| 270 | + std::vector<int> options_; | |
| 271 | + std::vector<Move> journal_; | |
| 272 | +}; | |
| 273 | + | |
| 274 | +std::vector<int> read_code(const std::filesystem::path& file) { | |
| 275 | + std::ifstream input(file); | |
| 276 | + if (!input) throw std::runtime_error("cannot read the starting code"); | |
| 277 | + const std::string json{std::istreambuf_iterator<char>(input), std::istreambuf_iterator<char>()}; | |
| 278 | + const std::regex word_pattern(R"re("([01]{17})")re"); | |
| 279 | + std::vector<int> result; | |
| 280 | + for (std::sregex_iterator i(json.begin(), json.end(), word_pattern), end; i != end; ++i) { | |
| 281 | + result.push_back(std::stoi((*i)[1], nullptr, 2)); | |
| 282 | + } | |
| 283 | + if (result.empty()) throw std::runtime_error("the starting code contains no words"); | |
| 284 | + return result; | |
| 285 | +} | |
| 286 | + | |
| 287 | +void write_code(const std::filesystem::path& file, std::vector<int> words) { | |
| 288 | + std::sort(words.begin(), words.end()); | |
| 289 | + std::filesystem::path temporary = file; | |
| 290 | + temporary += ".tmp"; | |
| 291 | + std::ofstream output(temporary); | |
| 292 | + output.exceptions(std::ios::failbit | std::ios::badbit); | |
| 293 | + output << "{\n \"n\": " << n << ",\n \"words\": [\n"; | |
| 294 | + for (std::size_t i = 0; i < words.size(); ++i) { | |
| 295 | + output << " \"" << std::bitset<n>(words[i]) << '"' << (i + 1 == words.size() ? "\n" : ",\n"); | |
| 296 | + } | |
| 297 | + output << " ]\n}\n"; | |
| 298 | + output.close(); | |
| 299 | + std::filesystem::rename(temporary, file); | |
| 300 | +} | |
| 301 | +} // namespace | |
| 302 | + | |
| 303 | +int main(int argc, char** argv) { | |
| 304 | + try { | |
| 305 | + if (argc != 5) throw std::runtime_error("usage: search SEED ROUNDS INPUT_JSON OUTPUT_JSON"); | |
| 306 | + const auto seed = std::stoull(argv[1]); | |
| 307 | + const int rounds = std::stoi(argv[2]); | |
| 308 | + if (rounds < 1 || seed > std::numeric_limits<std::uint32_t>::max()) throw std::runtime_error("invalid seed or round count"); | |
| 309 | + auto best = read_code(argv[3]); | |
| 310 | + const Incidence graph; | |
| 311 | + Search search(graph, static_cast<std::uint32_t>(seed)); | |
| 312 | + search.reset(best); | |
| 313 | + write_code(argv[4], best); | |
| 314 | + std::cout << "metric: code_size " << best.size() << std::endl; | |
| 315 | + for (int round = 1; round <= rounds; ++round) { | |
| 316 | + if (round % 1800 == 1 && round > 1) search.reset(best); | |
| 317 | + search.step(round, static_cast<int>(best.size())); | |
| 318 | + if (search.code().size() > static_cast<int>(best.size())) { | |
| 319 | + best = search.code().values(); | |
| 320 | + write_code(argv[4], best); | |
| 321 | + std::cout << "metric: code_size " << best.size() << "\nround " << round << std::endl; | |
| 322 | + } | |
| 323 | + if (round % 1000 == 0 || round == rounds) { | |
| 324 | + std::cout << "progress: " << round << '/' << rounds << " rounds, best " << best.size() << std::endl; | |
| 325 | + } | |
| 326 | + } | |
| 327 | + std::cout << "seed " << seed << ", best " << best.size() << " codewords" << std::endl; | |
| 328 | + } catch (const std::exception& error) { | |
| 329 | + std::cerr << error.what() << '\n'; | |
| 330 | + return 1; | |
| 331 | + } | |
| 332 | +} | |
+21 −0 routes/irisbud-codex/n17-local-search/search.sbatch added View file
| @@ -0,0 +1,21 @@ | ||
| 1 | +#!/usr/bin/env bash | |
| 2 | +#SBATCH --job-name=n17-local-search | |
| 3 | +#SBATCH --cpus-per-task=4 | |
| 4 | +#SBATCH --time=00:30:00 | |
| 5 | +#SBATCH --mem=1024M | |
| 6 | +set -euo pipefail | |
| 7 | + | |
| 8 | +rounds=${1:?usage: search.sbatch ROUNDS SEED SEED SEED SEED [START_JSON]} | |
| 9 | +shift | |
| 10 | +repo=${SS_INPUT:-$(git rev-parse --show-toplevel)} | |
| 11 | +input=${5:-$repo/constructions/n17.json} | |
| 12 | +if [[ $# -lt 4 || $# -gt 5 ]]; then | |
| 13 | + echo 'Specify four seeds and optionally a starting code.' >&2 | |
| 14 | + exit 2 | |
| 15 | +fi | |
| 16 | + | |
| 17 | +scratch=$(mktemp -d) | |
| 18 | +trap 'rm -rf -- "$scratch"' EXIT | |
| 19 | +"${CXX:-c++}" -O3 -std=c++17 search.cpp -o "$scratch/search.exe" | |
| 20 | +"${PYTHON:-python3}" -B run_seeds.py "$scratch/search.exe" "$scratch" "$input" "$rounds" "$1" "$2" "$3" "$4" | |
| 21 | +"${PYTHON:-python3}" -B "$repo/checks/judge/check.py" . | |
