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

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