RMOL Logo  1.00.0
C++ library of Revenue Management and Optimisation classes and functions
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
HistoricalBookingHolder.cpp
Go to the documentation of this file.
1 // //////////////////////////////////////////////////////////////////////
2 // Import section
3 // //////////////////////////////////////////////////////////////////////
4 // STL
5 #include <sstream>
6 #include <iostream>
7 #include <iomanip>
8 #include <cmath>
9 #include <cassert>
10 // StdAir
11 #include <stdair/service/Logger.hpp>
12 // RMOL
15 
16 namespace RMOL {
17 
18  // ////////////////////////////////////////////////////////////////////
20  }
21 
22  // ////////////////////////////////////////////////////////////////////
24  _historicalBookingVector.clear();
25  }
26 
27  // ////////////////////////////////////////////////////////////////////
29  return _historicalBookingVector.size();
30  }
31 
32  // ////////////////////////////////////////////////////////////////////
34  short lResult = 0;
35  const short lSize = _historicalBookingVector.size();
36 
37  for (short ite = 0; ite < lSize; ++ite) {
38  const stdair::Flag_T lFlag = _historicalBookingVector.at(ite).getFlag ();
39  if (lFlag == false) {
40  ++ lResult;
41  }
42  }
43 
44  return lResult;
45  }
46 
47  // ////////////////////////////////////////////////////////////////////
48  const stdair::NbOfBookings_T HistoricalBookingHolder::
50  stdair::NbOfBookings_T lResult = 0;
51  const short lSize = _historicalBookingVector.size();
52 
53  for (short ite = 0; ite < lSize; ++ite) {
54  const HistoricalBooking& lHistorialBooking =
55  _historicalBookingVector.at (ite);
56  const stdair::Flag_T lFlag = lHistorialBooking.getFlag ();
57  if (lFlag == false) {
58  const stdair::NbOfBookings_T& lBooking =
59  lHistorialBooking.getNbOfBookings ();
60  lResult += lBooking;
61  }
62  }
63 
64  return lResult;
65  }
66 
67  // ////////////////////////////////////////////////////////////////////
68  const double HistoricalBookingHolder::
69  getUncensoredStandardDeviation (const double& iMeanOfUncensoredBookings,
70  const short iNbOfUncensoredData) const {
71 
72  double lResult = 0;
73  const short lSize = _historicalBookingVector.size();
74 
75  for (short ite = 0; ite < lSize; ++ite) {
76  const stdair::Flag_T lFlag = _historicalBookingVector.at(ite).getFlag ();
77  if (lFlag == false) {
78  const HistoricalBooking& lHistorialBooking =
79  _historicalBookingVector.at (ite);
80 
81  const stdair::NbOfBookings_T& lBooking =
82  lHistorialBooking.getNbOfBookings ();
83 
84  lResult += (lBooking - iMeanOfUncensoredBookings)
85  * (lBooking - iMeanOfUncensoredBookings);
86  }
87  }
88  lResult /= (iNbOfUncensoredData - 1);
89  lResult = std::sqrt (lResult);
90 
91  return lResult;
92  }
93 
94  // ////////////////////////////////////////////////////////////////////
96  double lResult = 0;
97  const short lSize = _historicalBookingVector.size();
98 
99  for (short ite = 0; ite < lSize; ++ite) {
100  const HistoricalBooking& lHistorialBooking =
101  _historicalBookingVector.at(ite);
102 
103  const stdair::NbOfBookings_T& lDemand =
104  lHistorialBooking.getUnconstrainedDemand ();
105 
106  lResult += static_cast<double>(lDemand);
107  }
108 
109  lResult /= lSize;
110 
111  return lResult;
112  }
113 
114  // ////////////////////////////////////////////////////////////////////
116  (const double iDemandMean) const {
117  double lResult = 0;
118  const short lSize = _historicalBookingVector.size();
119 
120  for (short ite = 0; ite < lSize; ++ite) {
121  const HistoricalBooking& lHistorialBooking =
122  _historicalBookingVector.at(ite);
123 
124  const stdair::NbOfBookings_T& lDemand =
125  lHistorialBooking.getUnconstrainedDemand ();
126 
127  const double lDoubleDemand = static_cast<double> (lDemand);
128  lResult += (lDoubleDemand - iDemandMean) * (lDoubleDemand - iDemandMean);
129  }
130 
131  lResult /= (lSize - 1);
132 
133  lResult = std::sqrt (lResult);
134 
135  return lResult;
136  }
137 
138  // ////////////////////////////////////////////////////////////////////
139  const std::vector<bool> HistoricalBookingHolder::
141  std::vector<bool> lResult;
142  const short lSize = _historicalBookingVector.size();
143 
144  for (short ite = 0; ite < lSize; ++ite) {
145  const HistoricalBooking& lHistorialBooking =
146  _historicalBookingVector.at(ite);
147  const stdair::Flag_T lFlag = lHistorialBooking.getFlag ();
148  if (lFlag == true) {
149  lResult.push_back(true);
150  }
151  else {
152  lResult.push_back(false);
153  }
154  }
155 
156  return lResult;
157  }
158 
159  // ////////////////////////////////////////////////////////////////////
160  const stdair::NbOfBookings_T& HistoricalBookingHolder::
161  getHistoricalBooking (const short i) const {
162  const HistoricalBooking& lHistorialBooking =
163  _historicalBookingVector.at(i);
164  return lHistorialBooking.getNbOfBookings();
165  }
166 
167  // ////////////////////////////////////////////////////////////////////
168  const stdair::NbOfBookings_T& HistoricalBookingHolder::
169  getUnconstrainedDemand (const short i) const {
170  const HistoricalBooking& lHistorialBooking =
171  _historicalBookingVector.at(i);
172  return lHistorialBooking.getUnconstrainedDemand();
173  }
174 
175  // ////////////////////////////////////////////////////////////////////
176  const stdair::Flag_T& HistoricalBookingHolder::
177  getCensorshipFlag (const short i) const {
178  const HistoricalBooking& lHistorialBooking =
179  _historicalBookingVector.at(i);
180  return lHistorialBooking.getFlag();
181  }
182 
183  // ////////////////////////////////////////////////////////////////////
185  (const stdair::NbOfBookings_T& iExpectedDemand, const short i) {
186  _historicalBookingVector.at(i).setUnconstrainedDemand(iExpectedDemand);
187  }
188 
189  // ////////////////////////////////////////////////////////////////////
190  const stdair::NbOfBookings_T HistoricalBookingHolder::calculateExpectedDemand
191  (const double iMean, const double iSD,
192  const short i, const stdair::NbOfBookings_T iDemand) const {
193 
194  const HistoricalBooking lHistorialBooking =
195  _historicalBookingVector.at(i);
196  const double lBooking =
197  static_cast <double> (lHistorialBooking.getNbOfBookings());
198  double e, d1, d2;
199 
200  e = - (lBooking - iMean) * (lBooking - iMean) * 0.625 / (iSD * iSD);
201  //STDAIR_LOG_DEBUG ("e: " << e);
202  e = exp (e);
203  //STDAIR_LOG_DEBUG ("e: " << e);
204 
205  double s = std::sqrt (1 - e);
206  //STDAIR_LOG_DEBUG ("s: " << s);
207 
208  if (lBooking >= iMean) {
209  if (e < 0.01) {
210  return iDemand;
211  }
212  d1 = 0.5 * (1 - s);
213  }
214  else {
215  d1 = 0.5 * (1 + s);
216  }
217  //STDAIR_LOG_DEBUG ("d1: " << d1);
218 
219  e = - (lBooking - iMean) * (lBooking - iMean) * 0.5 / (iSD * iSD);
220  e = exp (e);
221  d2 = e * iSD / std::sqrt(2 * 3.14159265);
222  //STDAIR_LOG_DEBUG ("d2: " << d2);
223 
224  if (d1 == 0) {
225  return iDemand;
226  }
227 
228  const stdair::NbOfBookings_T lDemand =
229  static_cast<stdair::NbOfBookings_T> (iMean + d2/d1);
230 
231  return lDemand;
232  }
233 
234  // ////////////////////////////////////////////////////////////////////
236  (const HistoricalBooking& iHistoricalBooking) {
237  _historicalBookingVector.push_back(iHistoricalBooking);
238  }
239 
240  // ////////////////////////////////////////////////////////////////////
241  void HistoricalBookingHolder::toStream (std::ostream& ioOut) const {
242  const short lSize = _historicalBookingVector.size();
243 
244  ioOut << "Historical Booking; Unconstrained Demand; Flag" << std::endl;
245 
246  for (short ite = 0; ite < lSize; ++ite) {
247  const HistoricalBooking& lHistorialBooking =
248  _historicalBookingVector.at(ite);
249 
250  const stdair::NbOfBookings_T& lBooking =
251  lHistorialBooking.getNbOfBookings();
252 
253  const stdair::NbOfBookings_T& lDemand =
254  lHistorialBooking.getUnconstrainedDemand();
255 
256  const stdair::Flag_T lFlag = lHistorialBooking.getFlag();
257 
258  ioOut << lBooking << " "
259  << lDemand << " "
260  << lFlag << std::endl;
261  }
262  }
263 
264  // ////////////////////////////////////////////////////////////////////
265  const std::string HistoricalBookingHolder::describe() const {
266  std::ostringstream ostr;
267  ostr << "Holder of HistoricalBooking structs.";
268 
269  return ostr.str();
270  }
271 
272  // ////////////////////////////////////////////////////////////////////
274  toStream (std::cout);
275  }
276 }