summaryrefslogtreecommitdiff
path: root/stokes.hpp
blob: cbe5437cef3b70aa13f648abd3814f6cb4bcfc75 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#pragma once

#include "p-spin.hpp"
#include "complex_normal.hpp"
#include "dynamics.hpp"

class ropeRelaxationStallException: public std::exception {
  virtual const char* what() const throw() {
    return "Gradient descent stalled.";
  }
};

template <class Scalar>
Vector<Scalar> variation(const Vector<Scalar>& z, const Vector<Scalar>&, const Vector<Scalar>& z´´, const Vector<Scalar>& dH, const Matrix<Scalar>& ddH) {
  Real z² = z.squaredNorm();
  Real z´² =.squaredNorm();

  Vector<Scalar> ż = zDot(z, dH);
  Real ż² = ż.squaredNorm();

  Real Reż·z´ = real(ż.dot());

  Matrix<Scalar>= (dH.conjugate() - (dH.dot(z) /) * z.conjugate()) * z.adjoint() /;
  Matrix<Scalar> dżc = -ddH + (ddH * z.conjugate()) * z.transpose() /+ (z.dot(dH) /) * (
        Matrix<Scalar>::Identity(ddH.rows(), ddH.cols()) - z.conjugate() * z.transpose() /);

  Vector<Scalar> dLdz = - (
        dżc *+*.conjugate() - (* ż.conjugate() + dżc * ż) * Reż·z´ / ż²
      ) / sqrt(ż² * z´²) / 2;

  Vector<Scalar> ż´ = -(ddH *).conjugate() + ((ddH *).dot(z) /) * z.conjugate() + (
        dH.dot(z) *.conjugate() + dH.dot() * z.conjugate() - (
          dH.dot(z) * (.dot(z) + z.dot()) /) * z.conjugate()
      ) /;

  Real dReż·z´ = real(ż.dot(z´´) + ż´.dot());

  Vector<Scalar> ddtdLdz´ = - (
      (
        ż´.conjugate() - (
          Reż·z´ * z´´.conjugate() + dReż·z´ *.conjugate()
          - (Reż·z´ / z´²) * (z´´.dot() +.dot(z´´)) *.conjugate() 
          ) / z´²
        )
      - 0.5 * (
        (ż.dot(ż´) + ż´.dot(ż)) / ż² + (z´´.dot() +.dot(z´´)) / z´²
        ) * (ż.conjugate() - Reż·z´ / z´² *.conjugate())
      ) / sqrt(ż² * z´²) / 2;

  return dLdz - ddtdLdz´;
}

template <class Scalar>
class Rope {
  public:
    std::vector<Vector<Scalar>> z;

    Rope(unsigned N) : z(N + 2) {}

    template <int p>
    Rope(unsigned N, const Vector<Scalar>& z1, const Vector<Scalar>& z2, const Tensor<Scalar, p>& J) : z(N + 2) {
      Scalar H1, H2;
      std::tie(H1, std::ignore, std::ignore) = hamGradHess(J, z1);
      std::tie(H2, std::ignore, std::ignore) = hamGradHess(J, z2);

      if (real(H1) > real(H2)) {
        z.front() = z1;
        z.back() = z2;
      } else {
        z.front() = z2;
        z.back() = z1;
      }

      for (unsigned i = 0; i < N + 2; i++) {
        z[i] = normalize(z.front() + (z.back() - z.front()) * ((Real)i / (N + 1.0)));
      }
    }

    unsigned n() const {
      return z.size() - 2;
    }

    Real length() const {
      Real l = 0;

      for (unsigned i = 0; i < z.size() - 1; i++) {
        l += (z[i + 1] - z[i]).norm();
      }

      return l;
    }

    template <int p>
    Real error(const Tensor<Scalar, p>& J) const {
      Scalar H0, HN;
      std::tie(H0, std::ignore, std::ignore) = hamGradHess(J, z.front());
      std::tie(HN, std::ignore, std::ignore) = hamGradHess(J, z.back());

      Real ImH = imag((H0 + HN) / 2.0);

      Real err = 0;

      for (unsigned i = 1; i < z.size() - 1; i++) {
        Scalar Hi;
        std::tie(Hi, std::ignore, std::ignore) = hamGradHess(J, z[i]);

        err += pow(imag(Hi) - ImH, 2);
      }

      return sqrt(err);
    }

