summaryrefslogtreecommitdiff
path: root/hadamard_pt.hpp
blob: 724393faa09b56eb94c0f5f06e838f274cab05d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157

#pragma once
#include "hadamard_mcmc.hpp"
#include <execution>
#include <list>

void swap(MCMC& s1, MCMC& s2) {
  std::swap(s1.M, s2.M);
  std::swap(s1.E, s2.E);
}

class ParallelMeasurement {
public:
  virtual void after_step(bool, unsigned, double, double, const MCMC&, const MCMC&){};
};

class PT {
private:
  Rng rng;

public:
  std::vector<MCMC> Ms;
  ParallelMeasurement& B;
  std::vector<Measurement*>& As;

  PT(double β₀, double β₁, unsigned N, unsigned n, ParallelMeasurement& B,
     std::vector<Measurement*>& As)
      : B(B), As(As) {
    Ms.reserve(N);
    for (unsigned i = 0; i < N; i++) {
      double β = β₀ + i * (β₁ - β₀) / (N - 1);
      Ms.push_back(MCMC(n, β, *As[i]));
    }
  }

  double T(unsigned i) { return 1 / Ms[Ms.size() - i - 1].β; }

  bool step(unsigned i, unsigned j, bool dry = false) {
    double Δβ = Ms[i].β - Ms[j].β;
    double ΔE = Ms[i].E - Ms[j].E;

    bool accepted = Δβ * ΔE > 0 || exp(Δβ * ΔE) > rng.uniform((double)0.0, 1.0);

    if (accepted)
      swap(Ms[i], Ms[j]);

    if (!dry) {
      B.after_step(accepted, i, Δβ, ΔE, Ms[i], Ms[j]);
    }
    return accepted;
  }

  std::vector<double> tune(unsigned n0, unsigned m, double ε, double ε2) {
    unsigned n = n0;

    while (true) {
      std::vector<color> colors(Ms.size(), none);
      colors.front() = down;
      colors.back() = up;

      std::vector<unsigned> nu(Ms.size(), 0);
      std::vector<unsigned> nd(Ms.size(), 0);

      for (unsigned i = 0; i < n; i++) {
        std::for_each(std::execution::par_unseq, Ms.begin(), Ms.end(),
                      [m, ε](MCMC& M) { M.tune(m, ε); });

        for (unsigned k = 0; k < m * Ms.size(); k++) {
          unsigned j = rng.uniform((unsigned)0, (unsigned)(Ms.size() - 2));

          if (this->step(j, j + 1, true)) {
            std::swap(colors[j], colors[j + 1]);
            colors.front() = down;
            colors.back() = up;
          }
        }

        for (unsigned j = 0; j < Ms.size(); j++) {
          if (colors[j] == up) {
            nu[j]++;
          } else if (colors[j] == down) {
            nd[j]++;
          }
        }
      }

      std::vector<double> f(Ms.size());

      std::vector<unsigned> f_keep;
      double f_last = 0;
      for (unsigned i = 0; i < Ms.size(); i++) {
        f[i] = nu[i] / (double)(nu[i] + nd[i]);
        if (f[i] >= f_last) {
          f_keep.push_back(i);
          f_last = f[i];
        }
      }

      for (signed i = 0; i < f_keep.size() - 1; i++) {
        for (unsigned j = f_keep[i]; j < f_keep[i + 1]; j++) {
          f[j] = f[f_keep[i]] + (f[f_keep[i + 1]] - f[f_keep[i]]) /
                                    (Ms[f_keep[i + 1]].β - Ms[f_keep[i]].β) *
                                    (Ms[j].β - Ms[f_keep[i]].β);
        }
      }

      std::vector<double> η(Ms.size() - 1);

      for (unsigned i = 0; i < Ms.size() - 1; i++) {
        η[η.size() - i - 1] = sqrt((f[i + 1] - f[i])) / (1 / Ms[i].β - 1 / Ms[i + 1].β);
      }

      double C = 0;
      for (unsigned i = 0; i < η.size(); i++) {
        C += η[i] * (T(i + 1) - T(i));
      }

      std::vector<double> T1(Ms.size() - 2);

      double x = 0;
      unsigned j = 1;

      for (unsigned i = 0; i < η.size(); i++) {
        double xnew = x + η[i] * (T(i + 1) - T(i)) / C;
        while (j < xnew * η.size() && j < η.size()) {
          T1[j - 1] = T(i) + (j / (double)η.size() - x) / η[i] * C;
          j++;
        }
        x = xnew;
      }

      double err = 0;
      for (unsigned i = 0; i < T1.size(); i++) {
        err += fabs(T1[i] - 1 / Ms[Ms.size() - i - 2].β);
        Ms[Ms.size() - i - 2].β = 1 / T1[i];
      }

      if (err / T1.size() * Ms.size() / (1 / Ms.front().β - 1 / Ms.back().β) < ε2) {
        return f;
      }

      n += n0;
    }
  }

  void run(unsigned n, unsigned m, bool dry = false) {
    for (unsigned i = 0; i < n; i++) {
      std::for_each(std::execution::par_unseq, Ms.begin(), Ms.end(),
                    [m, dry](MCMC& M) { M.run(m, dry); });
      for (unsigned j = 0; j < Ms.size(); j++) {
        unsigned k = rng.uniform((unsigned)0, (unsigned)(Ms.size() - 2));
        this->step(k, k + 1, dry);
      }
    }
  }
};