A SymbolicFactorGraph
is a factor graph consisting purely of SymbolicFactor
objects. It represents the structure or topology of a probabilistic graphical model (like a Markov Random Field) without the numerical details.
It’s primarily used to illustrate symbolic elimination, which determines the structure of the resulting Bayes net or Bayes tree and finds an efficient variable elimination ordering (e.g., using COLAMD or METIS).
from gtsam import SymbolicFactorGraph, Ordering
from gtsam.symbol_shorthand import X, L
import graphviz
Creating and Manipulating Symbolic Factor Graphs¶
# Create an empty graph
graph = SymbolicFactorGraph()
# Add factors (using convenience methods)
graph.push_factor(X(0)) # Unary
graph.push_factor(X(0), X(1))
graph.push_factor(X(1), X(2))
graph.push_factor(X(0), L(1))
graph.push_factor(X(1), L(2))
# Print the graph structure
graph.print("Symbolic Factor Graph:\n")
# Visualize the graph using Graphviz
graphviz.Source(graph.dot())
Loading...
Symbolic Elimination¶
We can perform symbolic elimination to get the structure of the resulting Bayes net or Bayes tree.
# Define an elimination ordering (can also be computed automatically)
ordering = Ordering([L(1), L(2), X(0), X(1), X(2)])
# Eliminate sequentially to get a Bayes Net structure
bayes_net = graph.eliminateSequential(ordering)
bayes_net.print("\nResulting Symbolic Bayes Net:\n")
# Visualize the Bayes Net using Graphviz
graphviz.Source(bayes_net.dot())
Loading...
# Eliminate using Multifrontal method to get a Bayes Tree structure
bayes_tree = graph.eliminateMultifrontal(ordering)
bayes_tree.print("\nResulting Symbolic Bayes Tree:\n")
# Visualize the Bayes Tree using Graphviz
graphviz.Source(bayes_tree.dot())
Loading...