    Vector<Scalar> dz(unsigned i) const {
      return z[i + 1] - z[i - 1];
    }

    Vector<Scalar> ddz(unsigned i) const {
      return 4.0 * (z[i + 1] + z[i - 1] - 2.0 * z[i]);
    }

    template <int p>
    std::vector<Vector<Scalar>> generateGradientδz(const Tensor<Scalar, p>& J) const {
      std::vector<Vector<Scalar>> δz(z.size());

#pragma omp parallel for
      for (unsigned i = 1; i < z.size() - 1; i++) {
        Vector<Scalar> dH;
        Matrix<Scalar> ddH;
        std::tie(std::ignore, dH, ddH) = hamGradHess(J, z[i]);

        δz[i] = variation(z[i], dz(i), ddz(i), dH, ddH);
      }

      for (unsigned i = 1; i < z.size() - 1; i++) {
        δz[i] = δz[i].conjugate() - (δz[i].dot(z[i]) / z[i].squaredNorm()) * z[i].conjugate();
//        δz[i] = δz[i] - ((δz[i].conjugate().dot(dz(i))) / dz(i).squaredNorm()) * dz(i).conjugate();
      }

      // We return a δz with average norm of one.
      Real mag = 0;
      for (unsigned i = 1; i < z.size() - 1; i++) {
        mag += δz[i].norm();
      }

      for (unsigned i = 1; i < z.size() - 1; i++) {
        δz[i] /= mag / n();
      }

      return δz;
    }

    template <int p>
    std::vector<Vector<Scalar>> generateDiscreteGradientδz(const Tensor<Scalar, p>& J, Real γ) const {
      std::vector<Vector<Scalar>> δz(z.size());

      std::vector<Vector<Scalar>> ż(z.size());
      std::vector<Vector<Scalar>> dH(z.size());
      std::vector<Matrix<Scalar>> ddH(z.size());

#pragma omp parallel for
      for (unsigned i = 1; i < z.size() - 1; i++) {
        std::tie(std::ignore, dH[i], ddH[i]) = hamGradHess(J, z[i]);
        ż[i] = zDot(z[i], dH[i]);
      }

#pragma omp parallel for
      for (unsigned i = 1; i < z.size() - 1; i++) {
        Real z² = z[i].squaredNorm();
        Matrix<Scalar>= (dH[i].conjugate() - (dH[i].dot(z[i]) /) * z[i].conjugate()) * z[i].adjoint() /;
        Matrix<Scalar> dżc = -ddH[i] + (ddH[i] * z[i].conjugate()) * z[i].transpose() /+ (z[i].dot(dH[i]) /) * (
              Matrix<Scalar>::Identity(ddH[i].rows(), ddH[i].cols()) - z[i].conjugate() * z[i].transpose() /);

        Vector<Scalar> dC = -0.5 * (dżc * dz(i) +* dz(i).conjugate() - (dżc * ż[i] +* ż[i].conjugate()) * real(ż[i].dot(dz(i))) / ż[i].squaredNorm()) / (ż[i].norm() * dz(i).norm());

        if (i > 1) {
          dC += -0.5 * (ż[i - 1].conjugate() - dz(i - 1).conjugate() * real(ż[i - 1].dot(dz(i - 1))) / dz(i - 1).squaredNorm()) / (ż[i - 1].norm() * dz(i - 1).norm());
        }

        if (i < z.size() - 2) {
          dC +=  0.5 * (ż[i + 1].conjugate() - dz(i + 1).conjugate() * real(ż[i + 1].dot(dz(i + 1))) / dz(i + 1).squaredNorm()) / (ż[i + 1].norm() * dz(i + 1).norm());
        }

        dC += - γ * (z[i - 1] + z[i + 1]).conjugate();

        δz[i] = dC;
      }

      Real size = 0;
      for (unsigned i = 1; i < z.size() - 1; i++) {
        δz[i] = δz[i].conjugate() - (δz[i].dot(z[i]) / z[i].squaredNorm()) * z[i].conjugate();
      }

      return δz;
    }

