diff --git a/python/gtsam/examples/DiscreteBayesTree.ipynb b/python/gtsam/examples/DiscreteBayesTree.ipynb new file mode 100644 index 000000000..f980a22c0 --- /dev/null +++ b/python/gtsam/examples/DiscreteBayesTree.ipynb @@ -0,0 +1,1013 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# The Discrete Bayes Tree\n", + "\n", + "An example of building a Bayes net, then eliminating it into a Bayes tree. Mirrors the code in `testDiscreteBayesTree.cpp` ." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "tags": [ + "remove-cell" + ] + }, + "source": [ + "GTSAM Copyright 2010-2022, Georgia Tech Research Corporation,\n", + "Atlanta, Georgia 30332-0415\n", + "All Rights Reserved\n", + "\n", + "Authors: Frank Dellaert, et al. (see THANKS for the full author list)\n", + "\n", + "See LICENSE for the license information" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "remove-cell" + ] + }, + "outputs": [], + "source": [ + "# This needs gtbook:\n", + "% pip install --quiet gtbook" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from gtsam import DiscreteBayesTree, DiscreteBayesNet, DiscreteKeys, DiscreteFactorGraph, Ordering\n", + "from gtsam.symbol_shorthand import S" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "def P(*args):\n", + " \"\"\" Create a DiscreteKeys instances from a variable number of DiscreteKey pairs.\"\"\"\n", + " #TODO: We can make life easier by providing variable argument functions in C++ itself.\n", + " dks = DiscreteKeys()\n", + " for key in args:\n", + " dks.push_back(key)\n", + " return dks" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "import graphviz\n", + "class show(graphviz.Source):\n", + " \"\"\" Display an object with a dot method as a graph.\"\"\"\n", + "\n", + " def __init__(self, obj):\n", + " \"\"\"Construct from object with 'dot' method.\"\"\"\n", + " # This small class takes an object, calls its dot function, and uses the\n", + " # resulting string to initialize a graphviz.Source instance. This in turn\n", + " # has a _repr_mimebundle_ method, which then renders it in the notebook.\n", + " super().__init__(obj.dot())" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "image/svg+xml": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "G\n", + "\n", + "\n", + "\n", + "8\n", + "\n", + "8\n", + "\n", + "\n", + "\n", + "0\n", + "\n", + "0\n", + "\n", + "\n", + "\n", + "8->0\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "1\n", + "\n", + "1\n", + "\n", + "\n", + "\n", + "8->1\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "12\n", + "\n", + "12\n", + "\n", + "\n", + "\n", + "12->8\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "12->0\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "12->1\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "9\n", + "\n", + "9\n", + "\n", + "\n", + "\n", + "12->9\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2\n", + "\n", + "2\n", + "\n", + "\n", + "\n", + "12->2\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "3\n", + "\n", + "3\n", + "\n", + "\n", + "\n", + "12->3\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "9->2\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "9->3\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "10\n", + "\n", + "10\n", + "\n", + "\n", + "\n", + "4\n", + "\n", + "4\n", + "\n", + "\n", + "\n", + "10->4\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "5\n", + "\n", + "5\n", + "\n", + "\n", + "\n", + "10->5\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "13\n", + "\n", + "13\n", + "\n", + "\n", + "\n", + "13->10\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "13->4\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "13->5\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "11\n", + "\n", + "11\n", + "\n", + "\n", + "\n", + "13->11\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "6\n", + "\n", + "6\n", + "\n", + "\n", + "\n", + "13->6\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "7\n", + "\n", + "7\n", + "\n", + "\n", + "\n", + "13->7\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "11->6\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "11->7\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "14\n", + "\n", + "14\n", + "\n", + "\n", + "\n", + "14->8\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "14->12\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "14->9\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "14->10\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "14->13\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "14->11\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ], + "text/plain": [ + "<__main__.show at 0x109c615b0>" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Define DiscreteKey pairs.\n", + "keys = [(j, 2) for j in range(15)]\n", + "\n", + "# Create thin-tree Bayesnet.\n", + "bayesNet = DiscreteBayesNet()\n", + "\n", + "\n", + "bayesNet.add(keys[0], P(keys[8], keys[12]), \"2/3 1/4 3/2 4/1\")\n", + "bayesNet.add(keys[1], P(keys[8], keys[12]), \"4/1 2/3 3/2 1/4\")\n", + "bayesNet.add(keys[2], P(keys[9], keys[12]), \"1/4 8/2 2/3 4/1\")\n", + "bayesNet.add(keys[3], P(keys[9], keys[12]), \"1/4 2/3 3/2 4/1\")\n", + "\n", + "bayesNet.add(keys[4], P(keys[10], keys[13]), \"2/3 1/4 3/2 4/1\")\n", + "bayesNet.add(keys[5], P(keys[10], keys[13]), \"4/1 2/3 3/2 1/4\")\n", + "bayesNet.add(keys[6], P(keys[11], keys[13]), \"1/4 3/2 2/3 4/1\")\n", + "bayesNet.add(keys[7], P(keys[11], keys[13]), \"1/4 2/3 3/2 4/1\")\n", + "\n", + "bayesNet.add(keys[8], P(keys[12], keys[14]), \"T 1/4 3/2 4/1\")\n", + "bayesNet.add(keys[9], P(keys[12], keys[14]), \"4/1 2/3 F 1/4\")\n", + "bayesNet.add(keys[10], P(keys[13], keys[14]), \"1/4 3/2 2/3 4/1\")\n", + "bayesNet.add(keys[11], P(keys[13], keys[14]), \"1/4 2/3 3/2 4/1\")\n", + "\n", + "bayesNet.add(keys[12], P(keys[14]), \"3/1 3/1\")\n", + "bayesNet.add(keys[13], P(keys[14]), \"1/3 3/1\")\n", + "\n", + "bayesNet.add(keys[14], P(), \"1/3\")\n", + "\n", + "show(bayesNet)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "DiscreteValues{0: 1, 1: 1, 2: 0, 3: 1, 4: 1, 5: 1, 6: 0, 7: 1, 8: 0, 9: 0, 10: 0, 11: 0, 12: 1, 13: 1, 14: 0}\n", + "DiscreteValues{0: 0, 1: 1, 2: 0, 3: 0, 4: 1, 5: 0, 6: 0, 7: 0, 8: 1, 9: 1, 10: 0, 11: 1, 12: 0, 13: 0, 14: 1}\n", + "DiscreteValues{0: 1, 1: 0, 2: 1, 3: 1, 4: 0, 5: 0, 6: 1, 7: 0, 8: 1, 9: 0, 10: 1, 11: 1, 12: 0, 13: 1, 14: 0}\n", + "DiscreteValues{0: 1, 1: 1, 2: 0, 3: 0, 4: 1, 5: 1, 6: 1, 7: 1, 8: 0, 9: 1, 10: 0, 11: 0, 12: 1, 13: 0, 14: 1}\n", + "DiscreteValues{0: 0, 1: 0, 2: 1, 3: 0, 4: 1, 5: 1, 6: 1, 7: 0, 8: 1, 9: 1, 10: 0, 11: 1, 12: 0, 13: 0, 14: 1}\n" + ] + } + ], + "source": [ + "# Sample Bayes net (needs conditionals added in elimination order!)\n", + "for i in range(5):\n", + " print(bayesNet.sample())" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "image/svg+xml": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "var0\n", + "\n", + "0\n", + "\n", + "\n", + "\n", + "factor0\n", + "\n", + "\n", + "\n", + "\n", + "var0--factor0\n", + "\n", + "\n", + "\n", + "\n", + "var1\n", + "\n", + "1\n", + "\n", + "\n", + "\n", + "factor1\n", + "\n", + "\n", + "\n", + "\n", + "var1--factor1\n", + "\n", + "\n", + "\n", + "\n", + "var2\n", + "\n", + "2\n", + "\n", + "\n", + "\n", + "factor2\n", + "\n", + "\n", + "\n", + "\n", + "var2--factor2\n", + "\n", + "\n", + "\n", + "\n", + "var3\n", + "\n", + "3\n", + "\n", + "\n", + "\n", + "factor3\n", + "\n", + "\n", + "\n", + "\n", + "var3--factor3\n", + "\n", + "\n", + "\n", + "\n", + "var4\n", + "\n", + "4\n", + "\n", + "\n", + "\n", + "factor4\n", + "\n", + "\n", + "\n", + "\n", + "var4--factor4\n", + "\n", + "\n", + "\n", + "\n", + "var5\n", + "\n", + "5\n", + "\n", + "\n", + "\n", + "factor5\n", + "\n", + "\n", + "\n", + "\n", + "var5--factor5\n", + "\n", + "\n", + "\n", + "\n", + "var6\n", + "\n", + "6\n", + "\n", + "\n", + "\n", + "factor6\n", + "\n", + "\n", + "\n", + "\n", + "var6--factor6\n", + "\n", + "\n", + "\n", + "\n", + "var7\n", + "\n", + "7\n", + "\n", + "\n", + "\n", + "factor7\n", + "\n", + "\n", + "\n", + "\n", + "var7--factor7\n", + "\n", + "\n", + "\n", + "\n", + "var8\n", + "\n", + "8\n", + "\n", + "\n", + "\n", + "var8--factor0\n", + "\n", + "\n", + "\n", + "\n", + "var8--factor1\n", + "\n", + "\n", + "\n", + "\n", + "factor8\n", + "\n", + "\n", + "\n", + "\n", + "var8--factor8\n", + "\n", + "\n", + "\n", + "\n", + "var9\n", + "\n", + "9\n", + "\n", + "\n", + "\n", + "var9--factor2\n", + "\n", + "\n", + "\n", + "\n", + "var9--factor3\n", + "\n", + "\n", + "\n", + "\n", + "factor9\n", + "\n", + "\n", + "\n", + "\n", + "var9--factor9\n", + "\n", + "\n", + "\n", + "\n", + "var10\n", + "\n", + "10\n", + "\n", + "\n", + "\n", + "var10--factor4\n", + "\n", + "\n", + "\n", + "\n", + "var10--factor5\n", + "\n", + "\n", + "\n", + "\n", + "factor10\n", + "\n", + "\n", + "\n", + "\n", + "var10--factor10\n", + "\n", + "\n", + "\n", + "\n", + "var11\n", + "\n", + "11\n", + "\n", + "\n", + "\n", + "var11--factor6\n", + "\n", + "\n", + "\n", + "\n", + "var11--factor7\n", + "\n", + "\n", + "\n", + "\n", + "factor11\n", + "\n", + "\n", + "\n", + "\n", + "var11--factor11\n", + "\n", + "\n", + "\n", + "\n", + "var12\n", + "\n", + "12\n", + "\n", + "\n", + "\n", + "var14\n", + "\n", + "14\n", + "\n", + "\n", + "\n", + "var12--var14\n", + "\n", + "\n", + "\n", + "\n", + "var12--factor0\n", + "\n", + "\n", + "\n", + "\n", + "var12--factor1\n", + "\n", + "\n", + "\n", + "\n", + "var12--factor2\n", + "\n", + "\n", + "\n", + "\n", + "var12--factor3\n", + "\n", + "\n", + "\n", + "\n", + "var12--factor8\n", + "\n", + "\n", + "\n", + "\n", + "var12--factor9\n", + "\n", + "\n", + "\n", + "\n", + "var13\n", + "\n", + "13\n", + "\n", + "\n", + "\n", + "var13--var14\n", + "\n", + "\n", + "\n", + "\n", + "var13--factor4\n", + "\n", + "\n", + "\n", + "\n", + "var13--factor5\n", + "\n", + "\n", + "\n", + "\n", + "var13--factor6\n", + "\n", + "\n", + "\n", + "\n", + "var13--factor7\n", + "\n", + "\n", + "\n", + "\n", + "var13--factor10\n", + "\n", + "\n", + "\n", + "\n", + "var13--factor11\n", + "\n", + "\n", + "\n", + "\n", + "var14--factor8\n", + "\n", + "\n", + "\n", + "\n", + "var14--factor9\n", + "\n", + "\n", + "\n", + "\n", + "var14--factor10\n", + "\n", + "\n", + "\n", + "\n", + "var14--factor11\n", + "\n", + "\n", + "\n", + "\n", + "factor14\n", + "\n", + "\n", + "\n", + "\n", + "var14--factor14\n", + "\n", + "\n", + "\n", + "\n" + ], + "text/plain": [ + "<__main__.show at 0x109c61f10>" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Create a factor graph out of the Bayes net.\n", + "factorGraph = DiscreteFactorGraph(bayesNet)\n", + "show(factorGraph)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "image/svg+xml": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "G\n", + "\n", + "\n", + "\n", + "0\n", + "\n", + "8,12,14\n", + "\n", + "\n", + "\n", + "1\n", + "\n", + "0 : 8,12\n", + "\n", + "\n", + "\n", + "0->1\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2\n", + "\n", + "1 : 8,12\n", + "\n", + "\n", + "\n", + "0->2\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "3\n", + "\n", + "9 : 12,14\n", + "\n", + "\n", + "\n", + "0->3\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "6\n", + "\n", + "10,13 : 14\n", + "\n", + "\n", + "\n", + "0->6\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "4\n", + "\n", + "2 : 9,12\n", + "\n", + "\n", + "\n", + "3->4\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "5\n", + "\n", + "3 : 9,12\n", + "\n", + "\n", + "\n", + "3->5\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "7\n", + "\n", + "4 : 10,13\n", + "\n", + "\n", + "\n", + "6->7\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "8\n", + "\n", + "5 : 10,13\n", + "\n", + "\n", + "\n", + "6->8\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "9\n", + "\n", + "11 : 13,14\n", + "\n", + "\n", + "\n", + "6->9\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "10\n", + "\n", + "6 : 11,13\n", + "\n", + "\n", + "\n", + "9->10\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "11\n", + "\n", + "7 : 11,13\n", + "\n", + "\n", + "\n", + "9->11\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ], + "text/plain": [ + "<__main__.show at 0x109c61b50>" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Create a BayesTree out of the factor graph.\n", + "ordering = Ordering()\n", + "for j in range(15): ordering.push_back(j)\n", + "bayesTree = factorGraph.eliminateMultifrontal(ordering)\n", + "show(bayesTree)" + ] + } + ], + "metadata": { + "interpreter": { + "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6" + }, + "kernelspec": { + "display_name": "Python 3.8.9 64-bit", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.7" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/python/gtsam/examples/DiscreteSwitching.ipynb b/python/gtsam/examples/DiscreteSwitching.ipynb new file mode 100644 index 000000000..4af283cbd --- /dev/null +++ b/python/gtsam/examples/DiscreteSwitching.ipynb @@ -0,0 +1,805 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# A Discrete Switching System\n", + "\n", + "A la multi-hypothesis-smoother (MHS), but all discrete.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "tags": [ + "remove-cell" + ] + }, + "source": [ + "GTSAM Copyright 2010-2022, Georgia Tech Research Corporation,\n", + "Atlanta, Georgia 30332-0415\n", + "All Rights Reserved\n", + "\n", + "Authors: Frank Dellaert, et al. (see THANKS for the full author list)\n", + "\n", + "See LICENSE for the license information" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "remove-cell" + ] + }, + "outputs": [], + "source": [ + "# This needs gtbook:\n", + "% pip install --quiet gtbook" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from gtsam import DiscreteBayesNet, DiscreteKeys, DiscreteFactorGraph, Ordering\n", + "from gtsam.symbol_shorthand import S\n", + "from gtsam.symbol_shorthand import M" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "def P(*args):\n", + " \"\"\" Create a DiscreteKeys instances from a variable number of DiscreteKey pairs.\"\"\"\n", + " # TODO: We can make life easier by providing variable argument functions in C++ itself.\n", + " dks = DiscreteKeys()\n", + " for key in args:\n", + " dks.push_back(key)\n", + " return dks\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "import graphviz\n", + "\n", + "\n", + "class show(graphviz.Source):\n", + " \"\"\" Display an object with a dot method as a graph.\"\"\"\n", + "\n", + " def __init__(self, obj):\n", + " \"\"\"Construct from object with 'dot' method.\"\"\"\n", + " # This small class takes an object, calls its dot function, and uses the\n", + " # resulting string to initialize a graphviz.Source instance. This in turn\n", + " # has a _repr_mimebundle_ method, which then renders it in the notebook.\n", + " super().__init__(obj.dot())\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "

