TraDemGen Logo  1.00.0
C++ Simulated Travel Demand Generation Library
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
CategoricalAttribute.hpp
Go to the documentation of this file.
1 #ifndef __STDAIR_BAS_CATEGORICALATTRIBUTE_HPP
2 #define __STDAIR_BAS_CATEGORICALATTRIBUTE_HPP
3 
4 // //////////////////////////////////////////////////////////////////////
5 // Import section
6 // //////////////////////////////////////////////////////////////////////
7 // STL
8 #include <map>
9 #include <iosfwd>
10 // STDAIR
11 #include <stdair/STDAIR_Types.hpp>
12 #include <stdair/basic/DictionaryManager.hpp>
13 
14 namespace stdair {
15 
20  template <typename T>
22 
23  public:
24  // ///////////////////// Type definitions /////////////////////
28  typedef std::map<T, DictionaryKey_T> ProbabilityMassFunction_T;
29 
33  typedef std::map<DictionaryKey_T, T> InverseCumulativeDistribution_T;
34 
35 
36  private:
37  // ///////////// Getters ///////////
41  const ProbabilityMassFunction_T& getProbabilityMassFunction() const {
42  return _probabilityMassFunction;
43  }
44 
48  const InverseCumulativeDistribution_T& getInverseCumulativeDistribution() const {
49  return _inverseCumulativeDistribution;
50  }
51 
52  // ///////////// Setters ///////////
56  void setProbabilityMassFunction (const ProbabilityMassFunction_T& iProbabilityMassFunction) {
57  _probabilityMassFunction = iProbabilityMassFunction;
59  }
60 
61 
62  public:
63  // /////////////// Business Methods //////////
67  const T& getValue (const Probability_T& iCumulativeProbability) const {
68 
69  const DictionaryKey_T& lKey =
70  DictionaryManager::valueToKey (iCumulativeProbability);
71 
72  InverseCumulativeDistribution_T::const_iterator itT =
73  _inverseCumulativeDistribution.find (lKey);
74 
75  if (itT == _inverseCumulativeDistribution.end()) {
76  std::ostringstream oStr;
77  oStr << "The following cumulative probability is out of range: "
78  << iCumulativeProbability << displayInverseCumulativeDistribution();
79  throw IndexOutOfRangeException (oStr.str());
80  }
81 
82  return itT->second;
83  }
84 
85 
86  public:
87  // ////////////// Display Support Methods //////////
91  const std::string displayProbabilityMassFunction() const {
92  std::ostringstream oStr;
93  unsigned int idx = 0;
94 
95  for (typename ProbabilityMassFunction_T::const_iterator it =
96  _probabilityMassFunction.begin();
97  it != _probabilityMassFunction.end(); ++it, ++idx) {
98  if (idx != 0) {
99  oStr << ", ";
100  }
101  oStr << it->first << ":"
102  << DictionaryManager::keyToValue (it->second);
103  }
104 
105  return oStr.str();
106  }
107 
111  const std::string displayInverseCumulativeDistribution() const {
112  std::ostringstream oStr;
113 
114  for (typename InverseCumulativeDistribution_T::const_iterator it =
115  _inverseCumulativeDistribution.begin();
116  it != _inverseCumulativeDistribution.end(); ++it) {
117  oStr << "cumulative prob: " << DictionaryManager::keyToValue (it->first)
118  << " value: " << it->second << std::endl;
119  }
120 
121  return oStr.str();
122  }
123 
124  public:
125  // ////////// Constructors and destructors /////////
129  CategoricalAttribute (const ProbabilityMassFunction_T& iProbabilityMassFunction)
130  : _probabilityMassFunction (iProbabilityMassFunction) {
132  }
133 
138 
142  CategoricalAttribute (const CategoricalAttribute& iCategoricalAttribute)
143  : _probabilityMassFunction (iCategoricalAttribute._probabilityMassFunction) {
145  }
146 
150  virtual ~CategoricalAttribute() { }
151 
152 
158 
159  Probability_T cumulative_probability_so_far = 0.0;
160  for (typename ProbabilityMassFunction_T::const_iterator
161  itProbabilityMassFunction = _probabilityMassFunction.begin();
162  itProbabilityMassFunction != _probabilityMassFunction.end();
163  ++itProbabilityMassFunction) {
164 
165  Probability_T attribute_probability_mass =
166  DictionaryManager::keyToValue (itProbabilityMassFunction->second);
167 
168  if (attribute_probability_mass > 0) {
169  T attribute_value = itProbabilityMassFunction->first;
170  cumulative_probability_so_far += attribute_probability_mass;
171 
172  const DictionaryKey_T& lKey =
173  DictionaryManager::valueToKey (cumulative_probability_so_far);
174 
175  //_inverseCumulativeDistribution[lKey] = attribute_value;
176  _inverseCumulativeDistribution.
177  insert (typename InverseCumulativeDistribution_T::
178  value_type (lKey, attribute_value));
179  }
180  }
181  }
182 
183  private:
184  // ////////// Attributes //////////
188  ProbabilityMassFunction_T _probabilityMassFunction;
189 
193  InverseCumulativeDistribution_T _inverseCumulativeDistribution;
194  };
195 }
196 #endif // __STDAIR_BAS_CATEGORICALATTRIBUTE_HPP