RMOL Logo Get Revenue Management Optimisation Library at SourceForge.net. Fast, secure and Free Open Source software downloads
RMOL_Service.cpp
Go to the documentation of this file.
00001 // //////////////////////////////////////////////////////////////////////
00002 // Import section
00003 // //////////////////////////////////////////////////////////////////////
00004 // C
00005 #include <assert.h>
00006 // STL
00007 #include <iomanip>
00008 #include <sstream>
00009 #include <iostream>
00010 // RMOL
00011 #include <rmol/basic/BasConst_RMOL_Service.hpp>
00012 #include <rmol/field/FldYieldRange.hpp>
00013 #include <rmol/field/FldDistributionParameters.hpp>
00014 #include <rmol/bom/Demand.hpp>
00015 #include <rmol/bom/Bucket.hpp>
00016 #include <rmol/bom/BucketHolder.hpp>
00017 #include <rmol/bom/StudyStatManager.hpp>
00018 #include <rmol/factory/FacRmolServiceContext.hpp>
00019 #include <rmol/command/Optimiser.hpp>
00020 #include <rmol/command/Forecaster.hpp>
00021 #include <rmol/service/RMOL_ServiceContext.hpp>
00022 #include <rmol/service/Logger.hpp>
00023 #include <rmol/RMOL_Service.hpp>
00024 
00025 namespace RMOL {
00026 
00027   // //////////////////////////////////////////////////////////////////////
00028   RMOL_Service::RMOL_Service () :
00029     _rmolServiceContext (NULL) {
00030   }
00031 
00032   // //////////////////////////////////////////////////////////////////////
00033   RMOL_Service::RMOL_Service (const RMOL_Service& iService) :
00034     _rmolServiceContext (iService._rmolServiceContext) {
00035   }
00036 
00037   // //////////////////////////////////////////////////////////////////////
00038   RMOL_Service::RMOL_Service (std::ostream& ioLogStream) {
00039     // Initialise the context
00040     init (ioLogStream);
00041   }
00042 
00043   // //////////////////////////////////////////////////////////////////////
00044   RMOL_Service::RMOL_Service (std::ostream& ioLogStream,
00045                               const ResourceCapacity_T iResourceCapacity) {
00046     // Initialise the context
00047     init (ioLogStream, iResourceCapacity);
00048   }
00049 
00050   // //////////////////////////////////////////////////////////////////////
00051   RMOL_Service::~RMOL_Service () {
00052   }
00053 
00054   // //////////////////////////////////////////////////////////////////////
00055   void RMOL_Service::init (std::ostream& ioLogStream) {
00056     // Set the log file
00057     logInit (LOG::DEBUG, ioLogStream);
00058 
00059     // Initialise the context
00060     RMOL_ServiceContext& lRMOL_ServiceContext = 
00061                          FacRmolServiceContext::instance().create ();
00062     _rmolServiceContext = &lRMOL_ServiceContext;
00063   }
00064 
00065   // //////////////////////////////////////////////////////////////////////
00066   void RMOL_Service::init (std::ostream& ioLogStream,
00067                            const ResourceCapacity_T iResourceCapacity) {
00068     // Set the log file
00069     logInit (LOG::DEBUG, ioLogStream);
00070 
00071     // Initialise the context
00072     RMOL_ServiceContext& lRMOL_ServiceContext = 
00073       FacRmolServiceContext::instance().create (iResourceCapacity);
00074     _rmolServiceContext = &lRMOL_ServiceContext;
00075   }
00076   
00077   // //////////////////////////////////////////////////////////////////////
00078   void RMOL_Service::logInit (const LOG::EN_LogLevel iLogLevel,
00079                               std::ostream& ioLogOutputFile) {
00080     Logger::instance().setLogParameters (iLogLevel, ioLogOutputFile);
00081   }
00082 
00083   // //////////////////////////////////////////////////////////////////////
00084   void RMOL_Service::setUpStudyStatManager () {
00085     assert (_rmolServiceContext != NULL);
00086     _rmolServiceContext->setUpStudyStatManager ();
00087   }
00088 
00089   // //////////////////////////////////////////////////////////////////////
00090   void RMOL_Service::
00091   setResourceCapacity (const ResourceCapacity_T iResourceCapacity) {
00092     assert (_rmolServiceContext != NULL);
00093     _rmolServiceContext->setResourceCapacity (iResourceCapacity);
00094   }
00095 
00096   // //////////////////////////////////////////////////////////////////////
00097   void RMOL_Service::addBucket (const double iYieldRange, 
00098                                 const double iDemandMean,
00099                                 const double iDemandStandardDev) {
00100     assert (_rmolServiceContext != NULL);
00101     _rmolServiceContext->addBucket (iYieldRange, iDemandMean,
00102                                     iDemandStandardDev);
00103   }
00104 
00105   // //////////////////////////////////////////////////////////////////////
00106   void RMOL_Service::addBucket(const double iYieldRange, 
00107                                const double iDemandMean,
00108                                const double iDemandStandardDev,
00109                                GeneratedDemandVector_T* ioGeneratedDemandVector){
00110     assert (_rmolServiceContext != NULL);
00111     _rmolServiceContext->addBucket (iYieldRange, iDemandMean,
00112                                     iDemandStandardDev, ioGeneratedDemandVector);
00113   }
00114 
00115   // //////////////////////////////////////////////////////////////////////
00116   GeneratedDemandVector_T* RMOL_Service::
00117   generateDemand (const int K, const double& iMean, const double& iDeviation) {
00118     return _rmolServiceContext->generateDemand (K, iMean, iDeviation);
00119   }
00120 
00121   // //////////////////////////////////////////////////////////////////////
00122   GeneratedDemandVector_T* RMOL_Service::
00123   generateDemand (GeneratedDemandVector_T* ioFirstVector,
00124                   GeneratedDemandVector_T* ioSecondVector) {
00125     return _rmolServiceContext->generateDemand (ioFirstVector, ioSecondVector);
00126   }
00127 
00128   // //////////////////////////////////////////////////////////////////////
00129   void RMOL_Service::readFromInputFile (const std::string& iInputFileName) {
00130     assert (_rmolServiceContext != NULL);
00131     _rmolServiceContext->readFromInputFile (iInputFileName);
00132   }
00133 
00134   // //////////////////////////////////////////////////////////////////////
00135   void RMOL_Service::buildContextForMC (const int K) {
00136     assert (_rmolServiceContext != NULL);
00137     _rmolServiceContext->buildContextForMC (K);
00138   }
00139 
00140   // //////////////////////////////////////////////////////////////////////
00141   void RMOL_Service::reset () {
00142     assert (_rmolServiceContext != NULL);
00143     _rmolServiceContext->reset ();
00144   }
00145   
00146   // //////////////////////////////////////////////////////////////////////
00147   void RMOL_Service::
00148   optimalOptimisationByMCIntegration (const int K) {
00149     
00150     assert (_rmolServiceContext != NULL);
00151     const double iCapacity = _rmolServiceContext->getCapacity();
00152     BucketHolder* oBucketHolder_ptr = _rmolServiceContext->getBucketHolder();
00153     assert (oBucketHolder_ptr != NULL);
00154     BidPriceVector_T lBidPriceVector;
00155 
00156     StudyStatManager* lStudyStatManager_ptr =
00157       _rmolServiceContext->getStudyStatManager();
00158 
00159     if (lStudyStatManager_ptr == NULL) {
00160       Optimiser::optimalOptimisationByMCIntegration (K, iCapacity, 
00161                                                      *oBucketHolder_ptr,
00162                                                      lBidPriceVector);
00163     } else {
00164       Optimiser::optimalOptimisationByMCIntegration (K, iCapacity, 
00165                                                      *oBucketHolder_ptr,
00166                                                      lBidPriceVector,
00167                                                      *lStudyStatManager_ptr);
00168     }
00169     // DEBUG
00170     RMOL_LOG_DEBUG (oBucketHolder_ptr->display());
00171 
00172     std::ostringstream logStream;
00173     logStream << "Bid-Price Vector (BPV): ";
00174     unsigned int size = lBidPriceVector.size();
00175     
00176     for (unsigned int i = 0; i < size; ++i) {
00177       const double bidPrice = lBidPriceVector.at(i);
00178       logStream << std::fixed << std::setprecision (2) << bidPrice << " ";
00179     }
00180     RMOL_LOG_DEBUG (logStream.str());
00181 
00182     if (lStudyStatManager_ptr != NULL) {
00183       RMOL_LOG_DEBUG (lStudyStatManager_ptr->describe());
00184     }
00185   }
00186 
00187   // //////////////////////////////////////////////////////////////////////
00188   void RMOL_Service::
00189   optimalOptimisationByMCIntegration(const int K,
00190                                      BidPriceVector_T& ioBidPriceVector,
00191                                      BookingLimitVector_T& ioBookingLimitVector){
00192     
00193     assert (_rmolServiceContext != NULL);
00194     const double iCapacity = _rmolServiceContext->getCapacity();
00195     BucketHolder* oBucketHolder_ptr = _rmolServiceContext->getBucketHolder();
00196     assert (oBucketHolder_ptr != NULL);
00197 
00198     Optimiser::optimalOptimisationByMCIntegration (K, iCapacity, 
00199                                                    *oBucketHolder_ptr,
00200                                                    ioBidPriceVector);
00201 
00202     // Fill up booking vector
00203     oBucketHolder_ptr->fillup (ioBookingLimitVector);
00204   }
00205 
00206   // //////////////////////////////////////////////////////////////////////
00207   void RMOL_Service::
00208   optimalOptimisationByDP () {
00209     
00210     assert (_rmolServiceContext != NULL);
00211     const double iCapacity = _rmolServiceContext->getCapacity();
00212     BucketHolder* oBucketHolder_ptr = _rmolServiceContext->getBucketHolder();
00213     assert (oBucketHolder_ptr != NULL);
00214 
00215     Optimiser::optimalOptimisationByDP (iCapacity, *oBucketHolder_ptr);
00216 
00217     // DEBUG
00218     RMOL_LOG_DEBUG (oBucketHolder_ptr->display());
00219   }
00220 
00221   // //////////////////////////////////////////////////////////////////////
00222   void RMOL_Service::
00223   optimalOptimisationByDP (BookingLimitVector_T& ioBookingLimitVector) {
00224     
00225     assert (_rmolServiceContext != NULL);
00226     const double iCapacity = _rmolServiceContext->getCapacity();
00227     BucketHolder* oBucketHolder_ptr = _rmolServiceContext->getBucketHolder();
00228     assert (oBucketHolder_ptr != NULL);
00229 
00230     Optimiser::optimalOptimisationByDP (iCapacity, *oBucketHolder_ptr);
00231 
00232     // Fill up booking vector
00233     oBucketHolder_ptr->fillup (ioBookingLimitVector);
00234   }
00235   
00236   // //////////////////////////////////////////////////////////////////////
00237   void RMOL_Service::heuristicOptimisationByEmsr () {
00238     assert (_rmolServiceContext != NULL);
00239     const double iCapacity = _rmolServiceContext->getCapacity();
00240     BucketHolder* oBucketHolder_ptr = _rmolServiceContext->getBucketHolder();
00241     assert (oBucketHolder_ptr != NULL);
00242     BidPriceVector_T lBidPriceVector;
00243     
00244     StudyStatManager* lStudyStatManager_ptr =
00245       _rmolServiceContext->getStudyStatManager();
00246     
00247     if (lStudyStatManager_ptr == NULL) {
00248       Optimiser::heuristicOptimisationByEmsr (iCapacity, *oBucketHolder_ptr,
00249                                               lBidPriceVector);
00250     } else {      
00251       Optimiser::heuristicOptimisationByEmsr (iCapacity, *oBucketHolder_ptr,
00252                                               lBidPriceVector,
00253                                               *lStudyStatManager_ptr);
00254     }
00255     
00256     // DEBUG
00257     RMOL_LOG_DEBUG (oBucketHolder_ptr->display());
00258     std::ostringstream logStream;
00259     logStream << "Bid-Price Vector (BPV): ";
00260     unsigned int size = lBidPriceVector.size();
00261     
00262     for (unsigned int i = 0; i < size; ++i) {
00263       const double bidPrice = lBidPriceVector.at(i);
00264       logStream << std::fixed << std::setprecision (2) << bidPrice << " ";
00265     }
00266     RMOL_LOG_DEBUG (logStream.str());
00267 
00268     if (lStudyStatManager_ptr != NULL) {
00269       RMOL_LOG_DEBUG (lStudyStatManager_ptr->describe());
00270     }
00271   }
00272 
00273   // //////////////////////////////////////////////////////////////////////
00274   void RMOL_Service::
00275   heuristicOptimisationByEmsr (BidPriceVector_T& ioBidPriceVector,
00276                                BookingLimitVector_T& ioBookingLimitVector) {
00277     assert (_rmolServiceContext != NULL);
00278     const double iCapacity = _rmolServiceContext->getCapacity();
00279     BucketHolder* oBucketHolder_ptr = _rmolServiceContext->getBucketHolder();
00280     assert (oBucketHolder_ptr != NULL);
00281 
00282     Optimiser::heuristicOptimisationByEmsr (iCapacity, *oBucketHolder_ptr,
00283                                             ioBidPriceVector);
00284 
00285     // Update the booking limit vector.
00286     for (oBucketHolder_ptr->begin(); oBucketHolder_ptr->hasNotReachedEnd();
00287          oBucketHolder_ptr->iterate()) {
00288       Bucket& currentBucket = oBucketHolder_ptr->getCurrentBucket();
00289       const double lBookingLimit = currentBucket.getCumulatedBookingLimit();
00290       ioBookingLimitVector.push_back (lBookingLimit);
00291     }
00292     
00293   }
00294 
00295   // //////////////////////////////////////////////////////////////////////
00296   void RMOL_Service::heuristicOptimisationByEmsrA () {
00297     assert (_rmolServiceContext != NULL);
00298     const double iCapacity = _rmolServiceContext->getCapacity();
00299     BucketHolder* oBucketHolder_ptr = _rmolServiceContext->getBucketHolder();
00300     assert (oBucketHolder_ptr != NULL);
00301 
00302     Optimiser::heuristicOptimisationByEmsrA (iCapacity, *oBucketHolder_ptr);
00303 
00304     // DEBUG
00305     RMOL_LOG_DEBUG (oBucketHolder_ptr->display());
00306   }
00307 
00308   // //////////////////////////////////////////////////////////////////////
00309   void RMOL_Service::
00310   heuristicOptimisationByEmsrA (BidPriceVector_T& ioBidPriceVector,
00311                                 BookingLimitVector_T& ioBookingLimitVector) {
00312     assert (_rmolServiceContext != NULL);
00313     const double iCapacity = _rmolServiceContext->getCapacity();
00314     BucketHolder* oBucketHolder_ptr = _rmolServiceContext->getBucketHolder();
00315     assert (oBucketHolder_ptr != NULL);
00316 
00317     Optimiser::heuristicOptimisationByEmsrA (iCapacity, *oBucketHolder_ptr);
00318 
00319     // Fill up booking vector
00320     oBucketHolder_ptr->fillup (ioBookingLimitVector);
00321   }
00322   
00323   // //////////////////////////////////////////////////////////////////////
00324   void RMOL_Service::heuristicOptimisationByEmsrAwithSellup 
00325   (SellupProbabilityVector_T& iSellupProbabilityVector) {
00326 
00327     assert (_rmolServiceContext != NULL);
00328     const double iCapacity = _rmolServiceContext->getCapacity();
00329     BucketHolder* ioBucketHolder_ptr = 
00330       _rmolServiceContext->getBucketHolder();
00331     assert (ioBucketHolder_ptr != NULL);
00332 
00333     Optimiser::
00334       heuristicOptimisationByEmsrAwithSellup (iCapacity, 
00335                                               *ioBucketHolder_ptr,
00336                                               iSellupProbabilityVector);
00337 
00338     // DEBUG
00339     RMOL_LOG_DEBUG (ioBucketHolder_ptr->display());
00340   }
00341 
00342   // //////////////////////////////////////////////////////////////////////
00343   void RMOL_Service::heuristicOptimisationByEmsrAwithSellup 
00344   (SellupProbabilityVector_T& iSellupProbabilityVector,
00345    BidPriceVector_T& ioBidPriceVector,
00346    BookingLimitVector_T& ioBookingLimitVector) {
00347     
00348     assert (_rmolServiceContext != NULL);
00349     const double iCapacity = _rmolServiceContext->getCapacity();
00350     BucketHolder* ioBucketHolder_ptr = 
00351       _rmolServiceContext->getBucketHolder();
00352     assert (ioBucketHolder_ptr != NULL);
00353 
00354     Optimiser::
00355       heuristicOptimisationByEmsrAwithSellup (iCapacity, 
00356                                               *ioBucketHolder_ptr,
00357                                               iSellupProbabilityVector);
00358 
00359     // Fill up booking limit vector
00360     ioBucketHolder_ptr->fillup (ioBookingLimitVector);
00361   }
00362 
00363   // //////////////////////////////////////////////////////////////////////
00364   void RMOL_Service::heuristicOptimisationByEmsrB () {
00365     assert (_rmolServiceContext != NULL);
00366     const double iCapacity = _rmolServiceContext->getCapacity();
00367     BucketHolder* oBucketHolder_ptr = _rmolServiceContext->getBucketHolder();
00368     assert (oBucketHolder_ptr != NULL);
00369 
00370     Optimiser::heuristicOptimisationByEmsrB (iCapacity, *oBucketHolder_ptr);
00371 
00372     // DEBUG
00373     RMOL_LOG_DEBUG (oBucketHolder_ptr->display());
00374   }
00375 
00376   // //////////////////////////////////////////////////////////////////////
00377   void RMOL_Service::
00378   heuristicOptimisationByEmsrB (BidPriceVector_T& ioBidPriceVector,
00379                                 BookingLimitVector_T& ioBookingLimitVector) {
00380     assert (_rmolServiceContext != NULL);
00381     const double iCapacity = _rmolServiceContext->getCapacity();
00382     BucketHolder* oBucketHolder_ptr = _rmolServiceContext->getBucketHolder();
00383     assert (oBucketHolder_ptr != NULL);
00384 
00385     Optimiser::heuristicOptimisationByEmsrB (iCapacity, *oBucketHolder_ptr);
00386 
00387     // Fill up booking vector
00388     oBucketHolder_ptr->fillup (ioBookingLimitVector);
00389   }
00390 
00391   // ///////////////////////////////////////////////////////////////////////
00392   void RMOL_Service:: legOptimisationByMC () {
00393     assert (_rmolServiceContext != NULL);
00394     const ResourceCapacity_T iCapacity = _rmolServiceContext->getCapacity();
00395     BucketHolder* oBucketHolder_ptr = _rmolServiceContext->getBucketHolder();
00396     assert (oBucketHolder_ptr != NULL);
00397 
00398     BidPriceVector_T lBidPriceVector;
00399     Optimiser::legOptimisationByMC (iCapacity, *oBucketHolder_ptr,
00400                                     lBidPriceVector);
00401     
00402     // DEBUG
00403     RMOL_LOG_DEBUG (oBucketHolder_ptr->display());
00404     std::ostringstream logStream;
00405     logStream << "Bid-Price Vector (BPV): ";
00406     unsigned int size = lBidPriceVector.size();
00407     
00408     for (unsigned int i = 0; i < size; ++i) {
00409       const double bidPrice = lBidPriceVector.at(i);
00410       logStream << std::fixed << std::setprecision (2) << bidPrice << " ";
00411     }
00412     RMOL_LOG_DEBUG (logStream.str());
00413   }
00414 
00415   // ///////////////////////////////////////////////////////////////////////
00416   void RMOL_Service::
00417   legOptimisationByMC (BidPriceVector_T& ioBidPriceVector,
00418                        BookingLimitVector_T& ioBookingLimitVector) {
00419     assert (_rmolServiceContext != NULL);
00420     const ResourceCapacity_T iCapacity = _rmolServiceContext->getCapacity();
00421     BucketHolder* oBucketHolder_ptr = _rmolServiceContext->getBucketHolder();
00422     assert (oBucketHolder_ptr != NULL);
00423     
00424     Optimiser::legOptimisationByMC (iCapacity, *oBucketHolder_ptr,
00425                                     ioBidPriceVector);
00426 
00427     // Fill up booking vector
00428     oBucketHolder_ptr->fillup (ioBookingLimitVector);
00429   }
00430 
00431   // ///////////////////////////////////////////////////////////////////////
00432   void demandUnconstrainingByExpectationMaximization () {
00433 
00434     // DEBUG
00435     std::ostringstream logStream;
00436     logStream << "Testing demand unconstraining by Expectation Maximization";
00437     RMOL_LOG_DEBUG (logStream.str());
00438 
00439   }
00440 
00441   // ///////////////////////////////////////////////////////////////////////
00442   void RMOL_Service::demandForecastByQForecasting 
00443        (HistoricalDataHolderHolder_T& iHistoricalDataHolderHolder,
00444         PriceHolder_T& iPriceHolder) {
00445 
00446     // Declare forecaster output holder
00447     ForecastedDemandParameterList_T lForecastedDemandParameterList;
00448 
00449 //     // Make an element of a ForecastedDemandParameterList_T. It is needed 
00450 //     // to use the reference of lForecastedDemandParameterList as an 
00451 //     // input argument.
00452 //     double lMean = 0.0; 
00453 //     double lSD = 0.0;
00454 //     std::vector<double> lForecastedDemandParameters;
00455 //     lForecastedDemandParameters.push_back(lMean);
00456 //     lForecastedDemandParameters.push_back(lSD);
00457 
00458 //     // Instantiate the forecaster output holder with the element created above
00459 //     lForecastedDemandParameterList.insert
00460 //                       (std::map<std::string, std::vector<double> >::
00461 //                        value_type("YNCELHR", lForecastedDemandParameters));
00462 
00463 //     ForecastedDemandParameterList_T& lForecastedDemandParameterList_ref = 
00464 //                                               lForecastedDemandParameterList;
00465  
00466 //     // Run Q-Forecasting algorithm
00467 //     Forecaster::demandForecastByQForecasting (oForecastedDemandParameterList,
00468 //                                               iHistoricalDataHolderHolder,
00469 //                                               iPriceHolder);
00470     
00471     // DEBUG
00472 //     RMOL_LOG_DEBUG (oBucketHolder_ptr->display());
00473     std::ostringstream logStream;
00474     logStream << "Testing Demand Forecasting by Q-Forecasting";
00475     RMOL_LOG_DEBUG (logStream.str());
00476     
00477   }
00478 
00479   // ///////////////////////////////////////////////////////////////////////
00480   void RMOL_Service::demandForecastByQForecasting 
00481          (ForecastedDemandParameterList_T oForecastedDemandParameterList, 
00482           HistoricalDataHolderHolder_T& iHistoricalDataHolderHolder, 
00483           PriceHolder_T& iPriceHolder) {
00484 //     assert (_rmolServiceContext != NULL);
00485 //     const BucketHolder* oBucketHolder_ptr = 
00486 //                                      _rmolServiceContext->getBucketHolder();
00487 //     assert (oBucketHolder_ptr != NULL);
00488 
00489     // TO-DO
00490     Forecaster::demandForecastByQForecasting (oForecastedDemandParameterList,
00491                                               iHistoricalDataHolderHolder,
00492                                               iPriceHolder);
00493     
00494     // Fill in the forecasted demand parameters
00495 
00496     
00497   }
00498 
00499   
00500 }