Skip to content

for new experimental branch #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/polysolve/nonlinear/Solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,12 @@ namespace polysolve::nonlinear

m_current.fDeltaCount = (m_current.fDelta < m_stop.fDelta) ? (m_current.fDeltaCount + 1) : 0;

if (m_iteration_callback && m_iteration_callback(m_current))
{
m_status = Status::ObjectiveCustomStop;
m_logger.debug("[{}][{}] Iteration callback decided to stop", descent_strategy_name(), m_line_search->name());
}

m_logger.debug(
"[{}][{}] {} (stopping criteria: {})",
descent_strategy_name(), m_line_search->name(), m_current.print_message(), m_stop.print_message());
Expand Down
8 changes: 6 additions & 2 deletions src/polysolve/nonlinear/Solver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ namespace polysolve::nonlinear
void set_line_search(const json &params);
const json &info() const { return solver_info; }

void set_iteration_callback(std::function<bool(const Criteria &)> callback) { m_iteration_callback = callback; }

/// @brief If true the solver will not throw an error if the maximum number of iterations is reached
bool allow_out_of_iterations = false;

Expand All @@ -87,7 +89,7 @@ namespace polysolve::nonlinear
const std::shared_ptr<line_search::LineSearch> &line_search() const { return m_line_search; };

protected:
/// @brief Compute direction in which the argument should be updated
/// @brief Compute direction in which the argument should be updated
/// @param objFunc Problem to be minimized
/// @param x Current input (n x 1)
/// @param grad Gradient at current step (n x 1)
Expand Down Expand Up @@ -139,7 +141,7 @@ namespace polysolve::nonlinear
// ====================================================================
// Solver state
// ====================================================================

/// @brief Reset the solver at the start of a minimization
/// @param ndof number of degrees of freedom
void reset(const int ndof);
Expand All @@ -151,6 +153,8 @@ namespace polysolve::nonlinear

std::vector<int> m_iter_per_strategy;

std::function<bool(const Criteria &)> m_iteration_callback = nullptr;

// ====================================================================
// Solver info
// ====================================================================
Expand Down
23 changes: 19 additions & 4 deletions src/polysolve/nonlinear/line_search/LineSearch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,23 @@ namespace polysolve::nonlinear::line_search
}

const double nan_free_step_size = step_size;

// TODO: Fix this
const bool use_grad_norm = initial_grad.norm() < use_grad_norm_tol;
const double starting_step_size = step_size;

// ----------------------
// Find descent step size 1
// ----------------------
{
POLYSOLVE_SCOPED_STOPWATCH("energy min in LS", classical_line_search_time, m_logger);
step_size = compute_descent_step_size(x, delta_x, objFunc, use_grad_norm, initial_energy, initial_grad, step_size);
if (std::isnan(step_size))
{
// Superclass::save_sampled_values("failed-line-search-values.csv", x, delta_x, objFunc);
return NaN;
}
}
// -----------------------------
// Find collision-free step size
// -----------------------------
Expand All @@ -133,12 +150,10 @@ namespace polysolve::nonlinear::line_search
if (initial_grad.norm() < 1e-30)
return step_size;

// TODO: Fix this
const bool use_grad_norm = initial_grad.norm() < use_grad_norm_tol;
const double starting_step_size = step_size;


// ----------------------
// Find descent step size
// Find descent step size 2
// ----------------------
{
POLYSOLVE_SCOPED_STOPWATCH("energy min in LS", classical_line_search_time, m_logger);
Expand Down