4343
4444#include < algorithm>
4545#include < cmath>
46+ #include < map>
47+ #include < numeric>
4648#include < string>
4749#include < vector>
4850
@@ -57,6 +59,7 @@ using TBoolVec = std::vector<bool>;
5759using TDoubleVec = std::vector<double >;
5860using TSizeVec = std::vector<std::size_t >;
5961using TSizeVecVec = std::vector<TSizeVec>;
62+ using TSizeSizeMap = std::map<std::size_t , std::size_t >;
6063using TStrVec = std::vector<std::string>;
6164using TTimeVec = std::vector<core_t ::TTime>;
6265using TTimeTimePr = std::pair<core_t ::TTime, core_t ::TTime>;
@@ -307,7 +310,7 @@ const std::string LAST_UPDATE_OLD_TAG{"j"};
307310
308311// ////////////////////// Upgrade to Version 6.3 ////////////////////////
309312
310- const double MODEL_WEIGHT_UPGRADING_TO_VERSION_6p3 {48.0 };
313+ const double MODEL_WEIGHT_UPGRADING_TO_VERSION_6_3 {48.0 };
311314
312315bool upgradeTrendModelToVersion6p3 (const core_t ::TTime bucketLength,
313316 CTrendComponent& trend,
@@ -330,7 +333,7 @@ bool upgradeTrendModelToVersion6p3(const core_t::TTime bucketLength,
330333
331334 // Generate some samples from the old trend model.
332335
333- double weight{MODEL_WEIGHT_UPGRADING_TO_VERSION_6p3 *
336+ double weight{MODEL_WEIGHT_UPGRADING_TO_VERSION_6_3 *
334337 static_cast <double >(bucketLength) / static_cast <double >(4 * WEEK)};
335338
336339 CPRNG::CXorOShiro128Plus rng;
@@ -343,6 +346,18 @@ bool upgradeTrendModelToVersion6p3(const core_t::TTime bucketLength,
343346 return true ;
344347}
345348
349+ // This implements the mapping from restored states to their best
350+ // equivalents; specifically:
351+ // SC_NEW_COMPONENTS |-> SC_NEW_COMPONENTS
352+ // SC_NORMAL |-> SC_NORMAL
353+ // SC_FORECASTING |-> SC_NORMAL
354+ // SC_DISABLED |-> SC_DISABLED
355+ // SC_ERROR |-> SC_ERROR
356+ // Note that we don't try and restore the periodicity test state
357+ // (see CTimeSeriesDecomposition::acceptRestoreTraverser) and the
358+ // calendar test state is unchanged.
359+ const TSizeSizeMap SC_STATES_UPGRADING_TO_VERSION_6_3{{0 , 0 }, {1 , 1 }, {2 , 1 }, {3 , 2 }, {4 , 3 }};
360+
346361// //////////////////////////////////////////////////////////////////////
347362
348363// Constants
@@ -482,8 +497,9 @@ bool CTimeSeriesDecompositionDetail::CPeriodicityTest::acceptRestoreTraverser(
482497 do {
483498 const std::string& name{traverser.name ()};
484499 RESTORE (PERIODICITY_TEST_MACHINE_6_3_TAG,
485- traverser.traverseSubLevel (boost::bind (
486- &core::CStateMachine::acceptRestoreTraverser, &m_Machine, _1)))
500+ traverser.traverseSubLevel ([this ](core::CStateRestoreTraverser& traverser_) {
501+ return m_Machine.acceptRestoreTraverser (traverser_);
502+ }))
487503 RESTORE_SETUP_TEARDOWN (
488504 SHORT_WINDOW_6_3_TAG, m_Windows[E_Short].reset (this ->newWindow (E_Short)),
489505 m_Windows[E_Short] && traverser.traverseSubLevel (boost::bind (
@@ -759,8 +775,9 @@ bool CTimeSeriesDecompositionDetail::CCalendarTest::acceptRestoreTraverser(core:
759775 do {
760776 const std::string& name{traverser.name ()};
761777 RESTORE (CALENDAR_TEST_MACHINE_6_3_TAG,
762- traverser.traverseSubLevel (boost::bind (
763- &core::CStateMachine::acceptRestoreTraverser, &m_Machine, _1)))
778+ traverser.traverseSubLevel ([this ](core::CStateRestoreTraverser& traverser_) {
779+ return m_Machine.acceptRestoreTraverser (traverser_);
780+ }))
764781 RESTORE_BUILT_IN (LAST_MONTH_6_3_TAG, m_LastMonth);
765782 RESTORE_SETUP_TEARDOWN (
766783 CALENDAR_TEST_6_3_TAG, m_Test.reset (new CCalendarCyclicTest (m_DecayRate)),
@@ -963,8 +980,9 @@ bool CTimeSeriesDecompositionDetail::CComponents::acceptRestoreTraverser(core::C
963980 while (traverser.next ()) {
964981 const std::string& name{traverser.name ()};
965982 RESTORE (COMPONENTS_MACHINE_6_3_TAG,
966- traverser.traverseSubLevel (boost::bind (
967- &core::CStateMachine::acceptRestoreTraverser, &m_Machine, _1)));
983+ traverser.traverseSubLevel ([this ](core::CStateRestoreTraverser& traverser_) {
984+ return m_Machine.acceptRestoreTraverser (traverser_);
985+ }))
968986 RESTORE_BUILT_IN (DECAY_RATE_6_3_TAG, m_DecayRate);
969987 RESTORE (TREND_6_3_TAG,
970988 traverser.traverseSubLevel (boost::bind (
@@ -995,8 +1013,10 @@ bool CTimeSeriesDecompositionDetail::CComponents::acceptRestoreTraverser(core::C
9951013 do {
9961014 const std::string& name{traverser.name ()};
9971015 RESTORE (COMPONENTS_MACHINE_OLD_TAG,
998- traverser.traverseSubLevel (boost::bind (
999- &core::CStateMachine::acceptRestoreTraverser, &m_Machine, _1)));
1016+ traverser.traverseSubLevel ([this ](core::CStateRestoreTraverser& traverser_) {
1017+ return m_Machine.acceptRestoreTraverser (
1018+ traverser_, SC_STATES_UPGRADING_TO_VERSION_6_3);
1019+ }))
10001020 RESTORE_SETUP_TEARDOWN (TREND_OLD_TAG,
10011021 /* */ ,
10021022 traverser.traverseSubLevel (boost::bind (
@@ -1017,7 +1037,7 @@ bool CTimeSeriesDecompositionDetail::CComponents::acceptRestoreTraverser(core::C
10171037 /* */ )
10181038 } while (traverser.next ());
10191039
1020- m_MeanVarianceScale.add (1.0 , MODEL_WEIGHT_UPGRADING_TO_VERSION_6p3 );
1040+ m_MeanVarianceScale.add (1.0 , MODEL_WEIGHT_UPGRADING_TO_VERSION_6_3 );
10211041 }
10221042 return true ;
10231043}
@@ -1679,6 +1699,7 @@ bool CTimeSeriesDecompositionDetail::CComponents::SSeasonal::acceptRestoreTraver
16791699 RESTORE (ERRORS_OLD_TAG,
16801700 core::CPersistUtils::restore (ERRORS_OLD_TAG, s_PredictionErrors, traverser))
16811701 } while (traverser.next ());
1702+ s_PredictionErrors.resize (s_Components.size ());
16821703 }
16831704 return true ;
16841705}
@@ -1907,6 +1928,7 @@ bool CTimeSeriesDecompositionDetail::CComponents::SCalendar::acceptRestoreTraver
19071928 RESTORE (ERRORS_OLD_TAG,
19081929 core::CPersistUtils::restore (ERRORS_OLD_TAG, s_PredictionErrors, traverser))
19091930 } while (traverser.next ());
1931+ s_PredictionErrors.resize (s_Components.size ());
19101932 }
19111933 return true ;
19121934}
0 commit comments