TvlSim Logo  1.00.0
C++ Simulated Travel-Oriented Distribution System Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SimulationStatus.cpp
Go to the documentation of this file.
1 // //////////////////////////////////////////////////////////////////////
2 // Import section
3 // //////////////////////////////////////////////////////////////////////
4 // STL
5 #include <cassert>
6 #include <sstream>
7 // TvlSim
11 
12 namespace TVLSIM {
13 
14  // //////////////////////////////////////////////////////////////////////
16  : _key (iKey),
17  _startDate (DEFAULT_SIMULATION_START_DATE),
18  _currentDate (DEFAULT_SIMULATION_START_DATE),
19  _endDate (DEFAULT_SIMULATION_END_DATE),
20  _totalNumberOfRuns (DEFAULT_NUMBER_OF_RUNS),
21  _currentRun (DEFAULT_NUMBER_OF_RUNS),
22  _currentNbOfBookings (0),
23  _totalNbOfBookings (0),
24  _totalElapsedTime (0),
25  _estimatedRemainingTime (0),
26  _currentElapsedTime (0),
27  _currentEstimatedRemainingTime (0),
28  _simulationMode('s') {
29  }
30 
31  // //////////////////////////////////////////////////////////////////////
33  : _key (DEFAULT_TVLSIM_ID),
34  _startDate (DEFAULT_SIMULATION_START_DATE),
35  _currentDate (DEFAULT_SIMULATION_START_DATE),
36  _endDate (DEFAULT_SIMULATION_END_DATE),
37  _totalNumberOfRuns (DEFAULT_NUMBER_OF_RUNS),
38  _currentRun (0),
39  _currentNbOfBookings (0),
40  _totalNbOfBookings (0),
41  _totalElapsedTime (0),
42  _estimatedRemainingTime (0),
43  _currentElapsedTime (0),
44  _currentEstimatedRemainingTime (0),
45  _simulationMode ('s') {
46  }
47 
48  // //////////////////////////////////////////////////////////////////////
49  SimulationStatus::SimulationStatus (const SimulationStatus& iSimulationStatus)
50  : _key (iSimulationStatus._key),
51  _startDate (iSimulationStatus._startDate),
52  _currentDate (iSimulationStatus._currentDate),
53  _endDate (iSimulationStatus._endDate),
54  _totalNumberOfRuns (iSimulationStatus._totalNumberOfRuns),
55  _currentRun (iSimulationStatus._currentRun),
56  _currentNbOfBookings (iSimulationStatus._currentNbOfBookings),
57  _totalNbOfBookings (iSimulationStatus._totalNbOfBookings),
58  _totalElapsedTime (iSimulationStatus._totalElapsedTime),
59  _estimatedRemainingTime (iSimulationStatus._estimatedRemainingTime),
60  _currentElapsedTime (iSimulationStatus._currentElapsedTime),
61  _currentEstimatedRemainingTime (iSimulationStatus._currentEstimatedRemainingTime),
62  _simulationMode(iSimulationStatus.getSimulationMode()) {
63  assert (false);
64  }
65 
66  // //////////////////////////////////////////////////////////////////////
68 
69  }
70 
71  // //////////////////////////////////////////////////////////////////////
73  setCurrentProgressStatus (const stdair::ProgressStatus& iProgressStatus) {
74 
75  // Update the current progress status
76  _currentProgressStatus = iProgressStatus;
77  }
78 
79  // //////////////////////////////////////////////////////////////////////
81 
82  bool isTheSimulationDone = false;
83 
84  if (_simulationMode == SimulationMode::DONE) {
85  if (_currentRun >= _totalNumberOfRuns) {
86  isTheSimulationDone = true;
87  return isTheSimulationDone;
88  } else {
89  return isTheSimulationDone;
90  }
91  } else {
92  return isTheSimulationDone;
93  }
94 
95  return isTheSimulationDone;
96  }
97 
98  // //////////////////////////////////////////////////////////////////////
100  updateProgress (const stdair::EventType::EN_EventType& iType,
101  const stdair::ProgressStatus& iProgressStatus,
102  const double& iEventMeasure) {
103 
104  const stdair::Count_T lNbOfActualEventsOfSuchType =
105  iProgressStatus.getActualNb();
106  const stdair::Count_T lNbOfCurrentEventsOfSuchType =
107  iProgressStatus.getCurrentNb();
108  const stdair::Count_T lNbOfRemainingEventsOfSuchType =
109  lNbOfActualEventsOfSuchType - lNbOfCurrentEventsOfSuchType;
110  stdair::Count_T lPreviousNbOfActualEventsOfSuchType = 0;
111  stdair::Count_T lPreviousNbOfCurrentEventsOfSuchType = 0;
112 
113  // Retrieve, if existing, the ProgressStatus structure
114  // corresponding to the given event type and update it
115  SEVMGR::ProgressStatusMap_T::iterator itProgressStatus =
116  _progressStatusMap.find (iType);
117  if (itProgressStatus == _progressStatusMap.end()) {
118  const bool hasInsertBeenSuccessful =
119  _progressStatusMap.insert (SEVMGR::ProgressStatusMap_T::
120  value_type (iType, iProgressStatus)).second;
121 
122  if (hasInsertBeenSuccessful == false) {
123  STDAIR_LOG_ERROR ("No progress_status can be inserted "
124  << "for the following event type: "
125  << stdair::EventType::getLabel(iType) << ".");
126  throw stdair::EventException ("No progress_status can be inserted "
127  "for the following event type: "
128  + stdair::EventType::getLabel(iType));
129  }
130  } else {
131  stdair::ProgressStatus& lProgressStatus = itProgressStatus->second;
132  lPreviousNbOfActualEventsOfSuchType = lProgressStatus.getActualNb();
133  lPreviousNbOfCurrentEventsOfSuchType = lProgressStatus.getCurrentNb();
134  lProgressStatus = iProgressStatus;
135  }
136 
137  // Update the cuurent elapsed time chronometer
138  _currentElapsedTime += iEventMeasure;
139 
140  // Retrieve, if existing, the Chronometer structure
141  // corresponding to the given event type and update it
142  ChronometerMap_T::iterator itChronometer =
143  _chronometerMap.find (iType);
144  if (itChronometer == _chronometerMap.end()) {
145  const bool hasInsertBeenSuccessful =
146  _chronometerMap.insert (ChronometerMap_T::
147  value_type (iType, iEventMeasure)).second;
148 
149  if (hasInsertBeenSuccessful == false) {
150  STDAIR_LOG_ERROR ("No chronometer can be inserted "
151  << "for the following event type: "
152  << stdair::EventType::getLabel(iType) << ".");
153  throw stdair::EventException ("No chronometer can be inserted for the "
154  "following event type: "
155  + stdair::EventType::getLabel(iType));
156  }
157  _currentEstimatedRemainingTime +=
158  lNbOfRemainingEventsOfSuchType*iEventMeasure;
159  return;
160  } else {
161  double& lChronometer = itChronometer->second;
162  const stdair::Count_T lPreviousNbOfRemainingEventsOfSuchType =
163  lPreviousNbOfActualEventsOfSuchType -
164  lPreviousNbOfCurrentEventsOfSuchType;
165  if (lPreviousNbOfCurrentEventsOfSuchType > 0) {
166  _currentEstimatedRemainingTime -=
167  lPreviousNbOfRemainingEventsOfSuchType*
168  lChronometer/lPreviousNbOfCurrentEventsOfSuchType;
169  }
170  if (lNbOfCurrentEventsOfSuchType > 0) {
171  lChronometer += iEventMeasure;
172  _currentEstimatedRemainingTime += lNbOfRemainingEventsOfSuchType*
173  lChronometer/lNbOfCurrentEventsOfSuchType;
174  }
175  _currentEstimatedRemainingTime =
176  std::max(_currentEstimatedRemainingTime, 0.0);
177  }
178 
179  // Update the global chronometers
180  _totalElapsedTime = _currentElapsedTime
181  + (_currentRun - 1)
182  *(_currentElapsedTime + _currentEstimatedRemainingTime);
183  _estimatedRemainingTime = _currentEstimatedRemainingTime
184  + (_totalNumberOfRuns - _currentRun)
185  *(_currentElapsedTime + _currentEstimatedRemainingTime);
186 
187  }
188 
189  // //////////////////////////////////////////////////////////////////////
190  void SimulationStatus::
191  increaseGlobalNumberOfBookings (const stdair::PartySize_T& iPartySize) {
192  _currentNbOfBookings += iPartySize;
193  _totalNbOfBookings += iPartySize;
194  }
195 
196  // //////////////////////////////////////////////////////////////////////
198 
199  // Update the total number of bookings
200  _totalNbOfBookings -= _currentNbOfBookings;
201 
202  // Update the current and total time counters
203  _estimatedRemainingTime =
204  (_currentElapsedTime + _currentEstimatedRemainingTime)
205  *(_totalNumberOfRuns - _currentRun + 1);
206  _totalElapsedTime -= _currentElapsedTime;
207 
208  // Prepare the new run
209  prepareNewRun ();
210 
211  // Reset the simulation Mode
212  _simulationMode.setMode (SimulationMode::START);
213 
214  }
215 
216  // //////////////////////////////////////////////////////////////////////
218 
219  // Reset the start date
220  _currentDate = _startDate;
221 
222  // Update the current number of bookings
223  _currentNbOfBookings = 0;
224 
225  // Update the current and total time counters
226  _currentElapsedTime = 0;
227  _currentEstimatedRemainingTime = 0;
228 
229  // Reset the current progress status and maps
230  _currentProgressStatus.reset();
231  _progressStatusMap.clear();
232  _chronometerMap.clear();
233 
234  // Increase the current run number.
235  _currentRun++;
236 
237  }
238 
239  // //////////////////////////////////////////////////////////////////////
241 
242  std::ostringstream oStr;
243 
244  switch (_simulationMode.getMode()) {
245  case SimulationMode::START:
246  case SimulationMode::DONE:
247  // DEBUG
248  oStr << "Simulation";
249  if (_totalNumberOfRuns > 1) {
250  oStr << " [" <<_currentRun << "/" << _totalNumberOfRuns << "]";
251  }
252  oStr << " is starting..." << std::endl;
253  std::cout << oStr.str() << std::endl;
254  STDAIR_LOG_DEBUG (oStr.str());
255  break;
256 
257  case SimulationMode::BREAK:
258 
259  // DEBUG
260  oStr << "Resuming the Simulation ";
261  if (_totalNumberOfRuns > 1) {
262  oStr << "[" <<_currentRun << "/" << _totalNumberOfRuns << "]"
263  << std::endl;
264  }
265  std::cout << oStr.str() << std::endl;
266  STDAIR_LOG_DEBUG (oStr.str());
267  break;
268 
270  default :
271  break;
272  }
273  }
274 
275  // //////////////////////////////////////////////////////////////////////
277 
278  std::ostringstream oStr;
279 
280  switch (_simulationMode.getMode()) {
281  case SimulationMode::DONE:
282  // DEBUG
283  oStr << "Simulation";
284  if (_totalNumberOfRuns > 1) {
285  oStr << " [" <<_currentRun << "/" << _totalNumberOfRuns << "]";
286  }
287  oStr << " has ended." << std::endl;
288  std::cout << oStr.str() << std::endl;
289  STDAIR_LOG_DEBUG (oStr.str());
290  break;
291 
292  case SimulationMode::BREAK:
293 
294  // DEBUG
295  std::cout << "The simulation has stopped on '" << _currentDate
296  << "': break point encountered.\n" << describe() << std::endl;
297  STDAIR_LOG_DEBUG ("Break point encountered\n" << describe());
298  break;
299 
300  case SimulationMode::START:
302  default :
303  break;
304  }
305  }
306 
307  // //////////////////////////////////////////////////////////////////////
308  const std::string SimulationStatus::describe() const {
309  std::ostringstream oStr;
310 
311  //
312  // Display information on all the runs (if many)
313  //
314  if (_totalNumberOfRuns > 1) {
315  //
316  // Add the number of the run
317  //
318  oStr << "Simulation composed of " << _totalNumberOfRuns << " runs."
319  << std::endl;
320  oStr << "\nTotal number of bookings: " << _totalNbOfBookings
321  << "\n\nElapsed time: "
322  << std::setprecision (2) << std::fixed << _totalElapsedTime << " s"
323  << "\nEstimated remaining time: "
324  << _estimatedRemainingTime << " s"
325  << std::endl;
326 
327  }
328 
329  //
330  // Add the number of the run
331  //
332  NbOfRuns_T lRunNumberToDisplay = _currentRun;
333  if (lRunNumberToDisplay > _totalNumberOfRuns) {
334  lRunNumberToDisplay--;
335  }
336  oStr << "Current Run (" << lRunNumberToDisplay
337  << "/" << _totalNumberOfRuns << ")"
338  << std::endl;
339 
340  //
341  // Add the display of the start, current and end date
342  //
343  oStr << "\nStart Date ---- Current Date ---- End Date\n"
344  << _startDate << " " << _currentDate
345  << " " << _endDate << std::endl;
346 
347  //
348  // Add the display of the overall progress status
349  //
350  std::string lEventTypeStr = "All";
351  describeHelper(lEventTypeStr);
352  oStr << "\n\n----------------- Progress statuses ----------------"
353  << "\n " << lEventTypeStr << _currentProgressStatus.toString()
354  << "\n----------------------------------------------------";
355 
356  //
357  // Add the display of the specific progress statuses
358  //
359  std::string lOptionalStr;
360  SEVMGR::ProgressStatusMap_T::const_iterator itPS =
361  _progressStatusMap.begin();
362  while (itPS != _progressStatusMap.end()) {
363  const stdair::EventType::EN_EventType& lType = itPS->first;
364  const stdair::ProgressStatus& lProgressStatus = itPS->second;
365  lEventTypeStr = stdair::EventType::getLabel(lType);
366  describeHelper(lEventTypeStr);
367  oStr << "\n " << lEventTypeStr << lProgressStatus.toString();
368  itPS++;
369  lOptionalStr = "\n----------------------------------------------------";
370  }
371  oStr << lOptionalStr << std::endl;
372 
373  //
374  // Add other information such as the total number of bookings, ...
375  //
376  oStr << "\nTotal number of bookings: "
377  << _currentNbOfBookings
378  << "\n\nElapsed time: "
379  << std::setprecision (2) << std::fixed << _currentElapsedTime << " s"
380  << "\nEstimated remaining time: "
381  << _currentEstimatedRemainingTime << " s"
382  << std::endl;
383  return oStr.str();
384  }
385 
386  // //////////////////////////////////////////////////////////////////////
387  void SimulationStatus::describeHelper(std::string& ioEventTypeName) const {
388  ioEventTypeName += " Events:";
389  const std::size_t lSizeBeforePercent (30);
390  ioEventTypeName.resize (lSizeBeforePercent,' ');
391  }
392 
393 }