gtsam/gtsam/inference/doc/LabeledSymbol.ipynb

218 lines
12 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "lsymbol_intro_md"
},
"source": [
"# LabeledSymbol"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "lsymbol_desc_md"
},
"source": [
"A `LabeledSymbol` is a specialized version of `gtsam.Symbol` designed primarily for multi-robot applications or scenarios where an additional label is needed besides the type character and index. It encodes a type character (`unsigned char`), a label character (`unsigned char`), and an index (`uint64_t`) into a single 64-bit `gtsam.Key`."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "lsymbol_colab_md"
},
"source": [
"<a href=\"https://colab.research.google.com/github/borglab/gtsam/blob/develop/gtsam/inference/doc/LabeledSymbol.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "lsymbol_pip_code"
},
"outputs": [],
"source": [
"%pip install gtsam"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"id": "lsymbol_import_code"
},
"outputs": [],
"source": [
"import gtsam\n",
"from gtsam import LabeledSymbol"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "lsymbol_init_md"
},
"source": [
"## Initialization\n",
"\n",
"A `LabeledSymbol` can be created by providing a type character, a label character, and an index. It can also be created by decoding an existing `gtsam.Key` (integer)."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "lsymbol_create_code",
"outputId": "f1a2b3c4-d5e6-7890-f1a2-bcdef1234567"
},
"outputs": [
{
"ename": "TypeError",
"evalue": "__init__(): incompatible constructor arguments. The following argument types are supported:\n 1. gtsam.gtsam.LabeledSymbol(full_key: int)\n 2. gtsam.gtsam.LabeledSymbol(key: gtsam.gtsam.LabeledSymbol)\n 3. gtsam.gtsam.LabeledSymbol(valType: int, label: int, j: int)\n\nInvoked with: 'x', 'A', 7",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[2], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# Create LabeledSymbol 'x' from robot 'A' with index 7\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m lsym1 \u001b[38;5;241m=\u001b[39m \u001b[43mLabeledSymbol\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mx\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mA\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m7\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mLabeledSymbol from char/label/index: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mlsym1\u001b[38;5;241m.\u001b[39mstring()\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 5\u001b[0m \u001b[38;5;66;03m# Get the underlying integer key\u001b[39;00m\n",
"\u001b[1;31mTypeError\u001b[0m: __init__(): incompatible constructor arguments. The following argument types are supported:\n 1. gtsam.gtsam.LabeledSymbol(full_key: int)\n 2. gtsam.gtsam.LabeledSymbol(key: gtsam.gtsam.LabeledSymbol)\n 3. gtsam.gtsam.LabeledSymbol(valType: int, label: int, j: int)\n\nInvoked with: 'x', 'A', 7"
]
}
],
"source": [
"# Create LabeledSymbol 'x' from robot 'A' with index 7\n",
"lsym1 = LabeledSymbol('x', 'A', 7)\n",
"print(f\"LabeledSymbol from char/label/index: {lsym1.string()}\")\n",
"\n",
"# Get the underlying integer key\n",
"key1 = lsym1.key()\n",
"\n",
"# Reconstruct LabeledSymbol from the key\n",
"# Note: Decoding a key assumes it was encoded as a LabeledSymbol.\n",
"# If you decode a standard Symbol key, the label might be garbage.\n",
"x0_key = gtsam.Symbol('x', 0).key()\n",
"lsym2 = LabeledSymbol(x0_key)\n",
"print(f\"LabeledSymbol from key {x0_key}: {lsym2.string()}\") # Label might be non-printable or 0"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "lsymbol_props_md"
},
"source": [
"## Properties and Usage\n",
"\n",
"You can access the type character, label character, index, and underlying integer key."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "lsymbol_access_code",
"outputId": "a2b3c4d5-e6f7-8901-a2b3-cdef12345678"
},
"outputs": [
{
"ename": "TypeError",
"evalue": "__init__(): incompatible constructor arguments. The following argument types are supported:\n 1. gtsam.gtsam.LabeledSymbol(full_key: int)\n 2. gtsam.gtsam.LabeledSymbol(key: gtsam.gtsam.LabeledSymbol)\n 3. gtsam.gtsam.LabeledSymbol(valType: int, label: int, j: int)\n\nInvoked with: 'l', 'B', 3",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[3], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m robotB_landmark \u001b[38;5;241m=\u001b[39m \u001b[43mLabeledSymbol\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43ml\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mB\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mLabeledSymbol: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mrobotB_landmark\u001b[38;5;241m.\u001b[39mstring()\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 4\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m Char (Type): \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mrobotB_landmark\u001b[38;5;241m.\u001b[39mchr()\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n",
"\u001b[1;31mTypeError\u001b[0m: __init__(): incompatible constructor arguments. The following argument types are supported:\n 1. gtsam.gtsam.LabeledSymbol(full_key: int)\n 2. gtsam.gtsam.LabeledSymbol(key: gtsam.gtsam.LabeledSymbol)\n 3. gtsam.gtsam.LabeledSymbol(valType: int, label: int, j: int)\n\nInvoked with: 'l', 'B', 3"
]
}
],
"source": [
"robotB_landmark = LabeledSymbol('l', 'B', 3)\n",
"\n",
"print(f\"LabeledSymbol: {robotB_landmark.string()}\")\n",
"print(f\" Char (Type): {robotB_landmark.chr()}\")\n",
"print(f\" Label (Robot): {robotB_landmark.label()}\")\n",
"print(f\" Index: {robotB_landmark.index()}\")\n",
"print(f\" Key: {robotB_landmark.key()}\")\n",
"\n",
"# LabeledSymbols are often used directly where Keys are expected.\n",
"# Use the MultiRobotKeyFormatter for printing these keys meaningfully.\n",
"# e.g., graph.print(\"Graph: \\n\", gtsam.MultiRobotKeyFormatter)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "lsymbol_shorthand_md"
},
"source": [
"## Shorthand Function\n",
"\n",
"GTSAM provides a convenient shorthand function `gtsam.mrsymbol(c, label, j)`."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "lsymbol_shorthand_code",
"outputId": "b3c4d5e6-f7a8-9012-b3c4-def123456789"
},
"outputs": [
{
"ename": "TypeError",
"evalue": "__init__(): incompatible constructor arguments. The following argument types are supported:\n 1. gtsam.gtsam.LabeledSymbol(full_key: int)\n 2. gtsam.gtsam.LabeledSymbol(key: gtsam.gtsam.LabeledSymbol)\n 3. gtsam.gtsam.LabeledSymbol(valType: int, label: int, j: int)\n\nInvoked with: 'p', 'C', 2",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[4], line 4\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# Note: mrsymbol expects integer representations of chars (use ord())\u001b[39;00m\n\u001b[0;32m 2\u001b[0m pc2_key \u001b[38;5;241m=\u001b[39m gtsam\u001b[38;5;241m.\u001b[39mmrsymbol(\u001b[38;5;28mord\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mp\u001b[39m\u001b[38;5;124m'\u001b[39m), \u001b[38;5;28mord\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mC\u001b[39m\u001b[38;5;124m'\u001b[39m), \u001b[38;5;241m2\u001b[39m)\n\u001b[1;32m----> 4\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mLabeledSymbol(\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mp\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m, \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mC\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m, 2).key() == gtsam.mrsymbol(ord(\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mp\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m), ord(\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mC\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m), 2): \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[43mLabeledSymbol\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mp\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;250;43m \u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mC\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;250;43m \u001b[39;49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m)\u001b[49m\u001b[38;5;241m.\u001b[39mkey()\u001b[38;5;250m \u001b[39m\u001b[38;5;241m==\u001b[39m\u001b[38;5;250m \u001b[39mpc2_key\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n",
"\u001b[1;31mTypeError\u001b[0m: __init__(): incompatible constructor arguments. The following argument types are supported:\n 1. gtsam.gtsam.LabeledSymbol(full_key: int)\n 2. gtsam.gtsam.LabeledSymbol(key: gtsam.gtsam.LabeledSymbol)\n 3. gtsam.gtsam.LabeledSymbol(valType: int, label: int, j: int)\n\nInvoked with: 'p', 'C', 2"
]
}
],
"source": [
"# Note: mrsymbol expects integer representations of chars (use ord())\n",
"pc2_key = gtsam.mrsymbol(ord('p'), ord('C'), 2)\n",
"\n",
"print(f\"LabeledSymbol('p', 'C', 2).key() == gtsam.mrsymbol(ord('p'), ord('C'), 2): {LabeledSymbol('p', 'C', 2).key() == pc2_key}\")"
]
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "gtsam",
"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.13.1"
}
},
"nbformat": 4,
"nbformat_minor": 0
}