| @@ -0,0 +1,236 @@ |
| 1 | +#include <algorithm> |
| 2 | +#include <bitset> |
| 3 | +#include <chrono> |
| 4 | +#include <cstdint> |
| 5 | +#include <filesystem> |
| 6 | +#include <fstream> |
| 7 | +#include <iostream> |
| 8 | +#include <iterator> |
| 9 | +#include <map> |
| 10 | +#include <regex> |
| 11 | +#include <set> |
| 12 | +#include <stdexcept> |
| 13 | +#include <string> |
| 14 | +#include <vector> |
| 15 | + |
| 16 | +namespace { |
| 17 | +constexpr int n = 18; |
| 18 | +constexpr int word_count = 1 << n; |
| 19 | +constexpr int descendant_count = 1 << (n - 2); |
| 20 | + |
| 21 | +int delete_bit(int word, int position) { |
| 22 | + const int low = (1 << position) - 1; |
| 23 | + return (word & low) | ((word >> 1) & ~low); |
| 24 | +} |
| 25 | + |
| 26 | +struct Graph { |
| 27 | + std::vector<std::vector<std::uint16_t>> descendants{word_count}; |
| 28 | + |
| 29 | + Graph() { |
| 30 | + std::vector<int> last_seen(descendant_count, -1); |
| 31 | + for (int word = 0; word < word_count; ++word) { |
| 32 | + auto& row = descendants[word]; |
| 33 | + for (int second = 1; second < n; ++second) { |
| 34 | + const int once = delete_bit(word, second); |
| 35 | + for (int first = 0; first < second; ++first) { |
| 36 | + const int child = delete_bit(once, first); |
| 37 | + if (last_seen[child] == word) continue; |
| 38 | + last_seen[child] = word; |
| 39 | + row.push_back(static_cast<std::uint16_t>(child)); |
| 40 | + } |
| 41 | + } |
| 42 | + std::sort(row.begin(), row.end()); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + bool disjoint(int a, int b) const { |
| 47 | + const auto& left = descendants[a]; |
| 48 | + const auto& right = descendants[b]; |
| 49 | + auto i = left.begin(), j = right.begin(); |
| 50 | + while (i != left.end() && j != right.end()) { |
| 51 | + if (*i == *j) return false; |
| 52 | + if (*i < *j) ++i; |
| 53 | + else ++j; |
| 54 | + } |
| 55 | + return true; |
| 56 | + } |
| 57 | +}; |
| 58 | + |
| 59 | +std::vector<int> read_code(const std::filesystem::path& file) { |
| 60 | + std::ifstream input(file); |
| 61 | + if (!input) throw std::runtime_error("cannot read the starting code"); |
| 62 | + const std::string json{std::istreambuf_iterator<char>(input), std::istreambuf_iterator<char>()}; |
| 63 | + const std::regex word_pattern(R"re("([01]{18})")re"); |
| 64 | + std::vector<int> result; |
| 65 | + for (std::sregex_iterator i(json.begin(), json.end(), word_pattern), end; i != end; ++i) { |
| 66 | + result.push_back(std::stoi((*i)[1], nullptr, 2)); |
| 67 | + } |
| 68 | + if (result.empty()) throw std::runtime_error("the starting code contains no words"); |
| 69 | + return result; |
| 70 | +} |
| 71 | + |
| 72 | +void write_code(const std::filesystem::path& file, std::vector<int> words) { |
| 73 | + std::sort(words.begin(), words.end()); |
| 74 | + std::filesystem::path temporary = file; |
| 75 | + temporary += ".tmp"; |
| 76 | + std::ofstream output(temporary); |
| 77 | + output.exceptions(std::ios::failbit | std::ios::badbit); |
| 78 | + output << "{\n \"n\": " << n << ",\n \"words\": [\n"; |
| 79 | + for (std::size_t i = 0; i < words.size(); ++i) { |
| 80 | + output << " \"" << std::bitset<n>(words[i]) << '"' << (i + 1 == words.size() ? "\n" : ",\n"); |
| 81 | + } |
| 82 | + output << " ]\n}\n"; |
| 83 | + output.close(); |
| 84 | + std::filesystem::rename(temporary, file); |
| 85 | +} |
| 86 | + |
| 87 | +struct Audit { |
| 88 | + const Graph& graph; |
| 89 | + std::vector<int> code; |
| 90 | + std::map<std::vector<int>, std::vector<int>> groups; |
| 91 | + std::set<std::vector<int>> relevant; |
| 92 | + std::vector<int> replacement; |
| 93 | + std::uint64_t nodes = 0; |
| 94 | + std::uint64_t node_limit; |
| 95 | + bool exhausted = false; |
| 96 | + |
| 97 | + bool packing(const std::vector<int>& candidates, int target) { |
| 98 | + if (target == 0) return true; |
| 99 | + if (static_cast<int>(candidates.size()) < target || exhausted) return false; |
| 100 | + for (std::size_t i = 0; i + target <= candidates.size(); ++i) { |
| 101 | + if (++nodes > node_limit) { |
| 102 | + exhausted = true; |
| 103 | + return false; |
| 104 | + } |
| 105 | + std::vector<int> rest; |
| 106 | + rest.reserve(candidates.size() - i - 1); |
| 107 | + for (std::size_t j = i + 1; j < candidates.size(); ++j) { |
| 108 | + if (graph.disjoint(candidates[i], candidates[j])) rest.push_back(candidates[j]); |
| 109 | + } |
| 110 | + replacement.push_back(candidates[i]); |
| 111 | + if (packing(rest, target - 1)) return true; |
| 112 | + replacement.pop_back(); |
| 113 | + if (exhausted) return false; |
| 114 | + } |
| 115 | + return false; |
| 116 | + } |
| 117 | + |
| 118 | + bool run(int radius) { |
| 119 | + std::vector<int> owner(descendant_count, -1); |
| 120 | + std::vector<bool> incumbent(word_count, false); |
| 121 | + for (int i = 0; i < static_cast<int>(code.size()); ++i) { |
| 122 | + incumbent[code[i]] = true; |
| 123 | + for (int child : graph.descendants[code[i]]) { |
| 124 | + if (owner[child] >= 0) throw std::runtime_error("the starting code has a conflict"); |
| 125 | + owner[child] = i; |
| 126 | + } |
| 127 | + } |
| 128 | + for (int word = 0; word < word_count; ++word) { |
| 129 | + if (incumbent[word]) continue; |
| 130 | + std::vector<int> blockers; |
| 131 | + for (int child : graph.descendants[word]) { |
| 132 | + if (owner[child] >= 0) blockers.push_back(owner[child]); |
| 133 | + } |
| 134 | + std::sort(blockers.begin(), blockers.end()); |
| 135 | + blockers.erase(std::unique(blockers.begin(), blockers.end()), blockers.end()); |
| 136 | + if (blockers.size() <= static_cast<std::size_t>(radius)) { |
| 137 | + groups[blockers].push_back(word); |
| 138 | + if (!blockers.empty()) relevant.insert(blockers); |
| 139 | + } |
| 140 | + } |
| 141 | + if (const auto i = groups.find(std::vector<int>{}); i != groups.end() && !i->second.empty()) { |
| 142 | + replacement = {i->second.front()}; |
| 143 | + code.push_back(replacement.front()); |
| 144 | + return true; |
| 145 | + } |
| 146 | + for (int size = 1; size < radius; ++size) { |
| 147 | + std::vector<std::vector<int>> current; |
| 148 | + for (const auto& owners : relevant) { |
| 149 | + if (static_cast<int>(owners.size()) == size) current.push_back(owners); |
| 150 | + } |
| 151 | + for (const auto& left : current) { |
| 152 | + for (const auto& [right, words] : groups) { |
| 153 | + if (right.empty()) continue; |
| 154 | + std::vector<int> intersection; |
| 155 | + std::set_intersection(left.begin(), left.end(), right.begin(), right.end(), |
| 156 | + std::back_inserter(intersection)); |
| 157 | + if (intersection.empty()) continue; |
| 158 | + std::vector<int> joined; |
| 159 | + std::set_union(left.begin(), left.end(), right.begin(), right.end(), |
| 160 | + std::back_inserter(joined)); |
| 161 | + if (joined.size() > left.size() && joined.size() <= static_cast<std::size_t>(radius)) { |
| 162 | + relevant.insert(std::move(joined)); |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + for (int size = 1; size <= radius; ++size) { |
| 168 | + std::vector<std::vector<int>> subsets; |
| 169 | + for (const auto& owners : relevant) { |
| 170 | + if (static_cast<int>(owners.size()) == size) subsets.push_back(owners); |
| 171 | + } |
| 172 | + std::cout << "radius " << size << ": " << subsets.size() << " connected owner sets" << std::endl; |
| 173 | + for (const auto& owners : subsets) { |
| 174 | + std::vector<int> candidates; |
| 175 | + for (int mask = 1; mask < (1 << size); ++mask) { |
| 176 | + std::vector<int> subset; |
| 177 | + for (int i = 0; i < size; ++i) { |
| 178 | + if (mask & (1 << i)) subset.push_back(owners[i]); |
| 179 | + } |
| 180 | + if (const auto i = groups.find(subset); i != groups.end()) { |
| 181 | + candidates.insert(candidates.end(), i->second.begin(), i->second.end()); |
| 182 | + } |
| 183 | + } |
| 184 | + if (static_cast<int>(candidates.size()) <= size) continue; |
| 185 | + std::sort(candidates.begin(), candidates.end(), [&](int a, int b) { |
| 186 | + return graph.descendants[a].size() < graph.descendants[b].size(); |
| 187 | + }); |
| 188 | + replacement.clear(); |
| 189 | + nodes = 0; |
| 190 | + if (packing(candidates, size + 1)) { |
| 191 | + std::vector<bool> removed(code.size(), false); |
| 192 | + for (int index : owners) removed[index] = true; |
| 193 | + std::vector<int> next; |
| 194 | + for (int i = 0; i < static_cast<int>(code.size()); ++i) { |
| 195 | + if (!removed[i]) next.push_back(code[i]); |
| 196 | + } |
| 197 | + next.insert(next.end(), replacement.begin(), replacement.end()); |
| 198 | + code = std::move(next); |
| 199 | + return true; |
| 200 | + } |
| 201 | + if (exhausted) return false; |
| 202 | + } |
| 203 | + } |
| 204 | + return false; |
| 205 | + } |
| 206 | +}; |
| 207 | +} |
| 208 | + |
| 209 | +int main(int argc, char** argv) { |
| 210 | + try { |
| 211 | + if (argc != 4 && argc != 5) { |
| 212 | + throw std::runtime_error("usage: exchange INPUT_JSON OUTPUT_JSON RADIUS [NODE_LIMIT_PER_SET]"); |
| 213 | + } |
| 214 | + const int radius = std::stoi(argv[3]); |
| 215 | + if (radius < 1 || radius > 6) throw std::runtime_error("radius must be between 1 and 6"); |
| 216 | + const Graph graph; |
| 217 | + Audit audit{graph, read_code(argv[1]), {}, {}, {}, 0, |
| 218 | + argc == 5 ? std::stoull(argv[4]) : 1000000, false}; |
| 219 | + write_code(argv[2], audit.code); |
| 220 | + std::cout << "metric: code_size " << audit.code.size() << std::endl; |
| 221 | + const bool improved = audit.run(radius); |
| 222 | + if (audit.exhausted) { |
| 223 | + std::cout << "Audit stopped at the per-owner-set node limit; no certificate of optimality" << std::endl; |
| 224 | + } else if (!improved) { |
| 225 | + std::cout << "No improving exchange through radius " << radius << std::endl; |
| 226 | + } else { |
| 227 | + write_code(argv[2], audit.code); |
| 228 | + std::cout << "Improving exchange: " << audit.replacement.size() - 1 << " removed, " |
| 229 | + << audit.replacement.size() << " inserted" << std::endl; |
| 230 | + std::cout << "metric: code_size " << audit.code.size() << std::endl; |
| 231 | + } |
| 232 | + } catch (const std::exception& error) { |
| 233 | + std::cerr << error.what() << '\n'; |
| 234 | + return 1; |
| 235 | + } |
| 236 | +} |