|
| 1 | +#pragma once |
| 2 | +#include <functional> |
| 3 | +#include <algorithm> |
| 4 | +#include <vector> |
| 5 | + |
| 6 | +#include <Eigen/Dense> |
| 7 | + |
| 8 | +#include "solver_types.h" |
| 9 | +#include "Context.h" |
| 10 | + |
| 11 | +namespace symx |
| 12 | +{ |
| 13 | + static auto default_residual = [](const Eigen::VectorXd& r) { return r.cwiseAbs().maxCoeff(); }; |
| 14 | + |
| 15 | + /* |
| 16 | + Holds user-registered callbacks that the Newton solver invokes |
| 17 | + at well-defined points during the solve. Callbacks are registered |
| 18 | + via add_*() and run via run_*() (called internally by the solver). |
| 19 | + */ |
| 20 | + class SolverCallbacks |
| 21 | + { |
| 22 | + private: |
| 23 | + /* Fields */ |
| 24 | + std::vector<std::function<void()>> before_energy_evaluation; |
| 25 | + std::vector<std::function<void()>> before_step; |
| 26 | + std::vector<std::function<void()>> after_step; |
| 27 | + std::vector<std::function<bool()>> is_initial_state_valid; |
| 28 | + std::vector<std::function<bool()>> is_intermediate_state_valid; |
| 29 | + std::vector<std::function<void()>> on_intermediate_state_invalid; |
| 30 | + std::vector<std::function<void()>> on_armijo_fail; |
| 31 | + std::vector<std::function<bool()>> is_converged; |
| 32 | + std::vector<std::function<bool()>> is_converged_state_valid; |
| 33 | + std::vector<std::function<double()>> max_allowed_step; |
| 34 | + std::function<double(const Eigen::VectorXd&)> residual = default_residual; |
| 35 | + spContext context = nullptr; |
| 36 | + |
| 37 | + /* Internal helpers */ |
| 38 | + void _run(const std::vector<std::function<void()>>& fs) |
| 39 | + { |
| 40 | + for (auto& f : fs) { |
| 41 | + f(); |
| 42 | + } |
| 43 | + } |
| 44 | + bool _run_bool(bool default_bool, const std::vector<std::function<bool()>>& fs) |
| 45 | + { |
| 46 | + bool valid = default_bool; |
| 47 | + for (auto& f : fs) { |
| 48 | + valid = valid && f(); |
| 49 | + } |
| 50 | + return valid; |
| 51 | + } |
| 52 | + |
| 53 | + public: |
| 54 | + /* Construction */ |
| 55 | + SolverCallbacks(spContext context) : context(context) {} |
| 56 | + static std::shared_ptr<SolverCallbacks> create(spContext context) { return std::make_shared<SolverCallbacks>(context); } |
| 57 | + |
| 58 | + // ---- Register callbacks ---- |
| 59 | + |
| 60 | + void set_residual(std::function<double(const Eigen::VectorXd&)> f) { this->residual = f; } |
| 61 | + |
| 62 | + /// Called once per Newton iteration, before energy/gradient/Hessian evaluation. |
| 63 | + void add_before_energy_evaluation(std::function<void()> f) { this->before_energy_evaluation.push_back(f); } |
| 64 | + |
| 65 | + /// Called at the very start of each Newton iteration (iteration index is 0-based). |
| 66 | + void add_before_step(std::function<void()> f) { this->before_step.push_back(f); } |
| 67 | + |
| 68 | + /// Called at the end of each Newton iteration (including failed/early-exit iterations). |
| 69 | + void add_after_step(std::function<void()> f) { this->after_step.push_back(f); } |
| 70 | + |
| 71 | + void add_is_initial_state_valid(std::function<bool()> f) { this->is_initial_state_valid.push_back(f); } |
| 72 | + void add_is_intermediate_state_valid(std::function<bool()> f) { this->is_intermediate_state_valid.push_back(f); } |
| 73 | + void add_on_intermediate_state_invalid(std::function<void()> f) { this->on_intermediate_state_invalid.push_back(f); } |
| 74 | + void add_on_armijo_fail(std::function<void()> f) { this->on_armijo_fail.push_back(f); } |
| 75 | + void add_is_converged(std::function<bool()> f) { this->is_converged.push_back(f); } |
| 76 | + void add_is_converged_state_valid(std::function<bool()> f) { this->is_converged_state_valid.push_back(f); } |
| 77 | + void add_max_allowed_step(std::function<double()> f) { this->max_allowed_step.push_back(f); } |
| 78 | + |
| 79 | + // ---- Invoke callbacks (called by the solver) ---- |
| 80 | + |
| 81 | + void run_before_energy_evaluation() |
| 82 | + { |
| 83 | + auto _t = this->context->logger->time("before_energy_evaluation"); |
| 84 | + this->_run(this->before_energy_evaluation); |
| 85 | + } |
| 86 | + void run_before_step() |
| 87 | + { |
| 88 | + auto _t = this->context->logger->time("before_step"); |
| 89 | + this->_run(this->before_step); |
| 90 | + } |
| 91 | + void run_after_step() |
| 92 | + { |
| 93 | + auto _t = this->context->logger->time("after_step"); |
| 94 | + this->_run(this->after_step); |
| 95 | + } |
| 96 | + bool run_is_initial_state_valid() |
| 97 | + { |
| 98 | + auto _t = this->context->logger->time("is_initial_state_valid"); |
| 99 | + return this->_run_bool(true, this->is_initial_state_valid); |
| 100 | + } |
| 101 | + bool run_is_intermediate_state_valid() |
| 102 | + { |
| 103 | + auto _t = this->context->logger->time("is_intermediate_state_valid"); |
| 104 | + return this->_run_bool(true, this->is_intermediate_state_valid); |
| 105 | + } |
| 106 | + void run_on_intermediate_state_invalid() |
| 107 | + { |
| 108 | + auto _t = this->context->logger->time("on_intermediate_state_invalid"); |
| 109 | + this->_run(this->on_intermediate_state_invalid); |
| 110 | + } |
| 111 | + void run_on_armijo_fail() |
| 112 | + { |
| 113 | + auto _t = this->context->logger->time("on_armijo_fail"); |
| 114 | + this->_run(this->on_armijo_fail); |
| 115 | + } |
| 116 | + bool run_is_converged() |
| 117 | + { |
| 118 | + auto _t = this->context->logger->time("is_converged"); |
| 119 | + bool converged = false; |
| 120 | + for (auto& f : this->is_converged) { |
| 121 | + converged = f() || converged; |
| 122 | + } |
| 123 | + return converged; |
| 124 | + } |
| 125 | + bool run_is_converged_state_valid() |
| 126 | + { |
| 127 | + auto _t = this->context->logger->time("is_converged_state_valid"); |
| 128 | + return this->_run_bool(true, this->is_converged_state_valid); |
| 129 | + } |
| 130 | + double run_max_allowed_step() |
| 131 | + { |
| 132 | + auto _t = this->context->logger->time("max_allowed_step"); |
| 133 | + double max_step = 1.0; |
| 134 | + for (auto f : this->max_allowed_step) { |
| 135 | + max_step = std::min(max_step, f()); |
| 136 | + } |
| 137 | + return max_step; |
| 138 | + } |
| 139 | + |
| 140 | + // ---- Residual computation ---- |
| 141 | + double compute_residual(const Eigen::VectorXd& r) { return this->residual(r); } |
| 142 | + }; |
| 143 | + |
| 144 | + using spSolverCallbacks = std::shared_ptr<SolverCallbacks>; |
| 145 | +} |
0 commit comments