Skip to content

Commit

Permalink
fixed backtracking
Browse files Browse the repository at this point in the history
  • Loading branch information
teseoch committed Oct 4, 2023
1 parent 64183de commit 6600af7
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/polysolve/nonlinear/Solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,9 @@ namespace polysolve::nonlinear
// timer.getElapsedTimeInSec()
double tot_time = 0;
m_logger.info(
"[{}] Finished: {} Took {:g}s (niters={:d} f={:g} Δf={:g} ‖∇f‖={:g} ‖Δx‖={:g} fdelta={} ftol={})",
"[{}] Finished: {} Took {:g}s (niters={:d} f={:g} Δf={:g} ‖∇f‖={:g} ‖Δx‖={:g} ftol={})",
name(), this->m_status, tot_time, this->m_current.iterations,
old_energy, this->m_current.fDelta, this->m_current.gradNorm, this->m_current.xDelta, this->m_current.fDelta, this->m_stop.fDelta);
old_energy, this->m_current.fDelta, this->m_current.gradNorm, this->m_current.xDelta, this->m_stop.fDelta);

log_times();
update_solver_info(objFunc.value(x));
Expand Down
4 changes: 3 additions & 1 deletion src/polysolve/nonlinear/line_search/Backtracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ namespace polysolve::nonlinear::line_search
TVector grad(x.rows());
objFunc.gradient(x, grad);
const bool use_grad_norm = grad.norm() < this->use_grad_norm_tol;
if (grad.norm() < 1e-30)
return 1;

const double old_energy = use_grad_norm ? grad.squaredNorm() : old_energy_in;

Expand Down Expand Up @@ -168,7 +170,7 @@ namespace polysolve::nonlinear::line_search
m_logger.trace("ls it: {} delta: {} invalid: {} ", this->cur_iter, (cur_energy - old_energy), !is_step_valid);

// if (!std::isfinite(cur_energy) || (cur_energy >= old_energy && fabs(cur_energy - old_energy) > 1e-12) || !is_step_valid)
if (!std::isfinite(cur_energy) || cur_energy > old_energy || !is_step_valid)
if (!std::isfinite(cur_energy) || cur_energy >= old_energy || !is_step_valid)
{
step_size /= 2.0;
// max_step_size should return a collision free step
Expand Down
10 changes: 4 additions & 6 deletions tests/test_nonlinear_solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,19 @@ TEST_CASE("non-linear", "[solver]")
const double characteristic_length = 1;

static std::shared_ptr<spdlog::logger> logger = spdlog::stdout_color_mt("test_logger");
logger->set_level(spdlog::level::trace);
logger->set_level(spdlog::level::warn);
for (auto solver_name : Solver::available_solvers())
{
if (solver_name != "gradient_descent")
continue;
auto solver = Solver::create(solver_name,
solver_params,
linear_solver_params,
dt,
characteristic_length,
*logger);

// for (const auto &ls : line_search::LineSearch::available_methods())
for (const auto &ls : line_search::LineSearch::available_methods())
{
// solver_params["line_search"]["method"] = ls;
solver_params["line_search"]["method"] = ls;

QuadraticProblem::TVector x(prob.size());
x.setZero();
Expand All @@ -113,7 +111,7 @@ TEST_CASE("non-linear", "[solver]")
solver->minimize(prob, x);

const double err = (x - prob.solution()).norm();
// INFO("solver: " + solver_name + " LS: " + ls);
INFO("solver: " + solver_name + " LS: " + ls);
CHECK(err < 1e-8);

x.setRandom();
Expand Down

0 comments on commit 6600af7

Please sign in to comment.