ilqgames
A new real-time solver for large-scale differential games.
concatenated_dynamical_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 // Multi-player dynamical system comprised of several single player subsystems.
40 //
41 ///////////////////////////////////////////////////////////////////////////////
42 
43 #include <ilqgames/dynamics/concatenated_dynamical_system.h>
44 #include <ilqgames/utils/linear_dynamics_approximation.h>
45 #include <ilqgames/utils/types.h>
46 
47 #include <glog/logging.h>
48 
49 namespace ilqgames {
50 
51 ConcatenatedDynamicalSystem::ConcatenatedDynamicalSystem(
52  const SubsystemList& subsystems)
53  : MultiPlayerDynamicalSystem(std::accumulate(
54  subsystems.begin(), subsystems.end(), 0,
55  [](Dimension total,
56  const std::shared_ptr<SinglePlayerDynamicalSystem>& subsystem) {
57  CHECK_NOTNULL(subsystem.get());
58  return total + subsystem->XDim();
59  })),
60  subsystems_(subsystems) {
61  // Populate subsystem start dimensions.
62  subsystem_start_dims_.push_back(0);
63  for (const auto& subsystem : subsystems_) {
64  subsystem_start_dims_.push_back(subsystem_start_dims_.back() +
65  subsystem->XDim());
66  }
67 }
68 
69 VectorXf ConcatenatedDynamicalSystem::Evaluate(
70  Time t, const VectorXf& x, const std::vector<VectorXf>& us) const {
71  CHECK_EQ(us.size(), NumPlayers());
72 
73  // Populate 'xdot' one subsystem at a time.
74  VectorXf xdot(xdim_);
75  Dimension dims_so_far = 0;
76  for (size_t ii = 0; ii < NumPlayers(); ii++) {
77  const auto& subsystem = subsystems_[ii];
78  xdot.segment(dims_so_far, subsystem->XDim()) = subsystem->Evaluate(
79  t, x.segment(dims_so_far, subsystem->XDim()), us[ii]);
80  dims_so_far += subsystem->XDim();
81  }
82 
83  return xdot;
84 }
85 
86 LinearDynamicsApproximation ConcatenatedDynamicalSystem::Linearize(
87  Time t, const VectorXf& x, const std::vector<VectorXf>& us) const {
88  CHECK_EQ(us.size(), NumPlayers());
89 
90  // Populate a block-diagonal A, as well as Bs.
91  LinearDynamicsApproximation linearization(*this);
92 
93  Dimension dims_so_far = 0;
94  for (size_t ii = 0; ii < NumPlayers(); ii++) {
95  const auto& subsystem = subsystems_[ii];
96  const Dimension xdim = subsystem->XDim();
97  const Dimension udim = subsystem->UDim();
98  subsystem->Linearize(
99  t, x.segment(dims_so_far, xdim), us[ii],
100  linearization.A.block(dims_so_far, dims_so_far, xdim, xdim),
101  linearization.Bs[ii].block(dims_so_far, 0, xdim, udim));
102 
103  dims_so_far += subsystem->XDim();
104  }
105 
106  return linearization;
107 }
108 
109 float ConcatenatedDynamicalSystem::DistanceBetween(const VectorXf& x0,
110  const VectorXf& x1) const {
111  // HACK: assumes only first subsystem matters.
112  return subsystems_[0]->DistanceBetween(x0.head(subsystems_[0]->XDim()),
113  x1.head(subsystems_[0]->XDim()));
114 
115  // Dimension dims_so_far = 0;
116  // float total = 0.0;
117 
118  // // Accumulate total across all subsystems.
119  // for (const auto& subsystem : subsystems_) {
120  // const Dimension xdim = subsystem->XDim();
121  // total += subsystem->DistanceBetween(x0.segment(dims_so_far, xdim),
122  // x1.segment(dims_so_far, xdim));
123 
124  // dims_so_far += xdim;
125  // }
126 
127  // return total;
128 }
129 
130 std::vector<Dimension> ConcatenatedDynamicalSystem::PositionDimensions() const {
131  std::vector<Dimension> dims;
132 
133  for (const auto& s : subsystems_) {
134  const std::vector<Dimension> sub_dims = s->PositionDimensions();
135  dims.insert(dims.end(), sub_dims.begin(), sub_dims.end());
136  }
137 
138  return dims;
139 }
140 
141 } // namespace ilqgames