ilqgames
A new real-time solver for large-scale differential games.
multi_player_integrable_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 *integrable* dynamical systems.
40 // Supports (discrete-time) linearization and integration.
41 //
42 ///////////////////////////////////////////////////////////////////////////////
43 
44 #ifndef ILQGAMES_DYNAMICS_MULTI_PLAYER_INTEGRABLE_SYSTEM_H
45 #define ILQGAMES_DYNAMICS_MULTI_PLAYER_INTEGRABLE_SYSTEM_H
46 
47 #include <ilqgames/utils/operating_point.h>
48 #include <ilqgames/utils/strategy.h>
49 #include <ilqgames/utils/types.h>
50 
51 #include <vector>
52 
53 namespace ilqgames {
54 
56  public:
57  virtual ~MultiPlayerIntegrableSystem() {}
58 
59  // Integrate these dynamics forward in time.
60  // Options include integration for a single timestep, between arbitrary times,
61  // and within a single timestep.
62  virtual VectorXf Integrate(Time t0, Time time_interval, const VectorXf& x0,
63  const std::vector<VectorXf>& us) const = 0;
64  VectorXf Integrate(Time t0, Time t, const VectorXf& x0,
65  const OperatingPoint& operating_point,
66  const std::vector<Strategy>& strategies) const;
67  VectorXf Integrate(size_t initial_timestep, size_t final_timestep,
68  const VectorXf& x0, const OperatingPoint& operating_point,
69  const std::vector<Strategy>& strategies) const;
70  VectorXf IntegrateToNextTimeStep(
71  Time t0, const VectorXf& x0, const OperatingPoint& operating_point,
72  const std::vector<Strategy>& strategies) const;
73  VectorXf IntegrateFromPriorTimeStep(
74  Time t, const VectorXf& x0, const OperatingPoint& operating_point,
75  const std::vector<Strategy>& strategies) const;
76 
77  // Make a utility version of the above that operates on Eigen::Refs.
78  VectorXf Integrate(Time t0, Time time_interval,
79  const Eigen::Ref<VectorXf>& x0,
80  const std::vector<Eigen::Ref<VectorXf>>& us) const;
81 
82  // Can this system be treated as linear for the purposes of LQ solves?
83  // For example, linear systems and feedback linearizable systems should
84  // return true here.
85  virtual bool TreatAsLinear() const { return false; }
86 
87  // Stitch between two states of the system. By default, just takes the
88  // first one but concatenated systems, e.g., can interpret the first one
89  // as best for ego and the second as best for other players.
90  virtual VectorXf Stitch(const VectorXf& x_ego,
91  const VectorXf& x_others) const {
92  return x_ego;
93  }
94 
95  // Integrate using single step Euler or not, see below for more extensive
96  // description.
97  static void IntegrateUsingEuler() { integrate_using_euler_ = true; }
98  static void IntegrateUsingRK4() { integrate_using_euler_ = false; }
99  static bool IntegrationUsesEuler() { return integrate_using_euler_; }
100 
101  // Getters.
102  Dimension XDim() const { return xdim_; }
103  Dimension TotalUDim() const {
104  Dimension total = 0;
105  for (PlayerIndex ii = 0; ii < NumPlayers(); ii++) total += UDim(ii);
106  return total;
107  }
108  virtual Dimension UDim(PlayerIndex player_idx) const = 0;
109  virtual PlayerIndex NumPlayers() const = 0;
110  virtual std::vector<Dimension> PositionDimensions() const = 0;
111 
112  // Distance metric between two states. By default, just the *squared* 2-norm.
113  virtual float DistanceBetween(const VectorXf& x0, const VectorXf& x1) const {
114  return (x0 - x1).squaredNorm();
115  }
116 
117  protected:
118  MultiPlayerIntegrableSystem(Dimension xdim) : xdim_(xdim) {}
119 
120  // State dimension.
121  const Dimension xdim_;
122 
123  // Whether to use single Euler during integration. Typically this is false but
124  // it is typically used either for testing (we only derive Nash typically in
125  // this case) or for speed.
126  static bool integrate_using_euler_;
127 }; //\class MultiPlayerIntegrableSystem
128 
129 } // namespace ilqgames
130 
131 #endif