RMOL Logo  0.25.3
C++ library of Revenue Management and Optimisation classes and functions
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
rmol.cpp
Go to the documentation of this file.
1 // STL
2 #include <cassert>
3 #include <iostream>
4 #include <sstream>
5 #include <fstream>
6 #include <string>
7 // Boost (Extended STL)
8 #include <boost/date_time/posix_time/posix_time.hpp>
9 #include <boost/date_time/gregorian/gregorian.hpp>
10 #include <boost/program_options.hpp>
11 // StdAir
12 #include <stdair/service/Logger.hpp>
13 // RMOL
14 #include <rmol/RMOL_Service.hpp>
16 
17 // //////// Constants //////
19 const std::string K_RMOL_DEFAULT_LOG_FILENAME ("rmol.log");
20 
23 const bool K_RMOL_DEFAULT_BUILT_IN_INPUT = false;
24 
26 const std::string K_RMOL_DEFAULT_INPUT_FILENAME (STDAIR_SAMPLE_DIR "/rm01.csv");
27 
29 const int K_RMOL_DEFAULT_RANDOM_DRAWS = 100000;
30 
32 const double K_RMOL_DEFAULT_CAPACITY = 500.0;
33 
43 const short K_RMOL_DEFAULT_METHOD = 0;
44 
45 // ///////// Parsing of Options & Configuration /////////
46 // A helper function to simplify the main part.
47 template<class T> std::ostream& operator<< (std::ostream& os,
48  const std::vector<T>& v) {
49  std::copy (v.begin(), v.end(), std::ostream_iterator<T> (std::cout, " "));
50  return os;
51 }
52 
55 
57 int readConfiguration(int argc, char* argv[],
58  int& ioRandomDraws, double& ioCapacity,
59  short& ioMethod, bool& ioIsBuiltin,
60  std::string& ioInputFilename, std::string& ioLogFilename){
61 
62  // Default for the built-in input
63  ioIsBuiltin = K_RMOL_DEFAULT_BUILT_IN_INPUT;
64 
65  // Declare a group of options that will be allowed only on command line
66  boost::program_options::options_description generic ("Generic options");
67  generic.add_options()
68  ("prefix", "print installation prefix")
69  ("version,v", "print version string")
70  ("help,h", "produce help message");
71 
72  // Declare a group of options that will be allowed both on command
73  // line and in config file
74  boost::program_options::options_description config ("Configuration");
75  config.add_options()
76  ("draws,d",
77  boost::program_options::value<int>(&ioRandomDraws)->default_value(K_RMOL_DEFAULT_RANDOM_DRAWS),
78  "Number of to-be-generated random draws")
79  ("capacity,c",
80  boost::program_options::value<double>(&ioCapacity)->default_value(K_RMOL_DEFAULT_CAPACITY),
81  "Resource capacity (e.g., for a flight leg)")
82  ("method,m",
83  boost::program_options::value<short>(&ioMethod)->default_value(K_RMOL_DEFAULT_METHOD),
84  "Revenue Management method to be used (0 = Monte-Carlo, 1 = Dynamic Programming, 2 = EMSR, 3 = EMSR-a, 4 = EMSR-b)")
85  ("builtin,b",
86  "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")
87  ("input,i",
88  boost::program_options::value< std::string >(&ioInputFilename)->default_value(K_RMOL_DEFAULT_INPUT_FILENAME),
89  "(CSV) input file for the demand distribution parameters and resource (leg-cabin) capacities")
90  ("log,l",
91  boost::program_options::value< std::string >(&ioLogFilename)->default_value(K_RMOL_DEFAULT_LOG_FILENAME),
92  "Filename for the logs")
93  ;
94 
95  // Hidden options, will be allowed both on command line and
96  // in config file, but will not be shown to the user.
97  boost::program_options::options_description hidden ("Hidden options");
98  hidden.add_options()
99  ("copyright",
100  boost::program_options::value< std::vector<std::string> >(),
101  "Show the copyright (license)");
102 
103  boost::program_options::options_description cmdline_options;
104  cmdline_options.add(generic).add(config).add(hidden);
105 
106  boost::program_options::options_description config_file_options;
107  config_file_options.add(config).add(hidden);
108 
109  boost::program_options::options_description visible ("Allowed options");
110  visible.add(generic).add(config);
111 
112  boost::program_options::positional_options_description p;
113  p.add ("copyright", -1);
114 
115  boost::program_options::variables_map vm;
116  boost::program_options::
117  store (boost::program_options::command_line_parser (argc, argv).
118  options (cmdline_options).positional(p).run(), vm);
119 
120  std::ifstream ifs ("rmol.cfg");
121  boost::program_options::store (parse_config_file (ifs, config_file_options),
122  vm);
123  boost::program_options::notify (vm);
124 
125  if (vm.count ("help")) {
126  std::cout << visible << std::endl;
128  }
129 
130  if (vm.count ("version")) {
131  std::cout << PACKAGE_NAME << ", version " << PACKAGE_VERSION << std::endl;
133  }
134 
135  if (vm.count ("prefix")) {
136  std::cout << "Installation prefix: " << PREFIXDIR << std::endl;
138  }
139 
140  if (vm.count ("builtin")) {
141  ioIsBuiltin = true;
142  }
143  const std::string isBuiltinStr = (ioIsBuiltin == true)?"yes":"no";
144  std::cout << "The BOM should be built-in? " << isBuiltinStr << std::endl;
145 
146  if (ioIsBuiltin == false) {
147  if (vm.count ("input")) {
148  ioInputFilename = vm["input"].as< std::string >();
149  std::cout << "Input filename is: " << ioInputFilename << std::endl;
150  }
151  }
152 
153  if (vm.count ("log")) {
154  ioLogFilename = vm["log"].as< std::string >();
155  std::cout << "Log filename is: " << ioLogFilename << std::endl;
156  }
157 
158  std::cout << "The number of random draws is: " << ioRandomDraws << std::endl;
159  std::cout << "The resource capacity is: " << ioCapacity << std::endl;
160  std::cout << "The optimisation method is: " << ioMethod << std::endl;
161  std::cout << std::endl;
162 
163  return 0;
164 }
165 
166 // /////////////////////////////////////////////////////
167 void optimise (RMOL::RMOL_Service& rmolService,
168  const short& iMethod, const int& iRandomDraws) {
169 
170  switch (iMethod) {
171  case 0: {
172  // Calculate the optimal protections by the Monte Carlo
173  // Integration approach
174  rmolService.optimalOptimisationByMCIntegration (iRandomDraws);
175  break;
176  }
177  case 1: {
178  // Calculate the optimal protections by DP.
179  rmolService.optimalOptimisationByDP ();
180  break;
181  }
182  case 2: {
183  // Calculate the Bid-Price Vector by EMSR
184  rmolService.heuristicOptimisationByEmsr ();
185  break;
186  }
187  case 3: {
188  // Calculate the protections by EMSR-a
189  rmolService.heuristicOptimisationByEmsrA ();
190  break;
191  }
192  case 4: {
193  // Calculate the protections by EMSR-b
194  rmolService.heuristicOptimisationByEmsrB ();
195  break;
196  }
197  default: {
198  rmolService.optimalOptimisationByMCIntegration (iRandomDraws);
199  }
200  }
201 }
202 
203 // ///////// M A I N ////////////
204 int main (int argc, char* argv[]) {
205 
206  // Number of random draws to be generated (best if greater than 100)
207  int lRandomDraws = 0;
208 
209  // Cabin Capacity (it must be greater then 100 here)
210  double lCapacity = 0.0;
211 
212  // Methods of optimisation (0 = Monte-Carlo, 1 = Dynamic Programming,
213  // 2 = EMSR, 3 = EMSR-a, 4 = EMSR-b)
214  short lMethod = 0;
215 
216  // Built-in
217  bool isBuiltin;
218 
219  // Input file name
220  std::string lInputFilename;
221 
222  // Output log File
223  std::string lLogFilename;
224 
225  // Call the command-line option parser
226  const int lOptionParserStatus =
227  readConfiguration (argc, argv, lRandomDraws, lCapacity, lMethod,
228  isBuiltin, lInputFilename, lLogFilename);
229 
230  if (lOptionParserStatus == K_RMOL_EARLY_RETURN_STATUS) {
231  return 0;
232  }
233 
234  // Set the log parameters
235  std::ofstream logOutputFile;
236  // Open and clean the log outputfile
237  logOutputFile.open (lLogFilename.c_str());
238  logOutputFile.clear();
239 
240  // Initialise the log stream
241  const stdair::BasLogParams lLogParams (stdair::LOG::DEBUG, logOutputFile);
242 
243  // Initialise the RMOL service
244  RMOL::RMOL_Service rmolService (lLogParams);
245 
246  if (isBuiltin == true) {
247  // DEBUG
248  STDAIR_LOG_DEBUG ("No input file has been given."
249  "A sample BOM tree will therefore be built.");
250 
251  // Build a sample BOM tree
252  rmolService.buildSampleBom();
253 
254  } else {
255  // DEBUG
256  STDAIR_LOG_DEBUG ("RMOL will parse " << lInputFilename
257  << " and build the corresponding BOM tree.");
258 
259  //
260  rmolService.parseAndLoad (lCapacity, lInputFilename);
261  }
262 
263  // Launch the optimisation
264  optimise (rmolService, lMethod, lRandomDraws);
265 
266  //
267  logOutputFile.close();
268 
269  return 0;
270 }