DiscreteBayesNet of size 4

\n", + "

P(s2|m1,s1):

\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
m1s1012
000.90.10
010.10.80.1
0200.10.9
100.10.90
1100.10.9
120.900.1
\n", + "
\n", + "
\n", + "

P(s3|m2,s2):

\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
m2s2012
000.90.10
010.10.80.1
0200.10.9
100.10.90
1100.10.9
120.900.1
\n", + "
\n", + "
\n", + "

P(s4|m3,s3):

\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
m3s3012
000.90.10
010.10.80.1
0200.10.9
100.10.90
1100.10.9
120.900.1
\n", + "
\n", + "
\n", + "

P(s5|m4,s4):

\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
m4s4012
000.90.10
010.10.80.1
0200.10.9
100.10.90
1100.10.9
120.900.1
\n", + "
\n" + ], + "text/markdown": [ + "`DiscreteBayesNet` of size 4\n", + "\n", + " *P(s2|m1,s1):*\n", + "\n", + "|*m1*|*s1*|0|1|2|\n", + "|:-:|:-:|:-:|:-:|:-:|\n", + "|0|0|0.9|0.1|0|\n", + "|0|1|0.1|0.8|0.1|\n", + "|0|2|0|0.1|0.9|\n", + "|1|0|0.1|0.9|0|\n", + "|1|1|0|0.1|0.9|\n", + "|1|2|0.9|0|0.1|\n", + "\n", + " *P(s3|m2,s2):*\n", + "\n", + "|*m2*|*s2*|0|1|2|\n", + "|:-:|:-:|:-:|:-:|:-:|\n", + "|0|0|0.9|0.1|0|\n", + "|0|1|0.1|0.8|0.1|\n", + "|0|2|0|0.1|0.9|\n", + "|1|0|0.1|0.9|0|\n", + "|1|1|0|0.1|0.9|\n", + "|1|2|0.9|0|0.1|\n", + "\n", + " *P(s4|m3,s3):*\n", + "\n", + "|*m3*|*s3*|0|1|2|\n", + "|:-:|:-:|:-:|:-:|:-:|\n", + "|0|0|0.9|0.1|0|\n", + "|0|1|0.1|0.8|0.1|\n", + "|0|2|0|0.1|0.9|\n", + "|1|0|0.1|0.9|0|\n", + "|1|1|0|0.1|0.9|\n", + "|1|2|0.9|0|0.1|\n", + "\n", + " *P(s5|m4,s4):*\n", + "\n", + "|*m4*|*s4*|0|1|2|\n", + "|:-:|:-:|:-:|:-:|:-:|\n", + "|0|0|0.9|0.1|0|\n", + "|0|1|0.1|0.8|0.1|\n", + "|0|2|0|0.1|0.9|\n", + "|1|0|0.1|0.9|0|\n", + "|1|1|0|0.1|0.9|\n", + "|1|2|0.9|0|0.1|\n", + "\n" + ], + "text/plain": [ + "DiscreteBayesNet\n", + " \n", + "size: 4\n", + "conditional 0: P( s2 | m1 s1 ):\n", + " Choice(s2) \n", + " 0 Choice(s1) \n", + " 0 0 Choice(m1) \n", + " 0 0 0 Leaf 0.9\n", + " 0 0 1 Leaf 0.1\n", + " 0 1 Choice(m1) \n", + " 0 1 0 Leaf 0.1\n", + " 0 1 1 Leaf 0\n", + " 0 2 Choice(m1) \n", + " 0 2 0 Leaf 0\n", + " 0 2 1 Leaf 0.9\n", + " 1 Choice(s1) \n", + " 1 0 Choice(m1) \n", + " 1 0 0 Leaf 0.1\n", + " 1 0 1 Leaf 0.9\n", + " 1 1 Choice(m1) \n", + " 1 1 0 Leaf 0.8\n", + " 1 1 1 Leaf 0.1\n", + " 1 2 Choice(m1) \n", + " 1 2 0 Leaf 0.1\n", + " 1 2 1 Leaf 0\n", + " 2 Choice(s1) \n", + " 2 0 Choice(m1) \n", + " 2 0 0 Leaf 0\n", + " 2 0 1 Leaf 0\n", + " 2 1 Choice(m1) \n", + " 2 1 0 Leaf 0.1\n", + " 2 1 1 Leaf 0.9\n", + " 2 2 Choice(m1) \n", + " 2 2 0 Leaf 0.9\n", + " 2 2 1 Leaf 0.1\n", + "\n", + "conditional 1: P( s3 | m2 s2 ):\n", + " Choice(s3) \n", + " 0 Choice(s2) \n", + " 0 0 Choice(m2) \n", + " 0 0 0 Leaf 0.9\n", + " 0 0 1 Leaf 0.1\n", + " 0 1 Choice(m2) \n", + " 0 1 0 Leaf 0.1\n", + " 0 1 1 Leaf 0\n", + " 0 2 Choice(m2) \n", + " 0 2 0 Leaf 0\n", + " 0 2 1 Leaf 0.9\n", + " 1 Choice(s2) \n", + " 1 0 Choice(m2) \n", + " 1 0 0 Leaf 0.1\n", + " 1 0 1 Leaf 0.9\n", + " 1 1 Choice(m2) \n", + " 1 1 0 Leaf 0.8\n", + " 1 1 1 Leaf 0.1\n", + " 1 2 Choice(m2) \n", + " 1 2 0 Leaf 0.1\n", + " 1 2 1 Leaf 0\n", + " 2 Choice(s2) \n", + " 2 0 Choice(m2) \n", + " 2 0 0 Leaf 0\n", + " 2 0 1 Leaf 0\n", + " 2 1 Choice(m2) \n", + " 2 1 0 Leaf 0.1\n", + " 2 1 1 Leaf 0.9\n", + " 2 2 Choice(m2) \n", + " 2 2 0 Leaf 0.9\n", + " 2 2 1 Leaf 0.1\n", + "\n", + "conditional 2: P( s4 | m3 s3 ):\n", + " Choice(s4) \n", + " 0 Choice(s3) \n", + " 0 0 Choice(m3) \n", + " 0 0 0 Leaf 0.9\n", + " 0 0 1 Leaf 0.1\n", + " 0 1 Choice(m3) \n", + " 0 1 0 Leaf 0.1\n", + " 0 1 1 Leaf 0\n", + " 0 2 Choice(m3) \n", + " 0 2 0 Leaf 0\n", + " 0 2 1 Leaf 0.9\n", + " 1 Choice(s3) \n", + " 1 0 Choice(m3) \n", + " 1 0 0 Leaf 0.1\n", + " 1 0 1 Leaf 0.9\n", + " 1 1 Choice(m3) \n", + " 1 1 0 Leaf 0.8\n", + " 1 1 1 Leaf 0.1\n", + " 1 2 Choice(m3) \n", + " 1 2 0 Leaf 0.1\n", + " 1 2 1 Leaf 0\n", + " 2 Choice(s3) \n", + " 2 0 Choice(m3) \n", + " 2 0 0 Leaf 0\n", + " 2 0 1 Leaf 0\n", + " 2 1 Choice(m3) \n", + " 2 1 0 Leaf 0.1\n", + " 2 1 1 Leaf 0.9\n", + " 2 2 Choice(m3) \n", + " 2 2 0 Leaf 0.9\n", + " 2 2 1 Leaf 0.1\n", + "\n", + "conditional 3: P( s5 | m4 s4 ):\n", + " Choice(s5) \n", + " 0 Choice(s4) \n", + " 0 0 Choice(m4) \n", + " 0 0 0 Leaf 0.9\n", + " 0 0 1 Leaf 0.1\n", + " 0 1 Choice(m4) \n", + " 0 1 0 Leaf 0.1\n", + " 0 1 1 Leaf 0\n", + " 0 2 Choice(m4) \n", + " 0 2 0 Leaf 0\n", + " 0 2 1 Leaf 0.9\n", + " 1 Choice(s4) \n", + " 1 0 Choice(m4) \n", + " 1 0 0 Leaf 0.1\n", + " 1 0 1 Leaf 0.9\n", + " 1 1 Choice(m4) \n", + " 1 1 0 Leaf 0.8\n", + " 1 1 1 Leaf 0.1\n", + " 1 2 Choice(m4) \n", + " 1 2 0 Leaf 0.1\n", + " 1 2 1 Leaf 0\n", + " 2 Choice(s4) \n", + " 2 0 Choice(m4) \n", + " 2 0 0 Leaf 0\n", + " 2 0 1 Leaf 0\n", + " 2 1 Choice(m4) \n", + " 2 1 0 Leaf 0.1\n", + " 2 1 1 Leaf 0.9\n", + " 2 2 Choice(m4) \n", + " 2 2 0 Leaf 0.9\n", + " 2 2 1 Leaf 0.1\n" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "nrStates = 3\n", + "K = 5\n", + "\n", + "bayesNet = DiscreteBayesNet()\n", + "for k in range(1, K):\n", + " key = S(k), nrStates\n", + " key_plus = S(k+1), nrStates\n", + " mode = M(k), 2\n", + " bayesNet.add(key_plus, P(mode, key), \"9/1/0 1/8/1 0/1/9 1/9/0 0/1/9 9/0/1\")\n", + "\n", + "bayesNet" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "image/svg+xml": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "var7854277750134145025\n", + "\n", + "m1\n", + "\n", + "\n", + "\n", + "var8286623314361712642\n", + "\n", + "s2\n", + "\n", + "\n", + "\n", + "var7854277750134145025->var8286623314361712642\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "var7854277750134145026\n", + "\n", + "m2\n", + "\n", + "\n", + "\n", + "var8286623314361712643\n", + "\n", + "s3\n", + "\n", + "\n", + "\n", + "var7854277750134145026->var8286623314361712643\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "var7854277750134145027\n", + "\n", + "m3\n", + "\n", + "\n", + "\n", + "var8286623314361712644\n", + "\n", + "s4\n", + "\n", + "\n", + "\n", + "var7854277750134145027->var8286623314361712644\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "var7854277750134145028\n", + "\n", + "m4\n", + "\n", + "\n", + "\n", + "var8286623314361712645\n", + "\n", + "s5\n", + "\n", + "\n", + "\n", + "var7854277750134145028->var8286623314361712645\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "var8286623314361712641\n", + "\n", + "s1\n", + "\n", + "\n", + "\n", + "var8286623314361712641->var8286623314361712642\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "var8286623314361712642->var8286623314361712643\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "var8286623314361712643->var8286623314361712644\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "var8286623314361712644->var8286623314361712645\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ], + "text/plain": [ + "<__main__.show at 0x11216aea0>" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "show(bayesNet)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "image/svg+xml": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "var7854277750134145025\n", + "\n", + "m1\n", + "\n", + "\n", + "\n", + "factor0\n", + "\n", + "\n", + "\n", + "\n", + "var7854277750134145025--factor0\n", + "\n", + "\n", + "\n", + "\n", + "var7854277750134145026\n", + "\n", + "m2\n", + "\n", + "\n", + "\n", + "factor1\n", + "\n", + "\n", + "\n", + "\n", + "var7854277750134145026--factor1\n", + "\n", + "\n", + "\n", + "\n", + "var7854277750134145027\n", + "\n", + "m3\n", + "\n", + "\n", + "\n", + "factor2\n", + "\n", + "\n", + "\n", + "\n", + "var7854277750134145027--factor2\n", + "\n", + "\n", + "\n", + "\n", + "var7854277750134145028\n", + "\n", + "m4\n", + "\n", + "\n", + "\n", + "factor3\n", + "\n", + "\n", + "\n", + "\n", + "var7854277750134145028--factor3\n", + "\n", + "\n", + "\n", + "\n", + "var8286623314361712641\n", + "\n", + "s1\n", + "\n", + "\n", + "\n", + "var8286623314361712641--factor0\n", + "\n", + "\n", + "\n", + "\n", + "var8286623314361712642\n", + "\n", + "s2\n", + "\n", + "\n", + "\n", + "var8286623314361712642--factor0\n", + "\n", + "\n", + "\n", + "\n", + "var8286623314361712642--factor1\n", + "\n", + "\n", + "\n", + "\n", + "var8286623314361712643\n", + "\n", + "s3\n", + "\n", + "\n", + "\n", + "var8286623314361712643--factor1\n", + "\n", + "\n", + "\n", + "\n", + "var8286623314361712643--factor2\n", + "\n", + "\n", + "\n", + "\n", + "var8286623314361712644\n", + "\n", + "s4\n", + "\n", + "\n", + "\n", + "var8286623314361712644--factor2\n", + "\n", + "\n", + "\n", + "\n", + "var8286623314361712644--factor3\n", + "\n", + "\n", + "\n", + "\n", + "var8286623314361712645\n", + "\n", + "s5\n", + "\n", + "\n", + "\n", + "var8286623314361712645--factor3\n", + "\n", + "\n", + "\n", + "\n" + ], + "text/plain": [ + "<__main__.show at 0x1121a44d0>" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Create a factor graph out of the Bayes net.\n", + "factorGraph = DiscreteFactorGraph(bayesNet)\n", + "show(factorGraph)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Position 0: s1, s2, s3, s4, s5, m1, m2, m3, m4\n", + "\n" + ] + } + ], + "source": [ + "# Create a BayesTree out of the factor graph.\n", + "ordering = Ordering()\n", + "# First eliminate \"continuous\" states in time order\n", + "for k in range(1, K+1):\n", + " ordering.push_back(S(k))\n", + "for k in range(1, K):\n", + " ordering.push_back(M(k))\n", + "print(ordering)\n", + "bayesTree = factorGraph.eliminateMultifrontal(ordering)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "image/svg+xml": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "G\n", + "\n", + "\n", + "\n", + "0\n", + "\n", + "s4, s5, m1, m2, m3, m4\n", + "\n", + "\n", + "\n", + "1\n", + "\n", + "s3 : m1, m2, m3, s4\n", + "\n", + "\n", + "\n", + "0->1\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2\n", + "\n", + "s2 : m1, m2, s3\n", + "\n", + "\n", + "\n", + "1->2\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "3\n", + "\n", + "s1 : m1, s2\n", + "\n", + "\n", + "\n", + "2->3\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ], + "text/plain": [ + "<__main__.show at 0x11775c3e0>" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "show(bayesTree)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "py312", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.6" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/python/gtsam/examples/EqF.ipynb b/python/gtsam/examples/EqF.ipynb index 59ee2c525..9eec85d55 100644 --- a/python/gtsam/examples/EqF.ipynb +++ b/python/gtsam/examples/EqF.ipynb @@ -6,13 +6,19 @@ "source": [ "# Equivariant Filter Bias\n", "\n", - "Implementing the example in [Fornasier et al, 2022, Overcoming Bias: Equivariant Filter Design for Biased Attitude Estimation with Online Calibration](https://arxiv.org/pdf/2209.12038).\n", - "\n", - "This notebook uses [Alessandro Fornasier's equivariant filter code](https://github.com/aau-cns/ABC-EqF) converted to use GTSAM's libraries.\n", + "Implementing the example in the \"Overcoming Bias\" paper by {cite:t}`https://doi.org/10.1109/LRA.2022.3210867` ([arxiv version](https://arxiv.org/pdf/2209.12038)).\n", + "This notebook was created by converting [Alessandro Fornasier's equivariant filter code](https://github.com/aau-cns/ABC-EqF) to use GTSAM's built-in data structures.\n", "\n", "Authors: Jennifer Oum & Darshan Rajasekaran" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\"Open" + ] + }, { "cell_type": "markdown", "metadata": { diff --git a/python/gtsam/notebooks/easyPoint2KalmanFilter.ipynb b/python/gtsam/examples/easyPoint2KalmanFilter.ipynb similarity index 72% rename from python/gtsam/notebooks/easyPoint2KalmanFilter.ipynb rename to python/gtsam/examples/easyPoint2KalmanFilter.ipynb index 1bdd12b8e..5417fca3b 100644 --- a/python/gtsam/notebooks/easyPoint2KalmanFilter.ipynb +++ b/python/gtsam/examples/easyPoint2KalmanFilter.ipynb @@ -1,24 +1,76 @@ { "cells": [ { - "cell_type": "code", - "execution_count": 1, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "\"\"\"\n", + "# Extended Kalman Filter\n", + "\n", "Extended Kalman filter on a moving 2D point, but done using factor graphs.\n", "This example uses the ExtendedKalmanFilter class to perform filtering\n", "on a linear system, demonstrating the same operations as in elaboratePoint2KalmanFilter.\n", - "\"\"\"\n", "\n", + "Author: Matt Kielo" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "tags": [ + "remove-cell" + ] + }, + "source": [ + "GTSAM Copyright 2010-2022, Georgia Tech Research Corporation,\n", + "Atlanta, Georgia 30332-0415\n", + "All Rights Reserved\n", + "\n", + "Authors: Frank Dellaert, et al. (see THANKS for the full author list)\n", + "\n", + "See LICENSE for the license information" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "tags": [ + "remove-cell" + ] + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Note: you may need to restart the kernel to use updated packages.\n" + ] + } + ], + "source": [ + "%pip install --quiet gtsam gtbook" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ "import gtsam\n", "import numpy as np" ] }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -49,7 +101,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -64,8 +116,8 @@ "X3 Update: [3. 0.]\n", "\n", "Easy Final Covariance (after update):\n", - " [[0.0193 0. ]\n", - " [0. 0.0193]]\n" + " [[0.01930567 0. ]\n", + " [0. 0.01930567]]\n" ] } ], @@ -100,7 +152,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "py312", "language": "python", "name": "python3" }, @@ -114,7 +166,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.12" + "version": "3.12.6" } }, "nbformat": 4, diff --git a/python/gtsam/notebooks/elaboratePoint2KalmanFilter.ipynb b/python/gtsam/examples/elaboratePoint2KalmanFilter.ipynb similarity index 84% rename from python/gtsam/notebooks/elaboratePoint2KalmanFilter.ipynb rename to python/gtsam/examples/elaboratePoint2KalmanFilter.ipynb index 34c467c35..afd195c20 100644 --- a/python/gtsam/notebooks/elaboratePoint2KalmanFilter.ipynb +++ b/python/gtsam/examples/elaboratePoint2KalmanFilter.ipynb @@ -1,34 +1,69 @@ { "cells": [ { - "cell_type": "code", - "execution_count": 1, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "\"\"\"\n", + "\n", + "# Elaborate EKF Example\n", + "\n", "Simple linear Kalman filter on a moving 2D point using factor graphs in GTSAM.\n", "This example manually creates all of the needed data structures to show how\n", "the Kalman filter works under the hood using factor graphs, but uses a loop\n", "to handle the repetitive prediction and update steps.\n", "\n", - "Based on the C++ example by Frank Dellaert and Stephen Williams\n", - "\"\"\"\n", + "Author: Matt Kielo. Based on the C++ example by Frank Dellaert and Stephen Williams" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "tags": [ + "remove-cell" + ] + }, + "source": [ + "GTSAM Copyright 2010-2022, Georgia Tech Research Corporation,\n", + "Atlanta, Georgia 30332-0415\n", + "All Rights Reserved\n", "\n", + "Authors: Frank Dellaert, et al. (see THANKS for the full author list)\n", + "\n", + "See LICENSE for the license information" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ "import gtsam\n", "import numpy as np\n", "from gtsam import Point2, noiseModel\n", "from gtsam.symbol_shorthand import X" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The code below basically implements the SRIF (Square-root Information filter version of the EKF) with Cholesky factorization." + ] + }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ - "# [code below basically does SRIF with Cholesky]\n", - "\n", "# Setup containers for linearization points\n", "linearization_points = gtsam.Values()\n", "\n", diff --git a/python/gtsam/notebooks/DiscreteBayesTree.ipynb b/python/gtsam/notebooks/DiscreteBayesTree.ipynb deleted file mode 100644 index 066c31d6a..000000000 --- a/python/gtsam/notebooks/DiscreteBayesTree.ipynb +++ /dev/null @@ -1,200 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# The Discrete Bayes Tree\n", - "\n", - "An example of building a Bayes net, then eliminating it into a Bayes tree. Mirrors the code in `testDiscreteBayesTree.cpp` .\n" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "from gtsam import DiscreteBayesTree, DiscreteBayesNet, DiscreteKeys, DiscreteFactorGraph, Ordering\n", - "from gtsam.symbol_shorthand import S\n" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "def P(*args):\n", - " \"\"\" Create a DiscreteKeys instances from a variable number of DiscreteKey pairs.\"\"\"\n", - " #TODO: We can make life easier by providing variable argument functions in C++ itself.\n", - " dks = DiscreteKeys()\n", - " for key in args:\n", - " dks.push_back(key)\n", - " return dks" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [], - "source": [ - "import graphviz\n", - "class show(graphviz.Source):\n", - " \"\"\" Display an object with a dot method as a graph.\"\"\"\n", - "\n", - " def __init__(self, obj):\n", - " \"\"\"Construct from object with 'dot' method.\"\"\"\n", - " # This small class takes an object, calls its dot function, and uses the\n", - " # resulting string to initialize a graphviz.Source instance. This in turn\n", - " # has a _repr_mimebundle_ method, which then renders it in the notebook.\n", - " super().__init__(obj.dot())" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "image/svg+xml": "\n\n\n\n\n\nG\n\n\n\n8\n\n8\n\n\n\n0\n\n0\n\n\n\n8->0\n\n\n\n\n\n1\n\n1\n\n\n\n8->1\n\n\n\n\n\n12\n\n12\n\n\n\n12->8\n\n\n\n\n\n12->0\n\n\n\n\n\n12->1\n\n\n\n\n\n9\n\n9\n\n\n\n12->9\n\n\n\n\n\n2\n\n2\n\n\n\n12->2\n\n\n\n\n\n3\n\n3\n\n\n\n12->3\n\n\n\n\n\n9->2\n\n\n\n\n\n9->3\n\n\n\n\n\n10\n\n10\n\n\n\n4\n\n4\n\n\n\n10->4\n\n\n\n\n\n5\n\n5\n\n\n\n10->5\n\n\n\n\n\n13\n\n13\n\n\n\n13->10\n\n\n\n\n\n13->4\n\n\n\n\n\n13->5\n\n\n\n\n\n11\n\n11\n\n\n\n13->11\n\n\n\n\n\n6\n\n6\n\n\n\n13->6\n\n\n\n\n\n7\n\n7\n\n\n\n13->7\n\n\n\n\n\n11->6\n\n\n\n\n\n11->7\n\n\n\n\n\n14\n\n14\n\n\n\n14->8\n\n\n\n\n\n14->12\n\n\n\n\n\n14->9\n\n\n\n\n\n14->10\n\n\n\n\n\n14->13\n\n\n\n\n\n14->11\n\n\n\n\n\n", - "text/plain": [ - "<__main__.show at 0x109c615b0>" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Define DiscreteKey pairs.\n", - "keys = [(j, 2) for j in range(15)]\n", - "\n", - "# Create thin-tree Bayesnet.\n", - "bayesNet = DiscreteBayesNet()\n", - "\n", - "\n", - "bayesNet.add(keys[0], P(keys[8], keys[12]), \"2/3 1/4 3/2 4/1\")\n", - "bayesNet.add(keys[1], P(keys[8], keys[12]), \"4/1 2/3 3/2 1/4\")\n", - "bayesNet.add(keys[2], P(keys[9], keys[12]), \"1/4 8/2 2/3 4/1\")\n", - "bayesNet.add(keys[3], P(keys[9], keys[12]), \"1/4 2/3 3/2 4/1\")\n", - "\n", - "bayesNet.add(keys[4], P(keys[10], keys[13]), \"2/3 1/4 3/2 4/1\")\n", - "bayesNet.add(keys[5], P(keys[10], keys[13]), \"4/1 2/3 3/2 1/4\")\n", - "bayesNet.add(keys[6], P(keys[11], keys[13]), \"1/4 3/2 2/3 4/1\")\n", - "bayesNet.add(keys[7], P(keys[11], keys[13]), \"1/4 2/3 3/2 4/1\")\n", - "\n", - "bayesNet.add(keys[8], P(keys[12], keys[14]), \"T 1/4 3/2 4/1\")\n", - "bayesNet.add(keys[9], P(keys[12], keys[14]), \"4/1 2/3 F 1/4\")\n", - "bayesNet.add(keys[10], P(keys[13], keys[14]), \"1/4 3/2 2/3 4/1\")\n", - "bayesNet.add(keys[11], P(keys[13], keys[14]), \"1/4 2/3 3/2 4/1\")\n", - "\n", - "bayesNet.add(keys[12], P(keys[14]), \"3/1 3/1\")\n", - "bayesNet.add(keys[13], P(keys[14]), \"1/3 3/1\")\n", - "\n", - "bayesNet.add(keys[14], P(), \"1/3\")\n", - "\n", - "show(bayesNet)" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "DiscreteValues{0: 1, 1: 1, 2: 0, 3: 1, 4: 1, 5: 1, 6: 0, 7: 1, 8: 0, 9: 0, 10: 0, 11: 0, 12: 1, 13: 1, 14: 0}\n", - "DiscreteValues{0: 0, 1: 1, 2: 0, 3: 0, 4: 1, 5: 0, 6: 0, 7: 0, 8: 1, 9: 1, 10: 0, 11: 1, 12: 0, 13: 0, 14: 1}\n", - "DiscreteValues{0: 1, 1: 0, 2: 1, 3: 1, 4: 0, 5: 0, 6: 1, 7: 0, 8: 1, 9: 0, 10: 1, 11: 1, 12: 0, 13: 1, 14: 0}\n", - "DiscreteValues{0: 1, 1: 1, 2: 0, 3: 0, 4: 1, 5: 1, 6: 1, 7: 1, 8: 0, 9: 1, 10: 0, 11: 0, 12: 1, 13: 0, 14: 1}\n", - "DiscreteValues{0: 0, 1: 0, 2: 1, 3: 0, 4: 1, 5: 1, 6: 1, 7: 0, 8: 1, 9: 1, 10: 0, 11: 1, 12: 0, 13: 0, 14: 1}\n" - ] - } - ], - "source": [ - "# Sample Bayes net (needs conditionals added in elimination order!)\n", - "for i in range(5):\n", - " print(bayesNet.sample())" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "image/svg+xml": "\n\n\n\n\n\n\n\n\nvar0\n\n0\n\n\n\nfactor0\n\n\n\n\nvar0--factor0\n\n\n\n\nvar1\n\n1\n\n\n\nfactor1\n\n\n\n\nvar1--factor1\n\n\n\n\nvar2\n\n2\n\n\n\nfactor2\n\n\n\n\nvar2--factor2\n\n\n\n\nvar3\n\n3\n\n\n\nfactor3\n\n\n\n\nvar3--factor3\n\n\n\n\nvar4\n\n4\n\n\n\nfactor4\n\n\n\n\nvar4--factor4\n\n\n\n\nvar5\n\n5\n\n\n\nfactor5\n\n\n\n\nvar5--factor5\n\n\n\n\nvar6\n\n6\n\n\n\nfactor6\n\n\n\n\nvar6--factor6\n\n\n\n\nvar7\n\n7\n\n\n\nfactor7\n\n\n\n\nvar7--factor7\n\n\n\n\nvar8\n\n8\n\n\n\nvar8--factor0\n\n\n\n\nvar8--factor1\n\n\n\n\nfactor8\n\n\n\n\nvar8--factor8\n\n\n\n\nvar9\n\n9\n\n\n\nvar9--factor2\n\n\n\n\nvar9--factor3\n\n\n\n\nfactor9\n\n\n\n\nvar9--factor9\n\n\n\n\nvar10\n\n10\n\n\n\nvar10--factor4\n\n\n\n\nvar10--factor5\n\n\n\n\nfactor10\n\n\n\n\nvar10--factor10\n\n\n\n\nvar11\n\n11\n\n\n\nvar11--factor6\n\n\n\n\nvar11--factor7\n\n\n\n\nfactor11\n\n\n\n\nvar11--factor11\n\n\n\n\nvar12\n\n12\n\n\n\nvar14\n\n14\n\n\n\nvar12--var14\n\n\n\n\nvar12--factor0\n\n\n\n\nvar12--factor1\n\n\n\n\nvar12--factor2\n\n\n\n\nvar12--factor3\n\n\n\n\nvar12--factor8\n\n\n\n\nvar12--factor9\n\n\n\n\nvar13\n\n13\n\n\n\nvar13--var14\n\n\n\n\nvar13--factor4\n\n\n\n\nvar13--factor5\n\n\n\n\nvar13--factor6\n\n\n\n\nvar13--factor7\n\n\n\n\nvar13--factor10\n\n\n\n\nvar13--factor11\n\n\n\n\nvar14--factor8\n\n\n\n\nvar14--factor9\n\n\n\n\nvar14--factor10\n\n\n\n\nvar14--factor11\n\n\n\n\nfactor14\n\n\n\n\nvar14--factor14\n\n\n\n\n", - "text/plain": [ - "<__main__.show at 0x109c61f10>" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Create a factor graph out of the Bayes net.\n", - "factorGraph = DiscreteFactorGraph(bayesNet)\n", - "show(factorGraph)" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "image/svg+xml": "\n\n\n\n\n\nG\n\n\n\n0\n\n8,12,14\n\n\n\n1\n\n0 : 8,12\n\n\n\n0->1\n\n\n\n\n\n2\n\n1 : 8,12\n\n\n\n0->2\n\n\n\n\n\n3\n\n9 : 12,14\n\n\n\n0->3\n\n\n\n\n\n6\n\n10,13 : 14\n\n\n\n0->6\n\n\n\n\n\n4\n\n2 : 9,12\n\n\n\n3->4\n\n\n\n\n\n5\n\n3 : 9,12\n\n\n\n3->5\n\n\n\n\n\n7\n\n4 : 10,13\n\n\n\n6->7\n\n\n\n\n\n8\n\n5 : 10,13\n\n\n\n6->8\n\n\n\n\n\n9\n\n11 : 13,14\n\n\n\n6->9\n\n\n\n\n\n10\n\n6 : 11,13\n\n\n\n9->10\n\n\n\n\n\n11\n\n7 : 11,13\n\n\n\n9->11\n\n\n\n\n\n", - "text/plain": [ - "<__main__.show at 0x109c61b50>" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Create a BayesTree out of the factor graph.\n", - "ordering = Ordering()\n", - "for j in range(15): ordering.push_back(j)\n", - "bayesTree = factorGraph.eliminateMultifrontal(ordering)\n", - "show(bayesTree)" - ] - } - ], - "metadata": { - "interpreter": { - "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6" - }, - "kernelspec": { - "display_name": "Python 3.8.9 64-bit", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.9.7" - }, - "orig_nbformat": 4 - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/python/gtsam/notebooks/DiscreteSwitching.ipynb b/python/gtsam/notebooks/DiscreteSwitching.ipynb deleted file mode 100644 index 6872e78c8..000000000 --- a/python/gtsam/notebooks/DiscreteSwitching.ipynb +++ /dev/null @@ -1,155 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# A Discrete Switching System\n", - "\n", - "A la MHS, but all discrete.\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "from gtsam import DiscreteBayesNet, DiscreteKeys, DiscreteFactorGraph, Ordering\n", - "from gtsam.symbol_shorthand import S\n", - "from gtsam.symbol_shorthand import M\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "def P(*args):\n", - " \"\"\" Create a DiscreteKeys instances from a variable number of DiscreteKey pairs.\"\"\"\n", - " # TODO: We can make life easier by providing variable argument functions in C++ itself.\n", - " dks = DiscreteKeys()\n", - " for key in args:\n", - " dks.push_back(key)\n", - " return dks\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import graphviz\n", - "\n", - "\n", - "class show(graphviz.Source):\n", - " \"\"\" Display an object with a dot method as a graph.\"\"\"\n", - "\n", - " def __init__(self, obj):\n", - " \"\"\"Construct from object with 'dot' method.\"\"\"\n", - " # This small class takes an object, calls its dot function, and uses the\n", - " # resulting string to initialize a graphviz.Source instance. This in turn\n", - " # has a _repr_mimebundle_ method, which then renders it in the notebook.\n", - " super().__init__(obj.dot())\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "nrStates = 3\n", - "K = 5\n", - "\n", - "bayesNet = DiscreteBayesNet()\n", - "for k in range(1, K):\n", - " key = S(k), nrStates\n", - " key_plus = S(k+1), nrStates\n", - " mode = M(k), 2\n", - " bayesNet.add(key_plus, P(mode, key), \"9/1/0 1/8/1 0/1/9 1/9/0 0/1/9 9/0/1\")\n", - "\n", - "bayesNet" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "show(bayesNet)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Create a factor graph out of the Bayes net.\n", - "factorGraph = DiscreteFactorGraph(bayesNet)\n", - "show(factorGraph)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Create a BayesTree out of the factor graph.\n", - "ordering = Ordering()\n", - "# First eliminate \"continuous\" states in time order\n", - "for k in range(1, K+1):\n", - " ordering.push_back(S(k))\n", - "for k in range(1, K):\n", - " ordering.push_back(M(k))\n", - "print(ordering)\n", - "bayesTree = factorGraph.eliminateMultifrontal(ordering)\n", - "bayesTree" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "show(bayesTree)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [] - } - ], - "metadata": { - "interpreter": { - "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6" - }, - "kernelspec": { - "display_name": "Python 3.8.9 64-bit", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.9.7" - }, - "orig_nbformat": 4 - }, - "nbformat": 4, - "nbformat_minor": 2 -}