summaryrefslogtreecommitdiff
path: root/least_squares.cpp
blob: 0c53c04af56185434ddcf9e746c6ffc0eb998e47 (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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include <eigen3/Eigen/Dense>
#include <eigen3/unsupported/Eigen/CXX11/Tensor>
#include <eigen3/unsupported/Eigen/CXX11/TensorSymmetry>
#include <getopt.h>

#include "pcg-cpp/include/pcg_random.hpp"
#include "randutils/randutils.hpp"

using Rng = randutils::random_generator<pcg32>;

using Real = double;
using Vector = Eigen::Matrix<Real, Eigen::Dynamic, 1>;
using Matrix = Eigen::Matrix<Real, Eigen::Dynamic, Eigen::Dynamic>;

/* Eigen tensor manipulations are quite annoying, especially with the need to convert other types
 * into tensors beforehand. Here I overload multiplication operators to allow contraction between
 * vectors and the first or last index of a tensor.
 */
class Tensor : public Eigen::Tensor<Real, 3> {
  using Eigen::Tensor<Real, 3>::Tensor;

public:
  Matrix operator*(const Vector& x) const {
    const std::array<Eigen::IndexPair<int>, 1> ip20 = {Eigen::IndexPair<int>(2, 0)};
    const Eigen::Tensor<Real, 1> xT = Eigen::TensorMap<const Eigen::Tensor<Real, 1>>(x.data(), x.size());
    const Eigen::Tensor<Real, 2> JxT = contract(xT, ip20);
    return Eigen::Map<const Matrix>(JxT.data(), dimension(0), dimension(1));
  }

  Tensor operator+(const Eigen::Tensor<Real, 3>& J) const {
    return Eigen::Tensor<Real, 3>::operator+(J);
  }
};

Matrix operator*(const Eigen::Matrix<Real, 1, Eigen::Dynamic>& x, const Tensor& J) {
  const std::array<Eigen::IndexPair<int>, 1> ip00 = {Eigen::IndexPair<int>(0, 0)};
  const Eigen::Tensor<Real, 1> xT = Eigen::TensorMap<const Eigen::Tensor<Real, 1>>(x.data(), x.size());
  const Eigen::Tensor<Real, 2> JxT = J.contract(xT, ip00);
  return Eigen::Map<const Matrix>(JxT.data(), J.dimension(1), J.dimension(2));
}

Vector normalize(const Vector& x) {
  return x * sqrt((Real)x.size() / x.squaredNorm());
}

Vector makeTangent(const Vector& v, const Vector& x) {
  return v - (v.dot(x) / x.size()) * x;
}

Matrix projectionOperator(const Vector& x) {
  return Matrix::Identity(x.size(), x.size()) - (x * x.transpose()) / x.squaredNorm();
}

Real HFromV(const Vector& V) {
  return 0.5 * V.squaredNorm();
}

Vector dHFromVdV(const Vector& V, const Matrix& dV) {
  return V.transpose() * dV;
}

Vector VFromABJ(const Vector& b, const Matrix& A, const Matrix& Jx, const Vector& x) {
  return b + (A + 0.5 * Jx) * x;
}

class QuadraticModel {
private:
  Tensor J;
  Matrix A;
  Vector b;

public:
  unsigned N;
  unsigned M;
  QuadraticModel(unsigned N, unsigned M, Rng& r, double μ1, double μ2, double μ3) : N(N), M(M), J(M, N, N), A(M, N), b(M) {
    Eigen::StaticSGroup<Eigen::Symmetry<1,2>> ssym1;
    for (unsigned k = 0; k < N; k++) {
      for (unsigned j = k; j < N; j++) {
        for (unsigned i = 0; i < M; i++) {
          ssym1(J, i, j, k) = r.variate<Real, std::normal_distribution>(0, sqrt(2) * μ3 / N);
        }
      }
    }

    for (Real& Aij : A.reshaped()) {
      Aij = r.variate<Real, std::normal_distribution>(0, μ2 / sqrt(N));
    }

    for (Real& bi : b) {
      bi = r.variate<Real, std::normal_distribution>(0, μ1);
    }
  }

  std::tuple<Vector, Matrix, const Tensor&> VdVddV(const Vector& x) const {
    Matrix Jx = J * x;
    Vector V = VFromABJ(b, A, Jx, x);
    Matrix dV = A + Jx;

    return {V, dV, J};
  }

  std::tuple<Real, Vector, Matrix> HdHddH(const Vector& x) const {
    auto [V, dV, ddV] = VdVddV(x);

    Real H = HFromV(V);
    Vector dH = dHFromVdV(V, dV);
    Matrix ddH = V.transpose() * ddV + dV.transpose() * dV;

    return {H, dH, ddH};
  }

  std::tuple<Real, Vector> getHamGrad(const Vector& x) const {
    Vector V;
    Matrix dV;
    std::tie(V, dV, std::ignore) = VdVddV(x);

    Real H = HFromV(V);
    Vector dH = makeTangent(dHFromVdV(V, dV), x);

    return {H, dH};
  }

  std::tuple<Real, Vector, Matrix> hamGradHess(const Vector& x) const {
    auto [H, dH, ddH] = HdHddH(x);

    Vector gradH = dH - dH.dot(x) * x / (Real)N;
    Matrix hessH = ddH - (dH * x.transpose() + x.dot(dH) * Matrix::Identity(N, N) + (ddH * x) * x.transpose()) / (Real)N  + 2.0 * x * x.transpose();

    return {H, gradH, hessH};
  }

  /* Unfortunately benchmarking indicates that ignorning entries of a returned tuple doesn't result
   * in those execution paths getting optimized out. It is much more efficient to compute the
   * energy alone when only the energy is needed.
   */
  Real getHamiltonian(const Vector& x) const {
    return HFromV(VFromABJ(b, A, J * x, x));
  }

  Vector spectrum(const Vector& x) const {
    Matrix hessH;
    std::tie(std::ignore, std::ignore, hessH) = hamGradHess(x);
    Eigen::EigenSolver<Matrix> eigenS(hessH);
    return eigenS.eigenvalues().real();
  }
};

Vector gradientDescent(const QuadraticModel& M, const Vector& x0, Real ε = 1e-7) {
  Vector x = x0;
  Real λ = 10;

  auto [H, g] = M.getHamGrad(x);

  while (g.norm() / M.N > ε && λ > ε) {
    Real HNew;
    Vector xNew, gNew;

    while(
      xNew = normalize(x + λ * g),
      std::tie(HNew, gNew) = M.getHamGrad(xNew),
      HNew < H && λ > ε
    ) {
      λ /= 1.5;
    }

    x = xNew;
    H = HNew;
    g = gNew;

    λ *= 2;
  }

  return x;
}

Vector findMinimum(const QuadraticModel& M, const Vector& x0, Real ε = 1e-5) {
  Vector x = x0;
  Real λ = 100;

  auto [H, g, m] = M.hamGradHess(x0);

  while (λ * ε < 1) {
    Vector dx = (m - λ * (Matrix)m.diagonal().cwiseAbs().asDiagonal()).partialPivLu().solve(g);
    Vector xNew = normalize(x - makeTangent(dx, x));
    Real HNew = M.getHamiltonian(xNew);

    if (HNew > H) {
      x = xNew;
      std::tie(H, g, m) = M.hamGradHess(xNew);

      λ /= 2;
    } else {
      λ *= 1.5;
    }
  }

  return x;
}

Vector findSaddle(const QuadraticModel& M, const Vector& x0, Real ε = 1e-12) {
  Vector x = x0;
  Vector dx, g;
  Matrix m;

  while (
    std::tie(std::ignore, g, m) = M.hamGradHess(x),
    dx = makeTangent(m.partialPivLu().solve(g), x),
    dx.norm() > ε) {
    x = normalize(x - dx);
  }

  return x;
}

Vector metropolisSweep(const QuadraticModel& M, const Vector& x0, Real β, Rng& r, unsigned sweeps = 1, Real δ = 1) {
  Vector x = x0;
  Real H = M.getHamiltonian(x);

  for (unsigned j = 0; j < sweeps; j++) {
    Real rate = 0;

    for (unsigned i = 0; i < M.N; i++) {
      Vector xNew = x;

      for (Real& xNewᵢ : xNew) {
        xNewᵢ += δ * r.variate<Real, std::normal_distribution>();
      }

      xNew = normalize(xNew);

      Real Hnew = M.getHamiltonian(xNew);

      if (exp(-β * (Hnew - H)) > r.uniform<Real>(0.0, 0.1)) {
        x = xNew;
        H = Hnew;
        rate++;
      }
    }

    if (rate / M.N < 0.5) {
      δ /= 1.5;
    } else {
      δ *= 1.5;
    }
  }

  return x;
}

Vector subagAlgorithm(const QuadraticModel& M, Rng& r, unsigned k, unsigned m) {
  Vector σ = Vector::Zero(M.N);
  unsigned axis = r.variate<unsigned, std::uniform_int_distribution>(0, M.N - 1);
  σ(axis) = sqrt(M.N / k);

  for (unsigned i = 0; i < k; i++) {
    auto [H, dH] = M.getHamGrad(σ);
//    Matrix mx = projectionOperator(σ);

    /*
    Matrix hessH = mx.transpose() * ddH * mx;

    Vector v(M.N);
    for (Real& vi : v) {
      vi = r.variate<Real,std::normal_distribution>();
    }
    v = makeTangent(v, σ);
    Real L = hessH.norm();
    for (unsigned j = 0; j < m; j++) {
      Vector vNew = (hessH + sqrt(L) * Matrix::Identity(M.N, M.N)) * v;
      vNew = makeTangent(vNew, σ);
      vNew = vNew / vNew.norm();
      if ((v - vNew).norm() < 1e-6) {
        std::cerr << "Stopped approximation after " << m << " steps" << std::endl;
        v = vNew;
        break;
      }
      v = vNew;
    }

    if (v.dot(dH) < 0) {
      v = -v;
    }
    */

    Vector v = dH / dH.norm();

    σ += sqrt(M.N/k) * v;
  }

  return normalize(σ);
}

int main(int argc, char* argv[]) {
  unsigned N = 10;
  Real α = 1;
  Real σ = 1;
  Real A = 1;
  Real J = 1;
  Real β = 1;
  unsigned sweeps = 10;
  unsigned samples = 10;

  int opt;

  while ((opt = getopt(argc, argv, "N:a:s:A:J:b:S:n:")) != -1) {
    switch (opt) {
    case 'N':
      N = (unsigned)atof(optarg);
      break;
    case 'a':
      α = atof(optarg);
      break;
    case 's':
      σ = atof(optarg);
      break;
    case 'A':
      A = atof(optarg);
      break;
    case 'J':
      J = atof(optarg);
      break;
    case 'b':
      β = atof(optarg);
      break;
    case 'S':
      sweeps = atoi(optarg);
      break;
    case 'n':
      samples = atoi(optarg);
      break;
    default:
      exit(1);
    }
  }

  unsigned M = (unsigned)(α * N);

  Rng r;

  Vector x = Vector::Zero(N);
  x(0) = sqrt(N);

//  std::cout << N << " " << α << " " << β;

  for (unsigned sample = 0; sample < samples; sample++) {
    QuadraticModel leastSquares(N, M, r, σ, A, J);
    if (β != 0) {
      x = metropolisSweep(leastSquares, x, β, r, sweeps);
    }
    x = findMinimum(leastSquares, x);
    std::cout << " " << leastSquares.getHamiltonian(x) / N << std::flush;
    QuadraticModel leastSquares2(N, M, r, σ, A, J);
    Vector σ = subagAlgorithm(leastSquares2, r, N, 15);
    σ = findMinimum(leastSquares2, σ);
    std::cout << " " << leastSquares2.getHamiltonian(σ) / N << std::endl;
  }

  std::cout << std::endl;

  return 0;
}