RMOL Logo  1.00.0
C++ library of Revenue Management and Optimisation classes and functions
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
HybridForecasting.cpp
Go to the documentation of this file.
1 // //////////////////////////////////////////////////////////////////////
2 // Import section
3 // //////////////////////////////////////////////////////////////////////
4 // STL
5 #include <cassert>
6 #include <sstream>
7 #include <cmath>
8 // StdAir
9 #include <stdair/basic/BasConst_General.hpp>
10 #include <stdair/basic/BasConst_Inventory.hpp>
11 #include <stdair/basic/RandomGeneration.hpp>
12 #include <stdair/bom/BomManager.hpp>
13 #include <stdair/bom/LegDate.hpp>
14 #include <stdair/bom/SegmentDate.hpp>
15 #include <stdair/bom/LegCabin.hpp>
16 #include <stdair/bom/SegmentCabin.hpp>
17 #include <stdair/bom/SegmentSnapshotTable.hpp>
18 #include <stdair/bom/BookingClass.hpp>
19 #include <stdair/service/Logger.hpp>
20 // RMOL
21 #include <rmol/bom/Utilities.hpp>
28 
29 namespace RMOL {
30  // ////////////////////////////////////////////////////////////////////
32  forecast (stdair::SegmentCabin& ioSegmentCabin,
33  const stdair::Date_T& iCurrentDate,
34  const stdair::DTD_T& iCurrentDTD,
35  const stdair::UnconstrainingMethod& iUnconstrainingMethod,
36  const stdair::NbOfSegments_T& iNbOfDepartedSegments) {
37  // Call QForecasting to treat the price-oriented demand.
38  QForecasting::forecast (ioSegmentCabin, iCurrentDate, iCurrentDTD,
39  iUnconstrainingMethod, iNbOfDepartedSegments);
40 
41  // Retrieve the snapshot table.
42  const stdair::SegmentSnapshotTable& lSegmentSnapshotTable =
43  ioSegmentCabin.getSegmentSnapshotTable();
44 
45  // Retrieve the booking class list.
46  const stdair::BookingClassList_T& lBCList =
47  stdair::BomManager::getList<stdair::BookingClass>(ioSegmentCabin);
48 
49  // Browse all remaining DCP's and do unconstraining, forecasting for
50  // all product-oriented demand.
51  const stdair::DCPList_T lWholeDCPList = stdair::DEFAULT_DCP_LIST;
52  stdair::DCPList_T::const_iterator itDCP = lWholeDCPList.begin();
53  stdair::DCPList_T::const_iterator itNextDCP = itDCP; ++itNextDCP;
54  for (; itNextDCP != lWholeDCPList.end(); ++itDCP, ++itNextDCP) {
55  const stdair::DCP_T& lCurrentDCP = *itDCP;
56  const stdair::DCP_T& lNextDCP = *itNextDCP;
57 
58  // The end of the interval is after the current DTD.
59  if (lNextDCP < iCurrentDTD) {
60  // Get the number of similar segments which has already passed the
61  // (lNextDCP+1)
62  const stdair::NbOfSegments_T& lNbOfUsableSegments =
64  getNbOfSegmentAlreadyPassedThisDTD (lSegmentSnapshotTable,
65  lNextDCP+1,
66  iCurrentDate);
67  stdair::NbOfSegments_T lSegmentBegin = 0;
68  const stdair::NbOfSegments_T lSegmentEnd = lNbOfUsableSegments-1;
69  if (iNbOfDepartedSegments > 52) {
70  lSegmentBegin = iNbOfDepartedSegments - 52;
71  }
72 
73  // Browse the list of booking classes and forecast the product-oriented
74  // demand for each class.
75  for (stdair::BookingClassList_T::const_iterator itBC = lBCList.begin();
76  itBC != lBCList.end(); ++itBC) {
77  stdair::BookingClass* lBC_ptr = *itBC;
78  assert (lBC_ptr != NULL);
79 
80  // Retrieve the historical product-oriented bookings for the
81  // given class.
82  HistoricalBookingHolder lHBHolder;
83  prepareProductOrientedHistoricalBooking (ioSegmentCabin, *lBC_ptr,
84  lSegmentSnapshotTable,
85  lHBHolder,
86  lCurrentDCP, lNextDCP,
87  lSegmentBegin, lSegmentEnd);
88 
89  // Unconstrain the historical bookings.
90  Detruncator::unconstrain (lHBHolder, iUnconstrainingMethod);
91 
92  // Retrieve the historical unconstrained demand and perform the
93  // forecasting.
94  stdair::UncDemVector_T lUncDemVector;
95  const short lNbOfHistoricalFlights = lHBHolder.getNbOfFlights();
96  for (short i = 0; i < lNbOfHistoricalFlights; ++i) {
97  const stdair::NbOfBookings_T& lUncDemand =
98  lHBHolder.getUnconstrainedDemand (i);
99  lUncDemVector.push_back (lUncDemand);
100  }
101  stdair::MeanValue_T lMean = 0.0;
102  stdair::StdDevValue_T lStdDev = 0.0;
104  lMean, lStdDev);
105 
106  // Add the demand forecast to the booking class.
107  const stdair::MeanValue_T& lCurrentMean = lBC_ptr->getProductDemMean();
108  const stdair::StdDevValue_T& lCurrentStdDev =
109  lBC_ptr->getProductDemStdDev();
110 
111  const stdair::MeanValue_T lNewMean = lCurrentMean + lMean;
112  const stdair::StdDevValue_T lNewStdDev =
113  std::sqrt (lCurrentStdDev * lCurrentStdDev + lStdDev * lStdDev);
114 
115  lBC_ptr->setProductDemMean (lNewMean);
116  lBC_ptr->setProductDemStdDev (lNewStdDev);
117  }
118  }
119  }
120  return true;
121  }
122 
123  // ////////////////////////////////////////////////////////////////////
125  (const stdair::SegmentCabin& iSegmentCabin,
126  const stdair::BookingClass& iBookingClass,
127  const stdair::SegmentSnapshotTable& iSegmentSnapshotTable,
128  HistoricalBookingHolder& ioHBHolder,
129  const stdair::DCP_T& iDCPBegin, const stdair::DCP_T& iDCPEnd,
130  const stdair::NbOfSegments_T& iSegmentBegin,
131  const stdair::NbOfSegments_T& iSegmentEnd) {
132 
133  // Retrieve the booking class index within the snapshot table
134  const stdair::ClassIndex_T& lClassIdx =
135  iSegmentSnapshotTable.getClassIndex (iBookingClass.describeKey());
136 
137  // Retrieve the gross daily booking and availability snapshots.
138  const stdair::ConstSegmentCabinDTDRangeSnapshotView_T lBookingView =
139  iSegmentSnapshotTable.getConstSegmentCabinDTDRangeProductOrientedGrossBookingSnapshotView (iSegmentBegin, iSegmentEnd, iDCPEnd, iDCPBegin);
140  const stdair::ConstSegmentCabinDTDRangeSnapshotView_T lAvlView =
141  iSegmentSnapshotTable.getConstSegmentCabinDTDRangeAvailabilitySnapshotView (iSegmentBegin, iSegmentEnd, iDCPEnd, iDCPBegin);
142 
143  // Browse the list of segments and build the historical booking holder.
144  const stdair::ClassIndexMap_T& lVTIdxMap =
145  iSegmentSnapshotTable.getClassIndexMap();
146  const stdair::NbOfClasses_T lNbOfClasses = lVTIdxMap.size();
147 
148  for (short i = 0; i <= iSegmentEnd-iSegmentBegin; ++i) {
149  stdair::Flag_T lCensorshipFlag = false;
150  const short lNbOfDTDs = iDCPBegin - iDCPEnd + 1;
151  const stdair::UnsignedIndex_T lIdx = i*lNbOfClasses + lClassIdx;
152 
153  // Parse the DTDs during the period and compute the censorship flag
154  for (short j = 0; j < lNbOfDTDs; ++j) {
155  // Check if the data has been censored during this day.
156  // STDAIR_LOG_DEBUG ("i: " << i << ", NbOfClasses: " << lNbOfClasses
157  // << ", ClassIdx: " << iClassIdx << ", j: " << j);
158  if (lAvlView[lIdx][j] < 1.0) {
159  lCensorshipFlag = true;
160  break;
161  }
162  }
163 
164  // Retrieve the historical product-oriented bookings
165  stdair::NbOfBookings_T lNbOfHistoricalBkgs = 0.0;
166  for (short j = 0; j < lNbOfDTDs; ++j) {
167  lNbOfHistoricalBkgs += lBookingView[lIdx][j];
168  }
169  HistoricalBooking lHistoricalBkg (lNbOfHistoricalBkgs, lCensorshipFlag);
170  ioHBHolder.addHistoricalBooking (lHistoricalBkg);
171  }
172  }
173 
174 }