| @@ -0,0 +1,201 @@ |
| 1 | +#include <algorithm> |
| 2 | +#include <array> |
| 3 | +#include <cmath> |
| 4 | +#include <cstdint> |
| 5 | +#include <fstream> |
| 6 | +#include <iomanip> |
| 7 | +#include <iostream> |
| 8 | +#include <limits> |
| 9 | +#include <numeric> |
| 10 | +#include <random> |
| 11 | +#include <stdexcept> |
| 12 | +#include <string> |
| 13 | +#include <vector> |
| 14 | + |
| 15 | +constexpr int count = 20; |
| 16 | +struct Point { double x, y; }; |
| 17 | +struct Triple { int a, b, c; }; |
| 18 | +using Points = std::array<Point, count>; |
| 19 | + |
| 20 | +double double_area(const Points& points, Triple t) { |
| 21 | + const auto [a, b, c] = t; |
| 22 | + return std::abs((points[b].x - points[a].x) * (points[c].y - points[a].y) |
| 23 | + - (points[b].y - points[a].y) * (points[c].x - points[a].x)); |
| 24 | +} |
| 25 | + |
| 26 | +Point reflect(Point p) { return {1.0 - p.x, 1.0 - p.y}; } |
| 27 | + |
| 28 | +struct Search { |
| 29 | + std::mt19937_64 rng; |
| 30 | + bool symmetric; |
| 31 | + int variables; |
| 32 | + std::vector<Triple> triples; |
| 33 | + std::array<std::vector<int>, count> affected; |
| 34 | + |
| 35 | + Search(std::uint64_t seed, bool half_turn) |
| 36 | + : rng(seed), symmetric(half_turn), variables(half_turn ? count / 2 : count) { |
| 37 | + for (int a = 0; a < count; ++a) { |
| 38 | + for (int b = a + 1; b < count; ++b) { |
| 39 | + for (int c = b + 1; c < count; ++c) { |
| 40 | + const int id = static_cast<int>(triples.size()); |
| 41 | + triples.push_back({a, b, c}); |
| 42 | + for (int v = 0; v < variables; ++v) { |
| 43 | + if (a % variables == v || b % variables == v || c % variables == v) { |
| 44 | + affected[v].push_back(id); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + double uniform() { return static_cast<double>(rng() >> 11) * 0x1p-53; } |
| 53 | + int choose(int limit) { return static_cast<int>(rng() % limit); } |
| 54 | + |
| 55 | + void set(Points& points, int v, Point p) const { |
| 56 | + points[v] = p; |
| 57 | + if (symmetric) points[v + variables] = reflect(p); |
| 58 | + } |
| 59 | + |
| 60 | + Points fresh(int restart) { |
| 61 | + Points points; |
| 62 | + std::array<int, count> order; |
| 63 | + std::iota(order.begin(), order.end(), 0); |
| 64 | + for (int v = variables - 1; v > 0; --v) std::swap(order[v], order[choose(v + 1)]); |
| 65 | + |
| 66 | + for (int v = 0; v < variables; ++v) { |
| 67 | + Point p{uniform(), uniform()}; |
| 68 | + if (restart % 3 == 0) { |
| 69 | + p = {(v + p.x) / variables, (order[v] + p.y) / variables}; |
| 70 | + } |
| 71 | + set(points, v, p); |
| 72 | + } |
| 73 | + return points; |
| 74 | + } |
| 75 | + |
| 76 | + double minimum(const Points& points) const { |
| 77 | + double smallest = std::numeric_limits<double>::infinity(); |
| 78 | + for (const auto triple : triples) smallest = std::min(smallest, double_area(points, triple)); |
| 79 | + return smallest; |
| 80 | + } |
| 81 | + |
| 82 | + void try_move(Points& points, std::vector<double>& costs, std::vector<double>& proposed_costs, |
| 83 | + double target, double temperature, double radius) { |
| 84 | + const int v = choose(variables); |
| 85 | + const Point previous = points[v]; |
| 86 | + Point next{ |
| 87 | + std::clamp(previous.x + radius * (2 * uniform() - 1), 0.0, 1.0), |
| 88 | + std::clamp(previous.y + radius * (2 * uniform() - 1), 0.0, 1.0) |
| 89 | + }; |
| 90 | + const int move = choose(100); |
| 91 | + if (move < 7) { |
| 92 | + next = {uniform(), uniform()}; |
| 93 | + } else if (move < 12) { |
| 94 | + |
| 95 | + if (choose(2) == 0) next.x = choose(2); |
| 96 | + else next.y = choose(2); |
| 97 | + } |
| 98 | + set(points, v, next); |
| 99 | + double delta = 0.0; |
| 100 | + const auto& touched = affected[v]; |
| 101 | + for (std::size_t i = 0; i < touched.size(); ++i) { |
| 102 | + const int id = touched[i]; |
| 103 | + const double deficit = std::max(0.0, target - double_area(points, triples[id])); |
| 104 | + proposed_costs[i] = deficit * deficit; |
| 105 | + delta += proposed_costs[i] - costs[id]; |
| 106 | + } |
| 107 | + if (delta <= 0.0 || uniform() < std::exp(-delta / temperature)) { |
| 108 | + for (std::size_t i = 0; i < touched.size(); ++i) costs[touched[i]] = proposed_costs[i]; |
| 109 | + } else { |
| 110 | + set(points, v, previous); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + Points run(int restarts, int steps, double target) { |
| 115 | + Points best{}; |
| 116 | + double best_value = -1.0; |
| 117 | + std::vector<double> costs(triples.size()); |
| 118 | + std::vector<double> proposed_costs(triples.size()); |
| 119 | + for (int restart = 0; restart < restarts; ++restart) { |
| 120 | + Points points = fresh(restart); |
| 121 | + |
| 122 | + const int kind = restart % 5; |
| 123 | + if (restart > 0 && kind >= 3) { |
| 124 | + points = best; |
| 125 | + for (int j = 0; j < 3; ++j) { |
| 126 | + const int v = choose(variables); |
| 127 | + const double kick = kind == 3 ? 0.075 : 0.19; |
| 128 | + const Point next{ |
| 129 | + std::clamp(points[v].x + kick * (2 * uniform() - 1), 0.0, 1.0), |
| 130 | + std::clamp(points[v].y + kick * (2 * uniform() - 1), 0.0, 1.0) |
| 131 | + }; |
| 132 | + set(points, v, next); |
| 133 | + } |
| 134 | + } |
| 135 | + const double local_target = target * (0.88 + 0.06 * (restart % 4)); |
| 136 | + for (std::size_t i = 0; i < triples.size(); ++i) { |
| 137 | + const double deficit = std::max(0.0, local_target - double_area(points, triples[i])); |
| 138 | + costs[i] = deficit * deficit; |
| 139 | + } |
| 140 | + double restart_best = 0.0; |
| 141 | + auto sample = [&] { |
| 142 | + const double current = minimum(points); |
| 143 | + restart_best = std::max(restart_best, current); |
| 144 | + if (current > best_value) { |
| 145 | + best_value = current; |
| 146 | + best = points; |
| 147 | + } |
| 148 | + }; |
| 149 | + sample(); |
| 150 | + for (int step = 0; step < steps; ++step) { |
| 151 | + const double fraction = static_cast<double>(step) / steps; |
| 152 | + const double temperature = (kind >= 3 ? 2.5e-5 : 2.0e-4) * std::exp(-13 * fraction); |
| 153 | + const double radius = (kind >= 3 ? 0.11 : 0.23) * std::exp(-6 * fraction); |
| 154 | + try_move(points, costs, proposed_costs, local_target, temperature, radius); |
| 155 | + if (step % 128 == 0) sample(); |
| 156 | + } |
| 157 | + sample(); |
| 158 | + std::cerr << "restart=" << restart + 1 << " area=" << std::setprecision(12) |
| 159 | + << restart_best / 2 << " best=" << best_value / 2 << '\n'; |
| 160 | + } |
| 161 | + return best; |
| 162 | + } |
| 163 | +}; |
| 164 | + |
| 165 | +std::string decimal(double coordinate) { |
| 166 | + constexpr std::uint64_t scale = 1'000'000'000'000; |
| 167 | + auto units = static_cast<std::uint64_t>(coordinate * scale); |
| 168 | + |
| 169 | + if (std::fma(coordinate, static_cast<double>(scale), -static_cast<double>(units)) < 0) --units; |
| 170 | + std::string digits = std::to_string(scale + units % scale).substr(1); |
| 171 | + return std::to_string(units / scale) + "." + digits; |
| 172 | +} |
| 173 | + |
| 174 | +int main(int argc, char** argv) { |
| 175 | + try { |
| 176 | + if (argc != 7) throw std::invalid_argument("usage: search SEED RESTARTS STEPS TARGET free|symmetric OUTPUT"); |
| 177 | + const auto seed = std::stoull(argv[1]); |
| 178 | + const int restarts = std::stoi(argv[2]); |
| 179 | + const int steps = std::stoi(argv[3]); |
| 180 | + const double target = std::stod(argv[4]); |
| 181 | + const std::string mode = argv[5]; |
| 182 | + if (restarts <= 0 || steps <= 0 || !(target > 0.0 && target <= 1.0) || |
| 183 | + (mode != "free" && mode != "symmetric")) { |
| 184 | + throw std::invalid_argument("positive restarts and steps, a target in (0, 1], and a valid mode are required"); |
| 185 | + } |
| 186 | + Search search(seed, mode == "symmetric"); |
| 187 | + const auto best = search.run(restarts, steps, target); |
| 188 | + std::ofstream out(argv[6]); |
| 189 | + out << "{\n \"container\": \"square\",\n \"n\": 20,\n \"points\": [\n"; |
| 190 | + for (int i = 0; i < count; ++i) { |
| 191 | + out << " [\"" << decimal(best[i].x) << "\", \"" << decimal(best[i].y) << "\"]" |
| 192 | + << (i + 1 == count ? "\n" : ",\n"); |
| 193 | + } |
| 194 | + out << " ]\n}\n"; |
| 195 | + out.close(); |
| 196 | + if (!out) throw std::runtime_error("could not write candidate"); |
| 197 | + } catch (const std::exception& error) { |
| 198 | + std::cerr << error.what() << '\n'; |
| 199 | + return 2; |
| 200 | + } |
| 201 | +} |