ilqgames
A new real-time solver for large-scale differential games.
multi_player_integrable_system.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 // Base class for all multi-player *integrable* dynamical systems.
40 // Supports (discrete-time) linearization and integration.
41 //
42 ///////////////////////////////////////////////////////////////////////////////
43 
44 #include <ilqgames/dynamics/multi_player_integrable_system.h>
45 #include <ilqgames/utils/operating_point.h>
46 #include <ilqgames/utils/strategy.h>
47 #include <ilqgames/utils/types.h>
48 
49 #include <vector>
50 
51 namespace ilqgames {
52 
53 bool MultiPlayerIntegrableSystem::integrate_using_euler_ = false;
54 
55 VectorXf MultiPlayerIntegrableSystem::Integrate(
56  Time t0, Time t, const VectorXf& x0, const OperatingPoint& operating_point,
57  const std::vector<Strategy>& strategies) const {
58  CHECK_GE(t, t0);
59  CHECK_GE(t0, operating_point.t0);
60  CHECK_EQ(strategies.size(), NumPlayers());
61 
62  std::vector<VectorXf> us(NumPlayers());
63 
64  // Compute current timestep and final timestep.
65  const Time relative_t0 = t0 - operating_point.t0;
66  const size_t current_timestep =
67  static_cast<size_t>(relative_t0 / time::kTimeStep);
68 
69  const Time relative_t = t - operating_point.t0;
70  const size_t final_timestep =
71  static_cast<size_t>(relative_t / time::kTimeStep);
72 
73  // Handle case where 't0' is after 'operating_point.t0' by integrating from
74  // 't0' to the next discrete timestep.
75  VectorXf x(x0);
76  if (t0 > operating_point.t0)
77  x = IntegrateToNextTimeStep(t0, x0, operating_point, strategies);
78 
79  // Integrate forward step by step up to timestep including t.
80  x = Integrate(current_timestep + 1, final_timestep, x, operating_point,
81  strategies);
82 
83  // Integrate forward from this timestep to t.
84  return IntegrateFromPriorTimeStep(t, x, operating_point, strategies);
85 }
86 
87 VectorXf MultiPlayerIntegrableSystem::Integrate(
88  size_t initial_timestep, size_t final_timestep, const VectorXf& x0,
89  const OperatingPoint& operating_point,
90  const std::vector<Strategy>& strategies) const {
91  VectorXf x(x0);
92  std::vector<VectorXf> us(NumPlayers());
93  for (size_t kk = initial_timestep; kk < final_timestep; kk++) {
94  const Time t = operating_point.t0 + kk * time::kTimeStep;
95 
96  // Populate controls for all players.
97  for (PlayerIndex ii = 0; ii < NumPlayers(); ii++)
98  us[ii] = strategies[ii](kk, x - operating_point.xs[kk],
99  operating_point.us[kk][ii]);
100 
101  x = Integrate(t, time::kTimeStep, x, us);
102  }
103 
104  return x;
105 }
106 
107 VectorXf MultiPlayerIntegrableSystem::IntegrateToNextTimeStep(
108  Time t0, const VectorXf& x0, const OperatingPoint& operating_point,
109  const std::vector<Strategy>& strategies) const {
110  CHECK_GE(t0, operating_point.t0);
111 
112  // Compute remaining time this timestep.
113  const Time relative_t0 = t0 - operating_point.t0;
114  const size_t current_timestep = static_cast<size_t>(
115  (relative_t0 +
116  constants::kSmallNumber) // Add to avoid inadvertently subtracting 1.
117  / time::kTimeStep);
118  const Time remaining_time_this_step =
119  time::kTimeStep * (current_timestep + 1) - relative_t0;
120  CHECK_LT(remaining_time_this_step, time::kTimeStep + constants::kSmallNumber);
121  CHECK_LT(current_timestep, operating_point.xs.size());
122 
123  // Interpolate x0_ref.
124  const float frac = remaining_time_this_step / time::kTimeStep;
125  const VectorXf x0_ref =
126  (current_timestep + 1 < operating_point.xs.size())
127  ? frac * operating_point.xs[current_timestep] +
128  (1.0 - frac) * operating_point.xs[current_timestep + 1]
129  : operating_point.xs.back();
130 
131  // Populate controls for each player.
132  std::vector<VectorXf> us(NumPlayers());
133  for (PlayerIndex ii = 0; ii < NumPlayers(); ii++)
134  us[ii] = strategies[ii](current_timestep, x0 - x0_ref,
135  operating_point.us[current_timestep][ii]);
136 
137  return Integrate(t0, remaining_time_this_step, x0, us);
138 }
139 
140 VectorXf MultiPlayerIntegrableSystem::IntegrateFromPriorTimeStep(
141  Time t, const VectorXf& x0, const OperatingPoint& operating_point,
142  const std::vector<Strategy>& strategies) const {
143  // Compute time until next timestep.
144  const Time relative_t = t - operating_point.t0;
145  const size_t current_timestep =
146  static_cast<size_t>(relative_t / time::kTimeStep);
147  const Time remaining_time_until_t =
148  relative_t - time::kTimeStep * current_timestep;
149  CHECK_LT(current_timestep, operating_point.xs.size()) << t;
150  CHECK_LT(remaining_time_until_t, time::kTimeStep);
151 
152  // Populate controls for each player.
153  std::vector<VectorXf> us(NumPlayers());
154  for (PlayerIndex ii = 0; ii < NumPlayers(); ii++) {
155  us[ii] = strategies[ii](current_timestep,
156  x0 - operating_point.xs[current_timestep],
157  operating_point.us[current_timestep][ii]);
158  }
159 
160  return Integrate(operating_point.t0 + time::kTimeStep * current_timestep,
161  remaining_time_until_t, x0, us);
162 }
163 
164 VectorXf MultiPlayerIntegrableSystem::Integrate(
165  Time t0, Time time_interval, const Eigen::Ref<VectorXf>& x0,
166  const std::vector<Eigen::Ref<VectorXf>>& us) const {
167  std::vector<VectorXf> eval_us(us.size());
168  std::transform(us.begin(), us.end(), eval_us.begin(),
169  [](const Eigen::Ref<VectorXf>& u) { return u.eval(); });
170 
171  return Integrate(t0, time_interval, x0.eval(), eval_us);
172 };
173 
174 } // namespace ilqgames