    template<class Gen>
    std::vector<Vector<Scalar>> generateRandomδz(Gen& r) const {
      std::vector<Vector<Scalar>> δz(z.size());

      complex_normal_distribution<> d(0, 1, 0);
      for (unsigned i = 1; i < z.size() - 1; i++) {
        δz[i] = randomVector<Scalar>(z[0].size(), d, r);
      }

      return δz;
    }

    template<int p>
    Real perturb(const Tensor<Scalar, p>& J, Real δ₀, const std::vector<Vector<Scalar>>& δz, Real γ = 0) {
      Rope rNew = *this;
      Real δ = δ₀;
      // We rescale the step size by the distance between points.
      Real Δl = length() / (n() + 1);

      while (true) {
        for (unsigned i = 1; i < z.size() - 1; i++) {
          rNew.z[i] = normalize(z[i] - (δ * Δl) * δz[i]);
        }

        rNew.spread();

        if (rNew.cost(J, γ) < cost(J, γ)) {
          break;
        } else {
          δ /= 2;
        }

        if (δ < 1e-15) {
          throw ropeRelaxationStallException();
        }
      }

      z = rNew.z;

      return δ;
    }

    void spread() {
      Real l = length();

      Real a = 0;
      unsigned pos = 0;

      std::vector<Vector<Scalar>> zNew = z;

      for (unsigned i = 1; i < z.size() - 1; i++) {
        Real b = i * l / (z.size() - 1);

        while (b > a) {
          pos++;
          a += (z[pos] - z[pos - 1]).norm();
        } 

        Vector<Scalar> δz = z[pos] - z[pos - 1];

        zNew[i] = normalize(z[pos] - (a - b) / δz.norm() * δz);
      }

      z = zNew;
    }

    template <int p>
    void relaxGradient(const Tensor<Scalar, p>& J, unsigned N, Real δ0) {
      Real δ = δ0;
      try {
        for (unsigned i = 0; i < N; i++) {
          std::vector<Vector<Scalar>> δz = generateGradientδz(J);
          δ = 1.1 * perturb(J, δ, δz);
        }
      } catch (std::exception& e) {
      }
    }

    template <int p>
    void relaxDiscreteGradient(const Tensor<Scalar, p>& J, unsigned N, Real δ0, Real γ) {
      Real δ = δ0;
      try {
        for (unsigned i = 0; i < N; i++) {
          std::vector<Vector<Scalar>> δz = generateDiscreteGradientδz(J, γ);
          δ = 1.1 * perturb(J, δ, δz, γ);
        }
      } catch (std::exception& e) {
      }
    }

    template <int p, class Gen>
    void relaxRandom(const Tensor<Scalar, p>& J, unsigned N, Real δ0, Gen& r) {
      Real δ = δ0;
        for (unsigned i = 0; i < N; i++) {
          try {
            std::vector<Vector<Scalar>> δz = generateRandomδz(r);
            δ = 1.1 * perturb(J, δ, δz);
          } catch (std::exception& e) {
        }
      }
    }

    template <int p>
    Real cost(const Tensor<Scalar, p>& J, Real γ = 0) const {
      Real c = 0;

      for (unsigned i = 1; i < z.size() - 1; i++) {
        Vector<Scalar> dH;
        std::tie(std::ignore, dH, std::ignore) = hamGradHess(J, z[i]);

        Vector<Scalar> zD = zDot(z[i], dH);

        c += 1.0 - real(zD.dot(dz(i))) / zD.norm() / dz(i).norm();
      }

      for (unsigned i = 0; i < z.size() - 1; i++) {
        c += γ * (z[i + 1] - z[i]).squaredNorm();
      }

      return c;
    }

    Rope<Scalar> interpolate() const {
      Rope<Scalar> r(2 * n() + 1);

      for (unsigned i = 0; i < z.size(); i++) {
        r.z[2 * i] = z[i];
      }

      for (unsigned i = 0; i < z.size() - 1; i++) {
        r.z[2 * i + 1] = normalize(((z[i] + z[i + 1]) / 2.0));
      }

      return r;
    }
};