ilqgames
A new real-time solver for large-scale differential games.
solution_splicer.cpp
1 /*
2  * Copyright (c) 2019, The Regents of the University of California (Regents).
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following
14  * disclaimer in the documentation and/or other materials provided
15  * with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Please contact the author(s) of this library if you have any questions.
34  * Authors: David Fridovich-Keil ( dfk@eecs.berkeley.edu )
35  */
36 
37 ///////////////////////////////////////////////////////////////////////////////
38 //
39 // Splice together existing and new solutions to a receding horizon problem.
40 //
41 ///////////////////////////////////////////////////////////////////////////////
42 
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>
49 
50 #include <glog/logging.h>
51 #include <memory>
52 #include <vector>
53 
54 namespace ilqgames {
55 
56 SolutionSplicer::SolutionSplicer(const SolverLog& log)
57  : strategies_(log.FinalStrategies()),
58  operating_point_(log.FinalOperatingPoint()) {}
59 
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);
64 
65  const size_t current_timestep = static_cast<size_t>(
66  1e-4 + // Add a little so that conversion doesn't end up subtracting 1.
67  (log.FinalOperatingPoint().t0 - operating_point_.t0) / time::kTimeStep);
68 
69  // HACK! If we're close enough to the beginning of the old trajectory, just
70  // save the first few steps along it in case a lower-level path follower uses
71  // this information.
72  constexpr size_t kNumPreviousTimeStepsToSave = 5;
73  const size_t initial_timestep =
74  (static_cast<int>(current_timestep) <
75  static_cast<int>(kNumPreviousTimeStepsToSave))
76  ? 0
77  : current_timestep - kNumPreviousTimeStepsToSave;
78 
79  // HACK! Make sure the new solution starts several timesteps after the
80  // nearest match to guard against off-by-one issues.
81  constexpr size_t kNumExtraTimeStepsBeforeSplicingIn = 0;
82  const size_t first_timestep_new_solution =
83  kNumExtraTimeStepsBeforeSplicingIn + current_timestep;
84 
85  // (2) Copy over saved part of existing plan.
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]);
90 
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]);
94  }
95  }
96 
97  // Resize to be the appropriate length.
98  // NOTE: makes use of default behavior of std::vector<T>.resize() in that it
99  // does not delete earlier entries.
100  const size_t num_spliced_timesteps =
101  current_timestep - initial_timestep + time::kNumTimeSteps;
102  CHECK_LE(num_spliced_timesteps,
103  time::kNumTimeSteps + kNumPreviousTimeStepsToSave);
104 
105  operating_point_.xs.resize(num_spliced_timesteps);
106  operating_point_.us.resize(num_spliced_timesteps);
107  operating_point_.t0 += initial_timestep * time::kTimeStep;
108 
109  for (auto& strategy : strategies_) {
110  strategy.Ps.resize(num_spliced_timesteps);
111  strategy.alphas.resize(num_spliced_timesteps);
112  }
113 
114  // Copy over new solution to overwrite existing log after first timestep.
115  CHECK_EQ(current_timestep + time::kNumTimeSteps - initial_timestep,
116  operating_point_.xs.size());
117  for (size_t kk = kNumExtraTimeStepsBeforeSplicingIn; kk < time::kNumTimeSteps;
118  kk++) {
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];
122 
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];
127  }
128  }
129 }
130 
131 } // namespace ilqgames