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> > >
90 template<
class _GM,
class _ACC>
95 template<
class MP_PARAM>
98 if (mpParameter.specialParameter_.size() == 0) {
100 opengm::GraphicalModelDecomposer<GM> decomposer;
101 const opengm::GraphicalModelDecomposition decomposition = decomposer.decomposeIntoSpanningTrees(gm);
103 typedef typename GraphicalModelDecomposition::SubFactorListType SubFactorListType;
104 const std::vector<SubFactorListType>& subFactorList = decomposition.getFactorLists();
105 mpParameter.specialParameter_.resize(gm.numberOfFactors());
106 for (
size_t factorId = 0; factorId < gm.numberOfFactors(); ++factorId) {
107 mpParameter.specialParameter_[factorId] = 1.0 / subFactorList[factorId].size();
110 else if (mpParameter.specialParameter_.size() != gm.numberOfFactors()) {
111 throw RuntimeError(
"The parameter rho has been set incorrectly.");
115 OPENGM_ASSERT(mpParameter.specialParameter_.size() == gm.numberOfFactors());
116 for (
size_t i = 0; i < gm.numberOfFactors(); ++i) {
117 if(gm.numberOfVariables() < 2) {
126 template<
class GM,
class BUFFER,
class OP,
class ACC>
127 inline VariableHullTRBP<GM, BUFFER, OP, ACC>::VariableHullTRBP()
130 template<
class GM,
class BUFFER,
class OP,
class ACC>
131 inline void VariableHullTRBP<GM, BUFFER, OP, ACC>::assign
134 const size_t variableIndex,
135 const std::vector<ValueType>* rho
137 size_t numberOfFactors = gm.numberOfFactors(variableIndex);
138 rho_.resize(numberOfFactors);
139 for(
size_t j = 0; j < numberOfFactors; ++j) {
140 rho_[j] = (*rho)[gm.factorOfVariable(variableIndex,j)];
142 inBuffer_.resize(numberOfFactors);
143 outBuffer_.resize(numberOfFactors);
145 for(
size_t j = 0; j < numberOfFactors; ++j) {
146 inBuffer_[j].assign(gm.numberOfLabels(variableIndex), OP::template neutral<ValueType > ());
151 template<
class GM,
class BUFFER,
class OP,
class ACC>
152 inline size_t VariableHullTRBP<GM, BUFFER, OP, ACC>::numberOfBuffers()
const {
153 return inBuffer_.size();
156 template<
class GM,
class BUFFER,
class OP,
class ACC>
157 inline BUFFER& VariableHullTRBP<GM, BUFFER, OP, ACC>::connectFactorHullTRBP(
158 const size_t bufferNumber,
159 BUFFER& variableOutBuffer
161 outBuffer_[bufferNumber] =&variableOutBuffer;
162 return inBuffer_[bufferNumber];
165 template<
class GM,
class BUFFER,
class OP,
class ACC>
166 void VariableHullTRBP<GM, BUFFER, OP, ACC>::propagate(
170 const bool useNormalization
173 outBuffer_[id]->toggle();
174 if(numberOfBuffers() < 2) {
178 BufferArrayType& newMessage = outBuffer_[id]->current();
179 opengm::messagepassingOperations::operateW<GM>(inBuffer_, id, rho_, newMessage);
183 BufferArrayType& oldMessage = outBuffer_[id]->old();
184 if(useNormalization) {
185 opengm::messagepassingOperations::normalize<OP,ACC>(newMessage);
186 opengm::messagepassingOperations::normalize<OP,ACC>(oldMessage);
188 opengm::messagepassingOperations::weightedMean<OP>(newMessage, oldMessage, damping, newMessage);
190 if(useNormalization) {
191 opengm::messagepassingOperations::normalize<OP,ACC>(newMessage);
197 template<
class GM,
class BUFFER,
class OP,
class ACC>
198 inline void VariableHullTRBP<GM, BUFFER, OP, ACC>::propagateAll
202 const bool useNormalization
204 for(
size_t j = 0; j < numberOfBuffers(); ++j) {
205 propagate(gm, j, damping, useNormalization);
209 template<
class GM,
class BUFFER,
class OP,
class ACC>
210 inline void VariableHullTRBP<GM, BUFFER, OP, ACC>::marginal
213 const size_t variableIndex,
215 const bool useNormalization
218 out.assign(gm, &variableIndex, &variableIndex+1, OP::template neutral<ValueType>());
219 opengm::messagepassingOperations::operateW<GM>(inBuffer_, rho_, out);
222 if(useNormalization) {
223 opengm::messagepassingOperations::normalize<OP,ACC>(out);
251 template<
class GM,
class BUFFER,
class OP,
class ACC>
253 inline typename GM::ValueType VariableHullTRBP<GM, BUFFER, OP, ACC>::distance
257 return inBuffer_[j].template dist<DIST > ();
260 template<
class GM,
class BUFFER,
class OP,
class ACC>
261 inline FactorHullTRBP<GM, BUFFER, OP, ACC>::FactorHullTRBP()
264 template<
class GM,
class BUFFER,
class OP,
class ACC>
265 inline void FactorHullTRBP<GM, BUFFER, OP, ACC>::assign
268 const size_t factorIndex,
269 std::vector<VariableHullTRBP<GM, BUFFER, OP, ACC> >& variableHulls,
270 const std::vector<ValueType>* rho
272 rho_ = (*rho)[factorIndex];
274 inBuffer_.resize(gm[factorIndex].numberOfVariables());
275 outBuffer_.resize(gm[factorIndex].numberOfVariables());
277 for(
size_t n=0; n<gm.numberOfVariables(factorIndex); ++n) {
278 size_t var = gm.variableOfFactor(factorIndex,n);
279 inBuffer_[n].assign(gm.numberOfLabels(var), OP::template neutral<ValueType > ());
280 size_t bufferNumber = 1000000;
281 for(
size_t i=0; i<gm.numberOfFactors(var); ++i) {
282 if(gm.factorOfVariable(var,i)==factorIndex)
286 outBuffer_[n] =&(variableHulls[var].connectFactorHullTRBP(bufferNumber, inBuffer_[n]));
290 template<
class GM,
class BUFFER,
class OP,
class ACC>
291 void FactorHullTRBP<GM, BUFFER, OP, ACC>::propagate
295 const bool useNormalization
298 outBuffer_[id]->toggle();
299 BufferArrayType& newMessage = outBuffer_[id]->current();
300 opengm::messagepassingOperations::operateWF<GM,ACC>(*myFactor_, rho_, inBuffer_, id, newMessage);
304 BufferArrayType& oldMessage = outBuffer_[id]->old();
305 if(useNormalization) {
306 opengm::messagepassingOperations::normalize<OP,ACC>(newMessage);
307 opengm::messagepassingOperations::normalize<OP,ACC>(oldMessage);
309 opengm::messagepassingOperations::weightedMean<OP>(newMessage, oldMessage, damping, newMessage);
312 if(useNormalization) {
313 opengm::messagepassingOperations::normalize<OP,ACC>(newMessage);
318 template<
class GM,
class BUFFER,
class OP,
class ACC>
319 inline void FactorHullTRBP<GM, BUFFER, OP, ACC>::propagateAll
322 const bool useNormalization
324 for(
size_t j = 0; j < numberOfBuffers(); ++j) {
325 propagate(j, damping, useNormalization);
329 template<
class GM,
class BUFFER,
class OP,
class ACC>
330 inline void FactorHullTRBP<GM, BUFFER, OP, ACC>::marginal
333 const bool useNormalization
336 opengm::messagepassingOperations::operateWF<GM>(*(
const_cast<FactorType*
> (myFactor_)), rho_, inBuffer_, out);
338 if(useNormalization) {
339 opengm::messagepassingOperations::normalize<OP,ACC>(out);
371 template<
class GM,
class BUFFER,
class OP,
class ACC>
373 inline typename GM::ValueType FactorHullTRBP<GM, BUFFER, OP, ACC>::distance
377 return inBuffer_[j].template dist<DIST > ();
382 #endif // #ifndef OPENGM_BELIEFPROPAGATION_HXX
Update rules for the MessagePassing framework.
TrbpUpdateRules< _GM, ACC, BUFFER > type
std::vector< ValueType > SpecialParameterType
#define OPENGM_ASSERT(expression)
VariableHullTRBP< GM, BUFFER, OperatorType, ACC > VariableHullType
TrbpUpdateRules< _GM, _ACC, BUFFER > type
GM::IndependentFactorType IndependentFactorType
FactorHullTRBP< GM, BUFFER, OperatorType, ACC > FactorHullType
GM::OperatorType OperatorType
GM::FactorType FactorType
static void initializeSpecialParameter(const GM &gm, MP_PARAM &mpParameter)