Darwin  1.10(beta)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
drwnFactorGraph.h
1 /*****************************************************************************
2 ** DARWIN: A FRAMEWORK FOR MACHINE LEARNING RESEARCH AND DEVELOPMENT
3 ** Distributed under the terms of the BSD license (see the LICENSE file)
4 ** Copyright (c) 2007-2015, Stephen Gould
5 ** All rights reserved.
6 **
7 ******************************************************************************
8 ** FILENAME: drwnFactorGraph.h
9 ** AUTHOR(S): Stephen Gould <stephen.gould@anu.edu.au>
10 **
11 *****************************************************************************/
12 
13 #pragma once
14 
15 #include <cstdlib>
16 #include <cassert>
17 #include <iostream>
18 #include <fstream>
19 #include <iomanip>
20 #include <vector>
21 #include <map>
22 #include <list>
23 
24 #include "Eigen/Core"
25 
26 #include "drwnBase.h"
27 #include "drwnIO.h"
28 #include "drwnGraphUtils.h"
29 #include "drwnTableFactor.h"
30 
31 using namespace std;
32 using namespace Eigen;
33 
34 // drwnFactorGraph ---------------------------------------------------------
39 
41  protected:
43  vector<drwnTableFactor *> _factors;
44 
46  vector<_drwnFactorEdge> _edges;
47 
48  public:
55  virtual ~drwnFactorGraph();
56 
57  // access functions
58  virtual const char *type() const { return "drwnFactorGraph"; }
59  virtual drwnFactorGraph *clone() const { return new drwnFactorGraph(*this); }
60 
62  const drwnVarUniversePtr& getUniverse() const { return _pUniverse; }
64  int numVariables() const { return (_pUniverse == NULL) ? 0 : _pUniverse->numVariables(); }
66  int numFactors() const { return (int)_factors.size(); }
68  int numEdges() const { return (int)_edges.size(); }
69 
71  void addFactor(drwnTableFactor *psi);
73  void copyFactor(const drwnTableFactor *psi);
75  const drwnTableFactor* getFactor(int indx) const { return _factors[indx]; }
77  void deleteFactor(int indx);
79  int findFactor(const drwnClique& clique, bool bAllowSuperset = false) const;
81  drwnClique getClique(int indx) const;
83  drwnEdge getEdge(int eindx) const { return drwnEdge(_edges[eindx].first, _edges[eindx].second); }
85  drwnClique getSepSet(int eindx) const { return _edges[eindx].third; }
87  static drwnClique getSepSet(const drwnFactor& psiA, const drwnFactor& psiB);
88 
90  double getEnergy(const drwnFullAssignment& x) const;
91 
92  // graph connectivity
94  bool addEdge(const drwnEdge& e);
96  bool connectGraph(const set<drwnEdge>& edges);
98  virtual bool connectGraph();
100  bool connectBetheApprox();
101 
102  // check running intersection property
103  /*
104  bool checkRunIntProp();
105  */
106 
107  // i/o
108  bool save(drwnXMLNode& xml) const;
109  bool load(drwnXMLNode& xml);
110 
111  // operators
112  drwnTableFactor* operator[](unsigned indx) { return _factors[indx]; }
113  const drwnTableFactor* operator[](unsigned indx) const { return _factors[indx]; }
114 
115  //bool operator==(const drwnFactorGraph& g) const;
116 
117 
118  protected:
120  void computeSeparatorSets();
121 };
122 
123 // drwnFactorGraph utilities -----------------------------------------------
125 
126 namespace drwnFactorGraphUtils {
128  void writeDottyOutput(const char *filename, const drwnFactorGraph &graph);
129 
132  vector<set<int> > variableAdjacencyList(const drwnFactorGraph& graph);
133 
136 
138  double removeUniformFactors(drwnFactorGraph& graph);
139 
143  void absorbSmallFactors(drwnFactorGraph& graph, bool bIncludeUnary = true);
144 
147 };
const drwnVarUniversePtr & getUniverse() const
return the universe of variables for this factor graph
Definition: drwnFactorGraph.h:62
vector< drwnTableFactor * > _factors
list of factors in the model
Definition: drwnFactorGraph.h:43
drwnVarUniversePtr _pUniverse
all variables in the universe
Definition: drwnFactorGraph.h:42
virtual drwnFactorGraph * clone() const
returns a copy of the class usually implemented as virtual Foo* clone() { return new Foo(*this); } ...
Definition: drwnFactorGraph.h:59
void mergeDuplicateFactors(drwnFactorGraph &graph)
Merges (log-space) factors over identical cliques. Operates inline.
Definition: drwnFactorGraph.cpp:591
vector< set< int > > variableAdjacencyList(const drwnFactorGraph &graph)
Returns the neighbours (i.e., other variables appearing in the same clique) of each variable...
Definition: drwnFactorGraph.cpp:428
drwnClique getSepSet(int eindx) const
returns the set of separator variables for edge eindx
Definition: drwnFactorGraph.h:85
drwnFactorGraph createJunctionTree(const drwnFactorGraph &graph)
Create a junction tree from a factor graph.
Definition: drwnFactorGraph.cpp:445
void writeDottyOutput(const char *filename, const drwnFactorGraph &graph)
Generate output for dotty (Graphviz)
Definition: drwnFactorGraph.cpp:407
int numVariables() const
returns the number of variables in the universe
Definition: drwnFactorGraph.h:64
Container and utility functions for factor graphs.
Definition: drwnFactorGraph.h:40
const drwnTableFactor * getFactor(int indx) const
returns the factor at index indx
Definition: drwnFactorGraph.h:75
virtual const char * type() const
returns object type as a string (e.g., Foo::type() { return "Foo"; })
Definition: drwnFactorGraph.h:58
Factor which stores the value of each assignment explicitly in table form.
Definition: drwnTableFactor.h:144
std::set< int > drwnClique
variable clique
Definition: drwnGraphUtils.h:37
std::pair< int, int > drwnEdge
directed or undirected edges
Definition: drwnGraphUtils.h:33
std::vector< int > drwnFullAssignment
defines a complete assignment to all variables in the universe
Definition: drwnVarAssignment.h:36
int numFactors() const
returns the number of factors in the graph
Definition: drwnFactorGraph.h:66
void absorbSmallFactors(drwnFactorGraph &graph, bool bIncludeUnary=true)
Absorbs smaller (log-space) factors into larger ones (operates inline). The returned graph is disconn...
Definition: drwnFactorGraph.cpp:551
Generic interface for a factor. Currently only inherited by drwnTableFactor.
Definition: drwnTableFactor.h:40
Basic datatype for holding three objects of arbitrary type. Similar to the STL pair<> class...
Definition: drwnTriplet.h:33
vector< _drwnFactorEdge > _edges
list of edges and sep-sets
Definition: drwnFactorGraph.h:46
drwnEdge getEdge(int eindx) const
returns the indices for the factors adjacent to edge eindx
Definition: drwnFactorGraph.h:83
standard Darwin object interface (cloneable and writeable)
Definition: drwnInterfaces.h:72
Generic graph utilities.
double removeUniformFactors(drwnFactorGraph &graph)
Removes uniform factors from the graph (returns energy of factors removed)
Definition: drwnFactorGraph.cpp:512
int numEdges() const
returns the number of (hyper-)edges in the graph
Definition: drwnFactorGraph.h:68