RMOL Logo Get Revenue Management Optimisation Library at SourceForge.net. Fast, secure and Free Open Source software downloads

BucketHolder.cpp

Go to the documentation of this file.
00001 // //////////////////////////////////////////////////////////////////////
00002 // Import section
00003 // //////////////////////////////////////////////////////////////////////
00004 // C
00005 #include <assert.h>
00006 // STL
00007 #include <iostream>
00008 #include <iomanip>
00009 // RMOL
00010 #include <rmol/bom/Bucket.hpp>
00011 #include <rmol/bom/BucketHolder.hpp>
00012 
00013 namespace RMOL {
00014 
00015   // //////////////////////////////////////////////////////////////////////
00016   BucketHolder::BucketHolder () :
00017     _cabinCapacity (100.0),
00018     _totalMeanDemand (0.0), _demandFactor (0.0), _optimalRevenue (0.0) {
00019   }
00020 
00021   // //////////////////////////////////////////////////////////////////////
00022   BucketHolder::BucketHolder (const double iCabinCapacity) :
00023     _cabinCapacity (iCabinCapacity),
00024     _totalMeanDemand (0.0), _demandFactor (0.0), _optimalRevenue (0.0) {
00025   }
00026 
00027   // //////////////////////////////////////////////////////////////////////
00028   BucketHolder::~BucketHolder() {
00029     _bucketList.clear ();
00030   }
00031 
00032   // //////////////////////////////////////////////////////////////////////
00033   const short BucketHolder::getSize () const {
00034     return _bucketList.size();
00035   }
00036 
00037   // //////////////////////////////////////////////////////////////////////
00038   const std::string BucketHolder::describeShortKey() const {
00039     std::ostringstream oStr;
00040     oStr << _cabinCapacity;
00041     return oStr.str();
00042   }
00043   
00044   // //////////////////////////////////////////////////////////////////////
00045   const std::string BucketHolder::describeKey() const {
00046     return describeShortKey();
00047   }
00048 
00049   // //////////////////////////////////////////////////////////////////////
00050   std::string BucketHolder::toString() const {
00051     std::ostringstream oStr;
00052     oStr << describeShortKey()
00053          << ", " << _totalMeanDemand
00054          << ", " << _demandFactor  << ", " << _optimalRevenue
00055          << std::endl;
00056     
00057     return oStr.str();
00058   }   
00059 
00060   // //////////////////////////////////////////////////////////////////////
00061   void BucketHolder::toStream (std::ostream& ioOut) const {
00062     ioOut << toString();
00063   }
00064   
00065   // //////////////////////////////////////////////////////////////////////
00066   void BucketHolder::fromStream (std::istream& ioIn) {
00067   }
00068   
00069   // //////////////////////////////////////////////////////////////////////
00070   const std::string BucketHolder::shortDisplay() const {
00071     std::ostringstream oStr;
00072     oStr << describeKey();
00073     return oStr.str();
00074   }
00075   
00076   // //////////////////////////////////////////////////////////////////////
00077   const std::string BucketHolder::display() const {
00078     std::ostringstream oStr;
00079     oStr << shortDisplay();
00080     // Generate a CSV (Comma Separated Values) output
00081     oStr << "Class; Price; Mean; Std Dev; Protection; Cum. Protection; Cum. Bkg Limit; "
00082           << std::endl;
00083 
00084     BucketList_T::const_iterator itBucket = _bucketList.begin();
00085     for (short j=1; itBucket != _bucketList.end(); itBucket++, j++) {
00086       const Bucket* currentBucket_ptr = *itBucket;
00087       assert (currentBucket_ptr != NULL);
00088       
00089       oStr << j << "; " << currentBucket_ptr->display();
00090     }
00091 
00092     oStr << "Cabin Capacity = " << _cabinCapacity
00093          << "; Total Mean Demand = " << _totalMeanDemand
00094          << "; Demand Factor = " << _demandFactor
00095          << "; Optimal Revenue = " << _optimalRevenue << std::endl;
00096     return oStr.str();
00097   }
00098 
00099   // //////////////////////////////////////////////////////////////////////
00100   Bucket& BucketHolder::getCurrentBucket () const {
00101     Bucket* resultBucket_ptr = *_itCurrentBucket;
00102     assert (resultBucket_ptr != NULL);
00103     
00104     return (*resultBucket_ptr);
00105   }
00106 
00107   // //////////////////////////////////////////////////////////////////////
00108   Bucket& BucketHolder::getNextBucket () const {
00109     Bucket* resultBucket_ptr = *_itNextBucket;
00110     assert (resultBucket_ptr != NULL);
00111     
00112     return (*resultBucket_ptr);
00113   }
00114 
00115   // //////////////////////////////////////////////////////////////////////
00116   Bucket& BucketHolder::getTaggedBucket () const {
00117     Bucket* resultBucket_ptr = *_itTaggedBucket;
00118     assert (resultBucket_ptr != NULL);
00119     
00120     return (*resultBucket_ptr);
00121   }
00122 
00123   // //////////////////////////////////////////////////////////////////////
00124   void BucketHolder::begin () {
00125     _itCurrentBucket = _bucketList.begin();
00126     _itNextBucket = _bucketList.begin();
00127     if (_itNextBucket != _bucketList.end()) {
00128       _itNextBucket++;
00129     }
00130   }
00131 
00132   // //////////////////////////////////////////////////////////////////////
00133   void BucketHolder::tag () {
00134       _itTaggedBucket = _itCurrentBucket;
00135   }
00136 
00137   // //////////////////////////////////////////////////////////////////////
00138   bool BucketHolder::hasNotReachedEnd () const {
00139     bool result = (_itCurrentBucket != _bucketList.end());
00140     return result;
00141   }
00142 
00143   // //////////////////////////////////////////////////////////////////////
00144   void BucketHolder::iterate () {
00145     if (_itCurrentBucket != _bucketList.end()) {
00146       _itCurrentBucket++;
00147     }
00148     if (_itNextBucket != _bucketList.end()) {
00149       _itNextBucket++;
00150     }
00151   }
00152 
00153   // //////////////////////////////////////////////////////////////////////
00154   void BucketHolder::calculateMeanDemandAndOptimalRevenue () {
00155     _totalMeanDemand = 0.0;
00156     _optimalRevenue = 0.0;
00157 
00158     for (BucketList_T::const_iterator itBucket = _bucketList.begin();
00159          itBucket != _bucketList.end(); itBucket++) {
00160       const Bucket* currentBucket_ptr = *itBucket;
00161       assert (currentBucket_ptr != NULL);
00162 
00163       // Mean Demand
00164       const double currentMeanDemand = currentBucket_ptr->getMean();
00165       _totalMeanDemand += currentMeanDemand;
00166 
00167       // Optimal Revenue
00168       const double currentPrice = currentBucket_ptr->getAverageYield();
00169       const double currentProtection = currentBucket_ptr->getProtection();
00170       const double bucketOptimalRevenue = currentPrice * currentProtection;
00171       _optimalRevenue += bucketOptimalRevenue;
00172     }
00173 
00174     if (_cabinCapacity != 0.0) {
00175       _demandFactor = _totalMeanDemand / _cabinCapacity;
00176     }
00177   }
00178   
00179   // //////////////////////////////////////////////////////////////////////
00180   void BucketHolder::calculateProtectionAndBookingLimits () {
00181     // Number of classes/buckets: n
00182     const short nbOfClasses = getSize();
00183 
00189     begin();
00190     Bucket& firstBucket = getCurrentBucket();
00191 
00192     // Set the cumulated booking limit of Bucket(1) to be equal to the capacity
00193     firstBucket.setCumulatedBookingLimit (_cabinCapacity);
00194 
00197     firstBucket.setProtection (firstBucket.getCumulatedProtection());
00198 
00199     for (short j=1 ; j <= nbOfClasses - 1; j++, iterate()) {
00201       Bucket& currentBucket = getCurrentBucket();
00202       Bucket& nextBucket = getNextBucket();
00203 
00208       const double yjm1 = currentBucket.getCumulatedProtection();
00209       nextBucket.setCumulatedBookingLimit (_cabinCapacity - yjm1);
00210 
00213       const double yj = nextBucket.getCumulatedProtection();
00214       nextBucket.setProtection (yj - yjm1);
00215     }
00216   }
00217 
00218   // //////////////////////////////////////////////////////////////////////
00219   void BucketHolder::recalculate () {
00220     // Re-calculate the booking limits
00221     calculateProtectionAndBookingLimits();
00222     
00223     // Re-calculate the Optimal Revenue
00224     calculateMeanDemandAndOptimalRevenue();
00225   }
00226 
00227   // //////////////////////////////////////////////////////////////////////
00228   void BucketHolder::
00229   fillup (BookingLimitVector_T& ioBookingLimitVector) const {
00230     BucketList_T::const_iterator itBucket = _bucketList.begin();
00231     for (short j=1; itBucket != _bucketList.end(); itBucket++, j++) {
00232       const Bucket* currentBucket_ptr = *itBucket;
00233       assert (currentBucket_ptr != NULL);
00234       
00235       const double lCumulatedBookingLimit =
00236         currentBucket_ptr->getCumulatedBookingLimit();
00237       ioBookingLimitVector.push_back(lCumulatedBookingLimit);
00238     }
00239 
00240   }
00241 
00242 }
SourceForge Logo

Generated on Sun Jun 14 23:33:59 2009 for RMOL by Doxygen 1.5.8