2 #include <boost/python.hpp>
10 #include <boost/accumulators/accumulators.hpp>
11 #include <boost/accumulators/statistics/stats.hpp>
12 #include <boost/accumulators/statistics/mean.hpp>
13 #include <boost/accumulators/statistics/moment.hpp>
14 #include <boost/accumulators/statistics/min.hpp>
15 #include <boost/accumulators/statistics/max.hpp>
16 #include <boost/accumulators/statistics/variance.hpp>
18 #if BOOST_VERSION_MACRO >= 107200
19 #include <boost/timer/progress_display.hpp>
21 #include <boost/progress.hpp>
24 #include <stdair/stdair_basic_types.hpp>
25 #include <stdair/basic/ProgressStatusSet.hpp>
26 #include <stdair/basic/DemandGenerationMethod.hpp>
27 #include <stdair/bom/EventStruct.hpp>
28 #include <stdair/bom/BookingRequestStruct.hpp>
30 #include <stdair/service/Logger.hpp>
33 #include <trademgen/config/trademgen-paths.hpp>
36 namespace ba = boost::accumulators;
44 typedef ba::accumulator_set<double,
45 ba::stats<ba::tag::min, ba::tag::max,
46 ba::tag::mean (ba::immediate),
58 std::ios::fmtflags oldFlags = oStream.flags();
61 oStream.setf (std::ios::fixed);
64 oStream <<
"Statistics for the demand generation runs: " << std::endl;
65 oStream <<
" minimum = " << ba::min (iStatAcc) << std::endl;
66 oStream <<
" mean = " << ba::mean (iStatAcc) << std::endl;
67 oStream <<
" maximum = " << ba::max (iStatAcc) << std::endl;
68 oStream <<
" count = " << ba::count (iStatAcc) << std::endl;
69 oStream <<
" variance = " << ba::variance (iStatAcc) << std::endl;
72 oStream.flags (oldFlags);
85 const std::string& iDemandGenerationMethodString) {
86 std::ostringstream oStream;
89 const stdair::DemandGenerationMethod
90 iDemandGenerationMethod (iDemandGenerationMethodString);
93 if (_logOutputStream == NULL) {
94 oStream <<
"The log filepath is not valid." << std::endl;
97 assert (_logOutputStream != NULL);
102 *_logOutputStream <<
"Demand generation for " << iNbOfRuns <<
" runs, "
103 <<
"with the following method: "
104 << iDemandGenerationMethod << std::endl;
106 if (_trademgenService == NULL) {
107 oStream <<
"The TraDemGen service has not been initialised, "
108 <<
"i.e., the init() method has not been called "
109 <<
"correctly on the Trademgener object. Please "
110 <<
"check that all the parameters are not empty and "
111 <<
"point to actual files.";
112 *_logOutputStream << oStream.str();
113 return oStream.str();
115 assert (_trademgenService != NULL);
122 const stdair::Count_T& lExpectedNbOfEventsToBeGenerated =
126 #if BOOST_VERSION_MACRO >= 107200
127 boost::timer::progress_display
129 boost::progress_display
131 lProgressDisplay (lExpectedNbOfEventsToBeGenerated * iNbOfRuns);
133 for (
NbOfRuns_T runIdx = 1; runIdx <= iNbOfRuns; ++runIdx) {
135 *_logOutputStream <<
"Run number: " << runIdx << std::endl;
141 const stdair::Count_T& lActualNbOfEventsToBeGenerated =
145 *_logOutputStream <<
"[" << runIdx <<
"] Expected: "
146 << lExpectedNbOfEventsToBeGenerated <<
", actual: "
147 << lActualNbOfEventsToBeGenerated << std::endl;
156 while (_trademgenService->
isQueueDone() ==
false) {
159 stdair::EventStruct lEventStruct;
160 stdair::ProgressStatusSet lProgressStatusSet =
161 _trademgenService->
popEvent (lEventStruct);
168 const stdair::BookingRequestStruct& lPoppedRequest =
169 lEventStruct.getBookingRequest();
172 *_logOutputStream <<
"[" << runIdx <<
"] Poped booking request: '"
173 << lPoppedRequest.describe() <<
"'." << std::endl;
179 const stdair::DemandGeneratorKey_T& lDemandStreamKey =
180 lPoppedRequest.getDemandGeneratorKey();
184 const bool stillHavingRequestsToBeGenerated = _trademgenService->
185 stillHavingRequestsToBeGenerated (lDemandStreamKey,
187 iDemandGenerationMethod);
190 *_logOutputStream << lProgressStatusSet.describe() << std::endl;
191 *_logOutputStream <<
"=> [" << lDemandStreamKey
192 <<
"] is now processed. Still generate events "
193 <<
"for that demand stream? "
194 << stillHavingRequestsToBeGenerated << std::endl;
198 if (stillHavingRequestsToBeGenerated ==
true) {
200 stdair::BookingRequestPtr_T lNextRequest_ptr =
202 iDemandGenerationMethod);
204 assert (lNextRequest_ptr != NULL);
207 const stdair::Duration_T lDuration =
208 lNextRequest_ptr->getRequestDateTime()
209 - lPoppedRequest.getRequestDateTime();
210 if (lDuration.total_milliseconds() < 0) {
211 *_logOutputStream <<
"[" << lDemandStreamKey
212 <<
"] The date-time of the generated event ("
213 << lNextRequest_ptr->getRequestDateTime()
214 <<
") is lower than the date-time "
215 <<
"of the current event ("
216 << lPoppedRequest.getRequestDateTime()
222 *_logOutputStream <<
"[" << lDemandStreamKey
223 <<
"] Added request: '"
224 << lNextRequest_ptr->describe()
225 <<
"'. Is queue done? "
230 *_logOutputStream << std::endl;
237 lStatAccumulator (lActualNbOfEventsToBeGenerated);
240 _trademgenService->
reset();
244 *_logOutputStream <<
"End of the demand generation. Following are some "
245 <<
"statistics for the " << iNbOfRuns <<
" runs."
247 std::ostringstream oStatStr;
249 *_logOutputStream << oStatStr.str() << std::endl;
252 const std::string& lBOMStr = _trademgenService->
csvDisplay();
253 *_logOutputStream << lBOMStr << std::endl;
256 *_logOutputStream <<
"TraDemGen output: "
257 << oStream.str() << std::endl;
259 }
catch (
const stdair::RootException& eTrademgenError) {
260 oStream <<
"TraDemGen error: " << eTrademgenError.what() << std::endl;
262 }
catch (
const std::exception& eStdError) {
263 oStream <<
"Error: " << eStdError.what() << std::endl;
266 oStream <<
"Unknown error" << std::endl;
270 oStream <<
"TraDemGen has completed the generation of the booking "
271 <<
"requests. See the log file for more details." << std::endl;
273 return oStream.str();
278 Trademgener() : _trademgenService (NULL), _logOutputStream (NULL) {
283 : _trademgenService (iTrademgener._trademgenService),
284 _logOutputStream (iTrademgener._logOutputStream) {
289 _trademgenService = NULL;
290 _logOutputStream = NULL;
296 bool init (
const std::string& iLogFilepath,
297 const stdair::RandomSeed_T& iRandomSeed,
const bool isBuiltin,
298 const stdair::Filename_T& iDemandInputFilename) {
299 bool isEverythingOK =
true;
304 const bool isWriteable = (iLogFilepath.empty() ==
false);
306 if (isWriteable ==
false) {
307 isEverythingOK =
false;
308 return isEverythingOK;
312 _logOutputStream =
new std::ofstream;
313 assert (_logOutputStream != NULL);
316 _logOutputStream->open (iLogFilepath.c_str());
317 _logOutputStream->clear();
320 *_logOutputStream <<
"Python wrapper initialisation" << std::endl;
321 const stdair::BasLogParams lLogParams (stdair::LOG::DEBUG,
326 assert (_trademgenService != NULL);
329 if (isBuiltin ==
true) {
342 *_logOutputStream <<
"Python wrapper initialised" << std::endl;
344 }
catch (
const stdair::RootException& eTrademgenError) {
345 *_logOutputStream <<
"Trademgen error: " << eTrademgenError.what()
348 }
catch (
const std::exception& eStdError) {
349 *_logOutputStream <<
"Error: " << eStdError.what() << std::endl;
352 *_logOutputStream <<
"Unknown error" << std::endl;
355 return isEverythingOK;
361 std::ofstream* _logOutputStream;
368 boost::python::class_<TRADEMGEN::Trademgener> (
"Trademgener")
ba::accumulator_set< double, ba::stats< ba::tag::min, ba::tag::max, ba::tag::mean(ba::immediate), ba::tag::sum, ba::tag::variance > > stat_acc_type
BOOST_PYTHON_MODULE(pytrademgen)
ba::accumulator_set< double, ba::stats< ba::tag::min, ba::tag::max, ba::tag::mean(ba::immediate), ba::tag::sum, ba::tag::variance > > stat_acc_type
void stat_display(std::ostream &oStream, const stat_acc_type &iStatAcc)
Wrapper structure around the C++ API, so as to expose a Python API.
Trademgener(const Trademgener &iTrademgener)
bool init(const std::string &iLogFilepath, const stdair::RandomSeed_T &iRandomSeed, const bool isBuiltin, const stdair::Filename_T &iDemandInputFilename)
std::string trademgen(const NbOfRuns_T &iNbOfRuns, const std::string &iDemandGenerationMethodString)
class holding the services related to Travel Demand Generation.
std::string csvDisplay() const
void parseAndLoad(const DemandFilePath &)
stdair::ProgressStatusSet popEvent(stdair::EventStruct &) const
const stdair::Count_T & getExpectedTotalNumberOfRequestsToBeGenerated() const
stdair::Count_T generateFirstRequests(const stdair::DemandGenerationMethod &) const
stdair::BookingRequestPtr_T generateNextRequest(const stdair::DemandStreamKeyStr_T &, const stdair::DemandGenerationMethod &) const