summaryrefslogtreecommitdiff
path: root/dynamics.hpp
blob: 0597f35864ab0ca637058216157607fb63b5eab3 (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
#pragma once

#include <exception>
#include <eigen3/Eigen/LU>
#include <random>

#include <iostream>

#include "p-spin.hpp"
#include "stereographic.hpp"

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

template <class Real, class Scalar, int p>
std::tuple<Real, Vector<Scalar>> gradientDescent(const Tensor<Scalar, p>& J, const Vector<Scalar>& z0, Real ε, Real γ0 = 1, Real δγ = 2) {
  Vector<Scalar> z = z0;
  Real γ = γ0;

  auto [W, dW] = WdW(J, z);

  while (W > ε) {
    Vector<Scalar> zNew = normalize(z - γ * dW.conjugate());

    auto [WNew, dWNew] = WdW(J, zNew);

    if (WNew < W) { // If the step lowered the objective, accept it!
      z = zNew;
      W = WNew;
      dW = dWNew;
      γ = γ0;
    } else { // Otherwise, shrink the step and try again.
      γ /= δγ;
    }

    if (γ < 1e-50) {
      throw gradientDescentStallException();
    }
  }

  return {W, z};
}

template <class Real, class Scalar, int p>
Vector<Scalar> findSaddle(const Tensor<Scalar, p>& J, const Vector<Scalar>& z0, Real ε, Real δW = 2, Real γ0 = 1, Real δγ = 2) {
  Vector<Scalar> z = z0;

  Vector<Scalar> dH;
  Matrix<Scalar> ddH;
  std::tie(std::ignore, dH, ddH) = hamGradHess(J, z);

  Scalar zz = z.transpose() * z;
  Vector<Scalar> ż = zDot(z, dH) + z * (zz - (Real)z.size());
  Matrix<Scalar>= dzDot(z, dH) + Matrix<Scalar>::Identity(z.size(), z.size()) * (zz - (Real)z.size()) + 2.0 * z * z.transpose();
  Matrix<Scalar> dżc = dzDotConjugate(z, dH, ddH);

  Vector<Scalar> b(2 * z.size());
  Matrix<Scalar> M(2 * z.size(), 2 * z.size());

  b << ż.conjugate(), ż;
  M <<.conjugate(), dżc, dżc.conjugate(),;

  while (ż.norm() > ε) {
    Vector<Scalar> dz = M.partialPivLu().solve(b).tail(z.size());
    dz -= z.conjugate().dot(dz) / z.squaredNorm() * z.conjugate();
    z = normalize(z - dz);

    std::cout << "error : " << z.transpose() * z << " "<< ż.norm() << " " << dz.norm() << std::endl;
    getchar();

    std::tie(std::ignore, dH, ddH) = hamGradHess(J, z);

    zz = z.transpose() * z;
    ż = zDot(z, dH) + z * (zz - (Real)z.size());= dzDot(z, dH) + Matrix<Scalar>::Identity(z.size(), z.size()) * (zz - (Real)z.size()) + 2.0 * z * z.transpose();
    dżc = dzDotConjugate(z, dH, ddH);

    b << ż.conjugate(), ż;
    M <<.conjugate(), dżc, dżc.conjugate(),;
  }

  return z;
}

template <class Scalar, class Distribution, class Generator>
Vector<Scalar> randomVector(unsigned N, Distribution d, Generator& r) {
  Vector<Scalar> z(N);

  for (unsigned i = 0; i < N; i++) {
    z(i) = d(r);
  }

  return z;
}

template <class Real, class Scalar, int p, class Distribution, class Generator>
std::tuple<Real, Vector<Scalar>> metropolis(const Tensor<Scalar, p>& J, const Vector<Scalar>& z0,
    std::function<Real(const Tensor<Scalar, p>&, const Vector<Scalar>&)>& energy,
    Real T, Real γ, unsigned N, Distribution d, Generator& r) {
  Vector<Scalar> z = z0;

  Real E = energy(J, z);

  std::uniform_real_distribution<Real> D(0, 1);

  for (unsigned i = 0; i < N; i++) {
    Vector<Scalar> zNew = normalize(z + γ * randomVector<Scalar>(z.size(), d, r));

    Real ENew = energy(J, zNew);

    if (E - ENew > T * log(D(r))) {
      z = zNew;
      E = ENew;
    }
  }

  return {E, z};
}

template <class Real, class Scalar, int p, class Distribution, class Generator>
Vector<Scalar> randomSaddle(const Tensor<Scalar, p>& J, Distribution d, Generator& r, Real ε) {
  Vector<Scalar> zSaddle;
  bool foundSaddle = false;

  while (!foundSaddle) {
    Vector<Scalar> z0 = normalize(randomVector<Scalar>(J.dimension(0), d, r.engine()));

    try {
      zSaddle = findSaddle(J, z0, ε);
      foundSaddle = true;
    } catch (std::exception& e) {}
  }

  return zSaddle;
}