ilqgames
A new real-time solver for large-scale differential games.
multi_player_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 // Base class for all multi-player dynamical systems. Supports (discrete-time)
40 // linearization and integration.
41 //
42 ///////////////////////////////////////////////////////////////////////////////
43 
44 #ifndef ILQGAMES_DYNAMICS_MULTI_PLAYER_FLAT_SYSTEM_H
45 #define ILQGAMES_DYNAMICS_MULTI_PLAYER_FLAT_SYSTEM_H
46 
47 #include <ilqgames/dynamics/multi_player_integrable_system.h>
48 #include <ilqgames/utils/linear_dynamics_approximation.h>
49 #include <ilqgames/utils/operating_point.h>
50 #include <ilqgames/utils/quadratic_cost_approximation.h>
51 #include <ilqgames/utils/strategy.h>
52 #include <ilqgames/utils/types.h>
53 
54 #include <vector>
55 
56 namespace ilqgames {
57 
59  public:
60  virtual ~MultiPlayerFlatSystem() {}
61 
62  // Compute time derivative of state.
63  virtual VectorXf Evaluate(const VectorXf& x,
64  const std::vector<VectorXf>& us) const = 0;
65 
66  // Utilities for feedback linearization.
67  virtual MatrixXf InverseDecouplingMatrix(const VectorXf& x) const = 0;
68  virtual VectorXf AffineTerm(const VectorXf& x) const = 0;
69  virtual VectorXf LinearizingControl(const VectorXf& x, const VectorXf& v,
70  PlayerIndex player) const = 0;
71  virtual std::vector<VectorXf> LinearizingControls(
72  const VectorXf& x, const std::vector<VectorXf>& vs) const = 0;
73  virtual VectorXf ToLinearSystemState(const VectorXf& x) const = 0;
74  virtual VectorXf FromLinearSystemState(const VectorXf& xi) const = 0;
75 
76  // Gradient and hessian of map from xi to x.
77  virtual void ChangeCostCoordinates(
78  const VectorXf& xi, std::vector<QuadraticCostApproximation>* q) const = 0;
79  virtual void ChangeControlCostCoordinates(
80  const VectorXf& xi, std::vector<QuadraticCostApproximation>* q) const = 0;
81 
82  // Check if a state is singular.
83  virtual bool IsLinearSystemStateSingular(const VectorXf& xi) const = 0;
84 
85  // Integrate these dynamics forward in time.
86  // Options include integration for a single timestep, between arbitrary times,
87  // and within a single timestep.
88  VectorXf Integrate(Time time_interval, const VectorXf& xi0,
89  const std::vector<VectorXf>& vs) const;
90  VectorXf Integrate(Time t0, Time time_interval, const VectorXf& xi0,
91  const std::vector<VectorXf>& vs) const {
92  return Integrate(time_interval, xi0, vs);
93  }
94 
95  // Can this system be treated as linear for the purposes of LQ solves?
96  // For example, linear systems and feedback linearizable systems should return
97  // true here.
98  bool TreatAsLinear() const { return true; }
99 
100  // Getters.
101  const LinearDynamicsApproximation& LinearizedSystem() const {
102  if (!discrete_linear_system_) ComputeLinearizedSystem();
103  return *discrete_linear_system_;
104  }
105 
106  virtual Dimension UDim(PlayerIndex player_idx) const = 0;
107  virtual PlayerIndex NumPlayers() const = 0;
108 
109  protected:
110  MultiPlayerFlatSystem(Dimension xdim) : MultiPlayerIntegrableSystem(xdim) {}
111 
112  // Discrete time approximation of the underlying linearized system.
113  virtual void ComputeLinearizedSystem() const = 0;
114 
115  // Linearized system (discrete and continuous time).
116  mutable std::unique_ptr<const LinearDynamicsApproximation>
117  discrete_linear_system_;
118  mutable std::unique_ptr<const LinearDynamicsApproximation>
119  continuous_linear_system_;
120 
121 }; //\class MultiPlayerFlatSystem
122 
123 } // namespace ilqgames
124 
125 #endif