00001
00002 #ifndef DDF_PREDICTENGINEBASE_HPP
00003 #define DDF_PREDICTENGINEBASE_HPP
00004
00005 #include <BayesFilter/bayesFlt.hpp>
00006 #include <deque>
00007 #include "infocontainer.hpp"
00008 #include "definitions.hpp"
00009 #include "tools.hpp"
00010 #include "debugging.hpp"
00011
00012 namespace jafar
00013 {
00014 namespace ddf
00015 {
00016
00017 typedef enum{FS_UPDATE, FS_PREDICT} FS_STORE_TYPE;
00018
00019 template<typename PARAMS>
00020 class FSStorageBase
00021 {
00022 private:
00023 FS_STORE_TYPE m_fs_type;
00024
00025 public:
00026 InfoContainer m_MkIik;
00027 PARAMS m_params;
00028
00029 public:
00030 FSStorageBase(unsigned short sv_size, FS_STORE_TYPE st_type, PARAMS const& params): m_fs_type(st_type),m_MkIik(sv_size){ m_params = params; }
00031 FSStorageBase(unsigned short sv_size, FS_STORE_TYPE st_type): m_fs_type(st_type),m_MkIik(sv_size){ }
00032 FSStorageBase(FSStorageBase const& other): m_MkIik(other.GetSize()) {
00033 if (this == &other) return;
00034 this->m_MkIik = other.m_MkIik;
00035 this->m_fs_type = other.m_fs_type;
00036 this->m_params = other.m_params;
00037 }
00038
00039 FS_STORE_TYPE GetType() const { return m_fs_type; }
00040
00041 unsigned short GetSize() const { return m_MkIik.GetSize(); }
00042 time GetTime() const { return m_MkIik.GetTime(); }
00043 bool IsStoreUpdate() { return m_fs_type == FS_UPDATE; }
00044 bool IsStorePredict() { return m_fs_type == FS_PREDICT; }
00045 void Print() const {
00046 JFR_VDEBUG("Time: " << m_MkIik.m_time << " Type: " << (m_fs_type == FS_UPDATE ? "update" : "predict") << "\n");
00047 if(m_fs_type == FS_PREDICT)
00048 {
00049 PrettyPrintMatrix(m_MkIik.m_InfoMat, "Yk");
00050 PrettyPrintVector(m_MkIik.m_info,"yk");
00051 }
00052 else {
00053 PrettyPrintMatrix(m_MkIik.m_InfoMat, "Sum I");
00054 PrettyPrintVector(m_MkIik.m_info,"Sum i");
00055 }
00056 }
00057 };
00058
00063 class PredictionEngineBase
00064 {
00065 protected:
00066
00067 bool m_predict_available;
00068 time m_horizon;
00069 time m_current_time;
00070 mutable boost::mutex m_info_mutex;
00071 bool m_store_fstate;
00072
00073 public:
00074
00075 PredictionEngineBase(bool store_fstate = false): m_horizon(0,0),m_current_time(0,0),m_store_fstate(store_fstate) {}
00076 virtual ~PredictionEngineBase() {}
00077
00078 void StoreStates(bool yesorno) { m_store_fstate = yesorno; }
00079
00080 void SetCurrentStateTime(time const& t, bool use_mutex) { if (use_mutex) {boost::mutex::scoped_lock lock(m_info_mutex); m_current_time = t;}
00081 else m_current_time = t;}
00082 time const& GetCurrentStateTime() const { boost::mutex::scoped_lock lock(m_info_mutex); return m_current_time; }
00083
00085 virtual void InitState(VEC const& x, MSYM const& X)=0;
00086
00088 virtual void InitInformation(VEC const& y, MSYM const& Y)=0;
00089 virtual void Init(InfoContainer const& inityY)=0;
00090
00092 virtual void Predict(time const& horizon)=0;
00093
00095 virtual bool InsertDelayedMeasure(InfoContainer const& ICont, time const& avail_time)=0;
00096
00098 void Predict(InfoContainer &info, time const& horizon)
00099 {
00100 Init(info);
00101 Predict(horizon);
00102 info = GetPrediction();
00103 }
00104
00106 virtual InfoContainer const& GetPrediction()=0;
00107
00109 virtual InfoContainer const& GetInversePrediction()=0;
00110
00112 virtual void SetModelParams(time const& from, time const& horizon)=0;
00113
00114 virtual void PrintModel()=0;
00115
00117 virtual void UpdatePredictionWith(InfoContainer const& info)=0;
00118
00120 virtual bool IsPSD(bool use_mutex)=0;
00121 };
00122
00123
00129 template<typename PARAMS>
00130 class PredictionEngineGeneric : public PredictionEngineBase
00131 {
00132
00133 protected:
00134
00135 typedef std::deque<FSStorageBase<PARAMS> > FSStorageBuf;
00136 PARAMS m_params;
00137 bool m_model_need_update;
00138 time m_histo_buf_length;
00139 FSStorageBuf m_fs_storage;
00140
00141 public:
00142
00143 PredictionEngineGeneric(): m_model_need_update(true), m_histo_buf_length(3,0) {}
00144 virtual ~PredictionEngineGeneric() {}
00145
00146 virtual void UpdateModel()=0;
00147
00148 void UpdateModel(PARAMS const& params) {
00149 m_params = params;
00150 m_model_need_update = true;
00151 UpdateModel();
00152 }
00153
00154 void SetHistoBufLength(time const& length) { m_histo_buf_length = length; }
00155 time GetHistoBufLength() const { return m_histo_buf_length; }
00156 void ResizeHistory()
00157 {
00158 typename FSStorageBuf::iterator it_beg;
00159 typename FSStorageBuf::reverse_iterator it_back;
00160 time limit;
00161
00162 if (m_fs_storage.empty()) return;
00163
00164 it_beg = m_fs_storage.begin();
00165 it_back = m_fs_storage.rbegin();
00166
00167 if ((*it_back).GetTime() - (*it_beg).GetTime() < m_histo_buf_length) return;
00168
00169 limit = (*it_back).GetTime() - m_histo_buf_length;
00170 while ((*it_beg).GetTime() < limit) {
00171 m_fs_storage.pop_front();
00172 it_beg++;
00173 }
00174 }
00175
00176 time GetHistoryLength() {
00177 typename FSStorageBuf::iterator it_beg;
00178 typename FSStorageBuf::reverse_iterator it_back;
00179
00180 if (m_fs_storage.empty())
00181 return time::null();
00182
00183 it_beg = m_fs_storage.begin();
00184 it_back = m_fs_storage.rbegin();
00185 return (*it_back).GetTime() - (*it_beg).GetTime();
00186 }
00187
00188 bool GetHistoryClosestInfo(time const&t, InfoContainer & iCont)
00189 {
00190 typename FSStorageBuf::iterator it;
00191
00192 if (m_fs_storage.empty()) {
00193 JFR_VDEBUG("empty " << " t: " << t << "\n");
00194 return false;
00195 }
00196 it = m_fs_storage.begin();
00197
00198 if ((*it).GetTime() >= t) {
00199 iCont = (*it).m_MkIik;
00200 }
00201 else if (m_fs_storage.back().GetTime() <= t) {
00202 iCont = m_fs_storage.back().m_MkIik;
00203 }
00204 else
00205 {
00206 while ((*it).GetTime() < t) it++;
00207
00208 it--;
00209 iCont = (*it).m_MkIik;
00210 }
00211 JFR_VDEBUG("iCont time: " << iCont.GetTime() << " t: " << t << "\n");
00212
00213 return true;
00214 }
00215 };
00216
00217 }
00218 }
00219 #endif