ilqgames
A new real-time solver for large-scale differential games.
types.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 // Custom types.
40 //
41 ///////////////////////////////////////////////////////////////////////////////
42 
43 #ifndef ILQGAMES_UTILS_TYPES_H
44 #define ILQGAMES_UTILS_TYPES_H
45 
46 // ------------------------------- INCLUDES -------------------------------- //
47 
48 #include <math.h>
49 #include <algorithm>
50 #include <chrono>
51 #include <functional>
52 #include <iostream>
53 #include <limits>
54 #include <memory>
55 #include <random>
56 #include <string>
57 #include <unordered_map>
58 #include <vector>
59 
60 #include <Eigen/Dense>
61 #include <Eigen/Geometry>
62 #include <Eigen/StdVector>
63 
64 namespace ilqgames {
65 
66 // ------------------------ THIRD PARTY TYPEDEFS ---------------------------- //
67 
68 using Eigen::MatrixXf;
69 using Eigen::VectorXf;
70 
71 // --------------------------------- TYPES ---------------------------------- //
72 
73 using PlayerIndex = unsigned short;
74 using Dimension = int;
75 using Point2 = Eigen::Vector2f;
76 
77 // Rename the system clock for easier usage.
78 using Clock = std::chrono::system_clock;
79 
80 #ifdef __APPLE__
81 using PointList2 = std::vector<Point2, Eigen::aligned_allocator<Point2>>;
82 using Time = float;
83 #else
84 using PointList2 = std::vector<Point2>;
85 using Time = double;
86 #endif
87 
88 template <typename T>
89 using PlayerPtrMap = std::unordered_map<PlayerIndex, std::shared_ptr<T>>;
90 
91 template <typename T>
92 using PlayerPtrMultiMap =
93  std::unordered_multimap<PlayerIndex, std::shared_ptr<T>>;
94 
95 template <typename T>
96 using PlayerMap = std::unordered_map<PlayerIndex, T>;
97 
98 template <typename T>
99 using PlayerMultiMap = std::unordered_multimap<PlayerIndex, T>;
100 
101 using PlayerDualMap = std::unordered_map<PlayerIndex, float*>;
102 
103 template <typename T>
104 using PtrVector = std::vector<std::shared_ptr<T>>;
105 
106 using RefVector = std::vector<Eigen::Ref<VectorXf>>;
107 
108 // Empty struct for setting unused/unimplemented template args.
109 struct Empty {};
110 
111 // ------------------------------- CONSTANTS -------------------------------- //
112 
113 namespace constants {
114 
115 // Acceleration due to gravity (m/s/s).
116 static constexpr float kGravity = 9.81;
117 
118 // Small number for use in approximate equality checking.
119 static constexpr float kSmallNumber = 1e-4;
120 
121 // Float precision infinity.
122 static constexpr float kInfinity = std::numeric_limits<float>::infinity();
123 
124 // Constant for invalid values.
125 static constexpr float kInvalidValue = std::numeric_limits<float>::quiet_NaN();
126 
127 // Default multiplier values.
128 static constexpr float kDefaultLambda = 0.0;
129 static constexpr float kDefaultMu = 10.0;
130 
131 } // namespace constants
132 
133 namespace time {
134 // Time discretization (s).
135 static constexpr Time kTimeStep = 0.1;
136 
137 // Time horizon (s).
138 static constexpr Time kTimeHorizon = 10.0;
139 
140 // Number of time steps.
141 static constexpr size_t kNumTimeSteps =
142  static_cast<size_t>((kTimeHorizon + constants::kSmallNumber) / kTimeStep);
143 } // namespace time
144 
145 // ---------------------------- SIMPLE FUNCTIONS ---------------------------- //
146 
147 template <typename T, typename... Args>
148 std::unique_ptr<T> make_unique(Args&&... args) {
149  return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
150 }
151 
152 template <typename T>
153 inline constexpr T sgn(T x, std::false_type is_signed) {
154  return T(0) < x;
155 }
156 
157 template <typename T>
158 inline constexpr T sgn(T x, std::true_type is_signed) {
159  return (T(0) < x) - (x < T(0));
160 }
161 
162 template <typename T>
163 inline constexpr T sgn(T x) {
164  return sgn(x, std::is_signed<T>());
165 }
166 
167 template <typename T>
168 inline constexpr T signed_sqrt(T x) {
169  return sgn(x) * std::sqrt(std::abs(x));
170 }
171 
172 } // namespace ilqgames
173 
174 #endif