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