1 #ifndef TRWS_DECOMPOSITION_HXX_
2 #define TRWS_DECOMPOSITION_HXX_
12 #ifdef TRWS_DEBUG_OUTPUT
19 #ifdef TRWS_DEBUG_OUTPUT
20 using OUT::operator <<;
30 typedef opengm::GraphicalModelDecomposition::SubVariable
SubVariable;
44 #ifdef TRWS_DEBUG_OUTPUT
45 virtual void PrintTestData(std::ostream& fout);
54 typedef std::pair<IndexType,IndexType>
Edge;
100 IndexType
xsize()
const{
return _xsize;}
101 IndexType
ysize()
const{
return _ysize;}
103 IndexType _xsize, _ysize;
109 IndexType
_xysize()
const{
return _xsize*_ysize;}
111 IndexType
_pwIndexRow(IndexType x,IndexType y)
const;
112 IndexType
_pwIndexCol(IndexType x,IndexType y)
const;
113 IndexType
_varIndex(IndexType x,IndexType y)
const{
return x+_xsize*y;}
114 void _getRow(IndexType y,IndexList* plist)
const;
115 void _getCol(IndexType x,IndexList* plist)
const;
116 void _getPWRow(IndexType y, IndexList* plist)
const;
117 void _getPWCol(IndexType x,IndexList* plist)
const;
141 for (IndexType fId=0;fId<gm.numberOfFactors();++fId)
143 if (gm[fId].numberOfVariables()==1)
continue;
144 if ((gm[fId].numberOfVariables()>2) || (gm[fId].numberOfVariables()==0))
145 std::runtime_error(
"EdgeDecomposition<GM>::EdgeDecomposition(): Only factors of order 1 or 2 are supported!");
157 #ifdef TRWS_DEBUG_OUTPUT
158 template <
class SubFactor>
159 struct printSubFactor
161 printSubFactor(std::ostream& out):_out(out){};
162 void operator()(
const SubFactor& a)
164 _out <<
"("<<a.subModelId_ <<
","<< a.subFactorId_ <<
")"<<
", ";
172 #ifdef TRWS_DEBUG_OUTPUT
173 template <
class SubVariable>
174 struct printSubVariable
176 printSubVariable(std::ostream& out):_out(out){};
177 void operator()(
const SubVariable& a)
179 _out <<
"("<<a.subModelId_ <<
","<< a.subVariableId_ <<
")"<<
", ";
192 #ifdef TRWS_DEBUG_OUTPUT
196 fout <<
"_numberOfModels;" << _numberOfModels<<std::endl;
197 fout <<
"_variableLists:"<<_variableLists<<std::endl;
198 fout <<
"_pwFactorLists:"<<_pwFactorLists<<std::endl;
211 for (
IndexType start=0;start<nodeList.size();++start)
212 while (!nodeList[start].empty())
229 template<
class Factor>
238 IndexType numberOfVars=parent::_gm.numberOfVariables();
239 IndexType numTotal=parent::_gm.numberOfFactors();
240 std::vector<IndexType> ind;
241 for (
IndexType f=numberOfVars;f<numTotal;++f)
243 std::vector<IndexType> ind(parent::_gm[f].numberOfVariables());
245 throw std::runtime_error(
"GridDecomposition<GM>::_computeGridSizes():Incorrect grid structure! : only pairwise factors are supported !=0");
246 parent::_gm[f].variableIndices(ind.begin());
248 throw std::runtime_error(
"GridDecomposition<GM>::_computeGridSizes():Incorrect grid structure! : pairwise factors should be oriented from smaller to larger variable indices !=0");
250 if (ind[1]-ind[0]!=1)
252 _xsize=ind[1]-ind[0];
253 _ysize=numberOfVars/_xsize;
254 if (numberOfVars%_xsize !=0)
255 throw std::runtime_error(
"GridDecomposition<GM>::_computeGridSizes():Incorrect grid structure! : numberOfVars%xsize !=0");
257 }
else if (f==numTotal-1)
271 bool incorrect=
false;
291 throw std::runtime_error(
"GridDecomposition::_CheckGridModel():Incorrect grid structure!");
299 parent::_variableLists.resize(parent::_numberOfModels);
300 parent::_pwFactorLists.resize(parent::_numberOfModels);
303 _getCol(x,&parent::_variableLists[x]);
304 _getPWCol(x,&parent::_pwFactorLists[x]);
309 _getRow(y,&parent::_variableLists[_xsize+y]);
310 _getPWRow(y,&parent::_pwFactorLists[_xsize+y]);
319 varList.resize(gm.numberOfVariables());
320 for (
IndexType factorId=0;factorId<gm.numberOfFactors();++factorId)
322 if (gm[factorId].numberOfVariables()>2)
323 throw std::runtime_error(
"CreateEdgeList(): Only factors up to order 2 are supported!");
325 if (gm[factorId].numberOfVariables()==1)
continue;
326 std::vector<IndexType> varIndices(gm[factorId].variableIndicesBegin(),gm[factorId].variableIndicesEnd());
327 if (varIndices[0] < varIndices[1])
328 varList[varIndices[0]].push_back(std::make_pair(factorId,varIndices[1]));
330 varList[varIndices[1]].push_back(std::make_pair(factorId,varIndices[0]));
346 _pwFactorLists[_numberOfModels-1].push_back(factorId);
352 _variableLists[_numberOfModels-1].push_back(variableId);
358 assert(start < pnodeList->size());
360 if (!nodeList[start].empty())
361 parent::_addSubVariable(start);
364 while ( !nodeList[start].empty() )
366 typename parent::EdgeList::iterator it= nodeList[start].begin();
367 parent::_addSubVariable(it->second);
368 parent::_addSubFactor(it->first);
370 nodeList[start].erase(it);
380 for (
IndexType factorId=0;factorId<gm.numberOfFactors();++factorId)
382 std::vector<IndexType> varIndices(gm[factorId].variableIndicesBegin(),gm[factorId].variableIndicesEnd());
383 if (gm[factorId].numberOfVariables()==1)
385 if ( (factorId < gm.numberOfVariables()) && (varIndices[0]==factorId))
388 }
else if (factorId < gm.numberOfVariables())
392 throw std::runtime_error(
"Decomposition<GM>::CheckUnaryFactors(): Each variable has to have a unique unary factor, which moreover has the same index!");
399 std::vector<IndexType> numOfunaryFactors(gm.numberOfVariables(),(
IndexType)0);
400 for (
IndexType factorId=0;factorId<gm.numberOfFactors();++factorId)
402 if (gm[factorId].numberOfVariables()!=1)
405 numOfunaryFactors[gm[factorId].variableIndex(0)]++;
408 IndexType oneCount=std::count(numOfunaryFactors.begin(),numOfunaryFactors.end(),(
IndexType)1);
409 exception_check(oneCount==numOfunaryFactors.size(),
"Decomposition::CheckDuplicateUnaryFactors: all variables must have a unique associated unary factor!");
415 for (
IndexType varId=0;varId<gm.numberOfVariables();++varId)
417 bool isolatedNode=
true;
418 for (
IndexType localId=0;localId<gm.numberOfFactors(varId);++localId)
420 if (gm[gm.factorOfVariable(varId,localId)].numberOfVariables()>1)
423 if (isolatedNode==
true)
426 _addSubVariable(varId);
435 plist->resize(_gm.numberOfVariables());
436 for (
IndexType modelId=0;modelId<_numberOfModels;++modelId)
437 for (
IndexType varId=0;varId<_variableLists[modelId].size();++varId)
438 (*plist)[_variableLists[modelId][varId]].push_back(
SubVariable(modelId,varId));
447 if ((y==_ysize-1) && (x!=0))
return _pwIndexRow(0,y) + x;
448 return _xysize()+y*_pwrowsize()+2*x;
454 if (x==_xsize-1)
return _pwIndexCol(x-1,y)+1;
455 return _pwIndexRow(x,y)+1;
462 plist->resize(_xsize);
463 (*plist)[0]=_varIndex(0,y);
465 (*plist)[i]=(*plist)[i-1]+1;
472 plist->resize(_ysize);
473 (*plist)[0]=_varIndex(x,0);
475 (*plist)[i]=(*plist)[i-1]+_xsize;
482 plist->resize(_xsize-1);
485 (*plist)[0]=_pwIndexRow(0,y);
487 if (y==_ysize-1) step=1;
489 (*plist)[i]=(*plist)[i-1]+step;
496 plist->resize(_ysize-1);
500 (*plist)[0]=_pwIndexCol(x,0);
502 (*plist)[i]=(*plist)[i-1]+_pwrowsize();
parent::LabelType LabelType
parent::SubVariable SubVariable
void exception_check(bool condition, const std::string &str)
parent::SubVariableListType SubVariableListType
std::vector< IndexList > _pwFactorLists
parent::SubVariableListType SubVariableListType
IndexType _varIndex(IndexType x, IndexType y) const
returns an index of a column pairwise factor places to the down to var (x,y)
EdgeDecomposition(const GM &gm)
std::pair< IndexType, IndexType > Edge
parent::IndexType IndexType
bool dependsOnVariable(const Factor &f, typename Factor::IndexType varId)
std::vector< IndexList > _variableLists
void _addSubFactor(const IndexType &factorId)
IndexType _xysize() const
virtual const IndexList & getVariableList(IndexType subModelId) const
static void CheckUnaryFactors(const GM &gm)
checks whether all variables have corresp. unary factor with the same index and vice versa ...
parent::IndexList IndexList
opengm::GraphicalModelDecomposition::SubVariableListType SubVariableListType
parent::SubVariable SubVariable
VariablesIteratorType variableIndicesBegin() const
std::list< Edge > EdgeList
void CheckForIsolatedNodes(const GM &gm)
std::vector< IndexType > IndexList
parent::SubVariable SubVariable
Decomposition< GM > parent
GridDecomposition(const GM &gm, IndexType numSubModels=0)
numSubModels - ESTIMATED number of submodels to optimize memory allocation
static void _CreateNodeList(const GM &gm, NodeList *pnodeList)
void _addSubVariable(const IndexType &variableId)
VariablesIteratorType variableIndicesEnd() const
std::vector< EdgeList > NodeList
Abstraction (wrapper class) of factors, independent of the function used to implement the factor...
parent::IndexList IndexList
void _initDecompositionLists()
GraphicalModelType::IndexType IndexType
virtual const IndexList & getFactorList(IndexType subModelId) const
Decomposition< GM > parent
IndexType _pwIndexRow(IndexType x, IndexType y) const
returns an index of a row pairwise factor places to the right to var (x,y)
static void CheckDuplicateUnaryFactors(const GM &gm)
parent::IndexList IndexList
virtual void ComputeVariableDecomposition(std::vector< SubVariableListType > *plist) const
parent::IndexType IndexType
parent::IndexType IndexType
Decomposition< GM > parent
void _GetMaximalMonotoneSequence(typename parent::NodeList *pnodesList, IndexType start)
opengm::GraphicalModelDecomposition::SubVariable SubVariable
IndexType _pwIndexCol(IndexType x, IndexType y) const
returns an index of a row pairwise factor places to the right to var (x,y)
virtual IndexType getNumberOfSubModels() const
void _getPWCol(IndexType x, IndexList *plist) const
return indexes of pairwise factors in the row
IndexType _pwrowsize() const
IndexType _numberOfModels
void _getRow(IndexType y, IndexList *plist) const
returns indexes of variables in the row
parent::LabelType LabelType
void _getCol(IndexType x, IndexList *plist) const
returns indexes of variables in the row
parent::LabelType LabelType
virtual ~Decomposition()=0
void _getPWRow(IndexType y, IndexList *plist) const
returns indexes of variables in the column
parent::SubVariableListType SubVariableListType
Decomposition(const GM &gm, IndexType numSubModels=0)
MonotoneChainsDecomposition(const GM &gm, IndexType numSubModels=0)
numSubModels - ESTIMATED number of submodels to optimize memory allocation