13#include <boost/tokenizer.hpp>
15#include <boost/program_options.hpp>
17#include <boost/accumulators/accumulators.hpp>
18#include <boost/accumulators/statistics.hpp>
20#if BOOST_VERSION_MACRO >= 107200
21#include <boost/timer/progress_display.hpp>
23#include <boost/progress.hpp>
26#include <stdair/stdair_basic_types.hpp>
27#include <stdair/basic/BasConst_General.hpp>
28#include <stdair/basic/ProgressStatusSet.hpp>
29#include <stdair/basic/DemandGenerationMethod.hpp>
30#include <stdair/bom/EventStruct.hpp>
31#include <stdair/bom/BookingRequestStruct.hpp>
32#include <stdair/bom/BomDisplay.hpp>
33#include <stdair/service/Logger.hpp>
36#include <trademgen/config/trademgen-paths.hpp>
39namespace ba = boost::accumulators;
47typedef ba::accumulator_set<double,
48 ba::stats<ba::tag::min, ba::tag::max,
49 ba::tag::mean (ba::immediate),
73const stdair::DemandGenerationMethod
75 stdair::DemandGenerationMethod::POI_PRO;
87 stdair::DEFAULT_RANDOM_SEED;
112 std::ios::fmtflags oldFlags = oStream.flags();
115 oStream.setf (std::ios::fixed);
118 oStream <<
"Statistics for the demand generation runs: " << std::endl;
119 oStream <<
" minimum = " << ba::min (iStatAcc) << std::endl;
120 oStream <<
" mean = " << ba::mean (iStatAcc) << std::endl;
121 oStream <<
" maximum = " << ba::max (iStatAcc) << std::endl;
122 oStream <<
" count = " << ba::count (iStatAcc) << std::endl;
123 oStream <<
" variance = " << ba::variance (iStatAcc) << std::endl;
126 oStream.flags (oldFlags);
132 const std::vector<T>& v) {
133 std::copy (v.begin(), v.end(), std::ostream_iterator<T> (std::cout,
" "));
141 stdair::RandomSeed_T& ioRandomSeed,
143 stdair::Filename_T& ioInputFilename,
144 stdair::Filename_T& ioOutputFilename,
145 stdair::Filename_T& ioLogFilename,
146 stdair::DemandGenerationMethod& ioDemandGenerationMethod) {
149 char lDemandGenerationMethodChar;
155 boost::program_options::options_description generic (
"Generic options");
156 generic.add_options()
157 (
"prefix",
"print installation prefix")
158 (
"version,v",
"print version string")
159 (
"help,h",
"produce help message");
163 boost::program_options::options_description config (
"Configuration");
166 "The sample BOM tree can be either built-in or parsed from an input file. That latter must then be given with the -i/--input option")
169 "Seed for the random generation")
172 "Number of runs for the demand generations")
173 (
"demandgeneration,G",
175 "Method used to generate the demand (i.e., the booking requests): Poisson Process (P) or Order Statistics (S)")
178 "(CSV) input file for the demand distributions")
181 "(CSV) output file for the generated requests")
184 "Filepath for the logs")
189 boost::program_options::options_description hidden (
"Hidden options");
192 boost::program_options::value< std::vector<std::string> >(),
193 "Show the copyright (license)");
195 boost::program_options::options_description cmdline_options;
196 cmdline_options.add(generic).add(config).add(hidden);
198 boost::program_options::options_description config_file_options;
199 config_file_options.add(config).add(hidden);
201 boost::program_options::options_description visible (
"Allowed options");
202 visible.add(generic).add(config);
204 boost::program_options::positional_options_description p;
205 p.add (
"copyright", -1);
207 boost::program_options::variables_map vm;
208 boost::program_options::
209 store (boost::program_options::command_line_parser (argc, argv).
210 options (cmdline_options).positional(p).run(), vm);
212 std::ifstream ifs (
"trademgen.cfg");
213 boost::program_options::store (parse_config_file (ifs, config_file_options),
215 boost::program_options::notify (vm);
217 if (vm.count (
"help")) {
218 std::cout << visible << std::endl;
222 if (vm.count (
"version")) {
223 std::cout << PACKAGE_NAME <<
", version " << PACKAGE_VERSION << std::endl;
227 if (vm.count (
"prefix")) {
228 std::cout <<
"Installation prefix: " << PREFIXDIR << std::endl;
232 if (vm.count (
"builtin")) {
235 const std::string isBuiltinStr = (ioIsBuiltin ==
true)?
"yes":
"no";
236 std::cout <<
"The BOM should be built-in? " << isBuiltinStr << std::endl;
238 if (ioIsBuiltin ==
false) {
241 if (vm.count (
"input")) {
242 ioInputFilename = vm[
"input"].as< std::string >();
243 std::cout <<
"Input filename is: " << ioInputFilename << std::endl;
248 std::cerr <<
"Either one among the -b/--builtin and -i/--input "
249 <<
"options must be specified" << std::endl;
253 if (vm.count (
"output")) {
254 ioOutputFilename = vm[
"output"].as< std::string >();
255 std::cout <<
"Output filename is: " << ioOutputFilename << std::endl;
258 if (vm.count (
"log")) {
259 ioLogFilename = vm[
"log"].as< std::string >();
260 std::cout <<
"Log filename is: " << ioLogFilename << std::endl;
263 if (vm.count (
"demandgeneration")) {
264 ioDemandGenerationMethod =
265 stdair::DemandGenerationMethod (lDemandGenerationMethodChar);
266 std::cout <<
"Date-time request generation method is: "
267 << ioDemandGenerationMethod.describe() << std::endl;
271 std::cout <<
"The random generation seed is: " << ioRandomSeed << std::endl;
274 std::cout <<
"The number of runs is: " << ioRandomRuns << std::endl;
281 const stdair::Filename_T& iOutputFilename,
283 const stdair::DemandGenerationMethod& iDemandGenerationMethod) {
286 std::ofstream output;
287 output.open (iOutputFilename.c_str());
295 const stdair::Count_T& lExpectedNbOfEventsToBeGenerated =
299#if BOOST_VERSION_MACRO >= 107200
300 boost::timer::progress_display
302 boost::progress_display
304 lProgressDisplay (lExpectedNbOfEventsToBeGenerated
307 for (
NbOfRuns_T runIdx = 1; runIdx <= iNbOfRuns; ++runIdx) {
309 output <<
"Run number: " << runIdx << std::endl;
315 const stdair::Count_T& lActualNbOfEventsToBeGenerated =
319 STDAIR_LOG_DEBUG (
"[" << runIdx <<
"] Expected: "
320 << lExpectedNbOfEventsToBeGenerated <<
", actual: "
321 << lActualNbOfEventsToBeGenerated);
330 while (ioTrademgenService.
isQueueDone() ==
false) {
333 stdair::EventStruct lEventStruct;
334 stdair::ProgressStatusSet lProgressStatusSet =
335 ioTrademgenService.
popEvent (lEventStruct);
342 const stdair::BookingRequestStruct& lPoppedRequest =
343 lEventStruct.getBookingRequest();
346 STDAIR_LOG_DEBUG (
"[" << runIdx <<
"] Poped booking request: '"
347 << lPoppedRequest.describe() <<
"'.");
353 const stdair::DemandGeneratorKey_T& lDemandStreamKey =
354 lPoppedRequest.getDemandGeneratorKey();
357 const bool stillHavingRequestsToBeGenerated = ioTrademgenService.
358 stillHavingRequestsToBeGenerated (lDemandStreamKey,
360 iDemandGenerationMethod);
363 STDAIR_LOG_DEBUG (lProgressStatusSet.describe());
364 STDAIR_LOG_DEBUG (
"=> [" << lDemandStreamKey <<
"] is now processed. "
365 <<
"Still generate events for that demand stream? "
366 << stillHavingRequestsToBeGenerated);
370 if (stillHavingRequestsToBeGenerated ==
true) {
372 stdair::BookingRequestPtr_T lNextRequest_ptr =
374 iDemandGenerationMethod);
376 assert (lNextRequest_ptr != NULL);
379 const stdair::Duration_T lDuration =
380 lNextRequest_ptr->getRequestDateTime()
381 - lPoppedRequest.getRequestDateTime();
382 if (lDuration.total_milliseconds() < 0) {
383 STDAIR_LOG_ERROR (
"[" << lDemandStreamKey
384 <<
"] The date-time of the generated event ("
385 << lNextRequest_ptr->getRequestDateTime()
386 <<
") is lower than the date-time "
387 <<
"of the current event ("
388 << lPoppedRequest.getRequestDateTime() <<
")");
393 STDAIR_LOG_DEBUG (
"[" << lDemandStreamKey <<
"] Added request: '"
394 << lNextRequest_ptr->describe()
395 <<
"'. Is queue done? "
399 STDAIR_LOG_DEBUG (
"");
406 lStatAccumulator (lActualNbOfEventsToBeGenerated);
409 ioTrademgenService.
reset();
413 STDAIR_LOG_DEBUG (
"End of the demand generation. Following are some "
414 "statistics for the " << iNbOfRuns <<
" runs.");
415 std::ostringstream oStatStr;
417 STDAIR_LOG_DEBUG (oStatStr.str());
420 const std::string& lBOMStr = ioTrademgenService.
csvDisplay();
421 STDAIR_LOG_DEBUG (lBOMStr);
429int main (
int argc,
char* argv[]) {
435 stdair::RandomSeed_T lRandomSeed;
441 stdair::Filename_T lInputFilename;
444 stdair::Filename_T lOutputFilename;
447 stdair::Filename_T lLogFilename;
450 stdair::DemandGenerationMethod
454 const int lOptionParserStatus =
456 lInputFilename, lOutputFilename, lLogFilename,
457 lDemandGenerationMethod);
464 std::ofstream logOutputFile;
466 logOutputFile.open (lLogFilename.c_str());
467 logOutputFile.clear();
470 const stdair::BasLogParams lLogParams (stdair::LOG::DEBUG, logOutputFile);
476 if (isBuiltin ==
true) {
488 lDemandGenerationMethod);
491 logOutputFile.close();
int main(int argc, char *argv[])
void generateDemand(TRADEMGEN::TRADEMGEN_Service &ioTrademgenService, const stdair::Filename_T &iOutputFilename, const NbOfRuns_T &iNbOfRuns, const stdair::DemandGenerationMethod &iDemandGenerationMethod)
void stat_display(std::ostream &oStream, const stat_acc_type &iStatAcc)
const stdair::Filename_T K_TRADEMGEN_DEFAULT_INPUT_FILENAME(STDAIR_SAMPLE_DIR "/demand01.csv")
const stdair::RandomSeed_T K_TRADEMGEN_DEFAULT_RANDOM_SEED
const NbOfRuns_T K_TRADEMGEN_DEFAULT_RANDOM_DRAWS
int readConfiguration(int argc, char *argv[], bool &ioIsBuiltin, stdair::RandomSeed_T &ioRandomSeed, NbOfRuns_T &ioRandomRuns, stdair::Filename_T &ioInputFilename, stdair::Filename_T &ioOutputFilename, stdair::Filename_T &ioLogFilename, stdair::DemandGenerationMethod &ioDemandGenerationMethod)
std::ostream & operator<<(std::ostream &os, const std::vector< T > &v)
const stdair::DemandGenerationMethod K_TRADEMGEN_DEFAULT_DEMAND_GENERATION_METHOD
const stdair::Filename_T K_TRADEMGEN_DEFAULT_OUTPUT_FILENAME("request.csv")
const char K_TRADEMGEN_DEFAULT_DEMAND_GENERATION_METHOD_CHAR
const int K_TRADEMGEN_EARLY_RETURN_STATUS
const stdair::Filename_T K_TRADEMGEN_DEFAULT_LOG_FILENAME("trademgen_generateDemand.log")
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
const bool K_TRADEMGEN_DEFAULT_BUILT_IN_INPUT
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