8 const std::vector<std::shared_ptr<AugmentedLagrangianForm>> &alagr_form,
9 const double initial_al_weight,
11 const double max_al_weight,
13 const std::function<
void(
const Eigen::VectorXd &)> &update_barrier_stiffness)
14 : alagr_forms{alagr_form},
15 initial_al_weight(initial_al_weight),
17 max_al_weight(max_al_weight),
19 update_barrier_stiffness(update_barrier_stiffness)
24 const json &nl_solver_params,
25 const json &linear_solver,
26 const double characteristic_length,
27 std::shared_ptr<polysolve::nonlinear::Solver> nl_solverin)
29 assert(sol.size() == nl_problem.
full_size());
31 const Eigen::VectorXd initial_sol = sol;
40 double initial_error = 0;
42 initial_error += f->compute_error(sol);
48 f->set_initial_weight(al_weight);
50 double current_error = 0;
52 current_error += f->compute_error(sol);
54 logger().debug(
"Initial error = {}", current_error);
56 while (!std::isfinite(nl_problem.
value(tmp_sol))
63 logger().debug(
"Solving AL Problem with weight {}", al_weight);
72 auto nl_solver = nl_solverin ==
nullptr ? polysolve::nonlinear::Solver::create(
73 nl_solver_params, linear_solver, characteristic_length * scale,
logger())
75 nl_solver->minimize(nl_problem, tmp_sol);
78 catch (
const std::runtime_error &e)
80 std::string err_msg = e.what();
82 if (err_msg.find(
"f(x) is nan or inf; stopping") != std::string::npos)
84 if (err_msg.find(
"Reached iteration limit") != std::string::npos)
92 current_error += f->compute_error(sol);
93 logger().debug(
"Current error = {}", current_error);
94 const double eta = 1 - sqrt(current_error / initial_error);
96 logger().debug(
"Current eta = {}", eta);
100 logger().debug(
"Higher error than initial, increase weight and revert to previous solution");
112 f->update_lagrangian(sol, al_weight);
121 const json &nl_solver_params,
122 const json &linear_solver,
123 const double characteristic_length,
124 std::shared_ptr<polysolve::nonlinear::Solver> nl_solverin)
126 assert(sol.size() == nl_problem.
full_size());
132 if (!std::isfinite(nl_problem.
value(tmp_sol))
135 log_and_throw_error(
"Failed to apply constraints conditions; solve with augmented lagrangian first!");
140 logger().debug(
"Successfully applied constraints conditions; solving in reduced space");
142 nl_problem.
init(sol);
147 auto nl_solver = nl_solverin ==
nullptr ? polysolve::nonlinear::Solver::create(
148 nl_solver_params, linear_solver, characteristic_length * scale,
logger())
150 nl_solver->minimize(nl_problem, tmp_sol);
153 catch (
const std::runtime_error &e)
void solve_reduced(NLProblem &nl_problem, Eigen::MatrixXd &sol, std::shared_ptr< polysolve::nonlinear::Solver > nl_solver)
const double max_al_weight
std::vector< std::shared_ptr< AugmentedLagrangianForm > > alagr_forms
std::function< void(const Eigen::VectorXd &)> update_barrier_stiffness
ALSolver(const std::vector< std::shared_ptr< AugmentedLagrangianForm > > &alagr_form, const double initial_al_weight, const double scaling, const double max_al_weight, const double eta_tol, const std::function< void(const Eigen::VectorXd &)> &update_barrier_stiffness)
std::function< void(const double)> post_subsolve
const double initial_al_weight
void solve_al(NLProblem &nl_problem, Eigen::MatrixXd &sol, std::shared_ptr< polysolve::nonlinear::Solver > nl_solver)
virtual void line_search_end() override
virtual void init(const TVector &x0) override
void line_search_begin(const TVector &x0, const TVector &x1) override
double normalize_forms() override
virtual bool is_step_valid(const TVector &x0, const TVector &x1) override
TVector full_to_reduced(const TVector &full) const
virtual bool is_step_collision_free(const TVector &x0, const TVector &x1) override
TVector reduced_to_full(const TVector &reduced) const
virtual double value(const TVector &x) override
spdlog::logger & logger()
Retrieves the current logger.
void log_and_throw_error(const std::string &msg)