45 #include <ilqgames/dynamics/multi_player_dynamical_system.h> 46 #include <ilqgames/dynamics/multi_player_integrable_system.h> 47 #include <ilqgames/solver/augmented_lagrangian_solver.h> 48 #include <ilqgames/solver/game_solver.h> 49 #include <ilqgames/solver/ilq_solver.h> 50 #include <ilqgames/solver/lq_feedback_solver.h> 51 #include <ilqgames/solver/lq_open_loop_solver.h> 52 #include <ilqgames/solver/lq_solver.h> 53 #include <ilqgames/solver/problem.h> 54 #include <ilqgames/solver/solver_params.h> 55 #include <ilqgames/utils/linear_dynamics_approximation.h> 56 #include <ilqgames/utils/loop_timer.h> 57 #include <ilqgames/utils/operating_point.h> 58 #include <ilqgames/utils/quadratic_cost_approximation.h> 59 #include <ilqgames/utils/solver_log.h> 60 #include <ilqgames/utils/strategy.h> 61 #include <ilqgames/utils/types.h> 63 #include <glog/logging.h> 72 std::shared_ptr<SolverLog> AugmentedLagrangianSolver::Solve(
bool* success,
74 if (success) *success =
true;
77 const auto& initial_op = problem_->CurrentOperatingPoint();
78 const auto& initial_strategies = problem_->CurrentStrategies();
81 std::shared_ptr<SolverLog> log = CreateNewLog();
85 const Time max_runtime_unconstrained_problem =
86 (problem_->IsConstrained())
87 ? max_runtime / static_cast<Time>(params_.max_solver_iters)
91 bool unconstrained_success =
false;
92 const auto unconstrained_log = unconstrained_solver_->Solve(
93 &unconstrained_success, max_runtime_unconstrained_problem);
94 log->AddLog(*unconstrained_log);
96 VLOG_IF(2, !unconstrained_success)
97 <<
"Unconstrained solver failed on first call.";
98 VLOG_IF(2, unconstrained_success)
99 <<
"Unconstrained solver succeeded on first call.";
100 if (success) *success &= unconstrained_success;
103 if (!problem_->IsConstrained())
return log;
106 Time elapsed = max_runtime_unconstrained_problem;
107 float max_constraint_error = constants::kInfinity;
108 while (log->NumIterates() < params_.max_solver_iters &&
109 max_constraint_error > params_.constraint_error_tolerance &&
110 elapsed < max_runtime - timer_.RuntimeUpperBound()) {
116 max_constraint_error = -constants::kInfinity;
117 const OperatingPoint& op = log->FinalOperatingPoint();
118 for (
auto& pc : problem_->PlayerCosts()) {
119 for (
size_t kk = 0; kk < op.xs.size(); kk++) {
120 const Time t = op.t0 + time::kTimeStep *
static_cast<float>(kk);
121 const auto& x = op.xs[kk];
122 const auto& us = op.us[kk];
125 for (
const auto& constraint : pc.StateConstraints()) {
126 const float constraint_error = constraint->Evaluate(t, x);
127 max_constraint_error =
128 std::max(max_constraint_error, constraint_error);
129 constraint->IncrementLambda(t, constraint_error);
132 for (
const auto& pair : pc.ControlConstraints()) {
133 const float constraint_error =
134 pair.second->Evaluate(t, us[pair.first]);
135 max_constraint_error =
136 std::max(max_constraint_error, constraint_error);
137 pair.second->IncrementLambda(t, constraint_error);
143 Constraint::ScaleMu(params_.geometric_mu_scaling);
146 VLOG(2) <<
"Max constraint violation at iteration " << log->NumIterates()
147 <<
" is " << max_constraint_error;
151 if (unconstrained_success) {
152 problem_->OverwriteSolution(log->FinalOperatingPoint(),
153 log->FinalStrategies());
159 const auto unconstrained_log = unconstrained_solver_->Solve(
160 &unconstrained_success, max_runtime_unconstrained_problem);
162 VLOG_IF(2, unconstrained_success)
163 <<
"Unconstrained solver succeeded on iteration " << log->NumIterates();
166 if (!unconstrained_success) {
167 VLOG(2) <<
"Unconstrained solver failed at iteration " 168 << log->NumIterates();
169 VLOG(2) <<
"Downscaling all multipliers.";
170 for (
auto& pc : problem_->PlayerCosts()) {
171 for (
const auto& constraint : pc.StateConstraints())
172 constraint->ScaleLambdas(params_.geometric_lambda_downscaling);
173 for (
const auto& pair : pc.ControlConstraints())
174 pair.second->ScaleLambdas(params_.geometric_lambda_downscaling);
177 Constraint::ScaleMu(params_.geometric_mu_downscaling);
180 if (success) *success &= unconstrained_success;
181 log->AddLog(*unconstrained_log);
184 elapsed += timer_.Toc();
188 if (max_constraint_error > params_.constraint_error_tolerance) {
189 LOG(WARNING) <<
"Solver could not satisfy all constraints.";
190 if (success) *success =
false;
194 if (params_.reset_problem)
195 problem_->OverwriteSolution(initial_op, initial_strategies);
198 if (params_.reset_lambdas) {
199 for (
auto& pc : problem_->PlayerCosts()) {
200 for (
const auto& constraint : pc.StateConstraints())
201 constraint->ScaleLambdas(constants::kDefaultLambda);
202 for (
const auto& pair : pc.ControlConstraints())
203 pair.second->ScaleLambdas(constants::kDefaultLambda);
207 if (params_.reset_mu) Constraint::GlobalMu() = constants::kDefaultMu;