$treeview $search $mathjax
RMOL Logo  1.00.0
$projectbrief
$projectbrief
$searchbox

rmol/command/HybridForecasting.cpp

Go to the documentation of this file.
00001 // //////////////////////////////////////////////////////////////////////
00002 // Import section
00003 // //////////////////////////////////////////////////////////////////////
00004 // STL
00005 #include <cassert>
00006 #include <sstream>
00007 #include <cmath>
00008 // StdAir
00009 #include <stdair/basic/BasConst_General.hpp>
00010 #include <stdair/basic/BasConst_Inventory.hpp>
00011 #include <stdair/basic/RandomGeneration.hpp>
00012 #include <stdair/bom/BomManager.hpp>
00013 #include <stdair/bom/LegDate.hpp>
00014 #include <stdair/bom/SegmentDate.hpp>
00015 #include <stdair/bom/LegCabin.hpp>
00016 #include <stdair/bom/SegmentCabin.hpp>
00017 #include <stdair/bom/SegmentSnapshotTable.hpp>
00018 #include <stdair/bom/BookingClass.hpp>
00019 #include <stdair/service/Logger.hpp>
00020 // RMOL
00021 #include <rmol/bom/Utilities.hpp>
00022 #include <rmol/bom/SegmentSnapshotTableHelper.hpp>
00023 #include <rmol/bom/HistoricalBookingHolder.hpp>
00024 #include <rmol/bom/HistoricalBooking.hpp>
00025 #include <rmol/command/QForecasting.hpp>
00026 #include <rmol/command/HybridForecasting.hpp>
00027 #include <rmol/command/Detruncator.hpp>
00028 
00029 namespace RMOL {
00030   // ////////////////////////////////////////////////////////////////////
00031   bool HybridForecasting::
00032   forecast (stdair::SegmentCabin& ioSegmentCabin,
00033             const stdair::Date_T& iCurrentDate,
00034             const stdair::DTD_T& iCurrentDTD,
00035             const stdair::UnconstrainingMethod& iUnconstrainingMethod,
00036             const stdair::NbOfSegments_T& iNbOfDepartedSegments) {
00037     // Call QForecasting to treat the price-oriented demand.
00038     QForecasting::forecast (ioSegmentCabin, iCurrentDate, iCurrentDTD,
00039                             iUnconstrainingMethod, iNbOfDepartedSegments);
00040     
00041     // Retrieve the snapshot table.
00042     const stdair::SegmentSnapshotTable& lSegmentSnapshotTable =
00043       ioSegmentCabin.getSegmentSnapshotTable();
00044 
00045     // Retrieve the booking class list.
00046     const stdair::BookingClassList_T& lBCList =
00047       stdair::BomManager::getList<stdair::BookingClass>(ioSegmentCabin);      
00048 
00049     // Browse all remaining DCP's and do unconstraining, forecasting for
00050     // all product-oriented demand.
00051     const stdair::DCPList_T lWholeDCPList = stdair::DEFAULT_DCP_LIST;
00052     stdair::DCPList_T::const_iterator itDCP = lWholeDCPList.begin();
00053     stdair::DCPList_T::const_iterator itNextDCP = itDCP; ++itNextDCP;
00054     for (; itNextDCP != lWholeDCPList.end(); ++itDCP, ++itNextDCP) {
00055       const stdair::DCP_T& lCurrentDCP = *itDCP;
00056       const stdair::DCP_T& lNextDCP = *itNextDCP;
00057 
00058       // The end of the interval is after the current DTD.
00059       if (lNextDCP < iCurrentDTD) {
00060         // Get the number of similar segments which has already passed the
00061         // (lNextDCP+1)
00062         const stdair::NbOfSegments_T& lNbOfUsableSegments =
00063           SegmentSnapshotTableHelper::
00064           getNbOfSegmentAlreadyPassedThisDTD (lSegmentSnapshotTable,
00065                                               lNextDCP+1,
00066                                               iCurrentDate);
00067         stdair::NbOfSegments_T lSegmentBegin = 0;
00068         const stdair::NbOfSegments_T lSegmentEnd = lNbOfUsableSegments-1;
00069         if (iNbOfDepartedSegments > 52) {
00070           lSegmentBegin = iNbOfDepartedSegments - 52;
00071         }
00072 
00073         // Browse the list of booking classes and forecast the product-oriented
00074         // demand for each class.
00075         for (stdair::BookingClassList_T::const_iterator itBC = lBCList.begin();
00076              itBC != lBCList.end(); ++itBC) {
00077           stdair::BookingClass* lBC_ptr = *itBC;
00078           assert (lBC_ptr != NULL);
00079           
00080           // Retrieve the historical product-oriented bookings for the
00081           // given class.
00082           HistoricalBookingHolder lHBHolder;
00083           prepareProductOrientedHistoricalBooking (ioSegmentCabin, *lBC_ptr,
00084                                                    lSegmentSnapshotTable,
00085                                                    lHBHolder,
00086                                                    lCurrentDCP, lNextDCP,
00087                                                    lSegmentBegin, lSegmentEnd);
00088           
00089           // Unconstrain the historical bookings.
00090           Detruncator::unconstrain (lHBHolder, iUnconstrainingMethod);
00091 
00092           // Retrieve the historical unconstrained demand and perform the
00093           // forecasting.
00094           stdair::UncDemVector_T lUncDemVector;
00095           const short lNbOfHistoricalFlights = lHBHolder.getNbOfFlights();
00096           for (short i = 0; i < lNbOfHistoricalFlights; ++i) {
00097             const stdair::NbOfBookings_T& lUncDemand =
00098               lHBHolder.getUnconstrainedDemand (i);
00099             lUncDemVector.push_back (lUncDemand);
00100           }
00101           stdair::MeanValue_T lMean = 0.0;
00102           stdair::StdDevValue_T lStdDev = 0.0;
00103           Utilities::computeDistributionParameters (lUncDemVector,
00104                                                     lMean, lStdDev);
00105 
00106           // Add the demand forecast to the booking class.
00107           const stdair::MeanValue_T& lCurrentMean = lBC_ptr->getProductDemMean();
00108           const stdair::StdDevValue_T& lCurrentStdDev = 
00109             lBC_ptr->getProductDemStdDev();
00110 
00111           const stdair::MeanValue_T lNewMean = lCurrentMean + lMean;
00112           const stdair::StdDevValue_T lNewStdDev = 
00113             std::sqrt (lCurrentStdDev * lCurrentStdDev + lStdDev * lStdDev);
00114 
00115           lBC_ptr->setProductDemMean (lNewMean);
00116           lBC_ptr->setProductDemStdDev (lNewStdDev);
00117         }
00118       }
00119     }
00120     return true;
00121   }
00122   
00123   // ////////////////////////////////////////////////////////////////////
00124   void HybridForecasting::prepareProductOrientedHistoricalBooking
00125     (const stdair::SegmentCabin& iSegmentCabin,
00126      const stdair::BookingClass& iBookingClass,
00127      const stdair::SegmentSnapshotTable& iSegmentSnapshotTable,
00128      HistoricalBookingHolder& ioHBHolder,
00129      const stdair::DCP_T& iDCPBegin, const stdair::DCP_T& iDCPEnd,
00130      const stdair::NbOfSegments_T& iSegmentBegin,
00131      const stdair::NbOfSegments_T& iSegmentEnd) {
00132 
00133     // Retrieve the booking class index within the snapshot table
00134     const stdair::ClassIndex_T& lClassIdx = 
00135       iSegmentSnapshotTable.getClassIndex (iBookingClass.describeKey());
00136 
00137     // Retrieve the gross daily booking and availability snapshots.
00138     const stdair::ConstSegmentCabinDTDRangeSnapshotView_T lBookingView =
00139       iSegmentSnapshotTable.getConstSegmentCabinDTDRangeProductOrientedGrossBookingSnapshotView (iSegmentBegin, iSegmentEnd, iDCPEnd, iDCPBegin);
00140     const stdair::ConstSegmentCabinDTDRangeSnapshotView_T lAvlView =
00141       iSegmentSnapshotTable.getConstSegmentCabinDTDRangeAvailabilitySnapshotView (iSegmentBegin, iSegmentEnd, iDCPEnd, iDCPBegin);
00142     
00143     // Browse the list of segments and build the historical booking holder.
00144     const stdair::ClassIndexMap_T& lVTIdxMap =
00145       iSegmentSnapshotTable.getClassIndexMap();
00146     const stdair::NbOfClasses_T lNbOfClasses = lVTIdxMap.size();
00147 
00148     for (short i = 0; i <= iSegmentEnd-iSegmentBegin; ++i) {
00149       stdair::Flag_T lCensorshipFlag = false;
00150       const short lNbOfDTDs = iDCPBegin - iDCPEnd + 1;
00151       const stdair::UnsignedIndex_T lIdx = i*lNbOfClasses + lClassIdx;
00152 
00153       // Parse the DTDs during the period and compute the censorship flag
00154       for (short j = 0; j < lNbOfDTDs; ++j) {
00155         // Check if the data has been censored during this day.
00156         // STDAIR_LOG_DEBUG ("i: " << i << ", NbOfClasses: " << lNbOfClasses
00157         //                   << ", ClassIdx: " << iClassIdx << ", j: " << j);
00158         if (lAvlView[lIdx][j] < 1.0) {
00159           lCensorshipFlag = true;
00160           break;
00161         }
00162       }
00163 
00164       // Retrieve the historical product-oriented bookings
00165       stdair::NbOfBookings_T lNbOfHistoricalBkgs = 0.0;
00166       for (short j = 0; j < lNbOfDTDs; ++j) {
00167         lNbOfHistoricalBkgs += lBookingView[lIdx][j];
00168       }              
00169       HistoricalBooking lHistoricalBkg (lNbOfHistoricalBkgs, lCensorshipFlag);
00170       ioHBHolder.addHistoricalBooking (lHistoricalBkg);
00171     }
00172   }
00173   
00174 }