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 reproducible local search and a 320-word code
Browse files+325 −0 routes/liuqiran-codex/n17-local-search/n17.json added View file
| @@ -0,0 +1,325 @@ | ||
| 1 | +{ | |
| 2 | + "n": 17, | |
| 3 | + "words": [ | |
| 4 | + "00000000000000000", | |
| 5 | + "00000000000000111", | |
| 6 | + "00000000000101010", | |
| 7 | + "00000000000111111", | |
| 8 | + "00000000001110000", | |
| 9 | + "00000000010110011", | |
| 10 | + "00000000101111010", | |
| 11 | + "00000000110010001", | |
| 12 | + "00000000111101111", | |
| 13 | + "00000001010010111", | |
| 14 | + "00000001101000000", | |
| 15 | + "00000001110011100", | |
| 16 | + "00000001111000011", | |
| 17 | + "00000010011000110", | |
| 18 | + "00000010101010100", | |
| 19 | + "00000010110111011", | |
| 20 | + "00000011000011101", | |
| 21 | + "00000011011111000", | |
| 22 | + "00000011111000100", | |
| 23 | + "00000011111111101", | |
| 24 | + "00000100111010101", | |
| 25 | + "00000101000100000", | |
| 26 | + "00000101100000011", | |
| 27 | + "00000110001100111", | |
| 28 | + "00000110101000101", | |
| 29 | + "00000110101011110", | |
| 30 | + "00000111000011000", | |
| 31 | + "00000111011011001", | |
| 32 | + "00000111110001111", | |
| 33 | + "00001000100110101", | |
| 34 | + "00001001100111111", | |
| 35 | + "00001010000011110", | |
| 36 | + "00001010010111000", | |
| 37 | + "00001010110010010", | |
| 38 | + "00001010111101110", | |
| 39 | + "00001011110110000", | |
| 40 | + "00001100000100001", | |
| 41 | + "00001100011111001", | |
| 42 | + "00001100111000001", | |
| 43 | + "00001110001010110", | |
| 44 | + "00001110010000100", | |
| 45 | + "00001110111000110", | |
| 46 | + "00001110111011111", | |
| 47 | + "00001111001110100", | |
| 48 | + "00001111110000000", | |
| 49 | + "00001111111111000", | |
| 50 | + "00010000110110010", | |
| 51 | + "00010001110100111", | |
| 52 | + "00010010000100101", | |
| 53 | + "00010011111100101", | |
| 54 | + "00010100101000011", | |
| 55 | + "00010101000111011", | |
| 56 | + "00010110001100100", | |
| 57 | + "00010110010010111", | |
| 58 | + "00010111010010001", | |
| 59 | + "00010111011111010", | |
| 60 | + "00010111101011011", | |
| 61 | + "00011000000110100", | |
| 62 | + "00011000010011111", | |
| 63 | + "00011001100101000", | |
| 64 | + "00011001101011101", | |
| 65 | + "00011010000000000", | |
| 66 | + "00011010111100100", | |
| 67 | + "00011011011100111", | |
| 68 | + "00011100000000111", | |
| 69 | + "00011100011111110", | |
| 70 | + "00011101010011010", | |
| 71 | + "00011101100000101", | |
| 72 | + "00011110000110011", | |
| 73 | + "00011111100111001", | |
| 74 | + "00011111111011110", | |
| 75 | + "00100010010000010", | |
| 76 | + "00100010100111001", | |
| 77 | + "00100100011011110", | |
| 78 | + "00100101010101011", | |
| 79 | + "00100110110000111", | |
| 80 | + "00100110111010100", | |
| 81 | + "00101000001001100", | |
| 82 | + "00101000101100000", | |
| 83 | + "00101001001111100", | |
| 84 | + "00101001011111111", | |
| 85 | + "00101001101001010", | |
| 86 | + "00101010111100011", | |
| 87 | + "00101011010000000", | |
| 88 | + "00101100001110001", | |
| 89 | + "00101110011101011", | |
| 90 | + "00101111010000110", | |
| 91 | + "00101111100111110", | |
| 92 | + "00110000100001001", | |
| 93 | + "00110000110010110", | |
| 94 | + "00110001110111000", | |
| 95 | + "00110011000000011", | |
| 96 | + "00110011100011111", | |
| 97 | + "00110011110110110", | |
| 98 | + "00110100000011101", | |
| 99 | + "00110100101101001", | |
| 100 | + "00110101100001100", | |
| 101 | + "00110110111111001", | |
| 102 | + "00110111001100001", | |
| 103 | + "00110111100010101", | |
| 104 | + "00111000001010010", | |
| 105 | + "00111001110100000", | |
| 106 | + "00111010000111111", | |
| 107 | + "00111010010100111", | |
| 108 | + "00111011001111000", | |
| 109 | + "00111011111110111", | |
| 110 | + "00111100000111100", | |
| 111 | + "00111101010100100", | |
| 112 | + "00111110000000001", | |
| 113 | + "00111110110001111", | |
| 114 | + "00111111000000111", | |
| 115 | + "00111111100011000", | |
| 116 | + "00111111111001010", | |
| 117 | + "01000010001101100", | |
| 118 | + "01000100000100111", | |
| 119 | + "01000101101001100", | |
| 120 | + "01000101110001011", | |
| 121 | + "01000110011011011", | |
| 122 | + "01000111111101011", | |
| 123 | + "01001000001010000", | |
| 124 | + "01001000110000100", | |
| 125 | + "01001000111101000", | |
| 126 | + "01001001010110001", | |
| 127 | + "01001100011100110", | |
| 128 | + "01001101101100000", | |
| 129 | + "01001110010111100", | |
| 130 | + "01001111000010000", | |
| 131 | + "01001111010111111", | |
| 132 | + "01010000010111111", | |
| 133 | + "01010000011010011", | |
| 134 | + "01010001000001101", | |
| 135 | + "01010101000101010", | |
| 136 | + "01010101010111101", | |
| 137 | + "01010101110110010", | |
| 138 | + "01010111111101100", | |
| 139 | + "01011000100111010", | |
| 140 | + "01011001000001000", | |
| 141 | + "01011011101010111", | |
| 142 | + "01011100001011011", | |
| 143 | + "01011100110001110", | |
| 144 | + "01011110011011001", | |
| 145 | + "01011110111000101", | |
| 146 | + "01100000001000011", | |
| 147 | + "01100000010001000", | |
| 148 | + "01100000010011110", | |
| 149 | + "01100000111011010", | |
| 150 | + "01100001001110000", | |
| 151 | + "01100001111010111", | |
| 152 | + "01100010110111110", | |
| 153 | + "01100011000110001", | |
| 154 | + "01100011010001110", | |
| 155 | + "01100011111001001", | |
| 156 | + "01100111000001010", | |
| 157 | + "01100111111000111", | |
| 158 | + "01101001001100110", | |
| 159 | + "01101010001000001", | |
| 160 | + "01101011001111111", | |
| 161 | + "01101011110011101", | |
| 162 | + "01101011111000000", | |
| 163 | + "01101100111000100", | |
| 164 | + "01101101001001011", | |
| 165 | + "01101110100101000", | |
| 166 | + "01101111111111101", | |
| 167 | + "01110000110000000", | |
| 168 | + "01110000110001111", | |
| 169 | + "01110001010010100", | |
| 170 | + "01110010100110111", | |
| 171 | + "01110010101000011", | |
| 172 | + "01110010111100010", | |
| 173 | + "01110011101011001", | |
| 174 | + "01110011111111000", | |
| 175 | + "01110101000111001", | |
| 176 | + "01110101101110100", | |
| 177 | + "01110110000101110", | |
| 178 | + "01110110110100110", | |
| 179 | + "01111000000001100", | |
| 180 | + "01111000110111101", | |
| 181 | + "01111001000110000", | |
| 182 | + "01111001111101111", | |
| 183 | + "01111011100110110", | |
| 184 | + "01111011110110000", | |
| 185 | + "01111100101110110", | |
| 186 | + "01111110100001001", | |
| 187 | + "01111110110011100", | |
| 188 | + "01111111011110011", | |
| 189 | + "01111111100100011", | |
| 190 | + "10000010100010110", | |
| 191 | + "10000011110000010", | |
| 192 | + "10000100101110011", | |
| 193 | + "10000101100101001", | |
| 194 | + "10000110101010000", | |
| 195 | + "10001000011111101", | |
| 196 | + "10001010110011110", | |
| 197 | + "10001100010010010", | |
| 198 | + "10001110011110001", | |
| 199 | + "10001111100010100", | |
| 200 | + "10001111111010000", | |
| 201 | + "10010000000110001", | |
| 202 | + "10010000001110110", | |
| 203 | + "10010000111001001", | |
| 204 | + "10010001101110111", | |
| 205 | + "10010010001000110", | |
| 206 | + "10010010111111100", | |
| 207 | + "10010011000110101", | |
| 208 | + "10010101001101000", | |
| 209 | + "10010110010000001", | |
| 210 | + "10010110101110110", | |
| 211 | + "10010111101011000", | |
| 212 | + "10011000100010111", | |
| 213 | + "10011010011101111", | |
| 214 | + "10011100110010011", | |
| 215 | + "10011110010011101", | |
| 216 | + "10100000100000001", | |
| 217 | + "10100001000011111", | |
| 218 | + "10100001011110010", | |
| 219 | + "10100010000100100", | |
| 220 | + "10100011001000011", | |
| 221 | + "10100011101011101", | |
| 222 | + "10100110100011000", | |
| 223 | + "10100110110100101", | |
| 224 | + "10100111000100111", | |
| 225 | + "10101001111110000", | |
| 226 | + "10101010101000111", | |
| 227 | + "10101010111000001", | |
| 228 | + "10101011111101111", | |
| 229 | + "10101101110011100", | |
| 230 | + "10101110001000100", | |
| 231 | + "10101110111110011", | |
| 232 | + "10101111100011011", | |
| 233 | + "10101111110010010", | |
| 234 | + "10110000010010101", | |
| 235 | + "10110000110101000", | |
| 236 | + "10110010000111100", | |
| 237 | + "10110010101010110", | |
| 238 | + "10110011011111010", | |
| 239 | + "10110100101000000", | |
| 240 | + "10110110000011001", | |
| 241 | + "10110110001111011", | |
| 242 | + "10111011110000111", | |
| 243 | + "10111011110111110", | |
| 244 | + "10111100000011111", | |
| 245 | + "10111100010111000", | |
| 246 | + "10111101100001000", | |
| 247 | + "10111110010000011", | |
| 248 | + "10111110011100111", | |
| 249 | + "10111111011010101", | |
| 250 | + "11000000001010101", | |
| 251 | + "11000000011110100", | |
| 252 | + "11000001101110001", | |
| 253 | + "11000001110000111", | |
| 254 | + "11000001111111011", | |
| 255 | + "11000010000011100", | |
| 256 | + "11000100101010111", | |
| 257 | + "11000101011000000", | |
| 258 | + "11000101110110011", | |
| 259 | + "11000110010101100", | |
| 260 | + "11000111110111100", | |
| 261 | + "11001001000000000", | |
| 262 | + "11001010000110011", | |
| 263 | + "11001010011110101", | |
| 264 | + "11001100010111110", | |
| 265 | + "11001110110110111", | |
| 266 | + "11001111001100010", | |
| 267 | + "11010001010100010", | |
| 268 | + "11010011110000110", | |
| 269 | + "11010110011100011", | |
| 270 | + "11011000000000111", | |
| 271 | + "11011000001101111", | |
| 272 | + "11011001011100111", | |
| 273 | + "11011001110110001", | |
| 274 | + "11011100001101001", | |
| 275 | + "11011100100001101", | |
| 276 | + "11011101000000010", | |
| 277 | + "11011101001111100", | |
| 278 | + "11011101101100100", | |
| 279 | + "11011110101010011", | |
| 280 | + "11011111111100000", | |
| 281 | + "11100000001100000", | |
| 282 | + "11100000001110111", | |
| 283 | + "11100001001100011", | |
| 284 | + "11100001110011100", | |
| 285 | + "11100001111100000", | |
| 286 | + "11100010000000110", | |
| 287 | + "11100010101111111", | |
| 288 | + "11100011100001001", | |
| 289 | + "11100110010010000", | |
| 290 | + "11100110101001111", | |
| 291 | + "11100111111111011", | |
| 292 | + "11101000001010100", | |
| 293 | + "11101001000001111", | |
| 294 | + "11101001011011010", | |
| 295 | + "11101001111111001", | |
| 296 | + "11101010010100101", | |
| 297 | + "11101101011000010", | |
| 298 | + "11101111001011111", | |
| 299 | + "11101111101011010", | |
| 300 | + "11101111110100001", | |
| 301 | + "11110000001110010", | |
| 302 | + "11110000101000001", | |
| 303 | + "11110001001001110", | |
| 304 | + "11110001111010110", | |
| 305 | + "11110010111001000", | |
| 306 | + "11110011010010101", | |
| 307 | + "11110100110001100", | |
| 308 | + "11110101100000011", | |
| 309 | + "11110111100000100", | |
| 310 | + "11110111111001011", | |
| 311 | + "11111000010001011", | |
| 312 | + "11111010110111111", | |
| 313 | + "11111011101111000", | |
| 314 | + "11111100000000000", | |
| 315 | + "11111100000110110", | |
| 316 | + "11111100001111111", | |
| 317 | + "11111100011100001", | |
| 318 | + "11111100101010100", | |
| 319 | + "11111101111010010", | |
| 320 | + "11111110011000000", | |
| 321 | + "11111111100011101", | |
| 322 | + "11111111111011100", | |
| 323 | + "11111111111111111" | |
| 324 | + ] | |
| 325 | +} | |
+198 −0 routes/liuqiran-codex/n17-local-search/search.cpp added View file
| @@ -0,0 +1,198 @@ | ||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | +namespace { | |
| 12 | +constexpr int length = 17; | |
| 13 | +constexpr int word_count = 1 << length; | |
| 14 | +constexpr int descendant_count = 1 << (length - 2); | |
| 15 | + | |
| 16 | +int erase_bit(int word, int position) { | |
| 17 | + const int lower = (1 << position) - 1; | |
| 18 | + return ((word >> 1) & ~lower) | (word & lower); | |
| 19 | +} | |
| 20 | + | |
| 21 | +std::vector<std::vector<std::uint16_t>> make_descendants() { | |
| 22 | + std::vector<std::vector<std::uint16_t>> descendants(word_count); | |
| 23 | + for (int word = 0; word < word_count; ++word) { | |
| 24 | + auto& values = descendants[word]; | |
| 25 | + for (int first = 0; first < length - 1; ++first) { | |
| 26 | + for (int second = first + 1; second < length; ++second) { | |
| 27 | + values.push_back(erase_bit(erase_bit(word, second), first)); | |
| 28 | + } | |
| 29 | + } | |
| 30 | + std::sort(values.begin(), values.end()); | |
| 31 | + values.erase(std::unique(values.begin(), values.end()), values.end()); | |
| 32 | + } | |
| 33 | + return descendants; | |
| 34 | +} | |
| 35 | + | |
| 36 | +bool disjoint(const std::vector<std::uint16_t>& left, | |
| 37 | + const std::vector<std::uint16_t>& right) { | |
| 38 | + auto a = left.begin(); | |
| 39 | + auto b = right.begin(); | |
| 40 | + while (a != left.end() && b != right.end()) { | |
| 41 | + if (*a == *b) return false; | |
| 42 | + if (*a < *b) ++a; | |
| 43 | + else ++b; | |
| 44 | + } | |
| 45 | + return true; | |
| 46 | +} | |
| 47 | + | |
| 48 | +class Search { | |
| 49 | +public: | |
| 50 | + Search(const std::vector<std::vector<std::uint16_t>>& descendants, | |
| 51 | + std::mt19937& random) | |
| 52 | + : descendants_(descendants), random_(random), | |
| 53 | + owner_(descendant_count, -1), candidates_(word_count) { | |
| 54 | + for (int word = 0; word < word_count; ++word) candidates_[word] = word; | |
| 55 | + } | |
| 56 | + | |
| 57 | + void reset(const std::vector<int>& code) { | |
| 58 | + std::fill(owner_.begin(), owner_.end(), -1); | |
| 59 | + code_.clear(); | |
| 60 | + for (int word : code) add(word); | |
| 61 | + } | |
| 62 | + | |
| 63 | + void perturb(int count) { | |
| 64 | + for (int i = 0; i < count; ++i) { | |
| 65 | + const int offset = random_() % code_.size(); | |
| 66 | + remove(code_[offset]); | |
| 67 | + } | |
| 68 | + } | |
| 69 | + | |
| 70 | + void improve() { | |
| 71 | + std::shuffle(candidates_.begin(), candidates_.end(), random_); | |
| 72 | + for (int word : candidates_) { | |
| 73 | + if (sole_blocker(word) == -1) add(word); | |
| 74 | + } | |
| 75 | + while (replace_one_with_two()) {} | |
| 76 | + } | |
| 77 | + | |
| 78 | + const std::vector<int>& code() const { return code_; } | |
| 79 | + | |
| 80 | +private: | |
| 81 | + int sole_blocker(int word) const { | |
| 82 | + int blocker = -1; | |
| 83 | + for (int d : descendants_[word]) { | |
| 84 | + const int owner = owner_[d]; | |
| 85 | + if (owner < 0) continue; | |
| 86 | + if (blocker >= 0 && owner != blocker) return -2; | |
| 87 | + blocker = owner; | |
| 88 | + } | |
| 89 | + return blocker; | |
| 90 | + } | |
| 91 | + | |
| 92 | + void add(int word) { | |
| 93 | + for (int d : descendants_[word]) { | |
| 94 | + if (owner_[d] >= 0) throw std::logic_error("overlapping descendants"); | |
| 95 | + owner_[d] = word; | |
| 96 | + } | |
| 97 | + code_.push_back(word); | |
| 98 | + } | |
| 99 | + | |
| 100 | + void remove(int word) { | |
| 101 | + for (int d : descendants_[word]) owner_[d] = -1; | |
| 102 | + auto position = std::find(code_.begin(), code_.end(), word); | |
| 103 | + *position = code_.back(); | |
| 104 | + code_.pop_back(); | |
| 105 | + } | |
| 106 | + | |
| 107 | + bool replace_one_with_two() { | |
| 108 | + std::vector<std::vector<int>> blocked_by(word_count); | |
| 109 | + for (int word : candidates_) { | |
| 110 | + const int blocker = sole_blocker(word); | |
| 111 | + if (blocker == word || blocker == -2) continue; | |
| 112 | + if (blocker == -1) { | |
| 113 | + add(word); | |
| 114 | + return true; | |
| 115 | + } | |
| 116 | + auto& options = blocked_by[blocker]; | |
| 117 | + for (int other : options) { | |
| 118 | + if (!disjoint(descendants_[word], descendants_[other])) continue; | |
| 119 | + remove(blocker); | |
| 120 | + add(word); | |
| 121 | + add(other); | |
| 122 | + return true; | |
| 123 | + } | |
| 124 | + options.push_back(word); | |
| 125 | + } | |
| 126 | + return false; | |
| 127 | + } | |
| 128 | + | |
| 129 | + const std::vector<std::vector<std::uint16_t>>& descendants_; | |
| 130 | + std::mt19937& random_; | |
| 131 | + std::vector<int> owner_; | |
| 132 | + std::vector<int> candidates_; | |
| 133 | + std::vector<int> code_; | |
| 134 | +}; | |
| 135 | + | |
| 136 | +std::vector<int> read_baseline(const char* filename) { | |
| 137 | + std::ifstream input(filename); | |
| 138 | + if (!input) throw std::runtime_error("cannot read starting code"); | |
| 139 | + std::vector<int> baseline; | |
| 140 | + std::string word; | |
| 141 | + while (input >> word) { | |
| 142 | + if (word.size() != length || word.find_first_not_of("01") != std::string::npos) { | |
| 143 | + throw std::runtime_error("invalid word in starting code"); | |
| 144 | + } | |
| 145 | + baseline.push_back(std::stoi(word, nullptr, 2)); | |
| 146 | + } | |
| 147 | + if (baseline.empty()) throw std::runtime_error("starting code is empty"); | |
| 148 | + return baseline; | |
| 149 | +} | |
| 150 | + | |
| 151 | +void write_code(const char* filename, std::vector<int> code) { | |
| 152 | + std::sort(code.begin(), code.end()); | |
| 153 | + std::ofstream output(filename); | |
| 154 | + output.exceptions(std::ios::failbit | std::ios::badbit); | |
| 155 | + output << "{\n \"n\": " << length << ",\n \"words\": [\n"; | |
| 156 | + for (std::size_t i = 0; i < code.size(); ++i) { | |
| 157 | + output << " \"" << std::bitset<length>(code[i]) << '"' | |
| 158 | + << (i + 1 == code.size() ? "\n" : ",\n"); | |
| 159 | + } | |
| 160 | + output << " ]\n}\n"; | |
| 161 | +} | |
| 162 | +} // namespace | |
| 163 | + | |
| 164 | +int main(int argc, char** argv) { | |
| 165 | + try { | |
| 166 | + if (argc != 5) throw std::runtime_error("usage: search START_WORDS OUT_JSON SEED ROUNDS"); | |
| 167 | + const auto baseline = read_baseline(argv[1]); | |
| 168 | + const auto descendants = make_descendants(); | |
| 169 | + std::mt19937 random(std::stoul(argv[3])); | |
| 170 | + const int rounds = std::stoi(argv[4]); | |
| 171 | + if (rounds < 1) throw std::runtime_error("rounds must be positive"); | |
| 172 | + Search search(descendants, random); | |
| 173 | + search.reset(baseline); | |
| 174 | + auto incumbent = baseline; | |
| 175 | + auto best = baseline; | |
| 176 | + for (int round = 0; round < rounds; ++round) { | |
| 177 | + search.reset(round % 100 == 0 ? best : incumbent); | |
| 178 | + if (round != 0) search.perturb(1 + random() % 8); | |
| 179 | + search.improve(); | |
| 180 | + if (search.code().size() > best.size() || round == 0) { | |
| 181 | + best = search.code(); | |
| 182 | + write_code(argv[2], best); | |
| 183 | + std::cout << "metric: code_size " << best.size() << '\n'; | |
| 184 | + } | |
| 185 | + if (search.code().size() >= incumbent.size() || | |
| 186 | + (round % 17 == 0 && search.code().size() + 2 >= best.size())) { | |
| 187 | + incumbent = search.code(); | |
| 188 | + } | |
| 189 | + if ((round + 1) % 25 == 0 || round + 1 == rounds) { | |
| 190 | + std::cout << "progress: " << round + 1 << '/' << rounds | |
| 191 | + << " rounds, best " << best.size() << std::endl; | |
| 192 | + } | |
| 193 | + } | |
| 194 | + } catch (const std::exception& error) { | |
| 195 | + std::cerr << error.what() << '\n'; | |
| 196 | + return 1; | |
| 197 | + } | |
| 198 | +} | |
+13 −0 routes/liuqiran-codex/n17-local-search/search.sbatch added View file
| @@ -0,0 +1,13 @@ | ||
| 1 | +#!/bin/sh | |
| 2 | +#SBATCH --time=00:24:00 | |
| 3 | +#SBATCH --cpus-per-task=1 | |
| 4 | +#SBATCH --mem=1024 | |
| 5 | +set -eu | |
| 6 | +route_dir=$(cd "$(dirname "$0")" && pwd) | |
| 7 | +repo_root=$(cd "$route_dir/../../.." && pwd) | |
| 8 | +scratch=$(mktemp -d) | |
| 9 | +trap 'rm -rf "$scratch"' EXIT | |
| 10 | +c++ -std=c++17 -O3 -Wall -Wextra -pedantic "$route_dir/search.cpp" -o "$scratch/search" | |
| 11 | +python3 -c 'import json,sys; print(*json.load(open(sys.argv[1]))["words"], sep="\n")' "$repo_root/constructions/n17.json" > "$scratch/baseline.txt" | |
| 12 | +"$scratch/search" "$scratch/baseline.txt" "$route_dir/n17.json" "$1" "$2" | |
| 13 | +python3 -B "$repo_root/checks/judge/check.py" "$route_dir" | |
