From 4438ac2478ee8a3ff59bc841176c499bd9ad7621 Mon Sep 17 00:00:00 2001 From: Huangzizhou Date: Mon, 27 May 2024 23:15:15 -0400 Subject: [PATCH 1/4] enable slim --- src/polysolve/nonlinear/Problem.hpp | 2 ++ src/polysolve/nonlinear/Solver.cpp | 8 +++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/polysolve/nonlinear/Problem.hpp b/src/polysolve/nonlinear/Problem.hpp index 8c50113..221305b 100644 --- a/src/polysolve/nonlinear/Problem.hpp +++ b/src/polysolve/nonlinear/Problem.hpp @@ -89,6 +89,8 @@ namespace polysolve::nonlinear /// @param new_x New solution. virtual void solution_changed(const TVector &new_x) {} + virtual bool smooth_step(const TVector &x0, const TVector &x1) { return false; } + /// @brief Callback function used to determine if the solver should stop. /// @param state Current state of the solver. /// @param x Current solution. diff --git a/src/polysolve/nonlinear/Solver.cpp b/src/polysolve/nonlinear/Solver.cpp index 990c7b9..f728650 100644 --- a/src/polysolve/nonlinear/Solver.cpp +++ b/src/polysolve/nonlinear/Solver.cpp @@ -427,7 +427,13 @@ namespace polysolve::nonlinear continue; } - x += rate * delta_x; + { + TVector x1 = x + rate * delta_x; + if (objFunc.smooth_step(x, x1)) + objFunc.solution_changed(x1); + x = x1; + } + old_energy = energy; // Reset this for the next iterations From e7e41add311f86d91b313c1fef88a09e3c8bf278 Mon Sep 17 00:00:00 2001 From: Huangzizhou Date: Fri, 7 Jun 2024 14:25:12 -0400 Subject: [PATCH 2/4] one side derivatives --- src/polysolve/nonlinear/Solver.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/polysolve/nonlinear/Solver.cpp b/src/polysolve/nonlinear/Solver.cpp index f728650..ef271d9 100644 --- a/src/polysolve/nonlinear/Solver.cpp +++ b/src/polysolve/nonlinear/Solver.cpp @@ -580,6 +580,7 @@ namespace polysolve::nonlinear void Solver::verify_gradient(Problem &objFunc, const TVector &x, const TVector &grad) { bool match = false; + double J = objFunc(x); switch (gradient_fd_strategy) { @@ -598,15 +599,17 @@ namespace polysolve::nonlinear double J1 = objFunc(x1); double fd = (J2 - J1) / 2 / gradient_fd_eps; + double fd_right = (J2 - J) / gradient_fd_eps; + double fd_left = (J - J1) / gradient_fd_eps; double analytic = direc.dot(grad); match = abs(fd - analytic) < 1e-8 || abs(fd - analytic) < 1e-4 * abs(analytic); // Log error in either case to make it more visible in the logs. if (match) - m_logger.debug("step size: {}, finite difference: {}, derivative: {}", gradient_fd_eps, fd, analytic); + m_logger.debug("step size: {}, finite difference: {} {} {}, derivative: {}", gradient_fd_eps, fd, fd_left, fd_right, analytic); else - m_logger.error("step size: {}, finite difference: {}, derivative: {}", gradient_fd_eps, fd, analytic); + m_logger.error("step size: {}, finite difference: {} {} {}, derivative: {}", gradient_fd_eps, fd, fd_left, fd_right, analytic); } break; case FiniteDiffStrategy::FULL_FINITE_DIFF: From baa3f105902e7ea2b173c6efed22d5e76726f455 Mon Sep 17 00:00:00 2001 From: ZizhouHuang Date: Thu, 5 Sep 2024 14:55:34 -0400 Subject: [PATCH 3/4] fd -> fd_centered --- src/polysolve/nonlinear/Solver.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/polysolve/nonlinear/Solver.cpp b/src/polysolve/nonlinear/Solver.cpp index ef271d9..5c02141 100644 --- a/src/polysolve/nonlinear/Solver.cpp +++ b/src/polysolve/nonlinear/Solver.cpp @@ -598,18 +598,18 @@ namespace polysolve::nonlinear objFunc.solution_changed(x1); double J1 = objFunc(x1); - double fd = (J2 - J1) / 2 / gradient_fd_eps; + double fd_centered = (J2 - J1) / 2 / gradient_fd_eps; double fd_right = (J2 - J) / gradient_fd_eps; double fd_left = (J - J1) / gradient_fd_eps; double analytic = direc.dot(grad); - match = abs(fd - analytic) < 1e-8 || abs(fd - analytic) < 1e-4 * abs(analytic); + match = abs(fd_centered - analytic) < 1e-8 || abs(fd_centered - analytic) < 1e-4 * abs(analytic); // Log error in either case to make it more visible in the logs. if (match) - m_logger.debug("step size: {}, finite difference: {} {} {}, derivative: {}", gradient_fd_eps, fd, fd_left, fd_right, analytic); + m_logger.debug("step size: {}, finite difference: {} {} {}, derivative: {}", gradient_fd_eps, fd_centered, fd_left, fd_right, analytic); else - m_logger.error("step size: {}, finite difference: {} {} {}, derivative: {}", gradient_fd_eps, fd, fd_left, fd_right, analytic); + m_logger.error("step size: {}, finite difference: {} {} {}, derivative: {}", gradient_fd_eps, fd_centered, fd_left, fd_right, analytic); } break; case FiniteDiffStrategy::FULL_FINITE_DIFF: From 5e8eb3d55591dedb2b2c4c58de4d4da172457d27 Mon Sep 17 00:00:00 2001 From: ZizhouHuang Date: Thu, 12 Sep 2024 13:50:43 -0400 Subject: [PATCH 4/4] smooth_step -> after_line_search_custom_operation --- src/polysolve/nonlinear/Problem.hpp | 2 +- src/polysolve/nonlinear/Solver.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/polysolve/nonlinear/Problem.hpp b/src/polysolve/nonlinear/Problem.hpp index 5a7449d..3245aad 100644 --- a/src/polysolve/nonlinear/Problem.hpp +++ b/src/polysolve/nonlinear/Problem.hpp @@ -90,7 +90,7 @@ namespace polysolve::nonlinear /// @param new_x New solution. virtual void solution_changed(const TVector &new_x) {} - virtual bool smooth_step(const TVector &x0, const TVector &x1) { return false; } + virtual bool after_line_search_custom_operation(const TVector &x0, const TVector &x1) { return false; } /// @brief Callback function used to determine if the solver should stop. /// @param state Current state of the solver. diff --git a/src/polysolve/nonlinear/Solver.cpp b/src/polysolve/nonlinear/Solver.cpp index 5c02141..d16f8dc 100644 --- a/src/polysolve/nonlinear/Solver.cpp +++ b/src/polysolve/nonlinear/Solver.cpp @@ -429,7 +429,7 @@ namespace polysolve::nonlinear { TVector x1 = x + rate * delta_x; - if (objFunc.smooth_step(x, x1)) + if (objFunc.after_line_search_custom_operation(x, x1)) objFunc.solution_changed(x1); x = x1; }