From 4b37da6908be87b28b2aedcb42b9b82f5e547dd6 Mon Sep 17 00:00:00 2001 From: tqlong Date: Wed, 25 Aug 2010 03:18:14 +0000 Subject: [PATCH] Graphical Model : message pending --- .../message_pending_inference.h | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 fastlib/trunk/contrib/tqlong/GraphicalModel/message_pending_inference.h diff --git a/fastlib/trunk/contrib/tqlong/GraphicalModel/message_pending_inference.h b/fastlib/trunk/contrib/tqlong/GraphicalModel/message_pending_inference.h new file mode 100644 index 0000000000..adcf956b2e --- /dev/null +++ b/fastlib/trunk/contrib/tqlong/GraphicalModel/message_pending_inference.h @@ -0,0 +1,63 @@ +#ifndef __MESSAGE_PENDING_INFERENCE_H +#define __MESSAGE_PENDING_INFERENCE_H + +#include "gm.h" + +BEGIN_GRAPHICAL_MODEL_NAMESPACE; + +/** The sum-product algorithm with message priority queue: + * + Messages are picked to update according to theirs changes in previous iteration. + * + Messages are propagated along all edges for a certain number of iterations + * while the total change of messages is bigger than change tolerance. + * + Messages are used to calculate beliefs of variable-vertex and average of factor-vertex. + */ +template + class MessagePriorityInference : public SumProductInference<_F> +{ +public: + typedef SumProductInference<_F> _Base; + typedef typename _Base::factor_type factor_type; // map from Assignment --> factor_value_type + typedef typename _Base::factor_value_type factor_value_type; // a numeric type + typedef typename _Base::graph_type graph_type; // FactorGraph + typedef typename _Base::vertex_type vertex_type; // Vertex* (see factor_graph.h) + typedef typename _Base::vertex_vector_type vertex_vector_type; // Vector + typedef typename _Base::belief_type belief_type; // Map + typedef typename _Base::belief_map_type belief_map_type; // Map + typedef typename _Base::message_type message_type; // Map + typedef typename _Base::messages_map_type messages_map_type; // Map > + // usage: message[u][v] where u,v are vertex_type +public: + /** Preparing inference on a graph */ + MessagePriorityInference(const graph_type& graph, ConvergenceMeasure cvm = ConvergenceMeasure()) + : _Base(graph, cvm) {} + + /** The inference algorithm */ + void run(); +protected: + typedef std::pair vertex_pair_type; + typedef std::pair< vertex_pair_type, factor_value_type> msg_double_type; + typedef Vector msg_double_vector_type; + struct MsgCompare + { + bool operator() (const msg_double_type& lhs, const msg_double_type& rhs) const + { + return lhs.second < rhs.second; + } + }; + typedef PriorityQueue msg_double_queue_type; + + /** Priority queue of messages */ + msg_double_queue_type message_queue_; + /** Total change of messages in an iteration */ + factor_value_type change_sum; + + /** Init the message queue by updating all messages once */ + void initMessageQueue(); + + /** The L1 difference between two messages */ + factor_value_type difference(const message_type& oldMsg, const message_type& newMsg); +}; + +END_GRAPHICAL_MODEL_NAMESPACE; + +#endif