ilqgames
A new real-time solver for large-scale differential games.
receding_horizon_simulator.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 // Utility for solving a problem using a receding horizon, simulating dynamics
40 // forward at each stage to account for the passage of time.
41 //
42 // This class is intended as a facsimile of a real-time, online receding horizon
43 // problem in which short horizon problems are solved asynchronously throughout
44 // operation.
45 //
46 ///////////////////////////////////////////////////////////////////////////////
47 
48 #include <ilqgames/examples/receding_horizon_simulator.h>
49 #include <ilqgames/solver/ilq_solver.h>
50 #include <ilqgames/solver/problem.h>
51 #include <ilqgames/solver/solution_splicer.h>
52 #include <ilqgames/utils/solver_log.h>
53 #include <ilqgames/utils/strategy.h>
54 #include <ilqgames/utils/types.h>
55 
56 #include <glog/logging.h>
57 #include <chrono>
58 #include <memory>
59 #include <vector>
60 
61 namespace ilqgames {
62 
63 using clock = std::chrono::system_clock;
64 
65 std::vector<std::shared_ptr<const SolverLog>> RecedingHorizonSimulator(
66  Time final_time, Time planner_runtime, GameSolver* solver) {
67  CHECK_NOTNULL(solver);
68 
69  // Set up a list of solver logs, one per solver invocation.
70  std::vector<std::shared_ptr<const SolverLog>> logs;
71 
72  // Initial run of the solver. Keep track of time in order to know how much to
73  // integrate dynamics forward.
74  auto solver_call_time = clock::now();
75  bool success = false;
76  logs.push_back(solver->Solve(&success));
77  CHECK(success);
78  Time elapsed_time =
79  std::chrono::duration<Time>(clock::now() - solver_call_time).count();
80 
81  VLOG(1) << "Solved initial problem in " << elapsed_time << " seconds, with "
82  << logs.back()->NumIterates() << " iterations.";
83  const auto& dynamics = solver->GetProblem().Dynamics();
84 
85  // Keep a solution splicer to incorporate new receding horizon solutions.
86  SolutionSplicer splicer(*logs.front());
87 
88  // Repeatedly integrate dynamics forward, reset problem initial conditions,
89  // and resolve.
90  VectorXf x(solver->GetProblem().InitialState());
91  Time t = splicer.CurrentOperatingPoint().t0;
92 
93  while (true) {
94  // Break the loop if it's been long enough.
95  // Integrate a little more.
96  constexpr Time kExtraTime = 0.25;
97  t += kExtraTime; // + planner_runtime;
98 
99  if (t >= final_time ||
100  !splicer.ContainsTime(t + planner_runtime + time::kTimeStep))
101  break;
102 
103  x = solver->GetProblem().Dynamics()->Integrate(
104  t - kExtraTime, t, x, splicer.CurrentOperatingPoint(),
105  splicer.CurrentStrategies());
106 
107  // Overwrite problem with spliced solution.
108  solver->GetProblem().OverwriteSolution(splicer.CurrentOperatingPoint(),
109  splicer.CurrentStrategies());
110 
111  // Set up next receding horizon problem and solve.
112  solver->GetProblem().SetUpNextRecedingHorizon(x, t, planner_runtime);
113 
114  solver_call_time = clock::now();
115  logs.push_back(solver->Solve(&success, planner_runtime));
116  elapsed_time =
117  std::chrono::duration<Time>(clock::now() - solver_call_time).count();
118 
119  CHECK_LE(elapsed_time, planner_runtime);
120  VLOG(1) << "t = " << t << ": Solved warm-started problem in "
121  << elapsed_time << " seconds.";
122 
123  // Break the loop if it's been long enough.
124  t += elapsed_time;
125  if (t >= final_time || !splicer.ContainsTime(t)) break;
126 
127  // Integrate dynamics forward to account for solve time.
128  x = solver->GetProblem().Dynamics()->Integrate(
129  t - elapsed_time, t, x, splicer.CurrentOperatingPoint(),
130  splicer.CurrentStrategies());
131 
132  // Add new solution to splicer if it converged.
133  if (logs.back()->WasConverged()) splicer.Splice(*logs.back());
134  }
135 
136  return logs;
137 }
138 
139 } // namespace ilqgames