libDAI
clustergraph.h
Go to the documentation of this file.
1 /* This file is part of libDAI - http://www.libdai.org/
2  *
3  * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
4  *
5  * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6  */
7 
8 
13 
14 
15 #ifndef __defined_libdai_clustergraph_h
16 #define __defined_libdai_clustergraph_h
17 
18 
19 #include <set>
20 #include <vector>
21 #include <dai/varset.h>
22 #include <dai/bipgraph.h>
23 #include <dai/factorgraph.h>
24 
25 
26 namespace dai {
27 
28 
30 
34  class ClusterGraph {
35  private:
38 
40  std::vector<Var> _vars;
41 
43  std::vector<VarSet> _clusters;
44 
45  public:
47 
48  ClusterGraph() : _G(), _vars(), _clusters() {}
50 
52  ClusterGraph( const std::vector<VarSet>& cls );
53 
55 
58  ClusterGraph( const FactorGraph& fg, bool onlyMaximal );
60 
62 
63  const BipartiteGraph& bipGraph() const { return _G; }
65 
67  size_t nrVars() const { return _vars.size(); }
68 
70  const std::vector<Var>& vars() const { return _vars; }
71 
73  const Var& var( size_t i ) const {
74  DAI_DEBASSERT( i < nrVars() );
75  return _vars[i];
76  }
77 
79  size_t nrClusters() const { return _clusters.size(); }
80 
82  const std::vector<VarSet>& clusters() const { return _clusters; }
83 
85  const VarSet& cluster( size_t I ) const {
86  DAI_DEBASSERT( I < nrClusters() );
87  return _clusters[I];
88  }
89 
91  size_t findVar( const Var& n ) const {
92  return find( _vars.begin(), _vars.end(), n ) - _vars.begin();
93  }
94 
96  size_t findCluster( const VarSet& cl ) const {
97  return find( _clusters.begin(), _clusters.end(), cl ) - _clusters.begin();
98  }
99 
101  VarSet Delta( size_t i ) const {
102  VarSet result;
103  bforeach( const Neighbor& I, _G.nb1(i) )
104  result |= _clusters[I];
105  return result;
106  }
107 
109  VarSet delta( size_t i ) const {
110  return Delta( i ) / _vars[i];
111  }
112 
114  bool adj( size_t i1, size_t i2 ) const {
115  if( i1 == i2 )
116  return false;
117  bool result = false;
118  bforeach( const Neighbor& I, _G.nb1(i1) )
119  if( find( _G.nb2(I).begin(), _G.nb2(I).end(), i2 ) != _G.nb2(I).end() ) {
120  result = true;
121  break;
122  }
123  return result;
124  }
125 
127  bool isMaximal( size_t I ) const {
128  DAI_DEBASSERT( I < _G.nrNodes2() );
129  const VarSet & clI = _clusters[I];
130  bool maximal = true;
131  // The following may not be optimal, since it may repeatedly test the same cluster *J
132  bforeach( const Neighbor& i, _G.nb2(I) ) {
133  bforeach( const Neighbor& J, _G.nb1(i) )
134  if( (J != I) && (clI << _clusters[J]) ) {
135  maximal = false;
136  break;
137  }
138  if( !maximal )
139  break;
140  }
141  return maximal;
142  }
144 
146 
147 
151  size_t insert( const VarSet& cl ) {
152  size_t index = findCluster( cl ); // OPTIMIZE ME
153  if( index == _clusters.size() ) {
154  _clusters.push_back( cl );
155  // add variables (if necessary) and calculate neighborhood of new cluster
156  std::vector<size_t> nbs;
157  for( VarSet::const_iterator n = cl.begin(); n != cl.end(); n++ ) {
158  size_t iter = findVar( *n ); // OPTIMIZE ME
159  nbs.push_back( iter );
160  if( iter == _vars.size() ) {
161  _G.addNode1();
162  _vars.push_back( *n );
163  }
164  }
165  _G.addNode2( nbs.begin(), nbs.end(), nbs.size() );
166  }
167  return index;
168  }
169 
172  for( size_t I = 0; I < _G.nrNodes2(); ) {
173  if( !isMaximal(I) ) {
174  _clusters.erase( _clusters.begin() + I );
175  _G.eraseNode2(I);
176  } else
177  I++;
178  }
179  return *this;
180  }
181 
184  DAI_ASSERT( i < nrVars() );
185  while( _G.nb1(i).size() ) {
186  _clusters.erase( _clusters.begin() + _G.nb1(i)[0] );
187  _G.eraseNode2( _G.nb1(i)[0] );
188  }
189  return *this;
190  }
191 
193 
195  VarSet elimVar( size_t i ) {
196  DAI_ASSERT( i < nrVars() );
197  VarSet Di = Delta( i );
198  insert( Di / var(i) );
199  eraseSubsuming( i );
200  eraseNonMaximal();
201  return Di;
202  }
204 
206 
207  friend std::ostream& operator << ( std::ostream& os, const ClusterGraph& cl ) {
209  os << cl.clusters();
210  return os;
211  }
212 
214  std::string toString() const {
215  std::stringstream ss;
216  ss << *this;
217  return ss.str();
218  }
220 
222 
223 
230  template<class EliminationChoice>
231  ClusterGraph VarElim( EliminationChoice f, size_t maxStates=0 ) const {
232  // Make a copy
233  ClusterGraph cl(*this);
234  cl.eraseNonMaximal();
235 
236  ClusterGraph result;
237 
238  // Construct set of variable indices
239  std::set<size_t> varindices;
240  for( size_t i = 0; i < _vars.size(); ++i )
241  varindices.insert( i );
242 
243  // Do variable elimination
244  BigInt totalStates = 0;
245  while( !varindices.empty() ) {
246  size_t i = f( cl, varindices );
247  VarSet Di = cl.elimVar( i );
248  result.insert( Di );
249  if( maxStates ) {
250  totalStates += Di.nrStates();
251  if( totalStates > (BigInt)maxStates )
252  DAI_THROW(OUT_OF_MEMORY);
253  }
254  varindices.erase( i );
255  }
256 
257  return result;
258  }
260  };
261 
262 
264 
267  private:
269  std::vector<Var> seq;
271  size_t i;
272 
273  public:
275  sequentialVariableElimination( const std::vector<Var> s ) : seq(s), i(0) {}
276 
278  size_t operator()( const ClusterGraph &cl, const std::set<size_t> &/*remainingVars*/ );
279  };
280 
281 
283 
287  public:
289  typedef size_t (*eliminationCostFunction)(const ClusterGraph &, size_t);
290 
291  private:
294 
295  public:
297 
300 
302 
304  size_t operator()( const ClusterGraph &cl, const std::set<size_t>& remainingVars );
305  };
306 
307 
309 
313  size_t eliminationCost_MinNeighbors( const ClusterGraph& cl, size_t i );
314 
315 
317 
322  size_t eliminationCost_MinWeight( const ClusterGraph& cl, size_t i );
323 
324 
326 
330  size_t eliminationCost_MinFill( const ClusterGraph& cl, size_t i );
331 
332 
334 
339  size_t eliminationCost_WeightedMinFill( const ClusterGraph& cl, size_t i );
340 
341 
342 } // end of namespace dai
343 
344 
345 #endif
friend std::ostream & operator<<(std::ostream &os, const ClusterGraph &cl)
Writes a ClusterGraph to an output stream.
Definition: clustergraph.h:208
bool isMaximal(size_t I) const
Returns true if cluster I is not contained in a larger cluster.
Definition: clustergraph.h:127
iterator end()
Returns iterator that points beyond the last element.
Definition: smallset.h:198
const std::vector< VarSet > & clusters() const
Returns a constant reference to the clusters.
Definition: clustergraph.h:82
BigInt nrStates() const
Calculates the number of states of this VarSet, which is simply the number of possible joint states o...
Definition: varset.h:130
size_t eliminationCost_WeightedMinFill(const ClusterGraph &cl, size_t i)
Calculates cost of eliminating the i 'th variable from cluster graph cl according to the "WeightedMin...
Definition: clustergraph.cpp:122
const std::vector< Var > & vars() const
Returns a constant reference to the variables.
Definition: clustergraph.h:70
size_t addNode2()
Adds a node of type 2 without neighbors and returns the index of the added node.
Definition: bipgraph.h:152
ClusterGraph & eraseSubsuming(size_t i)
Erases all clusters that contain the i 'th variable.
Definition: clustergraph.h:183
ClusterGraph()
Default constructor.
Definition: clustergraph.h:49
BipartiteGraph _G
Stores the neighborhood structure.
Definition: clustergraph.h:37
const Neighbor & nb1(size_t i1, size_t _i2) const
Returns constant reference to the _i2 'th neighbor of node i1 of type 1.
Definition: bipgraph.h:87
Represents a factor graph.
Definition: factorgraph.h:65
iterator begin()
Returns iterator that points to the first element.
Definition: smallset.h:193
Defines the VarSet class, which represents a set of random variables.
size_t eliminationCost_MinNeighbors(const ClusterGraph &cl, size_t i)
Calculates cost of eliminating the i 'th variable from cluster graph cl according to the "MinNeighbor...
Definition: clustergraph.cpp:89
const BipartiteGraph & bipGraph() const
Returns a constant reference to the graph structure.
Definition: clustergraph.h:64
size_t i
Counter.
Definition: clustergraph.h:271
Represents the neighborhood structure of nodes in an undirected, bipartite graph. ...
Definition: bipgraph.h:45
const Var & var(size_t i) const
Returns a constant reference to the i'th variable.
Definition: clustergraph.h:73
sequentialVariableElimination(const std::vector< Var > s)
Construct from vector of variables.
Definition: clustergraph.h:275
size_t nrNodes2() const
Returns number of nodes of type 2.
Definition: bipgraph.h:224
size_t(* eliminationCostFunction)(const ClusterGraph &, size_t)
Type of cost functions to be used for greedy variable elimination.
Definition: clustergraph.h:289
size_t addNode1()
Adds a node of type 1 without neighbors and returns the index of the added node.
Definition: bipgraph.h:149
mpz_class BigInt
Arbitrary precision integer number.
Definition: util.h:101
size_t insert(const VarSet &cl)
Inserts a cluster (if it does not already exist) and creates new variables, if necessary.
Definition: clustergraph.h:151
Helper object for dai::ClusterGraph::VarElim()
Definition: clustergraph.h:286
ClusterGraph & eraseNonMaximal()
Erases all clusters that are not maximal.
Definition: clustergraph.h:171
VarSet elimVar(size_t i)
Eliminates variable with index i, without deleting the variable itself.
Definition: clustergraph.h:195
std::vector< Var >::const_iterator const_iterator
Constant iterator over the elements.
Definition: smallset.h:182
std::string toString() const
Formats a ClusterGraph as a string.
Definition: clustergraph.h:214
const VarSet & cluster(size_t I) const
Returns a constant reference to the I'th cluster.
Definition: clustergraph.h:85
const Neighbor & nb2(size_t i2, size_t _i1) const
Returns constant reference to the _i1 'th neighbor of node i2 of type 2.
Definition: bipgraph.h:100
A ClusterGraph is a hypergraph with variables as nodes, and "clusters" (sets of variables) as hypered...
Definition: clustergraph.h:34
Represents a set of variables.
Definition: varset.h:94
bool adj(size_t i1, size_t i2) const
Returns true if variables with indices i1 and i2 are adjacent, i.e., both contained in the same clust...
Definition: clustergraph.h:114
#define bforeach
An alias to the BOOST_FOREACH macro from the boost::bforeach library.
Definition: util.h:48
Helper object for dai::ClusterGraph::VarElim()
Definition: clustergraph.h:266
void eraseNode2(size_t n2)
Removes node n2 of type 2 and all incident edges; indices of other nodes are changed accordingly...
Definition: bipgraph.cpp:63
size_t eliminationCost_MinWeight(const ClusterGraph &cl, size_t i)
Calculates cost of eliminating the i 'th variable from cluster graph cl according to the "MinWeight" ...
Definition: clustergraph.cpp:94
size_t eliminationCost_MinFill(const ClusterGraph &cl, size_t i)
Calculates cost of eliminating the i 'th variable from cluster graph cl according to the "MinFill" cr...
Definition: clustergraph.cpp:105
size_t findVar(const Var &n) const
Returns the index of variable n.
Definition: clustergraph.h:91
size_t operator()(const ClusterGraph &cl, const std::set< size_t > &remainingVars)
Returns the best variable from remainingVars to eliminate in the cluster graph cl by greedily minimiz...
Definition: clustergraph.cpp:75
size_t nrClusters() const
Returns number of clusters.
Definition: clustergraph.h:79
Describes the neighbor relationship of two nodes in a graph.
Definition: graph.h:73
Represents a discrete random variable.
Definition: var.h:37
Namespace for libDAI.
Definition: alldai.cpp:16
size_t findCluster(const VarSet &cl) const
Returns the index of a cluster cl.
Definition: clustergraph.h:96
Defines the FactorGraph class, which represents factor graphs (e.g., Bayesian networks or Markov rand...
eliminationCostFunction heuristic
Pointer to the cost function used.
Definition: clustergraph.h:293
ClusterGraph VarElim(EliminationChoice f, size_t maxStates=0) const
Performs Variable Elimination, keeping track of the interactions that are created along the way...
Definition: clustergraph.h:231
#define DAI_ASSERT(condition)
Assertion mechanism, similar to the standard assert() macro. It is always active, even if NDEBUG is d...
Definition: exceptions.h:60
#define DAI_DEBASSERT(x)
Assertion mechanism similar to DAI_ASSERT which is only active if DAI_DEBUG is defined.
Definition: exceptions.h:65
size_t operator()(const ClusterGraph &cl, const std::set< size_t > &)
Returns next variable in sequence.
Definition: clustergraph.cpp:70
Defines the BipartiteGraph class, which represents a bipartite graph.
std::vector< Var > _vars
Stores the variables corresponding to the nodes.
Definition: clustergraph.h:40
VarSet delta(size_t i) const
Returns union of clusters that contain the i 'th (except this variable itself)
Definition: clustergraph.h:109
VarSet Delta(size_t i) const
Returns union of clusters that contain the i 'th variable.
Definition: clustergraph.h:101
greedyVariableElimination(eliminationCostFunction h)
Construct from cost function.
Definition: clustergraph.h:299
std::vector< Var > seq
The variable elimination sequence.
Definition: clustergraph.h:269
size_t nrVars() const
Returns number of variables.
Definition: clustergraph.h:67
std::vector< VarSet > _clusters
Stores the clusters corresponding to the hyperedges.
Definition: clustergraph.h:43