RMOL Logo  1.00.3
C++ library of Revenue Management and Optimisation classes and functions
pyrmol.cpp
Go to the documentation of this file.
1 // STL
2 #include <cassert>
3 #include <stdexcept>
4 #include <fstream>
5 #include <sstream>
6 #include <string>
7 // Boost (Extended STL)
8 #include <boost/date_time/gregorian/gregorian.hpp>
9 #include <boost/date_time/posix_time/posix_time.hpp>
10 // Boost Python
11 #include <boost/python.hpp>
12 // Boost Accumulators
13 #include <boost/accumulators/accumulators.hpp>
14 #include <boost/accumulators/statistics.hpp>
15 // StdAir
16 #include <stdair/stdair_basic_types.hpp>
17 #include <stdair/service/Logger.hpp>
18 // RMOL
19 #include <rmol/RMOL_Service.hpp>
21 
22 // Aliases for namespaces
23 namespace ba = boost::accumulators;
24 
25 // //////// Specific type definitions ///////
26 typedef unsigned int NbOfRuns_T;
27 
28 // /////////////////////////////////////////////////////
29 namespace RMOL {
30 
34  struct RMOLer {
35  public:
39  std::string rmol (const int& iRandomDraws, const short& iMethod,
40  const double& iCapacity) {
41  std::ostringstream oStream;
42 
43  // Sanity check
44  if (_logOutputStream == NULL) {
45  oStream << "The log filepath is not valid." << std::endl;
46  return oStream.str();
47  }
48  assert (_logOutputStream != NULL);
49 
50  try {
51 
52  // DEBUG
53  *_logOutputStream << "Optimisation for " << iRandomDraws << " draws, "
54  << "capacity of " << iCapacity
55  << ", and with the following method: "
56  << iMethod << std::endl;
57 
58  if (_rmolService == NULL) {
59  oStream << "The RMOL service has not been initialised, "
60  << "i.e., the init() method has not been called "
61  << "correctly on the RMOLer object. Please "
62  << "check that all the parameters are not empty and "
63  << "point to actual files.";
64  *_logOutputStream << oStream.str();
65  return oStream.str();
66  }
67  assert (_rmolService != NULL);
68 
69  switch (iMethod) {
70  case 0: {
71  // Calculate the optimal protections by the Monte Carlo
72  // Integration approach
73  _rmolService->optimize<OptimizationType::OPT_MC> (iRandomDraws);
74  break;
75  }
76  case 1: {
77  // Calculate the optimal protections by DP.
78  _rmolService->optimize<OptimizationType::OPT_DP>();
79  break;
80  }
81  case 2: {
82  // Calculate the Bid-Price Vector by EMSR
83  _rmolService->optimize<OptimizationType::HEUR_EMSR>();
84  break;
85  }
86  case 3: {
87  // Calculate the protections by EMSR-a
88  _rmolService->optimize<OptimizationType::HEUR_EMSRA>();
89  break;
90  }
91  case 4: {
92  // Calculate the protections by EMSR-b
93  _rmolService->optimize<OptimizationType::HEUR_EMSRB>();
94  break;
95  }
96  default: {
97  _rmolService->optimize<OptimizationType::OPT_MC> (iRandomDraws);
98  }
99  }
100 
101  // DEBUG
102  *_logOutputStream << "End of the optimisation." << std::endl;
103 
104  // DEBUG
105  *_logOutputStream << "RMOL output: "
106  << oStream.str() << std::endl;
107 
108  } catch (const stdair::RootException& eRMOLError) {
109  oStream << "RMOL error: " << eRMOLError.what() << std::endl;
110 
111  } catch (const std::exception& eStdError) {
112  oStream << "Error: " << eStdError.what() << std::endl;
113 
114  } catch (...) {
115  oStream << "Unknown error" << std::endl;
116  }
117 
118  //
119  oStream << "RMOL has completed the generation of the booking "
120  << "requests. See the log file for more details." << std::endl;
121 
122  return oStream.str();
123  }
124 
125  public:
127  RMOLer() : _rmolService (NULL), _logOutputStream (NULL) {
128  }
129 
131  RMOLer (const RMOLer& iRMOLer)
132  : _rmolService (iRMOLer._rmolService),
133  _logOutputStream (iRMOLer._logOutputStream) {
134  }
135 
138  _rmolService = NULL;
139  _logOutputStream = NULL;
140  }
141 
145  bool init (const std::string& iLogFilepath,
146  const short& iCapacity, const bool isBuiltin,
147  const stdair::Filename_T& iInputFilename) {
148  bool isEverythingOK = true;
149 
150  try {
151 
152  // Check that the file path given as input corresponds to an actual file
153  const bool isWriteable = (iLogFilepath.empty() == false);
154  // stdair::BasFileMgr::isWriteable (iLogFilepath);
155  if (isWriteable == false) {
156  isEverythingOK = false;
157  return isEverythingOK;
158  }
159 
160  // Set the log parameters
161  _logOutputStream = new std::ofstream;
162  assert (_logOutputStream != NULL);
163 
164  // Open and clean the log outputfile
165  _logOutputStream->open (iLogFilepath.c_str());
166  _logOutputStream->clear();
167 
168  // DEBUG
169  *_logOutputStream << "Python wrapper initialisation" << std::endl;
170  const stdair::BasLogParams lLogParams (stdair::LOG::DEBUG,
171  *_logOutputStream);
172 
173  // Initialise the context
174  _rmolService = new RMOL_Service (lLogParams);
175  assert (_rmolService != NULL);
176 
177  // Check wether or not a (CSV) input file should be read
178  if (isBuiltin == true) {
179  // Create sample BOM objects
180  _rmolService->buildSampleBom();
181 
182  } else {
183  // Create the RMOL objects from the input CSV file
184  _rmolService->parseAndLoad (iCapacity, iInputFilename);
185  }
186 
187  // DEBUG
188  *_logOutputStream << "Python wrapper initialised" << std::endl;
189 
190  } catch (const stdair::RootException& eRMOLError) {
191  *_logOutputStream << "RMOL error: " << eRMOLError.what()
192  << std::endl;
193 
194  } catch (const std::exception& eStdError) {
195  *_logOutputStream << "Error: " << eStdError.what() << std::endl;
196 
197  } catch (...) {
198  *_logOutputStream << "Unknown error" << std::endl;
199  }
200 
201  return isEverythingOK;
202  }
203 
204  private:
206  RMOL_Service* _rmolService;
207  std::ofstream* _logOutputStream;
208  };
209 
210 }
211 
212 // /////////////////////////////////////////////////////////////
214  boost::python::class_<RMOL::RMOLer> ("RMOLer")
215  .def ("rmol", &RMOL::RMOLer::rmol)
216  .def ("init", &RMOL::RMOLer::init);
217 }
void optimize(const stdair::NbOfSamples_T iDraws=0)
void parseAndLoad(const stdair::CabinCapacity_T &iCabinCapacity, const stdair::Filename_T &iDemandAndClassDataFile)
unsigned int NbOfRuns_T
Definition: pyrmol.cpp:26
RMOLer(const RMOLer &iRMOLer)
Definition: pyrmol.cpp:131
bool init(const std::string &iLogFilepath, const short &iCapacity, const bool isBuiltin, const stdair::Filename_T &iInputFilename)
Definition: pyrmol.cpp:145
Definition: BasConst.cpp:7
Interface for the RMOL Services.
BOOST_PYTHON_MODULE(pyrmol)
Definition: pyrmol.cpp:213
Wrapper structure around the C++ API, so as to expose a Python API.
Definition: pyrmol.cpp:34
std::string rmol(const int &iRandomDraws, const short &iMethod, const double &iCapacity)
Definition: pyrmol.cpp:39