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

rmol/batches/rmol.cpp

Go to the documentation of this file.
00001 // STL
00002 #include <cassert>
00003 #include <iostream>
00004 #include <sstream>
00005 #include <fstream>
00006 #include <string>
00007 // Boost (Extended STL)
00008 #include <boost/date_time/posix_time/posix_time.hpp>
00009 #include <boost/date_time/gregorian/gregorian.hpp>
00010 #include <boost/program_options.hpp>
00011 // StdAir
00012 #include <stdair/service/Logger.hpp>
00013 // RMOL
00014 #include <rmol/basic/BasConst_General.hpp>
00015 #include <rmol/RMOL_Service.hpp>
00016 #include <rmol/config/rmol-paths.hpp>
00017 
00018 // //////// Constants //////
00020 const std::string K_RMOL_DEFAULT_LOG_FILENAME ("rmol.log");
00021 
00024 const bool K_RMOL_DEFAULT_BUILT_IN_INPUT = false;
00025 
00027 const std::string K_RMOL_DEFAULT_INPUT_FILENAME (STDAIR_SAMPLE_DIR "/rm01.csv");
00028 
00030 const int K_RMOL_DEFAULT_RANDOM_DRAWS = RMOL::DEFAULT_NUMBER_OF_DRAWS_FOR_MC_SIMULATION;
00031 
00033 const double K_RMOL_DEFAULT_CAPACITY = 500.0;
00034 
00044 const short K_RMOL_DEFAULT_METHOD = 0;
00045 
00046 // ///////// Parsing of Options & Configuration /////////
00047 // A helper function to simplify the main part.
00048 template<class T> std::ostream& operator<< (std::ostream& os,
00049                                             const std::vector<T>& v) {
00050   std::copy (v.begin(), v.end(), std::ostream_iterator<T> (std::cout, " ")); 
00051   return os;
00052 }
00053 
00055 const int K_RMOL_EARLY_RETURN_STATUS = 99;
00056 
00058 int readConfiguration(int argc, char* argv[], 
00059                       int& ioRandomDraws, double& ioCapacity, 
00060                       short& ioMethod, bool& ioIsBuiltin,
00061                       std::string& ioInputFilename, std::string& ioLogFilename){
00062 
00063   // Default for the built-in input
00064   ioIsBuiltin = K_RMOL_DEFAULT_BUILT_IN_INPUT;
00065     
00066   // Declare a group of options that will be allowed only on command line
00067   boost::program_options::options_description generic ("Generic options");
00068   generic.add_options()
00069     ("prefix", "print installation prefix")
00070     ("version,v", "print version string")
00071     ("help,h", "produce help message");
00072     
00073   // Declare a group of options that will be allowed both on command
00074   // line and in config file
00075   boost::program_options::options_description config ("Configuration");
00076   config.add_options()
00077     ("draws,d",
00078      boost::program_options::value<int>(&ioRandomDraws)->default_value(K_RMOL_DEFAULT_RANDOM_DRAWS), 
00079      "Number of to-be-generated random draws")
00080     ("capacity,c",
00081      boost::program_options::value<double>(&ioCapacity)->default_value(K_RMOL_DEFAULT_CAPACITY), 
00082      "Resource capacity (e.g., for a flight leg)")
00083     ("method,m",
00084      boost::program_options::value<short>(&ioMethod)->default_value(K_RMOL_DEFAULT_METHOD), 
00085      "Revenue Management method to be used (0 = Monte-Carlo, 1 = Dynamic Programming, 2 = EMSR, 3 = EMSR-a, 4 = EMSR-b)")
00086     ("builtin,b",
00087      "The cabin set up can be either built-in or parsed from an input file. That latter must then be given with the -i/--input option")
00088     ("input,i",
00089      boost::program_options::value< std::string >(&ioInputFilename)->default_value(K_RMOL_DEFAULT_INPUT_FILENAME),
00090      "(CSV) input file for the demand distribution parameters and resource (leg-cabin) capacities")
00091     ("log,l",
00092      boost::program_options::value< std::string >(&ioLogFilename)->default_value(K_RMOL_DEFAULT_LOG_FILENAME),
00093      "Filename for the logs")
00094     ;
00095 
00096   // Hidden options, will be allowed both on command line and
00097   // in config file, but will not be shown to the user.
00098   boost::program_options::options_description hidden ("Hidden options");
00099   hidden.add_options()
00100     ("copyright",
00101      boost::program_options::value< std::vector<std::string> >(),
00102      "Show the copyright (license)");
00103         
00104   boost::program_options::options_description cmdline_options;
00105   cmdline_options.add(generic).add(config).add(hidden);
00106 
00107   boost::program_options::options_description config_file_options;
00108   config_file_options.add(config).add(hidden);
00109 
00110   boost::program_options::options_description visible ("Allowed options");
00111   visible.add(generic).add(config);
00112         
00113   boost::program_options::positional_options_description p;
00114   p.add ("copyright", -1);
00115         
00116   boost::program_options::variables_map vm;
00117   boost::program_options::
00118     store (boost::program_options::command_line_parser (argc, argv).
00119            options (cmdline_options).positional(p).run(), vm);
00120 
00121   std::ifstream ifs ("rmol.cfg");
00122   boost::program_options::store (parse_config_file (ifs, config_file_options),
00123                                  vm);
00124   boost::program_options::notify (vm);
00125     
00126   if (vm.count ("help")) {
00127     std::cout << visible << std::endl;
00128     return K_RMOL_EARLY_RETURN_STATUS;
00129   }
00130 
00131   if (vm.count ("version")) {
00132     std::cout << PACKAGE_NAME << ", version " << PACKAGE_VERSION << std::endl;
00133     return K_RMOL_EARLY_RETURN_STATUS;
00134   }
00135 
00136   if (vm.count ("prefix")) {
00137     std::cout << "Installation prefix: " << PREFIXDIR << std::endl;
00138     return K_RMOL_EARLY_RETURN_STATUS;
00139   }
00140 
00141   if (vm.count ("builtin")) {
00142     ioIsBuiltin = true;
00143   }
00144   const std::string isBuiltinStr = (ioIsBuiltin == true)?"yes":"no";
00145   std::cout << "The BOM should be built-in? " << isBuiltinStr << std::endl;
00146 
00147   if (ioIsBuiltin == false) {
00148     if (vm.count ("input")) {
00149       ioInputFilename = vm["input"].as< std::string >();
00150       std::cout << "Input filename is: " << ioInputFilename << std::endl;
00151     }
00152   }
00153 
00154   if (vm.count ("log")) {
00155     ioLogFilename = vm["log"].as< std::string >();
00156     std::cout << "Log filename is: " << ioLogFilename << std::endl;
00157   }
00158 
00159   std::cout << "The number of random draws is: " << ioRandomDraws << std::endl;
00160   std::cout << "The resource capacity is: " << ioCapacity << std::endl;
00161   std::cout << "The optimisation method is: " << ioMethod << std::endl;
00162   std::cout << std::endl;
00163   
00164   return 0;
00165 }
00166 
00167 // /////////////////////////////////////////////////////
00168 void optimise (RMOL::RMOL_Service& rmolService,
00169                const short& iMethod, const int& iRandomDraws) {
00170 
00171   switch (iMethod) {
00172   case 0: {
00173     // Calculate the optimal protections by the Monte Carlo
00174     // Integration approach
00175     rmolService.optimalOptimisationByMCIntegration (iRandomDraws);
00176     break;
00177   }
00178   case 1: {
00179     // Calculate the optimal protections by DP.
00180     rmolService.optimalOptimisationByDP ();
00181     break;
00182   }
00183   case 2: {
00184     // Calculate the Bid-Price Vector by EMSR
00185     rmolService.heuristicOptimisationByEmsr ();
00186     break;
00187   }
00188   case 3: {
00189     // Calculate the protections by EMSR-a
00190     rmolService.heuristicOptimisationByEmsrA ();
00191     break;
00192   }
00193   case 4: {
00194     // Calculate the protections by EMSR-b
00195     rmolService.heuristicOptimisationByEmsrB ();
00196     break;
00197   }
00198   default: {
00199     rmolService.optimalOptimisationByMCIntegration (iRandomDraws);
00200   }
00201   }
00202 }
00203 
00204 // ///////// M A I N ////////////
00205 int main (int argc, char* argv[]) {
00206 
00207   // Number of random draws to be generated (best if greater than 100)
00208   int lRandomDraws = 0;
00209     
00210   // Cabin Capacity (it must be greater then 100 here)
00211   double lCapacity = 0.0;
00212 
00213   // Methods of optimisation (0 = Monte-Carlo, 1 = Dynamic Programming, 
00214   // 2 = EMSR, 3 = EMSR-a, 4 = EMSR-b)
00215   short lMethod = 0;   
00216     
00217   // Built-in
00218   bool isBuiltin;
00219     
00220   // Input file name
00221   std::string lInputFilename;
00222 
00223   // Output log File
00224   std::string lLogFilename;
00225 
00226   // Call the command-line option parser
00227   const int lOptionParserStatus = 
00228     readConfiguration (argc, argv, lRandomDraws, lCapacity, lMethod,
00229                        isBuiltin, lInputFilename, lLogFilename);
00230 
00231   if (lOptionParserStatus == K_RMOL_EARLY_RETURN_STATUS) {
00232     return 0;
00233   }
00234 
00235   // Set the log parameters
00236   std::ofstream logOutputFile;
00237   // Open and clean the log outputfile
00238   logOutputFile.open (lLogFilename.c_str());
00239   logOutputFile.clear();
00240 
00241   // Initialise the log stream
00242   const stdair::BasLogParams lLogParams (stdair::LOG::DEBUG, logOutputFile);
00243 
00244   // Initialise the RMOL service
00245   RMOL::RMOL_Service rmolService (lLogParams);
00246 
00247   if (isBuiltin == true) {
00248     // DEBUG
00249     STDAIR_LOG_DEBUG ("No input file has been given."
00250                       "A sample BOM tree will therefore be built.");
00251 
00252     // Build a sample BOM tree
00253     rmolService.buildSampleBom();
00254 
00255   } else {
00256     // DEBUG
00257     STDAIR_LOG_DEBUG ("RMOL will parse " << lInputFilename
00258                       << " and build the corresponding BOM tree.");
00259 
00260     //
00261     rmolService.parseAndLoad (lCapacity, lInputFilename);
00262   }
00263 
00264   // Launch the optimisation
00265   optimise (rmolService, lMethod, lRandomDraws);
00266 
00267   //
00268   logOutputFile.close();
00269 
00270   return 0;     
00271 }