#include #include #include #include #include #include #include "randutils/randutils.hpp" #include "pcg-cpp/include/pcg_random.hpp" using Rng = randutils::random_generator; using Real = long double; class Edge { public: std::list weights; Real probability = 0; }; class Face { public: std::array, 4> edges; Face(Edge& a, Edge& b, Edge& c, Edge& d) : edges({a, b, c, d}) {} }; class AztecDiamond { public: std::vector edges; std::vector> faces; AztecDiamond(unsigned n) : edges(pow(2 * n, 2)), faces(n) { for (unsigned i = 1; i <= n; i++) { faces[n - i].reserve(pow(i, 2)); unsigned x0 = n - i; for (unsigned j = 0; j < pow(i, 2); j++) { unsigned x = 2 * (j % i); unsigned y = 2 * (j / i); faces[n - i].push_back(Face( edges[2 * n * (x0 + y) + x0 + x], edges[2 * n * (x0 + y) + x0 + x + 1], edges[2 * n * (x0 + y + 1) + x0 + x], edges[2 * n * (x0 + y + 1) + x0 + x + 1] )); } } } void computeWeights() { for (std::vector& fs : faces) { for (Face& f : fs) { Real w = f.edges[0].get().weights.back(); Real x = f.edges[1].get().weights.back(); Real y = f.edges[2].get().weights.back(); Real z = f.edges[3].get().weights.back(); Real cellFactor = w * z + x * y; f.edges[0].get().weights.push_back(z / cellFactor); f.edges[1].get().weights.push_back(y / cellFactor); f.edges[2].get().weights.push_back(x / cellFactor); f.edges[3].get().weights.push_back(w / cellFactor); } } // This process computes one extra weight per edge. for (Edge& e : edges) { e.weights.pop_back(); } } void computeProbabilities() { for (auto it = faces.rbegin(); it != faces.rend(); it++) { for (Face& f : *it) { Real p = f.edges[0].get().probability; Real q = f.edges[1].get().probability; Real r = f.edges[2].get().probability; Real s = f.edges[3].get().probability; Real w = f.edges[0].get().weights.back(); Real x = f.edges[1].get().weights.back(); Real y = f.edges[2].get().weights.back(); Real z = f.edges[3].get().weights.back(); Real cellFactor = w * z + x * y; Real deficit = 1 - p - q - r - s; f.edges[0].get().probability = s + deficit * w * z / cellFactor; f.edges[1].get().probability = r + deficit * x * y / cellFactor; f.edges[2].get().probability = q + deficit * x * y / cellFactor; f.edges[3].get().probability = p + deficit * w * z / cellFactor; for (Edge& e : f.edges) { e.weights.pop_back(); } } } } }; int main(int argc, char* argv[]) { unsigned n = 100; unsigned m = 100; Real T = 1; int opt; while ((opt = getopt(argc, argv, "n:m:T:")) != -1) { switch (opt) { case 'n': n = atoi(optarg); break; case 'm': m = (unsigned)atof(optarg); break; case 'T': T = atof(optarg); break; default: exit(1); } } Rng r; AztecDiamond a(n); std::vector avgProbabilities(a.edges.size()); for (unsigned i = 0; i < m; i++) { for (Edge& e : a.edges) { e.weights = {exp(- r.variate(1) / T)}; e.probability = 0; } a.computeWeights(); a.computeProbabilities(); for (unsigned j = 0; j < a.edges.size(); j++) { avgProbabilities[j] += a.edges[j].probability; } } for (Real& x : avgProbabilities) { std::cout << x << " "; } std::cout <