TraDemGen Logo  1.00.0
C++ Simulated Travel Demand Generation Library
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
ContinuousAttribute.hpp
Go to the documentation of this file.
1 #ifndef __TRADEMGEN_BAS_CONTINUOUSATTRIBUTE_HPP
2 #define __TRADEMGEN_BAS_CONTINUOUSATTRIBUTE_HPP
3 
4 // //////////////////////////////////////////////////////////////////////
5 // Import section
6 // //////////////////////////////////////////////////////////////////////
7 // STL
8 #include <string>
9 #include <map>
10 // StdAir
11 #include <stdair/stdair_date_time_types.hpp>
12 #include <stdair/service/Logger.hpp>
13 // TraDemGen
15 
16 namespace TRADEMGEN {
17 
20  template <class T>
22  public:
23 
24  // ///////////// Type definitions //////////////
26  typedef std::multimap<T, DictionaryKey_T> ContinuousDistribution_T;
27  typedef std::multimap<DictionaryKey_T, T> ContinuousInverseDistribution_T;
28 
29  private:
30  // ///////////// Getters ///////////
32  const ContinuousDistribution_T& getCumulativeDistribution() const {
33  return _cumulativeDistribution;
34  }
35 
37  const ContinuousInverseDistribution_T& getInverseCumulativeDistribution () const {
38  return _inverseCumulativeDistribution;
39  }
40 
41  private:
42  // ///////////// Setters ///////////
44  void setCumulativeDistribution (const ContinuousDistribution_T& iCumulativeDistribution) {
45  _cumulativeDistribution = iCumulativeDistribution;
47  }
48 
49  public:
50  // /////////////// Business Methods //////////
52  const T getValue (const stdair::Probability_T& iCumulativeProbability) const{
53  const DictionaryKey_T lKey =
54  DictionaryManager::valueToKey (iCumulativeProbability);
55  typename ContinuousInverseDistribution_T::const_iterator it =
56  _inverseCumulativeDistribution.lower_bound (lKey);
57 
58  stdair::Probability_T cumulativeProbabilityNextPoint =
60  T valueNextPoint = it->second;
61 
62  if (it == _inverseCumulativeDistribution.begin()) {
63  STDAIR_LOG_DEBUG ("Last element");
64  return valueNextPoint;
65  }
66  --it;
67 
68  stdair::Probability_T cumulativeProbabilityPreviousPoint =
70  T valuePreviousPoint = it->second;
71  if (cumulativeProbabilityNextPoint == cumulativeProbabilityPreviousPoint) {
72  return valuePreviousPoint;
73  }
74 
75  return valuePreviousPoint + (valueNextPoint - valuePreviousPoint)
76  * (iCumulativeProbability - cumulativeProbabilityPreviousPoint)
77  / (cumulativeProbabilityNextPoint - cumulativeProbabilityPreviousPoint);
78  }
79 
80  public:
81  // ////////////// Display Support Methods //////////
83  const std::string displayCumulativeDistribution() const {
84  std::ostringstream oStr;
85  unsigned int idx = 0;
86  for (typename ContinuousDistribution_T::const_iterator it =
87  _cumulativeDistribution.begin();
88  it != _cumulativeDistribution.end(); ++it, ++idx) {
89  if (idx != 0) {
90  oStr << ", ";
91  }
92  oStr << it->first << ":"
93  << DictionaryManager::keyToValue (it->second);
94  }
95  return oStr.str();
96  }
97 
99  const std::string displayInverseCumulativeDistribution() const {
100  std::ostringstream oStr;
101  for (typename ContinuousInverseDistribution_T::const_iterator it =
102  _inverseCumulativeDistribution.begin();
103  it != _inverseCumulativeDistribution.end(); ++it) {
104  oStr << "cumulative prob: " << DictionaryManager::keyToValue (it->first)
105  << " value: " << it->second << std::endl;
106  }
107  return oStr.str();
108  }
109 
110  public:
111  // ////////// Constructors and destructors /////////
114 
116  ContinuousAttribute (const ContinuousDistribution_T& iCumulativeDistribution)
117  : _cumulativeDistribution (iCumulativeDistribution) {
119  }
120 
122  ContinuousAttribute (const ContinuousAttribute& iContinuousAttribute)
123  : _cumulativeDistribution (iContinuousAttribute._cumulativeDistribution),
124  _inverseCumulativeDistribution (iContinuousAttribute._inverseCumulativeDistribution) {
125  }
126 
128  virtual ~ContinuousAttribute () { }
129 
133  for (typename ContinuousDistribution_T::iterator itCumulativeDistribution =
134  _cumulativeDistribution.begin();
135  itCumulativeDistribution != _cumulativeDistribution.end();
136  ++itCumulativeDistribution) {
137  _inverseCumulativeDistribution.
138  insert (typename ContinuousInverseDistribution_T::
139  value_type (itCumulativeDistribution->second,
140  itCumulativeDistribution->first));
141  }
142  }
143 
144  private:
145  // ////////// Attributes //////////
146 
148  ContinuousDistribution_T _cumulativeDistribution;
149 
151  ContinuousInverseDistribution_T _inverseCumulativeDistribution;
152  };
153 
154 }
155 #endif // __STDAIR_BAS_CONTINUOUSATTRIBUTE_HPP