TraDemGen Logo  1.00.13
C++ Simulated Travel Demand Generation Library
Loading...
Searching...
No Matches
ContinuousAttributeLite.hpp
Go to the documentation of this file.
1#ifndef __TRADEMGEN_BAS_CONTINUOUSATTRIBUTELITE_HPP
2#define __TRADEMGEN_BAS_CONTINUOUSATTRIBUTELITE_HPP
3
4// //////////////////////////////////////////////////////////////////////
5// Import section
6// //////////////////////////////////////////////////////////////////////
7// STL
8#include <cassert>
9#include <iosfwd>
10#include <string>
11#include <vector>
12#include <map>
13// StdAir
14#include <stdair/stdair_basic_types.hpp>
15// TraDemGen
18
19namespace TRADEMGEN {
20
25 template <typename T>
27 public:
28 // ///////////////////// Type definitions ///////////////////////
32 typedef std::map<T, stdair::Probability_T> ContinuousDistribution_T;
33
34 public:
35 // ////////////////////// Business Methods ////////////////////
39 const T getValue(const stdair::Probability_T& iCumulativeProbability) const{
40 const DictionaryKey_T& lKey =
41 DictionaryManager::valueToKey (iCumulativeProbability);
42
43 // Find the first cumulative probablity value greater or equal to lKey.
44 unsigned int idx = 0;
45 for (; idx < _size; ++idx) {
46 if (_cumulativeDistribution.at(idx) > lKey) {
47 break;
48 }
49 }
50
51 if (idx == 0) {
52 return _valueArray.at(idx);
53 }
54 if (idx == _size) {
55 return _valueArray.at(idx-1);
56 }
57
58 //
59 const stdair::Probability_T& lCumulativeCurrentPoint =
60 DictionaryManager::keyToValue (_cumulativeDistribution.at(idx));
61 const T& lValueCurrentPoint = _valueArray.at(idx);
62
63 //
64 const stdair::Probability_T& lCumulativePreviousPoint =
65 DictionaryManager::keyToValue (_cumulativeDistribution.at(idx-1));
66 const T& lValuePreviousPoint = _valueArray.at(idx-1);
67
68 if (lCumulativePreviousPoint == lCumulativeCurrentPoint) {
69 return lValuePreviousPoint;
70 }
71
72 T oValue= lValuePreviousPoint + (lValueCurrentPoint - lValuePreviousPoint)
73 * (iCumulativeProbability - lCumulativePreviousPoint)
74 / (lCumulativeCurrentPoint - lCumulativePreviousPoint);
75
76 return oValue;
77 }
78
82 const double getDerivativeValue(const T iKey) const{
83
84 // Find the first key value greater or equal to iKey.
85 unsigned int idx = 0;
86 for (; idx < _size; ++idx) {
87 if (_valueArray.at(idx) > iKey) {
88 break;
89 }
90 }
91
92 assert (idx != 0);
93 assert (idx != _size);
94
95 //
96 const stdair::Probability_T& lCumulativeCurrentPoint =
97 DictionaryManager::keyToValue (_cumulativeDistribution.at(idx));
98 const T& lValueCurrentPoint = _valueArray.at(idx);
99
100 //
101 const stdair::Probability_T& lCumulativePreviousPoint =
102 DictionaryManager::keyToValue (_cumulativeDistribution.at(idx-1));
103 const T& lValuePreviousPoint = _valueArray.at(idx-1);
104
105 assert (lValueCurrentPoint != lValuePreviousPoint);
106
107 const double oValue= (lCumulativeCurrentPoint - lCumulativePreviousPoint)
108 / (lValueCurrentPoint - lValuePreviousPoint);
109
110 return oValue;
111 }
112
116 const T getUpperBound (const T iKey) const {
117 // Find the first key value greater or equal to iKey.
118 unsigned int idx = 0;
119 for (; idx < _size; ++idx) {
120 if (_valueArray.at(idx) > iKey) {
121 break;
122 }
123 }
124 assert (idx != 0);
125 assert (idx != _size);
126
127 return _valueArray.at (idx);
128 }
129
130 public:
131 // ////////////// Display Support Methods ////////////////
135 const std::string displayCumulativeDistribution() const {
136 std::ostringstream oStr;
137
138 for (unsigned int idx = 0; idx < _size; ++idx) {
139 if (idx != 0) {
140 oStr << ", ";
141 }
142
143 const stdair::Probability_T& lProbability =
144 DictionaryManager::keyToValue (_cumulativeDistribution.at(idx));
145
146 oStr << _valueArray.at(idx) << ":" << lProbability;
147 }
148 return oStr.str();
149 }
150
151
152 public:
153 // ////////// Constructors and destructors //////////////
158 : _size (iValueMap.size()) {
159 init (iValueMap);
160 }
161
166 : _size (iCAL._size),
167 _cumulativeDistribution (iCAL._cumulativeDistribution),
168 _valueArray (iCAL._valueArray) {
169 }
170
175 _size = iCAL._size;
176 _cumulativeDistribution = iCAL._cumulativeDistribution;
177 _valueArray = iCAL._valueArray;
178 return *this;
179 }
180
185 }
186
187 private:
191 ContinuousAttributeLite() : _size(1) {
192 }
193
198 void init (const ContinuousDistribution_T& iValueMap) {
199 //
200 const unsigned int lSize = iValueMap.size();
201 _cumulativeDistribution.reserve (lSize);
202 _valueArray.reserve (lSize);
203
204 // Browse the map to retrieve the values and cumulative probabilities.
205 for (typename ContinuousDistribution_T::const_iterator it =
206 iValueMap.begin(); it != iValueMap.end(); ++it) {
207
208 const T& attributeValue = it->first;
209 const DictionaryKey_T& lKey = DictionaryManager::valueToKey (it->second);
210
211 // Build the two arrays.
212 _cumulativeDistribution.push_back (lKey);
213 _valueArray.push_back (attributeValue);
214 }
215 }
216
217
218 private:
219 // ////////// Attributes //////////
223 unsigned int _size;
224
228 std::vector<DictionaryKey_T> _cumulativeDistribution;
229
233 std::vector<T> _valueArray;
234 };
235
236}
237#endif // __TRADEMGEN_BAS_CONTINUOUSATTRIBUTELITE_HPP
stdair::Probability_T DictionaryKey_T
Class modeling the distribution of values that can be taken by a continuous attribute.
std::map< T, stdair::Probability_T > ContinuousDistribution_T
const T getValue(const stdair::Probability_T &iCumulativeProbability) const
ContinuousAttributeLite(const ContinuousDistribution_T &iValueMap)
const std::string displayCumulativeDistribution() const
const double getDerivativeValue(const T iKey) const
ContinuousAttributeLite(const ContinuousAttributeLite &iCAL)
ContinuousAttributeLite & operator=(const ContinuousAttributeLite &iCAL)
static const stdair::Probability_T keyToValue(const DictionaryKey_T)
static const DictionaryKey_T valueToKey(const stdair::Probability_T)