Ordering fixes

release/4.3a0
p-zach 2025-04-14 20:01:02 -04:00
parent 91924d92db
commit d1e8fd3863
1 changed files with 14 additions and 45 deletions

View File

@ -45,7 +45,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 6,
"metadata": { "metadata": {
"id": "ordering_import_code" "id": "ordering_import_code"
}, },
@ -74,7 +74,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 7,
"metadata": { "metadata": {
"colab": { "colab": {
"base_uri": "https://localhost:8080/" "base_uri": "https://localhost:8080/"
@ -99,7 +99,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 12,
"metadata": { "metadata": {
"colab": { "colab": {
"base_uri": "https://localhost:8080/" "base_uri": "https://localhost:8080/"
@ -109,14 +109,11 @@
}, },
"outputs": [ "outputs": [
{ {
"ename": "TypeError", "name": "stdout",
"evalue": "Colamd(): incompatible function arguments. The following argument types are supported:\n 1. (variableIndex: gtsam::VariableIndex) -> gtsam.gtsam.Ordering\n\nInvoked with: kwargs: graph=SymbolicFactorGraph \nsize: 7\nfactor 0: x0\nfactor 1: x0 x1\nfactor 2: x1 x2\nfactor 3: x0 l1\nfactor 4: x1 l1\nfactor 5: x1 l2\nfactor 6: x2 l2\n", "output_type": "stream",
"output_type": "error", "text": [
"traceback": [ "COLAMD Ordering: Position 0: l1, x0, x1, l2, x2\n",
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "Constrained COLAMD (x0, x2 last): Position 0: l2, l1, x1, x2, x0\n"
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[3], line 13\u001b[0m\n\u001b[0;32m 10\u001b[0m graph\u001b[38;5;241m.\u001b[39mpush_factor(X(\u001b[38;5;241m2\u001b[39m), L(\u001b[38;5;241m2\u001b[39m))\n\u001b[0;32m 12\u001b[0m \u001b[38;5;66;03m# COLAMD (Column Approximate Minimum Degree) ordering\u001b[39;00m\n\u001b[1;32m---> 13\u001b[0m colamd_ordering \u001b[38;5;241m=\u001b[39m \u001b[43mOrdering\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mColamd\u001b[49m\u001b[43m(\u001b[49m\u001b[43mgraph\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mgraph\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 14\u001b[0m colamd_ordering\u001b[38;5;241m.\u001b[39mprint(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCOLAMD Ordering: \u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 16\u001b[0m \u001b[38;5;66;03m# Constrained COLAMD (force x0 and x2 to be eliminated last)\u001b[39;00m\n",
"\u001b[1;31mTypeError\u001b[0m: Colamd(): incompatible function arguments. The following argument types are supported:\n 1. (variableIndex: gtsam::VariableIndex) -> gtsam.gtsam.Ordering\n\nInvoked with: kwargs: graph=SymbolicFactorGraph \nsize: 7\nfactor 0: x0\nfactor 1: x0 x1\nfactor 2: x1 x2\nfactor 3: x0 l1\nfactor 4: x1 l1\nfactor 5: x1 l2\nfactor 6: x2 l2\n"
] ]
} }
], ],
@ -133,23 +130,12 @@
"graph.push_factor(X(2), L(2))\n", "graph.push_factor(X(2), L(2))\n",
"\n", "\n",
"# COLAMD (Column Approximate Minimum Degree) ordering\n", "# COLAMD (Column Approximate Minimum Degree) ordering\n",
"colamd_ordering = Ordering.Colamd(graph)\n", "colamd_ordering = Ordering.Colamd(VariableIndex(graph))\n",
"colamd_ordering.print(\"COLAMD Ordering: \")\n", "colamd_ordering.print(\"COLAMD Ordering: \")\n",
"\n", "\n",
"# Constrained COLAMD (force x0 and x2 to be eliminated last)\n", "# Constrained COLAMD (force x0 and x2 to be eliminated last)\n",
"constrained_ordering = Ordering.ColamdConstrainedLast(graph, gtsam.KeyVector([X(0), X(2)]))\n", "constrained_ordering = Ordering.ColamdConstrainedLastSymbolicFactorGraph(graph, gtsam.KeyVector([X(0), X(2)]))\n",
"constrained_ordering.print(\"Constrained COLAMD (x0, x2 last): \")\n", "constrained_ordering.print(\"Constrained COLAMD (x0, x2 last): \")"
"\n",
"# METIS ordering (if GTSAM was built with METIS support)\n",
"try:\n",
" metis_ordering = Ordering.Metis(graph)\n",
" # metis_ordering.print(\"METIS Ordering: \")\n",
"except RuntimeError as e:\n",
" print(f\"\\nSkipping METIS: {e}\")\n",
"\n",
"# Natural ordering (based on key magnitude)\n",
"natural_ordering = Ordering.Natural(graph)\n",
"# natural_ordering.print(\"Natural Ordering: \")"
] ]
}, },
{ {
@ -165,7 +151,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 11, "execution_count": 14,
"metadata": { "metadata": {
"colab": { "colab": {
"base_uri": "https://localhost:8080/" "base_uri": "https://localhost:8080/"
@ -181,17 +167,6 @@
"Ordering size: 5\n", "Ordering size: 5\n",
"Key at position 0: l1\n" "Key at position 0: l1\n"
] ]
},
{
"ename": "AttributeError",
"evalue": "'gtsam.gtsam.Ordering' object has no attribute 'invert'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[11], line 8\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mKey at position 0: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mgtsam\u001b[38;5;241m.\u001b[39mDefaultKeyFormatter(key_at_0)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 7\u001b[0m \u001b[38;5;66;03m# Invert the ordering (map from Key to its position)\u001b[39;00m\n\u001b[1;32m----> 8\u001b[0m inverted \u001b[38;5;241m=\u001b[39m \u001b[43mcolamd_ordering\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43minvert\u001b[49m()\n\u001b[0;32m 9\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mInverted map (Key -> Position):\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 10\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m key, pos \u001b[38;5;129;01min\u001b[39;00m inverted\u001b[38;5;241m.\u001b[39mitems():\n",
"\u001b[1;31mAttributeError\u001b[0m: 'gtsam.gtsam.Ordering' object has no attribute 'invert'"
]
} }
], ],
"source": [ "source": [
@ -199,13 +174,7 @@
"\n", "\n",
"# Access by index\n", "# Access by index\n",
"key_at_0 = colamd_ordering.at(0)\n", "key_at_0 = colamd_ordering.at(0)\n",
"print(f\"Key at position 0: {gtsam.DefaultKeyFormatter(key_at_0)}\")\n", "print(f\"Key at position 0: {gtsam.DefaultKeyFormatter(key_at_0)}\")"
"\n",
"# Invert the ordering (map from Key to its position)\n",
"inverted = colamd_ordering.invert()\n",
"print(\"Inverted map (Key -> Position):\")\n",
"for key, pos in inverted.items():\n",
" print(f\" {gtsam.DefaultKeyFormatter(key)} -> {pos}\")"
] ]
}, },
{ {
@ -216,7 +185,7 @@
"source": [ "source": [
"## Appending Keys\n", "## Appending Keys\n",
"\n", "\n",
"You can append keys using `push_back`, `+=`, or `,`." "You can append keys using `push_back`."
] ]
}, },
{ {