Skip to content

Commit 3a1188a

Browse files
authored
Merge pull request #2105 from borglab/feature/BayesTree
Docs for symbolic folder
2 parents a61ce2a + 8df8741 commit 3a1188a

12 files changed

+1904
-1
lines changed

gtsam/linear/linear.i

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,21 @@ virtual class GaussianBayesNet {
657657
};
658658

659659
#include <gtsam/linear/GaussianBayesTree.h>
660+
class GaussianBayesTreeClique {
661+
GaussianBayesTreeClique();
662+
GaussianBayesTreeClique(const gtsam::GaussianConditional* conditional);
663+
bool equals(const gtsam::GaussianBayesTreeClique& other, double tol) const;
664+
void print(string s = "", const gtsam::KeyFormatter& keyFormatter =
665+
gtsam::DefaultKeyFormatter);
666+
const gtsam::GaussianConditional* conditional() const;
667+
bool isRoot() const;
668+
gtsam::GaussianBayesTreeClique* parent() const;
669+
size_t nrChildren() const;
670+
gtsam::GaussianBayesTreeClique* operator[](size_t j) const;
671+
size_t treeSize() const;
672+
size_t numCachedSeparatorMarginals() const;
673+
void deleteCachedShortcuts();
674+
};
660675
virtual class GaussianBayesTree {
661676
// Standard Constructors and Named Constructors
662677
GaussianBayesTree();
@@ -666,6 +681,8 @@ virtual class GaussianBayesTree {
666681
gtsam::DefaultKeyFormatter);
667682
size_t size() const;
668683
bool empty() const;
684+
const GaussianBayesTree::Roots& roots() const;
685+
const gtsam::GaussianBayesTreeClique* operator[](size_t j) const;
669686
size_t numCachedSeparatorMarginals() const;
670687

671688
string dot(const gtsam::KeyFormatter& keyFormatter =
Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# SymbolicBayesNet"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"A `SymbolicBayesNet` is a directed acyclic graph (DAG) composed of `SymbolicConditional` objects. It represents the structure of a factorized probability distribution P(X) = Π P(Xi | Parents(Xi)) purely in terms of variable connectivity.\n",
15+
"\n",
16+
"It is typically the result of running sequential variable elimination on a `SymbolicFactorGraph`."
17+
]
18+
},
19+
{
20+
"cell_type": "markdown",
21+
"metadata": {},
22+
"source": [
23+
"<a href=\"https://colab.research.google.com/github/borglab/gtsam/blob/develop/gtsam/symbolic/doc/SymbolicBayesNet.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": 13,
29+
"metadata": {
30+
"tags": [
31+
"remove-cell"
32+
]
33+
},
34+
"outputs": [
35+
{
36+
"name": "stdout",
37+
"output_type": "stream",
38+
"text": [
39+
"Note: you may need to restart the kernel to use updated packages.\n"
40+
]
41+
}
42+
],
43+
"source": [
44+
"%pip install --quiet gtsam-develop"
45+
]
46+
},
47+
{
48+
"cell_type": "code",
49+
"execution_count": 14,
50+
"metadata": {},
51+
"outputs": [],
52+
"source": [
53+
"from gtsam import SymbolicConditional, SymbolicFactorGraph, Ordering\n",
54+
"from gtsam.symbol_shorthand import X, L\n",
55+
"import graphviz"
56+
]
57+
},
58+
{
59+
"cell_type": "markdown",
60+
"metadata": {},
61+
"source": [
62+
"## Creating a SymbolicBayesNet\n",
63+
"\n",
64+
"SymbolicBayesNets are usually created by eliminating a [SymbolicFactorGraph](SymbolicFactorGraph.ipynb). But you can also build them directly:"
65+
]
66+
},
67+
{
68+
"cell_type": "code",
69+
"execution_count": 15,
70+
"metadata": {},
71+
"outputs": [
72+
{
73+
"name": "stdout",
74+
"output_type": "stream",
75+
"text": [
76+
"Directly Built Symbolic Bayes Net:\n",
77+
" \n",
78+
"size: 5\n",
79+
"conditional 0: P( l1 | x0)\n",
80+
"conditional 1: P( x0 | x1)\n",
81+
"conditional 2: P( l2 | x1)\n",
82+
"conditional 3: P( x1 | x2)\n",
83+
"conditional 4: P( x2)\n"
84+
]
85+
}
86+
],
87+
"source": [
88+
"from gtsam import SymbolicBayesNet\n",
89+
"\n",
90+
"# Create a new Bayes Net\n",
91+
"symbolic_bayes_net = SymbolicBayesNet()\n",
92+
"\n",
93+
"# Add conditionals directly\n",
94+
"symbolic_bayes_net.push_back(SymbolicConditional(L(1), X(0))) # P(l1 | x0)\n",
95+
"symbolic_bayes_net.push_back(SymbolicConditional(X(0), X(1))) # P(x0 | x1)\n",
96+
"symbolic_bayes_net.push_back(SymbolicConditional(L(2), X(1))) # P(l2 | x1)\n",
97+
"symbolic_bayes_net.push_back(SymbolicConditional(X(1), X(2))) # P(x1 | x2)\n",
98+
"symbolic_bayes_net.push_back(SymbolicConditional(X(2))) # P(x2)\n",
99+
"\n",
100+
"symbolic_bayes_net.print(\"Directly Built Symbolic Bayes Net:\\n\")"
101+
]
102+
},
103+
{
104+
"cell_type": "markdown",
105+
"metadata": {},
106+
"source": [
107+
"## Accessing Conditionals and Visualization"
108+
]
109+
},
110+
{
111+
"cell_type": "code",
112+
"execution_count": 16,
113+
"metadata": {},
114+
"outputs": [
115+
{
116+
"name": "stdout",
117+
"output_type": "stream",
118+
"text": [
119+
"Conditional at index 1: P( x0 | x1)\n"
120+
]
121+
},
122+
{
123+
"data": {
124+
"image/svg+xml": [
125+
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
126+
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
127+
" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
128+
"<!-- Generated by graphviz version 12.0.0 (0)\n",
129+
" -->\n",
130+
"<!-- Pages: 1 -->\n",
131+
"<svg width=\"134pt\" height=\"260pt\"\n",
132+
" viewBox=\"0.00 0.00 134.00 260.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
133+
"<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 256)\">\n",
134+
"<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-256 130,-256 130,4 -4,4\"/>\n",
135+
"<!-- var7782220156096217089 -->\n",
136+
"<g id=\"node1\" class=\"node\">\n",
137+
"<title>var7782220156096217089</title>\n",
138+
"<ellipse fill=\"none\" stroke=\"black\" cx=\"99\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
139+
"<text text-anchor=\"middle\" x=\"99\" y=\"-12.95\" font-family=\"Times,serif\" font-size=\"14.00\">l1</text>\n",
140+
"</g>\n",
141+
"<!-- var7782220156096217090 -->\n",
142+
"<g id=\"node2\" class=\"node\">\n",
143+
"<title>var7782220156096217090</title>\n",
144+
"<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n",
145+
"<text text-anchor=\"middle\" x=\"27\" y=\"-84.95\" font-family=\"Times,serif\" font-size=\"14.00\">l2</text>\n",
146+
"</g>\n",
147+
"<!-- var8646911284551352320 -->\n",
148+
"<g id=\"node3\" class=\"node\">\n",
149+
"<title>var8646911284551352320</title>\n",
150+
"<ellipse fill=\"none\" stroke=\"black\" cx=\"99\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n",
151+
"<text text-anchor=\"middle\" x=\"99\" y=\"-84.95\" font-family=\"Times,serif\" font-size=\"14.00\">x0</text>\n",
152+
"</g>\n",
153+
"<!-- var8646911284551352320&#45;&gt;var7782220156096217089 -->\n",
154+
"<g id=\"edge4\" class=\"edge\">\n",
155+
"<title>var8646911284551352320&#45;&gt;var7782220156096217089</title>\n",
156+
"<path fill=\"none\" stroke=\"black\" d=\"M99,-71.7C99,-64.41 99,-55.73 99,-47.54\"/>\n",
157+
"<polygon fill=\"black\" stroke=\"black\" points=\"102.5,-47.62 99,-37.62 95.5,-47.62 102.5,-47.62\"/>\n",
158+
"</g>\n",
159+
"<!-- var8646911284551352321 -->\n",
160+
"<g id=\"node4\" class=\"node\">\n",
161+
"<title>var8646911284551352321</title>\n",
162+
"<ellipse fill=\"none\" stroke=\"black\" cx=\"63\" cy=\"-162\" rx=\"27\" ry=\"18\"/>\n",
163+
"<text text-anchor=\"middle\" x=\"63\" y=\"-156.95\" font-family=\"Times,serif\" font-size=\"14.00\">x1</text>\n",
164+
"</g>\n",
165+
"<!-- var8646911284551352321&#45;&gt;var7782220156096217090 -->\n",
166+
"<g id=\"edge2\" class=\"edge\">\n",
167+
"<title>var8646911284551352321&#45;&gt;var7782220156096217090</title>\n",
168+
"<path fill=\"none\" stroke=\"black\" d=\"M54.65,-144.76C50.42,-136.55 45.19,-126.37 40.42,-117.09\"/>\n",
169+
"<polygon fill=\"black\" stroke=\"black\" points=\"43.68,-115.79 36,-108.49 37.46,-118.99 43.68,-115.79\"/>\n",
170+
"</g>\n",
171+
"<!-- var8646911284551352321&#45;&gt;var8646911284551352320 -->\n",
172+
"<g id=\"edge3\" class=\"edge\">\n",
173+
"<title>var8646911284551352321&#45;&gt;var8646911284551352320</title>\n",
174+
"<path fill=\"none\" stroke=\"black\" d=\"M71.35,-144.76C75.58,-136.55 80.81,-126.37 85.58,-117.09\"/>\n",
175+
"<polygon fill=\"black\" stroke=\"black\" points=\"88.54,-118.99 90,-108.49 82.32,-115.79 88.54,-118.99\"/>\n",
176+
"</g>\n",
177+
"<!-- var8646911284551352322 -->\n",
178+
"<g id=\"node5\" class=\"node\">\n",
179+
"<title>var8646911284551352322</title>\n",
180+
"<ellipse fill=\"none\" stroke=\"black\" cx=\"63\" cy=\"-234\" rx=\"27\" ry=\"18\"/>\n",
181+
"<text text-anchor=\"middle\" x=\"63\" y=\"-228.95\" font-family=\"Times,serif\" font-size=\"14.00\">x2</text>\n",
182+
"</g>\n",
183+
"<!-- var8646911284551352322&#45;&gt;var8646911284551352321 -->\n",
184+
"<g id=\"edge1\" class=\"edge\">\n",
185+
"<title>var8646911284551352322&#45;&gt;var8646911284551352321</title>\n",
186+
"<path fill=\"none\" stroke=\"black\" d=\"M63,-215.7C63,-208.41 63,-199.73 63,-191.54\"/>\n",
187+
"<polygon fill=\"black\" stroke=\"black\" points=\"66.5,-191.62 63,-181.62 59.5,-191.62 66.5,-191.62\"/>\n",
188+
"</g>\n",
189+
"</g>\n",
190+
"</svg>\n"
191+
],
192+
"text/plain": [
193+
"<graphviz.sources.Source at 0x10c18fda0>"
194+
]
195+
},
196+
"metadata": {},
197+
"output_type": "display_data"
198+
}
199+
],
200+
"source": [
201+
"# Access a conditional by index\n",
202+
"conditional_1 = bayes_net.at(1) # P(x0 | l1)\n",
203+
"conditional_1.print(\"Conditional at index 1: \")\n",
204+
"\n",
205+
"# Visualize the Bayes Net structure\n",
206+
"display(graphviz.Source(bayes_net.dot()))"
207+
]
208+
}
209+
],
210+
"metadata": {
211+
"kernelspec": {
212+
"display_name": "py312",
213+
"language": "python",
214+
"name": "python3"
215+
},
216+
"language_info": {
217+
"codemirror_mode": {
218+
"name": "ipython",
219+
"version": 3
220+
},
221+
"file_extension": ".py",
222+
"mimetype": "text/x-python",
223+
"name": "python",
224+
"nbconvert_exporter": "python",
225+
"pygments_lexer": "ipython3",
226+
"version": "3.12.6"
227+
}
228+
},
229+
"nbformat": 4,
230+
"nbformat_minor": 4
231+
}

0 commit comments

Comments
 (0)