43 #include <ilqgames/dynamics/multi_player_integrable_system.h> 44 #include <ilqgames/solver/solution_splicer.h> 45 #include <ilqgames/utils/operating_point.h> 46 #include <ilqgames/utils/solver_log.h> 47 #include <ilqgames/utils/strategy.h> 48 #include <ilqgames/utils/types.h> 50 #include <glog/logging.h> 56 SolutionSplicer::SolutionSplicer(
const SolverLog& log)
57 : strategies_(log.FinalStrategies()),
58 operating_point_(log.FinalOperatingPoint()) {}
60 void SolutionSplicer::Splice(
const SolverLog& log) {
61 CHECK_GE(log.FinalOperatingPoint().t0, operating_point_.t0);
62 CHECK_GE(operating_point_.xs.size(), time::kNumTimeSteps);
63 CHECK_EQ(log.FinalOperatingPoint().xs.size(), time::kNumTimeSteps);
65 const size_t current_timestep =
static_cast<size_t>(
67 (log.FinalOperatingPoint().t0 - operating_point_.t0) / time::kTimeStep);
72 constexpr
size_t kNumPreviousTimeStepsToSave = 5;
73 const size_t initial_timestep =
74 (
static_cast<int>(current_timestep) <
75 static_cast<int>(kNumPreviousTimeStepsToSave))
77 : current_timestep - kNumPreviousTimeStepsToSave;
81 constexpr
size_t kNumExtraTimeStepsBeforeSplicingIn = 0;
82 const size_t first_timestep_new_solution =
83 kNumExtraTimeStepsBeforeSplicingIn + current_timestep;
86 for (
size_t kk = initial_timestep; kk < first_timestep_new_solution; kk++) {
87 const size_t kk_new_solution = kk - initial_timestep;
88 operating_point_.xs[kk_new_solution].swap(operating_point_.xs[kk]);
89 operating_point_.us[kk_new_solution].swap(operating_point_.us[kk]);
91 for (
auto& strategy : strategies_) {
92 strategy.Ps[kk_new_solution].swap(strategy.Ps[kk]);
93 strategy.alphas[kk_new_solution].swap(strategy.alphas[kk]);
100 const size_t num_spliced_timesteps =
101 current_timestep - initial_timestep + time::kNumTimeSteps;
102 CHECK_LE(num_spliced_timesteps,
103 time::kNumTimeSteps + kNumPreviousTimeStepsToSave);
105 operating_point_.xs.resize(num_spliced_timesteps);
106 operating_point_.us.resize(num_spliced_timesteps);
107 operating_point_.t0 += initial_timestep * time::kTimeStep;
109 for (
auto& strategy : strategies_) {
110 strategy.Ps.resize(num_spliced_timesteps);
111 strategy.alphas.resize(num_spliced_timesteps);
115 CHECK_EQ(current_timestep + time::kNumTimeSteps - initial_timestep,
116 operating_point_.xs.size());
117 for (
size_t kk = kNumExtraTimeStepsBeforeSplicingIn; kk < time::kNumTimeSteps;
119 const size_t kk_new_solution = current_timestep + kk - initial_timestep;
120 operating_point_.xs[kk_new_solution] = log.FinalOperatingPoint().xs[kk];
121 operating_point_.us[kk_new_solution] = log.FinalOperatingPoint().us[kk];
123 for (PlayerIndex ii = 0; ii < log.NumPlayers(); ii++) {
124 strategies_[ii].Ps[kk_new_solution] = log.FinalStrategies()[ii].Ps[kk];
125 strategies_[ii].alphas[kk_new_solution] =
126 log.FinalStrategies()[ii].alphas[kk];