2 #ifndef OPENGM_BELIEFPROPAGATION_HXX 3 #define OPENGM_BELIEFPROPAGATION_HXX 22 template<
class GM,
class BUFFER,
class OP,
class ACC>
23 class VariableHullBP {
25 typedef GM GraphicalModelType;
26 typedef BUFFER BufferType;
27 typedef typename BUFFER::ArrayType BufferArrayType;
28 typedef typename GM::FactorType FactorType;
29 typedef typename GM::IndependentFactorType IndependentFactorType;
30 typedef typename GM::ValueType ValueType;
33 void assign(
const GM&,
const size_t,
const meta::EmptyType*);
34 BUFFER& connectFactorHullBP(
const size_t, BUFFER&);
35 size_t numberOfBuffers()
const;
36 void propagateAll(
const GM&,
const ValueType& = 0,
const bool =
false);
37 void propagate(
const GM&,
const size_t,
const ValueType& = 0,
const bool =
false);
38 void marginal(
const GM&,
const size_t, IndependentFactorType&,
const bool =
true)
const;
40 template<
class DIST> ValueType distance(
const size_t)
const;
41 const typename BUFFER::ArrayType& outBuffer(
const size_t)
const;
44 std::vector<BUFFER* > outBuffer_;
45 std::vector<BUFFER > inBuffer_;
48 template<
class GM,
class BUFFER,
class OP,
class ACC>
51 typedef GM GraphicalModelType;
52 typedef BUFFER BufferType;
53 typedef typename BUFFER::ArrayType BufferArrayType;
54 typedef typename GM::FactorType FactorType;
55 typedef typename GM::IndependentFactorType IndependentFactorType;
56 typedef typename GM::ValueType ValueType;
58 size_t numberOfBuffers()
const {
return inBuffer_.size(); }
59 void assign(
const GM&,
const size_t, std::vector<VariableHullBP<GM,BUFFER,OP,ACC> >&,
const meta::EmptyType*);
60 void propagateAll(
const ValueType& = 0,
const bool =
true);
61 void propagate(
const size_t,
const ValueType& = 0,
const bool =
true);
62 void marginal(IndependentFactorType&,
const bool =
true)
const;
64 template<
class DIST> ValueType distance(
const size_t)
const;
67 FactorType
const* myFactor_;
68 std::vector<BUFFER* > outBuffer_;
69 std::vector<BUFFER > inBuffer_;
75 template<
class GM,
class ACC,
class BUFFER = MessageBuffer<marray::Marray<
double> > >
86 typedef FactorHullBP<GM, BufferType, OperatorType, ACC> FactorHullType;
87 typedef VariableHullBP<GM, BufferType, OperatorType, ACC> VariableHullType;
88 typedef meta::EmptyType SpecialParameterType;
95 template<
class _GM,
class _ACC>
96 struct RebindGmAndAcc{
101 template<
class MP_PARAM>
102 static void initializeSpecialParameter(
const GM& gm, MP_PARAM& mpParameter)
107 template<
class GM,
class BUFFER,
class OP,
class ACC>
108 inline VariableHullBP<GM, BUFFER, OP, ACC>::VariableHullBP()
111 template<
class GM,
class BUFFER,
class OP,
class ACC>
112 inline void VariableHullBP<GM, BUFFER, OP, ACC>::assign
115 const size_t variableIndex,
116 const meta::EmptyType* et
118 size_t numberOfFactors = gm.numberOfFactors(variableIndex);
119 inBuffer_.resize(numberOfFactors);
120 outBuffer_.resize(numberOfFactors);
121 for(
size_t j = 0; j < numberOfFactors; ++j) {
122 inBuffer_[j].assign(gm.numberOfLabels(variableIndex), OP::template neutral<ValueType>());
126 template<
class GM,
class BUFFER,
class OP,
class ACC>
127 inline size_t VariableHullBP<GM, BUFFER, OP, ACC>::numberOfBuffers()
const {
128 return outBuffer_.size();
131 template<
class GM,
class BUFFER,
class OP,
class ACC>
132 inline BUFFER& VariableHullBP<GM, BUFFER, OP, ACC>::connectFactorHullBP
134 const size_t bufferNumber,
135 BUFFER& variableOutBuffer
138 outBuffer_[bufferNumber] = &variableOutBuffer;
139 return inBuffer_[bufferNumber];
142 template<
class GM,
class BUFFER,
class OP,
class ACC >
143 inline void VariableHullBP<GM, BUFFER, OP, ACC>::propagate
146 const size_t bufferNumber,
148 const bool useNormalization
151 outBuffer_[bufferNumber]->toggle();
152 if(inBuffer_.size() < 2) {
157 opengm::messagepassingOperations::operate<OP>(inBuffer_, bufferNumber, newMessage);
162 opengm::messagepassingOperations::weightedMean<OP>(newMessage, oldMessage, damping, newMessage);
165 if(useNormalization) {
166 opengm::messagepassingOperations::normalize<OP,ACC>(newMessage);
171 template<
class GM,
class BUFFER,
class OP,
class ACC>
172 inline void VariableHullBP<GM, BUFFER, OP, ACC>::propagateAll
176 const bool useNormalization
178 for(
size_t bufferNumber = 0; bufferNumber < numberOfBuffers(); ++bufferNumber) {
179 propagate(gm, bufferNumber, damping, useNormalization);
183 template<
class GM,
class BUFFER,
class OP,
class ACC >
184 inline void VariableHullBP<GM, BUFFER, OP, ACC>::marginal
187 const size_t variableIndex,
189 const bool useNormalization
193 out.assign(gm, &variableIndex, &variableIndex+1, OP::template neutral<ValueType>());
194 opengm::messagepassingOperations::operate<OP>(inBuffer_, out);
197 if(useNormalization) {
198 opengm::messagepassingOperations::normalize<OP,ACC>(out);
228 template<
class GM,
class BUFFER,
class OP,
class ACC >
230 inline typename GM::ValueType VariableHullBP<GM, BUFFER, OP, ACC>::distance
232 const size_t bufferNumber
234 return inBuffer_[bufferNumber].template dist<DIST > ();
237 template<
class GM,
class BUFFER,
class OP,
class ACC >
238 inline const typename BUFFER::ArrayType& VariableHullBP<GM, BUFFER, OP, ACC>::outBuffer
240 const size_t bufferIndex
243 return outBuffer_[bufferIndex]->current();
246 template<
class GM,
class BUFFER,
class OP,
class ACC>
247 inline void FactorHullBP<GM, BUFFER, OP, ACC>::assign
250 const size_t factorIndex,
251 std::vector<VariableHullBP<GM, BUFFER, OP, ACC> >& variableHulls,
252 const meta::EmptyType* et
254 myFactor_ = (
FactorType *
const)(&gm[factorIndex]);
255 inBuffer_.resize(gm[factorIndex].numberOfVariables());
256 outBuffer_.resize(gm[factorIndex].numberOfVariables());
257 for(
size_t n=0; n<gm.numberOfVariables(factorIndex); ++n) {
258 size_t variableIndex = gm.variableOfFactor(factorIndex,n);
259 inBuffer_[n].assign(gm.numberOfLabels(variableIndex), OP::template neutral<ValueType > ());
260 size_t bufferNumber = 1000000;
261 for(
size_t i=0; i<gm.numberOfFactors(variableIndex); ++i) {
262 if(gm.factorOfVariable(variableIndex,i) == factorIndex) {
267 outBuffer_[n] =&(variableHulls[variableIndex].connectFactorHullBP(bufferNumber, inBuffer_[n]));
271 template<
class GM,
class BUFFER,
class OP,
class ACC >
272 inline void FactorHullBP<GM, BUFFER, OP, ACC>::propagate
276 const bool useNormalization
279 outBuffer_[id]->toggle();
281 opengm::messagepassingOperations::operateF<GM,ACC>(*myFactor_, inBuffer_, id, newMessage);
286 opengm::messagepassingOperations::weightedMean<OP>(newMessage, oldMessage, damping, newMessage);
289 if(useNormalization) {
290 opengm::messagepassingOperations::normalize<OP,ACC>(newMessage);
294 template<
class GM,
class BUFFER,
class OP,
class ACC >
295 inline void FactorHullBP<GM, BUFFER, OP, ACC>::propagateAll
298 const bool useNormalization
300 for(
size_t j = 0; j < inBuffer_.size(); ++j) {
301 propagate(j, damping, useNormalization);
305 template<
class GM,
class BUFFER,
class OP,
class ACC>
306 inline void FactorHullBP<GM, BUFFER, OP, ACC>::marginal
309 const bool useNormalization
312 opengm::messagepassingOperations::operateF<GM>(*myFactor_, inBuffer_,out);
314 if(useNormalization) {
315 opengm::messagepassingOperations::normalize<OP,ACC>(out);
341 template<
class GM,
class BUFFER,
class OP,
class ACC >
343 inline typename GM::ValueType FactorHullBP<GM, BUFFER, OP, ACC>::distance
347 return inBuffer_[j].template dist<DIST > ();
352 #endif // #ifndef OPENGM_BELIEFPROPAGATION_HXX Update rules for the MessagePassing framework.
GM::FactorType FactorType
BUFFER::ArrayType BufferArrayType
GM::IndependentFactorType IndependentFactorType
#define OPENGM_ASSERT(expression)
GM::OperatorType OperatorType