ilqgames
A new real-time solver for large-scale differential games.
concatenated_flat_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_FLAT_SYSTEM_H
44 #define ILQGAMES_DYNAMICS_CONCATENATED_FLAT_SYSTEM_H
45 
46 #include <ilqgames/dynamics/multi_player_flat_system.h>
47 #include <ilqgames/dynamics/single_player_flat_system.h>
48 #include <ilqgames/utils/linear_dynamics_approximation.h>
49 #include <ilqgames/utils/types.h>
50 
51 namespace ilqgames {
52 
53 using FlatSubsystemList =
54  std::vector<std::shared_ptr<SinglePlayerFlatSystem>>;
55 
57  public:
59  ConcatenatedFlatSystem(const FlatSubsystemList& subsystems);
60 
61  // Compute time derivative of state.
62  VectorXf Evaluate(const VectorXf& x, const std::vector<VectorXf>& us) const;
63 
64  // Discrete time approximation of the underlying linearized system.
65  void ComputeLinearizedSystem() const;
66 
67  // Utilities for feedback linearization.
68  MatrixXf InverseDecouplingMatrix(const VectorXf& x) const;
69  VectorXf AffineTerm(const VectorXf& x) const;
70  VectorXf LinearizingControl(const VectorXf& x, const VectorXf& v,
71  PlayerIndex player) const;
72  std::vector<VectorXf> LinearizingControls(
73  const VectorXf& x, const std::vector<VectorXf>& vs) const;
74  VectorXf ToLinearSystemState(const VectorXf& x) const;
75  VectorXf ToLinearSystemState(const VectorXf& x,
76  PlayerIndex subsystem_idx) const;
77  VectorXf FromLinearSystemState(const VectorXf& xi) const;
78  VectorXf FromLinearSystemState(const VectorXf& xi,
79  PlayerIndex subsystem_idx) const;
80  bool IsLinearSystemStateSingular(const VectorXf& xi) const;
81 
82  // Get the subset of this full state corresponding to the given subsystem.
83  VectorXf SubsystemStates(const VectorXf& x, PlayerIndex subsystem_idx) const;
84 
85  // Deprecated utilities for changing cost coordinates.
86  void ChangeCostCoordinates(const VectorXf& xi,
87  std::vector<QuadraticCostApproximation>* q) const;
88  void ChangeControlCostCoordinates(
89  const VectorXf& xi, std::vector<QuadraticCostApproximation>* q) const;
90 
91  // Distance metric between two states.
92  float DistanceBetween(const VectorXf& x0, const VectorXf& x1) const;
93 
94  // Getters.
95  const FlatSubsystemList& Subsystems() const { return subsystems_; }
96  PlayerIndex NumPlayers() const { return subsystems_.size(); }
97  Dimension SubsystemXDim(PlayerIndex player_idx) const {
98  return subsystems_[player_idx]->XDim();
99  }
100  Dimension SubsystemStartDim(PlayerIndex player_idx) const {
101  return subsystem_start_dims_[player_idx];
102  }
103  Dimension UDim(PlayerIndex player_idx) const {
104  return subsystems_[player_idx]->UDim();
105  }
106  std::vector<Dimension> PositionDimensions() const;
107 
108  private:
109  // List of subsystems, each of which controls the affects of a single player.
110  const FlatSubsystemList subsystems_;
111 
112  // Cumulative sum of dimensions of each subsystem.
113  std::vector<Dimension> subsystem_start_dims_;
114 }; // namespace ilqgames
115 
116 } // namespace ilqgames
117 
118 #endif