2 #ifndef OPENGM_TREEREWEIGTHEDBELIEFPROPAGATION_HXX
3 #define OPENGM_TREEREWEIGTHEDBELIEFPROPAGATION_HXX
20 template<
class GM,
class BUFFER,
class OP,
class ACC>
21 class VariableHullTRBP {
23 typedef GM GraphicalModelType;
24 typedef BUFFER BufferType;
25 typedef typename BUFFER::ArrayType BufferArrayType;
26 typedef typename GM::ValueType ValueType;
27 typedef typename GM::IndependentFactorType IndependentFactorType;
30 void assign(
const GM&,
const size_t,
const std::vector<ValueType>*);
31 BUFFER& connectFactorHullTRBP(
const size_t, BUFFER&);
32 size_t numberOfBuffers()
const;
33 void propagateAll(
const GM&,
const ValueType& = 0,
const bool =
false);
34 void propagate(
const GM&,
const size_t,
const ValueType& = 0,
const bool =
false);
35 void marginal(
const GM&,
const size_t, IndependentFactorType&,
const bool =
true)
const;
37 template<
class DIST> ValueType distance(
const size_t)
const;
40 std::vector<BUFFER* > outBuffer_;
41 std::vector<BUFFER > inBuffer_;
42 std::vector<ValueType> rho_;
46 template<
class GM,
class BUFFER,
class OP,
class ACC>
47 class FactorHullTRBP {
49 typedef GM GraphicalModelType;
50 typedef BUFFER BufferType;
51 typedef typename BUFFER::ArrayType BufferArrayType;
52 typedef typename GM::FactorType FactorType;
53 typedef typename GM::IndependentFactorType IndependentFactorType;
54 typedef typename GM::ValueType ValueType;
57 size_t numberOfBuffers()
const {
return inBuffer_.size(); }
59 void assign(
const GM&,
const size_t, std::vector<VariableHullTRBP<GM,BUFFER,OP,ACC> >&,
const std::vector<ValueType>*);
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* myFactor_;
68 std::vector<BUFFER* > outBuffer_;
69 std::vector<BUFFER > inBuffer_;
75 template<
class GM,
class ACC,
class BUFFER = opengm::MessageBuffer<marray::Marray<
double> > >
86 template<
class MP_PARAM>
89 if (mpParameter.specialParameter_.size() == 0) {
91 opengm::GraphicalModelDecomposer<GM> decomposer;
92 const opengm::GraphicalModelDecomposition decomposition = decomposer.decomposeIntoSpanningTrees(gm);
94 typedef typename GraphicalModelDecomposition::SubFactorListType SubFactorListType;
95 const std::vector<SubFactorListType>& subFactorList = decomposition.getFactorLists();
96 mpParameter.specialParameter_.resize(gm.numberOfFactors());
97 for (
size_t factorId = 0; factorId < gm.numberOfFactors(); ++factorId) {
98 mpParameter.specialParameter_[factorId] = 1.0 / subFactorList[factorId].size();
101 else if (mpParameter.specialParameter_.size() != gm.numberOfFactors()) {
102 throw RuntimeError(
"The parameter rho has been set incorrectly.");
106 OPENGM_ASSERT(mpParameter.specialParameter_.size() == gm.numberOfFactors());
107 for (
size_t i = 0; i < gm.numberOfFactors(); ++i) {
108 if(gm.numberOfVariables() < 2) {
117 template<
class GM,
class BUFFER,
class OP,
class ACC>
118 inline VariableHullTRBP<GM, BUFFER, OP, ACC>::VariableHullTRBP()
121 template<
class GM,
class BUFFER,
class OP,
class ACC>
122 inline void VariableHullTRBP<GM, BUFFER, OP, ACC>::assign
125 const size_t variableIndex,
126 const std::vector<ValueType>* rho
128 size_t numberOfFactors = gm.numberOfFactors(variableIndex);
129 rho_.resize(numberOfFactors);
130 for(
size_t j = 0; j < numberOfFactors; ++j) {
131 rho_[j] = (*rho)[gm.factorOfVariable(variableIndex,j)];
133 inBuffer_.resize(numberOfFactors);
134 outBuffer_.resize(numberOfFactors);
136 for(
size_t j = 0; j < numberOfFactors; ++j) {
137 inBuffer_[j].assign(gm.numberOfLabels(variableIndex), OP::template neutral<ValueType > ());
142 template<
class GM,
class BUFFER,
class OP,
class ACC>
143 inline size_t VariableHullTRBP<GM, BUFFER, OP, ACC>::numberOfBuffers()
const {
144 return inBuffer_.size();
147 template<
class GM,
class BUFFER,
class OP,
class ACC>
148 inline BUFFER& VariableHullTRBP<GM, BUFFER, OP, ACC>::connectFactorHullTRBP(
149 const size_t bufferNumber,
150 BUFFER& variableOutBuffer
152 outBuffer_[bufferNumber] =&variableOutBuffer;
153 return inBuffer_[bufferNumber];
156 template<
class GM,
class BUFFER,
class OP,
class ACC>
157 void VariableHullTRBP<GM, BUFFER, OP, ACC>::propagate(
160 const ValueType& damping,
161 const bool useNormalization
164 outBuffer_[id]->toggle();
165 if(numberOfBuffers() < 2) {
169 BufferArrayType& newMessage = outBuffer_[id]->current();
170 opengm::messagepassingOperations::operateW<GM>(inBuffer_, id, rho_, newMessage);
174 BufferArrayType& oldMessage = outBuffer_[id]->old();
175 if(useNormalization) {
176 opengm::messagepassingOperations::normalize<OP,ACC>(newMessage);
177 opengm::messagepassingOperations::normalize<OP,ACC>(oldMessage);
179 opengm::messagepassingOperations::weightedMean<OP>(newMessage, oldMessage, damping, newMessage);
181 if(useNormalization) {
182 opengm::messagepassingOperations::normalize<OP,ACC>(newMessage);
188 template<
class GM,
class BUFFER,
class OP,
class ACC>
189 inline void VariableHullTRBP<GM, BUFFER, OP, ACC>::propagateAll
192 const ValueType& damping,
193 const bool useNormalization
195 for(
size_t j = 0; j < numberOfBuffers(); ++j) {
196 propagate(gm, j, damping, useNormalization);
200 template<
class GM,
class BUFFER,
class OP,
class ACC>
201 inline void VariableHullTRBP<GM, BUFFER, OP, ACC>::marginal
204 const size_t variableIndex,
205 IndependentFactorType& out,
206 const bool useNormalization
209 out.assign(gm, &variableIndex, &variableIndex+1, OP::template neutral<ValueType>());
210 opengm::messagepassingOperations::operateW<GM>(inBuffer_, rho_, out);
213 if(useNormalization) {
214 opengm::messagepassingOperations::normalize<OP,ACC>(out);
242 template<
class GM,
class BUFFER,
class OP,
class ACC>
244 inline typename GM::ValueType VariableHullTRBP<GM, BUFFER, OP, ACC>::distance
248 return inBuffer_[j].template dist<DIST > ();
251 template<
class GM,
class BUFFER,
class OP,
class ACC>
252 inline FactorHullTRBP<GM, BUFFER, OP, ACC>::FactorHullTRBP()
255 template<
class GM,
class BUFFER,
class OP,
class ACC>
256 inline void FactorHullTRBP<GM, BUFFER, OP, ACC>::assign
259 const size_t factorIndex,
260 std::vector<VariableHullTRBP<GM, BUFFER, OP, ACC> >& variableHulls,
261 const std::vector<ValueType>* rho
263 rho_ = (*rho)[factorIndex];
264 myFactor_ = (FactorType*) (&gm[factorIndex]);
265 inBuffer_.resize(gm[factorIndex].numberOfVariables());
266 outBuffer_.resize(gm[factorIndex].numberOfVariables());
268 for(
size_t n=0; n<gm.numberOfVariables(factorIndex); ++n) {
269 size_t var = gm.variableOfFactor(factorIndex,n);
270 inBuffer_[n].assign(gm.numberOfLabels(var), OP::template neutral<ValueType > ());
271 size_t bufferNumber = 1000000;
272 for(
size_t i=0; i<gm.numberOfFactors(var); ++i) {
273 if(gm.factorOfVariable(var,i)==factorIndex)
277 outBuffer_[n] =&(variableHulls[var].connectFactorHullTRBP(bufferNumber, inBuffer_[n]));
281 template<class GM, class BUFFER, class OP, class ACC>
282 void FactorHullTRBP<GM, BUFFER, OP, ACC>::propagate
285 const ValueType& damping,
286 const
bool useNormalization
289 outBuffer_[id]->toggle();
290 BufferArrayType& newMessage = outBuffer_[id]->current();
291 opengm::messagepassingOperations::operateWF<GM,ACC>(*myFactor_, rho_, inBuffer_, id, newMessage);
295 BufferArrayType& oldMessage = outBuffer_[id]->old();
296 if(useNormalization) {
297 opengm::messagepassingOperations::normalize<OP,ACC>(newMessage);
298 opengm::messagepassingOperations::normalize<OP,ACC>(oldMessage);
300 opengm::messagepassingOperations::weightedMean<OP>(newMessage, oldMessage, damping, newMessage);
303 if(useNormalization) {
304 opengm::messagepassingOperations::normalize<OP,ACC>(newMessage);
309 template<
class GM,
class BUFFER,
class OP,
class ACC>
310 inline void FactorHullTRBP<GM, BUFFER, OP, ACC>::propagateAll
312 const ValueType& damping,
313 const bool useNormalization
315 for(
size_t j = 0; j < numberOfBuffers(); ++j) {
316 propagate(j, damping, useNormalization);
320 template<
class GM,
class BUFFER,
class OP,
class ACC>
321 inline void FactorHullTRBP<GM, BUFFER, OP, ACC>::marginal
323 IndependentFactorType& out,
324 const bool useNormalization
327 opengm::messagepassingOperations::operateWF<GM>(*(
const_cast<FactorType*
> (myFactor_)), rho_, inBuffer_, out);
329 if(useNormalization) {
330 opengm::messagepassingOperations::normalize<OP,ACC>(out);
362 template<
class GM,
class BUFFER,
class OP,
class ACC>
364 inline typename GM::ValueType FactorHullTRBP<GM, BUFFER, OP, ACC>::distance
368 return inBuffer_[j].template dist<DIST > ();
373 #endif // #ifndef OPENGM_BELIEFPROPAGATION_HXX
Update rules for the MessagePassing framework.
std::vector< ValueType > SpecialParameterType
#define OPENGM_ASSERT(expression)
VariableHullTRBP< GM, BUFFER, OperatorType, ACC > VariableHullType
GM::IndependentFactorType IndependentFactorType
FactorHullTRBP< GM, BUFFER, OperatorType, ACC > FactorHullType
GM::OperatorType OperatorType
GM::FactorType FactorType
static void initializeSpecialParameter(const GM &gm, MP_PARAM &mpParameter)