From 95df02c90a455c2e539e795acb1921b688e8bc66 Mon Sep 17 00:00:00 2001 From: Jaron Kent-Dobias Date: Wed, 17 Feb 2021 15:46:00 +0100 Subject: Algorithmic tweaks to rope relaxation now suceeds in finding Stokes lines relatively quickly. --- stokes.hpp | 73 ++++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 57 insertions(+), 16 deletions(-) (limited to 'stokes.hpp') diff --git a/stokes.hpp b/stokes.hpp index 6a00637..95bb9c4 100644 --- a/stokes.hpp +++ b/stokes.hpp @@ -1,6 +1,4 @@ #include "p-spin.hpp" -#include -#include "dynamics.hpp" template Vector zDot(const Vector& z, const Vector& dH) { @@ -13,6 +11,12 @@ double segmentCost(const Vector& z, const Vector& dz, const Vect return 1.0 - pow(real(zD.dot(dz)), 2) / zD.squaredNorm() / dz.squaredNorm(); } +class ropeRelaxationStallException: public std::exception { + virtual const char* what() const throw() { + return "Gradient descent stalled."; + } +}; + template Vector variation(const Vector& z, const Vector& z´, const Vector& z´´, const Vector& dH, const Matrix& ddH) { double z² = z.squaredNorm(); @@ -78,29 +82,58 @@ class Rope { } template - void perturb(const Tensor& J, double δ) { + std::vector> δz(const Tensor& J) const { + std::vector> δz(z.size()); + +#pragma omp parallel for for (unsigned i = 1; i < z.size() - 1; i++) { Vector dH; Matrix ddH; std::tie(std::ignore, dH, ddH) = hamGradHess(J, z[i]); - Vector δz = variation(z[i], this->dz(i), this->ddz(i), dH, ddH); + δz[i] = variation(z[i], this->dz(i), this->ddz(i), dH, ddH); + } - z[i] -= δ * δz.conjugate(); - z[i] = normalize(z[i]); + return δz; + } + + template + double perturb(const Tensor& J, double δ0) { + std::vector> Δz = this->δz(J); + + Rope rNew = *this; + double δ = δ0; + + while (rNew.cost(J) >= this->cost(J)) { + for (unsigned i = 1; i < z.size() - 1; i++) { + rNew.z[i] = z[i] - δ * Δz[i].conjugate(); + rNew.z[i] = normalize(rNew.z[i]); + } + + δ /= 2; + + if (δ < 1e-30) { + throw ropeRelaxationStallException(); + } } + + z = rNew.z; + + return δ; } void spread() { double l = 0; - for (unsigned i= 0; i < z.size() - 1; i++) { + for (unsigned i = 0; i < z.size() - 1; i++) { l += (z[i + 1] - z[i]).norm(); } double a = 0; unsigned pos = 0; + std::vector> zNew = z; + for (unsigned i = 1; i < z.size() - 1; i++) { double b = i * l / (z.size() - 1); @@ -111,15 +144,19 @@ class Rope { Vector δz = z[pos] - z[pos - 1]; - z[i] = z[pos] - (a - b) / δz.norm() * δz; - z[i] = normalize(z[i]); + zNew[i] = z[pos] - (a - b) / δz.norm() * δz; + zNew[i] = normalize(zNew[i]); } + + z = zNew; } template - void step(const Tensor& J, double δ) { - this->perturb(J, δ); + double step(const Tensor& J, double δ) { + double δNew = this->perturb(J, δ); this->spread(); + + return δNew; } template @@ -137,21 +174,25 @@ class Rope { } template - void relax(const Tensor& J, unsigned N, double δ) { - for (unsigned i = 0; i < N; i++) { - this->step(J, δ); + void relax(const Tensor& J, unsigned N, double δ0) { + double δ = δ0; + try { + for (unsigned i = 0; i < N; i++) { + δ = 2 * this->step(J, δ); + } + } catch (std::exception& e) { } } Rope interpolate() const { - Rope r(2 * z.size() - 1, z[0], z[z.size() - 1]); + Rope r(2 * (z.size() - 2) + 1, z[0], z[z.size() - 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] = (z[i] + z[i + 1]) / 2.0; + r.z[2 * i + 1] = normalize(((z[i] + z[i + 1]) / 2.0).eval()); } return r; -- cgit v1.2.3-54-g00ecf