RMOL Logo  0.25.3
C++ library of Revenue Management and Optimisation classes and functions
EMDetruncator.cpp
Go to the documentation of this file.
00001 // //////////////////////////////////////////////////////////////////////
00002 // Import section
00003 // //////////////////////////////////////////////////////////////////////
00004 // STL
00005 #include <iostream>
00006 #include <cmath>
00007 #include <vector>
00008 #include <cassert>
00009 // StdAir
00010 #include <stdair/stdair_basic_types.hpp>
00011 #include <stdair/service/Logger.hpp>
00012 // RMOL
00013 #include <rmol/bom/HistoricalBookingHolder.hpp>
00014 #include <rmol/bom/EMDetruncator.hpp>
00015 
00016 namespace RMOL {
00017     
00018   // ////////////////////////////////////////////////////////////////////
00019   void EMDetruncator::unconstrainUsingEMMethod
00020   (HistoricalBookingHolder& ioHistoricalBookingHolder) {
00021       
00022     // Number of flights.
00023     const short lNbOfFlights =
00024       ioHistoricalBookingHolder.getNbOfFlights();
00025 
00026     // Number of uncensored booking data.
00027     const short lNbOfUncensoredData =
00028       ioHistoricalBookingHolder.getNbOfUncensoredData();
00029 
00030     if (lNbOfUncensoredData > 1) {
00031       // Number of uncensored bookings.
00032       const stdair::NbOfBookings_T lNbOfUncensoredBookings =
00033         ioHistoricalBookingHolder.getNbOfUncensoredBookings();
00034         
00035       const double lMeanOfUncensoredBookings =
00036         static_cast<double>(lNbOfUncensoredBookings/lNbOfUncensoredData);
00037 
00038       const double lStdDevOfUncensoredBookings =
00039         ioHistoricalBookingHolder.getUncensoredStandardDeviation
00040         (lMeanOfUncensoredBookings, lNbOfUncensoredData);
00041 
00042       std::vector<bool> toBeUnconstrained =
00043         ioHistoricalBookingHolder.getListOfToBeUnconstrainedFlags();
00044 
00045       double lDemandMean = lMeanOfUncensoredBookings;
00046       double lStdDev = lStdDevOfUncensoredBookings;
00047 
00048       // DEBUG
00049       STDAIR_LOG_DEBUG ("mean: " << lDemandMean << ", std: " << lStdDev);
00050         
00051       if (lStdDev != 0) {
00052         bool stopUnconstraining = false;
00053         while (stopUnconstraining == false) {
00054           stopUnconstraining = true;
00055             
00056           for (short i = 0; i < lNbOfFlights; ++i) {
00057             if (toBeUnconstrained.at(i) == true) {
00058               // Get the unconstrained demand of the (i+1)-th flight.
00059               const stdair::NbOfBookings_T demand =
00060                 ioHistoricalBookingHolder.getUnconstrainedDemand (i);
00061               //STDAIR_LOG_DEBUG ("demand: " << demand);
00062                 
00063               // Execute the Expectation step.
00064               const stdair::NbOfBookings_T expectedDemand =
00065                 ioHistoricalBookingHolder.
00066                 calculateExpectedDemand (lDemandMean, lStdDev, i, demand);
00067               //STDAIR_LOG_DEBUG ("expected: " << expectedDemand);
00068               assert (expectedDemand >= 0 || expectedDemand < 0);
00069                 
00070               double absDiff =
00071                 static_cast<double>(expectedDemand - demand);
00072                 
00073               if (absDiff < 0) {
00074                 absDiff = - absDiff;
00075               }
00076               if (absDiff < 0.001) {
00077                 toBeUnconstrained.at (i) = false;
00078               }
00079               else {
00080                 stopUnconstraining = false;
00081               }
00082                 
00083               ioHistoricalBookingHolder.setUnconstrainedDemand (expectedDemand,
00084                                                                 i);
00085             }
00086           }
00087             
00088           if (stopUnconstraining == false) {
00089             lDemandMean = ioHistoricalBookingHolder.getDemandMean();
00090             lStdDev =
00091               ioHistoricalBookingHolder.getStandardDeviation (lDemandMean);
00092           }
00093         }
00094       }
00095     }
00096       
00097   }
00098 }