libDAI
|
Represents a factor graph. More...
#include <dai/factorgraph.h>
Public Member Functions | |
Constructors and destructors | |
FactorGraph () | |
Default constructor. More... | |
FactorGraph (const std::vector< Factor > &P) | |
Constructs a factor graph from a vector of factors. More... | |
template<typename FactorInputIterator , typename VarInputIterator > | |
FactorGraph (FactorInputIterator facBegin, FactorInputIterator facEnd, VarInputIterator varBegin, VarInputIterator varEnd, size_t nrFacHint=0, size_t nrVarHint=0) | |
Constructs a factor graph from given factor and variable iterators. More... | |
virtual | ~FactorGraph () |
Destructor. More... | |
virtual FactorGraph * | clone () const |
Virtual copy constructor. More... | |
Accessors and mutators | |
const Var & | var (size_t i) const |
Returns constant reference the i 'th variable. More... | |
const std::vector< Var > & | vars () const |
Returns constant reference to all variables. More... | |
const Factor & | factor (size_t I) const |
Returns constant reference to I 'th factor. More... | |
const std::vector< Factor > & | factors () const |
Returns constant reference to all factors. More... | |
const Neighbors & | nbV (size_t i) const |
Returns constant reference to neighbors of the i 'th variable. More... | |
const Neighbors & | nbF (size_t I) const |
Returns constant reference to neighbors of the I 'th factor. More... | |
const Neighbor & | nbV (size_t i, size_t _I) const |
Returns constant reference to the _I 'th neighbor of the i 'th variable. More... | |
const Neighbor & | nbF (size_t I, size_t _i) const |
Returns constant reference to the _i 'th neighbor of the I 'th factor. More... | |
Queries | |
const BipartiteGraph & | bipGraph () const |
Returns neighborhood structure. More... | |
size_t | nrVars () const |
Returns number of variables. More... | |
size_t | nrFactors () const |
Returns number of factors. More... | |
size_t | nrEdges () const |
Calculates number of edges. More... | |
size_t | findVar (const Var &n) const |
Returns the index of a particular variable. More... | |
SmallSet< size_t > | findVars (const VarSet &ns) const |
Returns a set of indexes corresponding to a set of variables. More... | |
size_t | findFactor (const VarSet &ns) const |
Returns index of the first factor that depends on the variables. More... | |
VarSet | inds2vars (const std::vector< size_t > &inds) const |
Returns the VarSet corresponding to a vector of variable indices. More... | |
VarSet | Delta (size_t i) const |
Return all variables that occur in a factor involving the i 'th variable, itself included. More... | |
VarSet | Delta (const VarSet &vs) const |
Return all variables that occur in a factor involving some variable in vs, vs itself included. More... | |
VarSet | delta (size_t i) const |
Return all variables that occur in a factor involving the i 'th variable, itself excluded. More... | |
VarSet | delta (const VarSet &vs) const |
Return all variables that occur in a factor involving some variable in vs, vs itself excluded. More... | |
SmallSet< size_t > | Deltai (size_t i) const |
Return all the indices of variables that occur in a factor involving the i 'th variable, itself included. More... | |
SmallSet< size_t > | deltai (size_t i) const |
Return all the indices of variables that occur in a factor involving the i 'th variable, itself excluded. More... | |
bool | isConnected () const |
Returns true if the factor graph is connected. More... | |
bool | isTree () const |
Returns true if the factor graph is a tree (i.e., has no cycles and is connected) More... | |
bool | isPairwise () const |
Returns true if each factor depends on at most two variables. More... | |
bool | isBinary () const |
Returns true if each variable has only two possible values. More... | |
GraphAL | MarkovGraph () const |
Constructs the corresponding Markov graph. More... | |
bool | isMaximal (size_t I) const |
Returns whether the I 'th factor is maximal. More... | |
size_t | maximalFactor (size_t I) const |
Returns the index of a maximal factor that contains the I 'th factor. More... | |
std::vector< VarSet > | maximalFactorDomains () const |
Returns the maximal factor domains in this factorgraph. More... | |
Real | logScore (const std::vector< size_t > &statevec) const |
Evaluates the log score (i.e., minus the energy) of the joint configuration statevec. More... | |
Backup/restore mechanism for factors | |
virtual void | setFactor (size_t I, const Factor &newFactor, bool backup=false) |
Set the content of the I 'th factor and make a backup of its old content if backup == true . More... | |
virtual void | setFactors (const std::map< size_t, Factor > &facs, bool backup=false) |
Set the contents of all factors as specified by facs and make a backup of the old contents if backup == true . More... | |
void | backupFactor (size_t I) |
Makes a backup of the I 'th factor. More... | |
void | restoreFactor (size_t I) |
Restores the I 'th factor from the backup (it should be backed up first) More... | |
virtual void | backupFactors (const std::set< size_t > &facs) |
Backup the factors specified by indices in facs. More... | |
virtual void | restoreFactors () |
Restore all factors to the backup copies. More... | |
void | backupFactors (const VarSet &ns) |
Makes a backup of all factors connected to a set of variables. More... | |
void | restoreFactors (const VarSet &ns) |
Restores all factors connected to a set of variables from their backups. More... | |
Transformations | |
FactorGraph | maximalFactors () const |
Returns a copy of *this , where all factors that are subsumed by some larger factor are merged with the larger factors. More... | |
FactorGraph | clamped (size_t i, size_t x) const |
Returns a copy of *this , where the i 'th variable has been clamped to value x. More... | |
Operations | |
virtual void | clamp (size_t i, size_t x, bool backup=false) |
Clamp the i 'th variable to value x (i.e. multiply with a Kronecker delta ) More... | |
void | clampVar (size_t i, const std::vector< size_t > &xis, bool backup=false) |
Clamp a variable in a factor graph to have one out of a list of values. More... | |
void | clampFactor (size_t I, const std::vector< size_t > &xIs, bool backup=false) |
Clamp a factor in a factor graph to have one out of a list of values. More... | |
virtual void | makeCavity (size_t i, bool backup=false) |
Set all factors interacting with the i 'th variable to 1. More... | |
virtual void | makeRegionCavity (std::vector< size_t > facInds, bool backup) |
Set all factors indexed by facInds to 1. More... | |
Private Member Functions | |
void | constructGraph (size_t nrEdges) |
Part of constructors (creates edges, neighbors and adjacency matrix) More... | |
Private Attributes | |
BipartiteGraph | _G |
Stores the neighborhood structure. More... | |
std::vector< Var > | _vars |
Stores the variables. More... | |
std::vector< Factor > | _factors |
Stores the factors. More... | |
std::map< size_t, Factor > | _backup |
Stores backups of some factors. More... | |
Input/Output | |
virtual void | ReadFromFile (const char *filename) |
Reads a factor graph from a file. More... | |
virtual void | WriteToFile (const char *filename, size_t precision=15) const |
Writes a factor graph to a file. More... | |
virtual void | printDot (std::ostream &os) const |
Writes a factor graph to a GraphViz .dot file. More... | |
std::string | toString () const |
Formats a factor graph as a string. More... | |
void | fromString (const std::string &s) |
Reads a factor graph from a string. More... | |
std::ostream & | operator<< (std::ostream &os, const FactorGraph &fg) |
Writes a factor graph to an output stream. More... | |
std::istream & | operator>> (std::istream &is, FactorGraph &fg) |
Reads a factor graph from an input stream. More... | |
Represents a factor graph.
Both Bayesian Networks and Markov random fields can be represented in a unifying representation, called factor graph [KFL01], implemented in libDAI by the FactorGraph class.
Consider a probability distribution over discrete random variables that factorizes as a product of factors, each of which depends on some subset of the variables:
Each factor is a function from an associated subset of variables to the nonnegative real numbers.
For a Bayesian network, each factor corresponds to a (conditional) probability table, whereas for a Markov random field, each factor corresponds to a maximal clique of the undirected graph.
Factor graphs explicitly express the factorization structure of the corresponding probability distribution. A factor graph is a bipartite graph, containing variable nodes and factor nodes, and an edge between a variable node and a factor node if the corresponding factor depends on that variable. In libDAI, this structure is represented by a BipartiteGraph.
So basically, a FactorGraph consists of a BipartiteGraph, a vector of Var 's and a vector of TFactor 's.
|
inline |
Default constructor.
dai::FactorGraph::FactorGraph | ( | const std::vector< Factor > & | P | ) |
Constructs a factor graph from a vector of factors.
dai::FactorGraph::FactorGraph | ( | FactorInputIterator | facBegin, |
FactorInputIterator | facEnd, | ||
VarInputIterator | varBegin, | ||
VarInputIterator | varEnd, | ||
size_t | nrFacHint = 0 , |
||
size_t | nrVarHint = 0 |
||
) |
Constructs a factor graph from given factor and variable iterators.
FactorInputIterator | Iterates over instances of type dai::Factor |
VarInputIterator | Iterates over instances of type Var |
|
inlinevirtual |
Destructor.
|
inlinevirtual |
Virtual copy constructor.
Reimplemented in dai::RegionGraph, and dai::CobwebGraph.
|
inline |
Returns constant reference the i 'th variable.
|
inline |
Returns constant reference to all variables.
|
inline |
Returns constant reference to I 'th factor.
|
inline |
Returns constant reference to all factors.
|
inline |
Returns constant reference to neighbors of the i 'th variable.
|
inline |
Returns constant reference to neighbors of the I 'th factor.
|
inline |
Returns constant reference to the _I 'th neighbor of the i 'th variable.
|
inline |
Returns constant reference to the _i 'th neighbor of the I 'th factor.
|
inline |
Returns neighborhood structure.
|
inline |
Returns number of variables.
|
inline |
Returns number of factors.
|
inline |
Calculates number of edges.
|
inline |
Returns the index of a particular variable.
OBJECT_NOT_FOUND | if the variable is not part of this factor graph |
Returns a set of indexes corresponding to a set of variables.
OBJECT_NOT_FOUND | if one of the variables is not part of this factor graph |
|
inline |
Returns index of the first factor that depends on the variables.
OBJECT_NOT_FOUND | if no factor in this factor graph depends on those variables |
|
inline |
Returns the VarSet corresponding to a vector of variable indices.
VarSet dai::FactorGraph::Delta | ( | size_t | i | ) | const |
Return all variables that occur in a factor involving the i 'th variable, itself included.
Return all variables that occur in a factor involving some variable in vs, vs itself included.
|
inline |
Return all variables that occur in a factor involving the i 'th variable, itself excluded.
Return all variables that occur in a factor involving some variable in vs, vs itself excluded.
SmallSet< size_t > dai::FactorGraph::Deltai | ( | size_t | i | ) | const |
Return all the indices of variables that occur in a factor involving the i 'th variable, itself included.
|
inline |
Return all the indices of variables that occur in a factor involving the i 'th variable, itself excluded.
|
inline |
Returns true
if the factor graph is connected.
|
inline |
Returns true
if the factor graph is a tree (i.e., has no cycles and is connected)
bool dai::FactorGraph::isPairwise | ( | ) | const |
Returns true
if each factor depends on at most two variables.
bool dai::FactorGraph::isBinary | ( | ) | const |
Returns true
if each variable has only two possible values.
GraphAL dai::FactorGraph::MarkovGraph | ( | ) | const |
Constructs the corresponding Markov graph.
bool dai::FactorGraph::isMaximal | ( | size_t | I | ) | const |
Returns whether the I 'th factor is maximal.
size_t dai::FactorGraph::maximalFactor | ( | size_t | I | ) | const |
Returns the index of a maximal factor that contains the I 'th factor.
vector< VarSet > dai::FactorGraph::maximalFactorDomains | ( | ) | const |
Returns the maximal factor domains in this factorgraph.
Real dai::FactorGraph::logScore | ( | const std::vector< size_t > & | statevec | ) | const |
Evaluates the log score (i.e., minus the energy) of the joint configuration statevec.
|
inlinevirtual |
Set the content of the I 'th factor and make a backup of its old content if backup == true
.
Reimplemented in dai::RegionGraph.
|
inlinevirtual |
Set the contents of all factors as specified by facs and make a backup of the old contents if backup == true
.
Reimplemented in dai::RegionGraph.
void dai::FactorGraph::backupFactor | ( | size_t | I | ) |
Makes a backup of the I 'th factor.
MULTIPLE_UNDO | if a backup already exists |
void dai::FactorGraph::restoreFactor | ( | size_t | I | ) |
Restores the I 'th factor from the backup (it should be backed up first)
OBJECT_NOT_FOUND | if a backup does not exist |
|
virtual |
Backup the factors specified by indices in facs.
MULTIPLE_UNDO | if a backup already exists |
|
virtual |
Restore all factors to the backup copies.
void dai::FactorGraph::backupFactors | ( | const VarSet & | ns | ) |
Makes a backup of all factors connected to a set of variables.
MULTIPLE_UNDO | if a backup already exists |
void dai::FactorGraph::restoreFactors | ( | const VarSet & | ns | ) |
Restores all factors connected to a set of variables from their backups.
FactorGraph dai::FactorGraph::maximalFactors | ( | ) | const |
Returns a copy of *this
, where all factors that are subsumed by some larger factor are merged with the larger factors.
FactorGraph dai::FactorGraph::clamped | ( | size_t | i, |
size_t | x | ||
) | const |
Returns a copy of *this
, where the i 'th variable has been clamped to value x.
|
virtual |
Clamp the i 'th variable to value x (i.e. multiply with a Kronecker delta )
If backup == true
, make a backup of all factors that are changed
void dai::FactorGraph::clampVar | ( | size_t | i, |
const std::vector< size_t > & | xis, | ||
bool | backup = false |
||
) |
Clamp a variable in a factor graph to have one out of a list of values.
If backup == true
, make a backup of all factors that are changed
void dai::FactorGraph::clampFactor | ( | size_t | I, |
const std::vector< size_t > & | xIs, | ||
bool | backup = false |
||
) |
Clamp a factor in a factor graph to have one out of a list of values.
If backup == true
, make a backup of all factors that are changed
|
virtual |
Set all factors interacting with the i 'th variable to 1.
If backup == true
, make a backup of all factors that are changed
|
virtual |
Set all factors indexed by facInds to 1.
If backup == true
, make a backup of all factors that are changed
|
virtual |
Reads a factor graph from a file.
CANNOT_READ_FILE | if the file cannot be opened |
INVALID_FACTORGRAPH_FILE | if the file is not valid |
Reimplemented in dai::RegionGraph, and dai::CobwebGraph.
|
virtual |
Writes a factor graph to a file.
CANNOT_WRITE_FILE | if the file cannot be written |
Reimplemented in dai::RegionGraph, and dai::CobwebGraph.
|
virtual |
Writes a factor graph to a GraphViz .dot file.
Reimplemented in dai::CobwebGraph, and dai::RegionGraph.
|
inline |
Formats a factor graph as a string.
|
inline |
Reads a factor graph from a string.
|
private |
Part of constructors (creates edges, neighbors and adjacency matrix)
|
friend |
Writes a factor graph to an output stream.
|
friend |
Reads a factor graph from an input stream.
INVALID_FACTORGRAPH_FILE | if the input stream is not valid |
|
private |
Stores the neighborhood structure.
|
private |
Stores the variables.
|
private |
Stores the factors.
|
private |
Stores backups of some factors.