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;
90 template<
class MP_PARAM>
91 static void initializeSpecialParameter(
const GM& gm, MP_PARAM& mpParameter)
96 template<
class GM,
class BUFFER,
class OP,
class ACC>
97 inline VariableHullBP<GM, BUFFER, OP, ACC>::VariableHullBP()
100 template<
class GM,
class BUFFER,
class OP,
class ACC>
101 inline void VariableHullBP<GM, BUFFER, OP, ACC>::assign
104 const size_t variableIndex,
105 const meta::EmptyType* et
107 size_t numberOfFactors = gm.numberOfFactors(variableIndex);
108 inBuffer_.resize(numberOfFactors);
109 outBuffer_.resize(numberOfFactors);
110 for(
size_t j = 0; j < numberOfFactors; ++j) {
111 inBuffer_[j].assign(gm.numberOfLabels(variableIndex), OP::template neutral<ValueType>());
115 template<
class GM,
class BUFFER,
class OP,
class ACC>
116 inline size_t VariableHullBP<GM, BUFFER, OP, ACC>::numberOfBuffers()
const {
117 return outBuffer_.size();
120 template<
class GM,
class BUFFER,
class OP,
class ACC>
121 inline BUFFER& VariableHullBP<GM, BUFFER, OP, ACC>::connectFactorHullBP
123 const size_t bufferNumber,
124 BUFFER& variableOutBuffer
127 outBuffer_[bufferNumber] = &variableOutBuffer;
128 return inBuffer_[bufferNumber];
131 template<
class GM,
class BUFFER,
class OP,
class ACC >
132 inline void VariableHullBP<GM, BUFFER, OP, ACC>::propagate
135 const size_t bufferNumber,
136 const ValueType& damping,
137 const bool useNormalization
140 outBuffer_[bufferNumber]->toggle();
141 if(inBuffer_.size() < 2) {
145 BufferArrayType& newMessage = outBuffer_[bufferNumber]->current();
146 opengm::messagepassingOperations::operate<OP>(inBuffer_, bufferNumber, newMessage);
150 BufferArrayType& oldMessage = outBuffer_[bufferNumber]->old();
151 opengm::messagepassingOperations::weightedMean<OP>(newMessage, oldMessage, damping, newMessage);
154 if(useNormalization) {
155 opengm::messagepassingOperations::normalize<OP,ACC>(newMessage);
160 template<
class GM,
class BUFFER,
class OP,
class ACC>
161 inline void VariableHullBP<GM, BUFFER, OP, ACC>::propagateAll
164 const ValueType& damping,
165 const bool useNormalization
167 for(
size_t bufferNumber = 0; bufferNumber < numberOfBuffers(); ++bufferNumber) {
168 propagate(gm, bufferNumber, damping, useNormalization);
172 template<
class GM,
class BUFFER,
class OP,
class ACC >
173 inline void VariableHullBP<GM, BUFFER, OP, ACC>::marginal
176 const size_t variableIndex,
177 IndependentFactorType& out,
178 const bool useNormalization
182 out.assign(gm, &variableIndex, &variableIndex+1, OP::template neutral<ValueType>());
183 opengm::messagepassingOperations::operate<OP>(inBuffer_, out);
186 if(useNormalization) {
187 opengm::messagepassingOperations::normalize<OP,ACC>(out);
217 template<
class GM,
class BUFFER,
class OP,
class ACC >
219 inline typename GM::ValueType VariableHullBP<GM, BUFFER, OP, ACC>::distance
221 const size_t bufferNumber
223 return inBuffer_[bufferNumber].template dist<DIST > ();
226 template<
class GM,
class BUFFER,
class OP,
class ACC >
227 inline const typename BUFFER::ArrayType& VariableHullBP<GM, BUFFER, OP, ACC>::outBuffer
229 const size_t bufferIndex
232 return outBuffer_[bufferIndex]->current();
235 template<
class GM,
class BUFFER,
class OP,
class ACC>
236 inline void FactorHullBP<GM, BUFFER, OP, ACC>::assign
239 const size_t factorIndex,
240 std::vector<VariableHullBP<GM, BUFFER, OP, ACC> >& variableHulls,
241 const meta::EmptyType* et
243 myFactor_ = (FactorType *
const)(&gm[factorIndex]);
244 inBuffer_.resize(gm[factorIndex].numberOfVariables());
245 outBuffer_.resize(gm[factorIndex].numberOfVariables());
246 for(
size_t n=0; n<gm.numberOfVariables(factorIndex); ++n) {
247 size_t variableIndex = gm.variableOfFactor(factorIndex,n);
248 inBuffer_[n].assign(gm.numberOfLabels(variableIndex), OP::template neutral<ValueType > ());
249 size_t bufferNumber = 1000000;
250 for(
size_t i=0; i<gm.numberOfFactors(variableIndex); ++i) {
251 if(gm.factorOfVariable(variableIndex,i) == factorIndex) {
256 outBuffer_[n] =&(variableHulls[variableIndex].connectFactorHullBP(bufferNumber, inBuffer_[n]));
260 template<
class GM,
class BUFFER,
class OP,
class ACC >
261 inline void FactorHullBP<GM, BUFFER, OP, ACC>::propagate
264 const ValueType& damping,
265 const bool useNormalization
268 outBuffer_[id]->toggle();
269 BufferArrayType& newMessage = outBuffer_[id]->current();
270 opengm::messagepassingOperations::operateF<GM,ACC>(*myFactor_, inBuffer_, id, newMessage);
274 BufferArrayType& oldMessage = outBuffer_[id]->old();
275 opengm::messagepassingOperations::weightedMean<OP>(newMessage, oldMessage, damping, newMessage);
278 if(useNormalization) {
279 opengm::messagepassingOperations::normalize<OP,ACC>(newMessage);
283 template<
class GM,
class BUFFER,
class OP,
class ACC >
284 inline void FactorHullBP<GM, BUFFER, OP, ACC>::propagateAll
286 const ValueType& damping,
287 const bool useNormalization
289 for(
size_t j = 0; j < inBuffer_.size(); ++j) {
290 propagate(j, damping, useNormalization);
294 template<
class GM,
class BUFFER,
class OP,
class ACC>
295 inline void FactorHullBP<GM, BUFFER, OP, ACC>::marginal
297 IndependentFactorType& out,
298 const bool useNormalization
301 opengm::messagepassingOperations::operateF<GM>(*myFactor_, inBuffer_,out);
303 if(useNormalization) {
304 opengm::messagepassingOperations::normalize<OP,ACC>(out);
330 template<
class GM,
class BUFFER,
class OP,
class ACC >
332 inline typename GM::ValueType FactorHullBP<GM, BUFFER, OP, ACC>::distance
336 return inBuffer_[j].template dist<DIST > ();
341 #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