Eclipse SUMO - Simulation of Urban MObility
CharacteristicMap.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 // Copyright (C) 2002-2024 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
18 // Characteristic map for vehicle type parameters as needed by the MMPEVEM model
19 // Teaching and Research Area Mechatronics in Mobile Propulsion (MMP), RWTH Aachen
20 /****************************************************************************/
21 
22 
23 /******************************************************************************
24  * ============================= Example Usage ============================== *
25  ******************************************************************************
26  * *
27  * Assume a function f which maps from R^2 to R^1 according to... *
28  * *
29  * | 0 | 1 | 2 | 3 | 4 -> x_1 *
30  * ----|------------------------ *
31  * -1 | 1 | 3 | 1 | -2 | *
32  * 1 | 1 | -2 | -3 | 7 | 5 *
33  * 3 | -2 | -1 | 0 | 1 | 3 *
34  * 5 | | 0 | 8 | 4 | 4 *
35  * *
36  * | *
37  * v *
38  * x_2 *
39  * *
40  * ... so that, for example, f(3, 1) = 7. Note that f is not defined at *
41  * (4, -1) and (0, 5). There are two ways to create a CharacteristicMap *
42  * object for this function. *
43  * 1) Using the standard constructor: *
44  * // Axes *
45  * std::vector<std::vector<double>> axes; *
46  * axes.push_back(std::vector<double>{0, 1, 2, 3, 4}); // Axis 1 *
47  * axes.push_back(std::vector<double>{-1, 1, 3, 5}); // Axis 2 *
48  * // Flattened row-major map entries *
49  * std::vector<double> flattenedMap{1, 3, 1, -2, std::nan(""), *
50  * 1, -2, -3, 7, 5, -2, -1, 0, 1, 3, std::nan(""), 0, 8, 4, 4}; *
51  * *
52  * CharacteristicMap map1(2, // Mapping from R^2... *
53  * 1, // ... to R^1 *
54  * axes, *
55  * flattenedMap); *
56  * *
57  * 2) Using a string-encoding of the map: *
58  * CharacteristicMap map2("2,1|0,1,2,3,4;-1,1,3,5|1,3,1,-2,nan," *
59  * "1,-2,-3,7,5,-2,-1,0,1,3,nan,0,8,4,4"); *
60  * *
61  * See below for an in-depth explanation of the format. *
62  * *
63  * *
64  * To evaluate the map at, for instance, p = (2.2, 2), one must call: *
65  * std::vector<double> res = map1.eval(std::vector<double>{2.2, 2}, // p *
66  * 1e-3); // eps *
67  * if(std::isnan(res[0])) *
68  * std::cout << "[WARNING] Couldn't evaluate the map." << std::endl; *
69  * else *
70  * std::cout << "res = " << res[0] << std::endl; *
71  * *
72  * The epsilon value is used for numerical reasons and decides how much a *
73  * point must deviate from its nearest neighbor before linear interpolation *
74  * is applied or when a point is considered outside of the map. The default *
75  * is 1e-6. *
76  * *
77  * *
78  * The string-encoding of a CharacteristicMap that maps from R^m to R^n is *
79  * formally defined as *
80  * "m,n|A_1[1],A_1[2],...,A_1[l1];A_2[1],A_2[2],...,A_2[l2];...;\ *
81  * A_m[1],A_m[2],...,A_m[lm]|\ *
82  * M_flat[1],M_flat[2],...,M_flat[l1*l2*...*lm]" *
83  * where A_i[j] denotes the j-th value of the i-th axis (which has li values *
84  * total) and M_flat[i] stands for the i-th entry in the row-major flattened *
85  * map. To be more specific, given a map M, its flattened version is *
86  * computed as follows (using pseudo code): *
87  * M_flat = "" *
88  * for i_m in {1,2,...,lm}: // Last axis *
89  * ... *
90  * for i_2 in {1,2,...,l2}: // Second axis *
91  * for i_1 in {1,2,...,l1}: // First axis (i.e. row axis) *
92  * for d in {1,2,...,n}: // Image dimensions *
93  * M_flat += M[i_1,i_2,...,i_m][d] + "," *
94  * removeTrailingComma(M_flat) *
95  * *
96  ******************************************************************************/
97 #pragma once
98 
99 #include <vector>
100 #include <string>
101 
102 
110 private:
113 
115  int imageDim;
116 
118  std::vector<std::vector<double>> axes;
119 
121  std::vector<double> flattenedMap;
122 
124  std::vector<int> strides;
125 
129  void determineStrides();
130 
138  int calcFlatIdx(const std::vector<int>& ref_idxs) const;
139 
151  int findNearestNeighborIdxs(const std::vector<double>& ref_p,
152  std::vector<int>& ref_idxs, double eps = 1e-6) const;
153 
162  std::vector<double> at(const std::vector<int>& ref_idxs) const;
163 
164 
165 
166 public:
179  const std::vector<std::vector<double>>& ref_axes,
180  const std::vector<double>& ref_flattenedMap);
181 
189  CharacteristicMap(const std::string& ref_mapString);
190 
197  std::string toString() const;
198 
204  int getDomainDim() const;
205 
211  int getImageDim() const;
212 
230  std::vector<double> eval(const std::vector<double>& ref_p,
231  double eps = 1e-6) const;
232 };
The purpose of this class is to store a characteristic map (German: Kennfeld) of arbitrary dimensions...
int imageDim
Image dimension of the map.
int calcFlatIdx(const std::vector< int > &ref_idxs) const
Compute the index of a map entry in the flattened map.
CharacteristicMap(int domainDim, int imageDim, const std::vector< std::vector< double >> &ref_axes, const std::vector< double > &ref_flattenedMap)
Constructor.
std::vector< int > strides
Stride for each domain dimension in the flattened map.
std::vector< double > flattenedMap
Flattened map entries.
std::vector< double > eval(const std::vector< double > &ref_p, double eps=1e-6) const
Evaluate a point in the map using linear interpolation.
int getDomainDim() const
Get the dimension of the map's domain.
std::vector< std::vector< double > > axes
Vector containing the values along each domain axis in ascending order.
int findNearestNeighborIdxs(const std::vector< double > &ref_p, std::vector< int > &ref_idxs, double eps=1e-6) const
Determine the indices of the nearest neighbor of a point in the map.
void determineStrides()
Determine the stride for each map dimension in the flattened map.
std::vector< double > at(const std::vector< int > &ref_idxs) const
Access a map entry using its indices.
int domainDim
Dimension of the map's domain.
std::string toString() const
Encode the map as a string.
int getImageDim() const
Get the image dimension of the map.