ilqgames
A new real-time solver for large-scale differential games.
concatenated_dynamical_system.h
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 #ifndef ILQGAMES_DYNAMICS_CONCATENATED_DYNAMICAL_SYSTEM_H
44 #define ILQGAMES_DYNAMICS_CONCATENATED_DYNAMICAL_SYSTEM_H
45 
46 #include <ilqgames/dynamics/multi_player_dynamical_system.h>
47 #include <ilqgames/dynamics/single_player_dynamical_system.h>
48 #include <ilqgames/utils/linear_dynamics_approximation.h>
49 #include <ilqgames/utils/types.h>
50 
51 #include <algorithm>
52 
53 namespace ilqgames {
54 
55 using SubsystemList = std::vector<std::shared_ptr<SinglePlayerDynamicalSystem>>;
56 
58  public:
60  ConcatenatedDynamicalSystem(const SubsystemList& subsystems);
61 
62  // Compute time derivative of state.
63  VectorXf Evaluate(Time t, const VectorXf& x,
64  const std::vector<VectorXf>& us) const;
65 
66  // Compute a discrete-time Jacobian linearization.
67  LinearDynamicsApproximation Linearize(Time t, const VectorXf& x,
68  const std::vector<VectorXf>& us) const;
69 
70  // Distance metric between two states.
71  float DistanceBetween(const VectorXf& x0, const VectorXf& x1) const;
72 
73  // Stitch between two states of the system. Interprets the first one as best
74  // for ego and the second as best for other players.
75  VectorXf Stitch(const VectorXf& x_ego, const VectorXf& x_others) const {
76  VectorXf x(x_ego.size());
77 
78  const Dimension ego_state_dim = subsystems_[0]->XDim();
79  x.head(ego_state_dim) = x_ego.head(ego_state_dim);
80  x.tail(x_others.size() - ego_state_dim) =
81  x_others.tail(x_others.size() - ego_state_dim);
82 
83  return x;
84  }
85 
86  // Getters.
87  const SubsystemList& Subsystems() const { return subsystems_; }
88  PlayerIndex NumPlayers() const { return subsystems_.size(); }
89  Dimension SubsystemStartDim(PlayerIndex player_idx) const {
90  return subsystem_start_dims_[player_idx];
91  }
92  Dimension SubsystemXDim(PlayerIndex player_idx) const {
93  return subsystems_[player_idx]->XDim();
94  }
95  Dimension UDim(PlayerIndex player_idx) const {
96  return subsystems_[player_idx]->UDim();
97  }
98  std::vector<Dimension> PositionDimensions() const;
99 
100  private:
101  // List of subsystems, each of which controls the affects of a single player.
102  const SubsystemList subsystems_;
103 
104  // Cumulative sum of dimensions of each subsystem.
105  std::vector<Dimension> subsystem_start_dims_;
106 }; // namespace ilqgames
107 
108 } // namespace ilqgames
109 
110 #endif