libDAI
cobwebgraph.h
Go to the documentation of this file.
1 /* This file is part of libDAI - http://www.libdai.org/
2  *
3  * Copyright (c) 2006-2012, 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 
11 
12 
13 #ifndef __defined_libdai_cobwebgraph_h
14 #define __defined_libdai_cobwebgraph_h
15 
16 
17 #include <iostream>
18 #include <dai/factorgraph.h>
19 #include <dai/weightedgraph.h>
20 #include <dai/smallset.h>
21 #include <dai/enum.h>
22 #include <algorithm>
23 #include <map>
24 #include <set>
25 
26 
27 namespace dai {
28 
29 
31 
34 class CobwebGraph : public FactorGraph {
35  public:
37  struct Connection {
39  size_t my;
41  size_t his;
43  size_t iter;
45  size_t dual;
49  std::vector<size_t> varinds;
51 
55  std::vector<size_t> fc;
57  std::vector<VarSet> subregions;
58  };
59 
67  DAI_ENUM(NeighborType,ALL,TOP,CLOSEST);
68 
69  protected:
71  std::vector<SmallSet<size_t> > _INRs;
73  std::vector<SmallSet<size_t> > _EXRs;
75  std::vector<SmallSet<size_t> > _Rfs;
77  std::vector<SmallSet<size_t> > _Rifs;
79  std::vector<SmallSet<size_t> > _Rxfs;
81  std::vector<std::vector<VarSet> > _outM;
83  std::vector<std::vector<Connection> > _M;
84 
86  std::vector<std::vector<size_t> > _var2CW;
87 
92  std::vector<std::map<VarSet, std::pair<int,std::vector<size_t> > > > _cn;
93 
96 
97  public:
99 
100  CobwebGraph() : FactorGraph(), _INRs(), _EXRs(),_Rfs(),_Rifs(),_Rxfs(), _M(), _var2CW(), _cn(), isPartition(true) {}
102 
104  CobwebGraph( const FactorGraph& fg ): FactorGraph(), _INRs(), _EXRs(),_Rfs(), _M(), _var2CW(), _cn(), isPartition(true) {
105  // Copy factor graph structure
106  FactorGraph::operator=( fg );
107  }
108 
110  virtual CobwebGraph* clone() const { return new CobwebGraph(*this); }
112 
114 
115  size_t nrCWs() const { return _INRs.size(); }
117 
119  const std::map<VarSet, std::pair<int,std::vector<size_t> > >& cn( size_t R ) const {
120  DAI_DEBASSERT( R < nrCWs() );
121  return _cn[R];
122  }
123 
125  std::map<VarSet, std::pair<int,std::vector<size_t> > >& cn( size_t R ) {
126  DAI_DEBASSERT( R < nrCWs() );
127  return _cn[R];
128  }
129 
131  const std::vector<VarSet>& outM( size_t R ) const {
132  DAI_DEBASSERT( R < _outM.size() );
133  return _outM[R];
134  }
135 
137  std::vector<VarSet>& outM( size_t R ) {
138  DAI_DEBASSERT( R < _outM.size() );
139  return _outM[R];
140  }
141 
143 
145  const VarSet& outM( size_t R, size_t j ) const {
146  DAI_DEBASSERT( R < _outM.size() );
147  DAI_DEBASSERT( j < _outM[R].size() );
148  return _outM[R][j];
149  }
150 
152 
154  VarSet& outM( size_t R, size_t j ) {
155  DAI_DEBASSERT( R < _outM.size() );
156  DAI_DEBASSERT( j < _outM[R].size() );
157  return _outM[R][j];
158  }
159 
161  const SmallSet<size_t>& Rfs( size_t R ) const {
162  DAI_DEBASSERT( R < _Rfs.size() );
163  return _Rfs[R];
164  }
165 
167  SmallSet<size_t>& Rfs( size_t R ) {
168  DAI_DEBASSERT( R < _Rfs.size() );
169  return _Rfs[R];
170  }
171 
173  const SmallSet<size_t>& EXRs( size_t R ) const {
174  DAI_DEBASSERT( R < _EXRs.size() );
175  return _EXRs[R];
176  }
177 
179  SmallSet<size_t>& EXRs( size_t R ) {
180  DAI_DEBASSERT( R < _EXRs.size() );
181  return _EXRs[R];
182  }
183 
185  const SmallSet<size_t>& INRs( size_t R ) const {
186  DAI_DEBASSERT( R < _INRs.size() );
187  return _INRs[R];
188  }
189 
191  SmallSet<size_t>& INRs( size_t R ) {
192  DAI_DEBASSERT( R < _INRs.size() );
193  return _INRs[R];
194  }
195 
197  const Connection& M( size_t R, size_t i ) const {
198  DAI_DEBASSERT(R < _M.size());
199  DAI_DEBASSERT(i < _M[R].size());
200  return _M[R][i]; }
201 
203  Connection& M( size_t R, size_t i ) {
204  DAI_DEBASSERT(R < _M.size());
205  DAI_DEBASSERT(i < _M[R].size());
206  return _M[R][i]; }
207 
209  const std::vector<Connection>& M( size_t R ) const {
210  DAI_DEBASSERT(R < _M.size());
211  return _M[R];
212  }
213 
215  std::vector<Connection>& M( size_t R ) { return _M[R]; }
216 
218  const std::vector<size_t>& var2CW( size_t i ) const { return _var2CW[i]; }
220 
222 
223  void setRgn( std::vector<SmallSet<size_t> > regions, NeighborType neighbors, bool debugging = false );
226 
228 
229 
232  virtual void ReadFromFile( const char* /*filename*/ ) {
233  DAI_THROW(NOT_IMPLEMENTED);
234  }
235 
237 
239  virtual void WriteToFile( const char* /*filename*/, size_t /*precision*/=15 ) const {
240  DAI_THROW(NOT_IMPLEMENTED);
241  }
242 
244  friend std::ostream& operator<< ( std::ostream& /*os*/, const CobwebGraph& /*rg*/ ) {
245  DAI_THROW(NOT_IMPLEMENTED);
246  }
247 
249  std::string toString() const {
250  std::stringstream ss;
251  ss << *this;
252  return ss.str();
253  }
254 
256 
258  virtual void printDot( std::ostream& /*os*/ ) const {
259  DAI_THROW(NOT_IMPLEMENTED);
260  }
262 
263 
264  protected:
266  bool checkPartition( const std::vector<SmallSet<size_t> >& regions ) const;
267 
269  void setVar2CW();
270 
272  void setExtnFact();
273 
275  void setMSGs( NeighborType neighbors );
276 
278  void setCountingNumbers( bool debugging = false );
279 
281  void eraseNonMaximal( std::vector<SmallSet<size_t> >& regions );
282 };
283 
284 
285 } // end of namespace dai
286 
287 
288 #endif
size_t dual
Index of the mirror of this connection in the connections of the second region. (reference of this me...
Definition: cobwebgraph.h:45
const std::vector< Connection > & M(size_t R) const
Returns constant reference to the vector of connection structure from region R to all its neighbours...
Definition: cobwebgraph.h:209
const VarSet & outM(size_t R, size_t j) const
Returns constant reference to the variables in the outgoing message from region R to its j'th neighbo...
Definition: cobwebgraph.h:145
const Connection & M(size_t R, size_t i) const
Returns constant reference to the connection structure from region R to its i'th neighbour.
Definition: cobwebgraph.h:197
std::vector< SmallSet< size_t > > _Rifs
Index of factors internal to each region, i.e., all its variables are internal to the region...
Definition: cobwebgraph.h:77
void setCountingNumbers(bool debugging=false)
Sets _cn.
Definition: cobwebgraph.cpp:65
void eraseNonMaximal(std::vector< SmallSet< size_t > > &regions)
For the given set of variables for each region, removes the regions that are non-maximal.
Definition: cobwebgraph.cpp:48
const std::vector< VarSet > & outM(size_t R) const
Returns constant reference the vector of domain of all outgoing messages from region R...
Definition: cobwebgraph.h:131
const SmallSet< size_t > & Rfs(size_t R) const
Returns constant reference to the index of factors of region R.
Definition: cobwebgraph.h:161
void setExtnFact()
Setup the _INRs, _EXRs and all the factor indices (e.g. Rifs)
Definition: cobwebgraph.cpp:239
const std::map< VarSet, std::pair< int, std::vector< size_t > > > & cn(size_t R) const
Returns constant reference to the _cn for region R.
Definition: cobwebgraph.h:119
Defines some utility functions for (weighted) undirected graphs, trees and rooted trees...
Represents a factor graph.
Definition: factorgraph.h:65
std::vector< SmallSet< size_t > > _EXRs
Vector of variable indices on the boundary of each region ( r)
Definition: cobwebgraph.h:73
std::vector< SmallSet< size_t > > _INRs
Vector of variable indices internal to each region (r)
Definition: cobwebgraph.h:71
std::vector< std::map< VarSet, std::pair< int, std::vector< size_t > > > > _cn
Definition: cobwebgraph.h:92
virtual CobwebGraph * clone() const
Clone *this (virtual copy constructor)
Definition: cobwebgraph.h:110
size_t iter
Index of this connection in the connections of the first region.
Definition: cobwebgraph.h:43
std::vector< VarSet > & outM(size_t R)
Returns reference the vector of domain of all outgoing messages from region R.
Definition: cobwebgraph.h:137
std::vector< std::vector< size_t > > _var2CW
For each i the index of (cobweb) regions that contain variable i.
Definition: cobwebgraph.h:86
Defines the DAI_ENUM macro, which can be used to define an enum with additional functionality.
std::vector< std::vector< VarSet > > _outM
The vector of domain of messages leaving each region ( r_{p,q})
Definition: cobwebgraph.h:81
DAI_ENUM(NeighborType, ALL, TOP, CLOSEST)
Represents a set of variables.
Definition: varset.h:94
void setMSGs(NeighborType neighbors)
Helper function that setups the msgs (_M, _outM) using the values in _INRs and _EXRs.
Definition: cobwebgraph.cpp:177
std::vector< SmallSet< size_t > > _Rfs
Index of factors in each region.
Definition: cobwebgraph.h:75
SmallSet< size_t > & EXRs(size_t R)
Returns reference to the index of variables on the boundary of region R ( r)
Definition: cobwebgraph.h:179
std::vector< SmallSet< size_t > > _Rxfs
Index of factors that bridge each region, i.e., not all its variables are internal to the region...
Definition: cobwebgraph.h:79
size_t my
Index of the first region (p)
Definition: cobwebgraph.h:39
bool checkPartition(const std::vector< SmallSet< size_t > > &regions) const
The function to check for partitioning.
Definition: cobwebgraph.cpp:154
size_t his
Index of the second region (q)
Definition: cobwebgraph.h:41
std::vector< std::vector< Connection > > _M
Vector of all connections to each region.
Definition: cobwebgraph.h:83
Factor msg
The message sent from region q (his) to p (my)
Definition: cobwebgraph.h:47
friend std::ostream & operator<<(std::ostream &, const CobwebGraph &)
Writes a cobweb graph to an output stream.
Definition: cobwebgraph.h:244
void setVar2CW()
Helper function that sets the regions containing each variable using the values in _INRs...
Definition: cobwebgraph.cpp:166
std::vector< Connection > & M(size_t R)
Returns vector of all connections to region R.
Definition: cobwebgraph.h:215
Factor newmsg
Used as a temporary factor only.
Definition: cobwebgraph.h:53
Defines the SmallSet<> class, which represents a set (optimized for a small number of elements)...
A CobwebGraph is a special type of region graph used by the GLC algorithm.
Definition: cobwebgraph.h:34
SmallSet< size_t > & INRs(size_t R)
Returns reference to the index of variables inside region R (r)
Definition: cobwebgraph.h:191
VarSet & outM(size_t R, size_t j)
Returns a reference to the variables in the outgoing message from region R to its j'th neighbor...
Definition: cobwebgraph.h:154
virtual void WriteToFile(const char *, size_t=15) const
Writes a cobweb graph to a file.
Definition: cobwebgraph.h:239
bool isPartition
Whether a given set of region vars makes a partitioning or not.
Definition: cobwebgraph.h:95
std::vector< VarSet > subregions
Regions rho that are descendents of msg from q to p in p's msg-region graph (not used in partitioning...
Definition: cobwebgraph.h:57
virtual void ReadFromFile(const char *)
Reads a cobweb graph from a file.
Definition: cobwebgraph.h:232
void setRgn(std::vector< SmallSet< size_t > > regions, NeighborType neighbors, bool debugging=false)
Sets up all the regions and messages.
Definition: cobwebgraph.cpp:21
Namespace for libDAI.
Definition: alldai.cpp:16
const std::vector< size_t > & var2CW(size_t i) const
Returns the vector of region indices that contain i as internal variable.
Definition: cobwebgraph.h:218
Defines the FactorGraph class, which represents factor graphs (e.g., Bayesian networks or Markov rand...
std::vector< size_t > fc
Index of factors in common.
Definition: cobwebgraph.h:55
size_t nrCWs() const
Returns the number of regions.
Definition: cobwebgraph.h:116
std::vector< size_t > varinds
"Index" of variables in the message
Definition: cobwebgraph.h:49
CobwebGraph(const FactorGraph &fg)
Constructs a cobweb graph from a factor graph.
Definition: cobwebgraph.h:104
#define DAI_DEBASSERT(x)
Assertion mechanism similar to DAI_ASSERT which is only active if DAI_DEBUG is defined.
Definition: exceptions.h:65
std::string toString() const
Formats a cobweb graph as a string.
Definition: cobwebgraph.h:249
SmallSet< size_t > & Rfs(size_t R)
Returns reference to the index of factors of region R.
Definition: cobwebgraph.h:167
Connection & M(size_t R, size_t i)
Returns reference to the connection structure from region R to its i'th neighbour.
Definition: cobwebgraph.h:203
std::map< VarSet, std::pair< int, std::vector< size_t > > > & cn(size_t R)
Returns a reference to the _cn for region R.
Definition: cobwebgraph.h:125
const SmallSet< size_t > & INRs(size_t R) const
Returns constant reference to the index of variables inside region R (r)
Definition: cobwebgraph.h:185
const SmallSet< size_t > & EXRs(size_t R) const
Returns constant reference to the index of variables on the boundary of region R ( r) ...
Definition: cobwebgraph.h:173
The information in connection between two regions.
Definition: cobwebgraph.h:37
virtual void printDot(std::ostream &) const
Writes a cobweb graph to a GraphViz .dot file.
Definition: cobwebgraph.h:258
CobwebGraph()
Default constructor.
Definition: cobwebgraph.h:101