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 const double BucketHolder::getPreviousCumulatedProtection () const { 00155 // Get the cumulated protection of the previous bucket. If the 00156 // current bucket is the first one, the function returns 0.0 00157 if (_itCurrentBucket == _bucketList.begin()) { 00158 return 0.0; 00159 } else { 00160 BucketList_T::iterator itPreviousBucket = _itCurrentBucket; 00161 --itPreviousBucket; 00162 Bucket* lPreviousBucket_ptr = *itPreviousBucket; 00163 const double oPreviousCumulatedProtection = 00164 lPreviousBucket_ptr->getCumulatedProtection(); 00165 return oPreviousCumulatedProtection; 00166 } 00167 } 00168 00169 // ////////////////////////////////////////////////////////////////////// 00170 void BucketHolder::calculateMeanDemandAndOptimalRevenue () { 00171 _totalMeanDemand = 0.0; 00172 _optimalRevenue = 0.0; 00173 00174 for (BucketList_T::const_iterator itBucket = _bucketList.begin(); 00175 itBucket != _bucketList.end(); itBucket++) { 00176 const Bucket* currentBucket_ptr = *itBucket; 00177 assert (currentBucket_ptr != NULL); 00178 00179 // Mean Demand 00180 const double currentMeanDemand = currentBucket_ptr->getMean(); 00181 _totalMeanDemand += currentMeanDemand; 00182 00183 // Optimal Revenue 00184 const double currentPrice = currentBucket_ptr->getAverageYield(); 00185 const double currentProtection = currentBucket_ptr->getProtection(); 00186 const double bucketOptimalRevenue = currentPrice * currentProtection; 00187 _optimalRevenue += bucketOptimalRevenue; 00188 } 00189 00190 if (_cabinCapacity != 0.0) { 00191 _demandFactor = _totalMeanDemand / _cabinCapacity; 00192 } 00193 } 00194 00195 // ////////////////////////////////////////////////////////////////////// 00196 void BucketHolder::calculateProtectionAndBookingLimits () { 00197 // Number of classes/buckets: n 00198 const short nbOfClasses = getSize(); 00199 00205 begin(); 00206 Bucket& firstBucket = getCurrentBucket(); 00207 00208 // Set the cumulated booking limit of Bucket(1) to be equal to the capacity 00209 firstBucket.setCumulatedBookingLimit (_cabinCapacity); 00210 00213 firstBucket.setProtection (firstBucket.getCumulatedProtection()); 00214 00215 for (short j=1 ; j <= nbOfClasses - 1; j++, iterate()) { 00217 Bucket& currentBucket = getCurrentBucket(); 00218 Bucket& nextBucket = getNextBucket(); 00219 00224 const double yjm1 = currentBucket.getCumulatedProtection(); 00225 nextBucket.setCumulatedBookingLimit (_cabinCapacity - yjm1); 00226 00229 const double yj = nextBucket.getCumulatedProtection(); 00230 nextBucket.setProtection (yj - yjm1); 00231 } 00232 } 00233 00234 // ////////////////////////////////////////////////////////////////////// 00235 void BucketHolder::recalculate () { 00236 // Re-calculate the booking limits 00237 calculateProtectionAndBookingLimits(); 00238 00239 // Re-calculate the Optimal Revenue 00240 calculateMeanDemandAndOptimalRevenue(); 00241 } 00242 00243 // ////////////////////////////////////////////////////////////////////// 00244 void BucketHolder:: 00245 fillup (BookingLimitVector_T& ioBookingLimitVector) const { 00246 BucketList_T::const_iterator itBucket = _bucketList.begin(); 00247 for (short j=1; itBucket != _bucketList.end(); itBucket++, j++) { 00248 const Bucket* currentBucket_ptr = *itBucket; 00249 assert (currentBucket_ptr != NULL); 00250 00251 const double lCumulatedBookingLimit = 00252 currentBucket_ptr->getCumulatedBookingLimit(); 00253 ioBookingLimitVector.push_back(lCumulatedBookingLimit); 00254 } 00255 00256 } 00257 00258 }
Generated on Sat Jun 6 13:48:51 2009 for RMOL by Doxygen 1.5.7.1