File size: 24,734 Bytes
e516f1f
 
 
 
 
 
 
 
 
 
beeea66
e516f1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
beeea66
e516f1f
 
beeea66
e516f1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
beeea66
 
 
 
 
 
 
 
 
 
e516f1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
beeea66
 
e516f1f
 
beeea66
 
 
 
 
 
e516f1f
 
beeea66
e516f1f
beeea66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e516f1f
 
 
 
beeea66
e516f1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
beeea66
e516f1f
 
 
 
 
 
 
 
 
beeea66
 
e516f1f
 
 
 
beeea66
 
 
 
e516f1f
 
 
 
 
 
 
 
 
 
 
beeea66
 
e516f1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
beeea66
e516f1f
 
beeea66
 
 
 
 
 
 
 
 
e516f1f
 
 
 
 
 
 
 
 
 
 
 
beeea66
 
e516f1f
 
 
beeea66
e516f1f
 
 
 
 
 
beeea66
e516f1f
 
 
 
beeea66
 
 
 
 
e516f1f
 
 
 
beeea66
 
 
 
 
e516f1f
 
beeea66
e516f1f
 
 
 
 
beeea66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e516f1f
 
 
 
 
 
beeea66
e516f1f
beeea66
e516f1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
beeea66
 
 
 
e516f1f
beeea66
 
e516f1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
from fastapi import FastAPI, UploadFile, HTTPException
from fastapi.responses import HTMLResponse, RedirectResponse
from contextlib import asynccontextmanager
from pathlib import Path
from types import SimpleNamespace
import json
import uvicorn
import quantum.config.hdf5parser as h5parser
import quantum.config.parser as config_parser
from quantum import map
from quantum.builder import QUBOBuilder, GraphQUBO, GridILPBuilder, GraphILPBuilder
from quantum.robotConfiguration import RobotConfig
from quantum.visualizer import QuantumRoboticsVisualizer
import quantum.pathFormulation as pathfinding
import registry
from profiles.robot import Robot, RegisterRobotRequest
from profiles.models import (
    MapInfo,
    RobotMapsResponse,
    PlanRequest,
    PlanResponse,
    MapRegistryResponse,
    MapUploadResponse,
    StatelessPlanRequest,
    StatelessPlanResponse,
    RobotPathResult,
)
from config_api import (
    load_solver_configs,
    global_solver_configs,
    global_penalties_params,
)
from typing import Dict, Optional
import datetime
import time

# This app's own assets (config/, web/), addressed relative to this file so
# uvicorn can be launched from any directory. Library-owned data (penalty sets,
# maps) lives under registry.QUANTUM_ROOT instead.
APP_ROOT = Path(__file__).resolve().parent

# Global registry: robot_id β†’ Robot instance
robots: Dict[str, Robot] = {}

# This should be retrieved from config/robot_templates.yaml
# Atlhough I still don't have structure for this, need more data
TEMPLATES = {
    "default": {},
    "mobile-robot": {"active_solver": "classic.ilp"},
    "quantum-agent": {"active_solver": "dwave.general"},
    "research-qaoa": {"active_solver": "pennylane.qaoa_QNG"},
    "exact-planner": {"active_solver": "classic.ilp"},
}


def _warm_start_gpu_devices():
    """
    Pay PennyLane's lightning.gpu CUDA/cuStateVec cold-start cost (~2s, a
    one-time cost per process) here at startup instead of on whichever
    request happens to be the first to use a GPU solver profile. The cost is
    process-wide, not per solver instance, so a single throwaway device
    covers every pennylane.* profile configured with device=lightning.gpu.
    """
    needs_gpu = any(
        cfg.get("backend") == "pennylane" and cfg.get("device") == "lightning.gpu"
        for cfg in global_solver_configs.values()
    )
    if not needs_gpu:
        return
    try:
        import pennylane as qml

        t0 = time.time()
        qml.device("lightning.gpu", wires=2)
        print(f"Warmed lightning.gpu device ({time.time() - t0:.2f}s).")
    except Exception as e:
        print(f"Skipping lightning.gpu warm-start: {e}")


@asynccontextmanager
async def lifespan(app: FastAPI):
    """
    Application lifespan context manager to handle startup and shutdown events.
    """
    try:
        solvers_config = config_parser.load_config(
            APP_ROOT / "config/solvers.yaml", sections=["solvers"]
        )
        solvers = solvers_config.get("solvers", {})

        load_solver_configs(solvers)
        print("Solver configurations loaded.")
        _warm_start_gpu_devices()

        penalties_conf = config_parser.load_config(
            registry.QUANTUM_ROOT / "config/config.yaml", sections=["penalty_sets"]
        )
        global_penalties_params.update(penalties_conf["penalty_sets"])

        maps_conf = config_parser.load_config(
            APP_ROOT / "config/maps.yaml", sections=["maps"]
        )
        registry.load_map_registry(maps_conf.get("maps") or {})
        print(
            f"Map registry loaded ({len(maps_conf.get('maps') or {})} entries, lazy-loaded on first use)."
        )

        yield  # Application is ready to handle requests
    finally:
        # Cleanup if needed
        global_solver_configs.clear()
        print("Application shutdown complete.")


app = FastAPI(lifespan=lifespan)

DEMO_HTML_PATH = APP_ROOT / "web" / "demo.html"


@app.get("/", include_in_schema=False)
def root():
    """Redirect to /demo β€” matters for HF Spaces' Docker SDK, which iframes '/'."""
    return RedirectResponse(url="/demo")


@app.get("/demo", response_class=HTMLResponse)
def demo_page():
    """
    Self-contained demo UI: map/solver pickers, a robot form, and a live Plotly view.
    No-store: this file changes often during development and carries no ETag/
    Last-Modified, so without an explicit directive some browsers will serve a
    stale cached copy on a plain reload instead of refetching.
    """
    return HTMLResponse(
        content=DEMO_HTML_PATH.read_text(encoding="utf-8"),
        headers={"Cache-Control": "no-store"},
    )


@app.get("/solvers")
def list_solvers() -> Dict[str, dict]:
    """
    List all available solvers and their configurations.

    Returns:
        Dict mapping solver_id β†’ configuration
    """
    return global_solver_configs


@app.get("/v1/penalty-sets")
def list_penalty_sets() -> Dict[str, object]:
    """List available penalty_set names for /v1/plan (see quantum/config/config.yaml)."""
    return {"penalty_sets": list(global_penalties_params.keys()), "default": "crash"}


@app.post("/robots/{robot_id}/maps/{map_id}")
async def upload_map(
    robot_id: str,
    map_id: str,
    file: UploadFile,
    materials_file: Optional[UploadFile] = None,
):
    if robot_id not in robots:
        raise HTTPException(404, "Robot not found")

    robot = robots[robot_id]
    try:
        file.file.seek(0)
        map_conf = h5parser.load_map_from_hdf5(file.file)

        materials_conf = None
        if materials_file:
            materials_conf = config_parser.load_config(materials_file.file)["materials"]

        map_obj = map.Grid.from_hdf5_data(map_conf, materials_conf)

        robot.maps[map_id] = map_obj
        if robot.active_map is None:
            robot.active_map = map_id  # auto-activate first map

        return {"status": "map_uploaded", "map_id": map_id}

    except Exception as e:
        raise HTTPException(status_code=400, detail=f"Map loading failed: {str(e)}")


@app.get("/robots/{robot_id}/maps", response_model=RobotMapsResponse)
def list_robot_maps(robot_id: str) -> Dict[str, dict]:
    """
    List all maps loaded for a specific robot.

    Returns:
        Dict mapping map_id β†’ metadata (name, grid size, materials, etc.)
    """
    if robot_id not in robots:
        raise HTTPException(404, "Robot not found")

    robot = robots[robot_id]
    result = {}

    for map_id, map_obj in robot.maps.items():
        try:
            # Extract metadata from your MapObject (adjust based on your class)
            result[map_id] = MapInfo(
                name=getattr(map_obj, "name", map_id),
                grid_size=f"{getattr(map_obj, 'M', 'unknown')}x{getattr(map_obj, 'N', 'unknown')}",
                resolution=getattr(map_obj, "resolution", "unknown"),
                materials=getattr(map_obj, "materials", []),
                loaded=True,
                is_active=(map_id == robot.active_map),
            )
        except Exception as e:
            result[map_id] = {"error": f"Failed to read metadata: {str(e)}"}

    return {"robot_id": robot_id, "map_count": len(result), "maps": result}


@app.get("/robots/{robot_id}/maps/{map_id}")
def get_robot_map_info(robot_id: str, map_id: str):
    """
    Get detailed info about a specific map for a robot.
    """
    if robot_id not in robots:
        raise HTTPException(404, "Robot not found")

    robot = robots[robot_id]

    if map_id not in robot.maps:
        raise HTTPException(404, "Map not found for this robot")

    map_obj = robot.maps[map_id]

    # You can expose more attributes based on your MapObject
    return {
        "robot_id": robot_id,
        "map_id": map_id,
        "name": getattr(map_obj, "name", map_id),
        "grid_size": [getattr(map_obj, "M", None), getattr(map_obj, "N", None)],
        "resolution": getattr(map_obj, "resolution", None),
        "materials": getattr(map_obj, "materials", []),
        "is_active": map_id == robot.active_map,
        # Optional: expose internal flags
        "has_terrain": map_obj.terrain is not None,
        "has_elevation": map_obj.elevation is not None,
        "metadata": "Map from HDF5 with custom layers",
    }


@app.delete("/robots/{robot_id}/maps/{map_id}")
def delete_robot_map(robot_id: str, map_id: str):
    """
    Delete a specific map from a robot's storage.
    If the active map is deleted, active_map_id is set to None.
    """
    if robot_id not in robots:
        raise HTTPException(404, "Robot not found")

    robot = robots[robot_id]

    if map_id not in robot.maps:
        raise HTTPException(404, "Map not found for this robot")

    # Remove map
    del robot.maps[map_id]

    # If it was the active map, clear active_map_id
    if robot.active_map == map_id:
        if robot.maps:
            # Set to another map if available
            robot.active_map = next(iter(robot.maps))
        else:
            robot.active_map = None

    return {"status": "deleted", "robot_id": robot_id, "map_id": map_id}


# ROBOTS


@app.post("/robots")
def register_robot(request: RegisterRobotRequest):
    if not request.robot_id:
        raise HTTPException(400, "robot_id is required")

    if request.robot_id in robots:
        # Optional: return existing
        return {
            "status": "already_registered",
            "robot": robots[request.robot_id].to_dict(),
        }

    # Validate template
    if request.template not in TEMPLATES:
        raise HTTPException(400, f"Unknown template: {request.template}")

    # New robot
    robot = Robot(robot_id=request.robot_id, template=request.template)

    # Apply template defaults (if any)
    template_config = TEMPLATES[request.template]
    if "active_solver" in template_config:
        robot.active_solver = template_config["active_solver"]

    # Save
    robots[request.robot_id] = robot

    return {"status": "registered", "robot": robot.to_dict()}


@app.get("/robots/{robot_id}")
def get_robot(robot_id: str):
    if robot_id not in robots:
        raise HTTPException(404, "Robot not found")
    return {"robot": robots[robot_id].to_dict()}


@app.get("/robots")
def list_robots():
    return {"robots": [robot.to_dict() for robot in robots.values()]}


# PLANNING


@app.post("/robots/{robot_id}/plan", response_model=PlanResponse)
def plan_path(robot_id: str, request: PlanRequest):
    if robot_id not in robots:
        raise HTTPException(404, "Robot not found")

    robot = robots[robot_id]

    # --- 1. Resolve map ---
    if request.map_id not in robot.maps:
        raise HTTPException(404, "Map not found for this robot")
    map_obj = robot.maps[request.map_id]

    # --- 2. Resolve solver (smart: uses active if not specified) ---
    try:
        solver = robot.get_solver(request.solver)  # None β†’ uses active_solver
    except Exception as e:
        raise HTTPException(400, str(e))

    # --- 3. Run planning ---
    try:
        robot_config = RobotConfig(
            robot_id=robot_id, start=tuple(request.start), goal=tuple(request.goal),
            coordinate_format=request.coordinate_format,
        )
        problem = pathfinding.PathfindingProblem(robot_config, grid=map_obj)
        if solver.solver == "ilp":
            builder = GridILPBuilder(problem, name="standard")
        else:
            builder = QUBOBuilder(
                problem, penalties=global_penalties_params["crash"], name="standard"
            )
        start_time = time.time()
        builder.build()
        solution = solver.solve(builder)
        planning_time = time.time() - start_time
        raw_path = solver.decode_path(solution["solution"], problem)
        if request.clip_at_goal:
            # Clip on matrix-native coords (against robot.goal) before
            # formatting, so the response stops at goal instead of showing
            # it parked there.
            clipped_robot_paths = solver.clip_paths_at_goal(
                solver.get_robot_paths(raw_path), problem
            )
            raw_path = [
                ((i, j, t), robot_num)
                for robot_num, coords in clipped_robot_paths.items()
                for (i, j, t) in coords
            ]
        formatted_path = solver.format_output_path(raw_path, problem)
        decoded_path = [[i, j] for (i, j, t), _ in formatted_path]
        energy = float(solver.total_energy(solution))
        print("Energy", energy)
        response = PlanResponse(
            path=decoded_path,
            coordinate_format=request.coordinate_format,
            cost=energy,
            # success=path["success"],
            map_id=request.map_id,
            # solve_time_ms=path["solve_time_ms"],
            solver_used=request.solver or robot.active_solver,
            metrics={
                "start": request.start,
                "goal": request.goal,
                "planning_time": planning_time,
                "timestamp": datetime.datetime.now(datetime.UTC).isoformat(),
            },
        )
        if request.details:
            response.solver_details = solver.to_dict()

        return response
    except Exception as e:
        raise HTTPException(500, f"Planning failed: {str(e)}")


# STATELESS PLANNER (v1) β€” no per-robot session, maps/solvers already in memory


@app.get("/v1/maps", response_model=MapRegistryResponse)
def list_registered_maps():
    """List every map_id in the registry (curated + runtime-uploaded), and whether it's loaded yet."""
    maps = registry.list_maps()
    return {"map_count": len(maps), "maps": maps}


@app.get("/v1/maps/{map_id}/preview")
def preview_map(map_id: str, embed: str = "html", coordinate_format: str = "matrix"):
    """
    Render map_id's grid (obstacles + terrain, no robots/paths).
    embed="html" (default): a standalone-ish Plotly HTML fragment, plotly.js via
    CDN β€” good for a direct browser open or Swagger link, not for injecting into
    a page that wants to update the same figure later (script tags in an
    innerHTML-injected fragment don't execute).
    embed="json": {"data": [...], "layout": {...}} β€” for pages that already load
    plotly.js themselves and want to call Plotly.newPlot/react directly (this is
    what /demo uses, so it can later update the same figure with a solved path).
    coordinate_format="matrix" (default) or "cartesian" β€” purely a display choice
    (axis labels/origin/direction); the underlying grid data is unaffected.
    Grid-only: graph-only maps have no visualizer support and return 400.
    """
    if embed not in ("html", "json"):
        raise HTTPException(400, f"Unknown embed: {embed}. Must be 'html' or 'json'.")
    if coordinate_format not in ("matrix", "cartesian"):
        raise HTTPException(
            400, f"Unknown coordinate_format: {coordinate_format}. Must be 'matrix' or 'cartesian'."
        )

    try:
        grid = registry.get_map(map_id, format="grid")
    except KeyError:
        raise HTTPException(404, f"Unknown map_id: {map_id}")
    except ValueError as e:
        raise HTTPException(400, str(e))
    except Exception as e:
        raise HTTPException(400, f"Failed to load map '{map_id}': {e}")

    visualizer = QuantumRoboticsVisualizer(
        grid_size=(grid.M, grid.N), title=f"Map preview: {map_id}",
        convention="robotics" if coordinate_format == "cartesian" else "matrix",
    )
    problem_stub = SimpleNamespace(grid=grid)
    fig = visualizer.create_static_plot(obstacles=grid.obstacles, problem=problem_stub)

    if embed == "json":
        return json.loads(fig.to_json())

    html = fig.to_html(full_html=False, include_plotlyjs="cdn")
    return HTMLResponse(content=html)


@app.post("/v1/maps/{map_id}", response_model=MapUploadResponse)
async def upload_registered_map(
    map_id: str, file: UploadFile, materials_file: Optional[UploadFile] = None
):
    """
    Register a new map at runtime by uploading its HDF5 file.
    Stored in the same in-memory registry as the curated maps.yaml entries,
    but not persisted back to maps.yaml β€” it only lives for this process.
    Both grid and graph representations are parsed if present in the file.
    """
    try:
        file.file.seek(0)
        data = h5parser.load_both_from_hdf5(file.file)

        materials_conf = None
        if materials_file:
            materials_conf = config_parser.load_config(materials_file.file)["materials"]

        grid = None
        if data["has_map"] and data["map_data"]:
            grid = map.Grid.from_hdf5_data(data["map_data"], materials_conf)

        graph = None
        if data["has_graph"] and data["graph_data"]:
            graph = map.Graph.from_hdf5_data(data["graph_data"])

        if grid is None and graph is None:
            raise ValueError(
                "HDF5 file contains neither a grid ('map_structure') nor a graph representation"
            )

        registry.register_uploaded_map(map_id, grid=grid, graph=graph)

        return MapUploadResponse(
            status="registered",
            map_id=map_id,
            grid_size=f"{grid.M}x{grid.N}" if grid else None,
            has_graph=graph is not None,
        )

    except Exception as e:
        raise HTTPException(status_code=400, detail=f"Map loading failed: {str(e)}")


@app.post("/v1/plan", response_model=StatelessPlanResponse)
def plan_stateless(request: StatelessPlanRequest):
    """
    Stateless planning: map_id + solver + robots in, paths + cost out.
    No robot registration, no per-call map upload β€” map and solver instances are
    resolved from the in-memory registries. `robots` always takes a list β€” one
    entry for a single robot, more for multi-robot β€” and both the grid and
    graph builder are supported (request.format).

    Positions are always given as [row, col] β€” even in graph mode, where they're
    resolved to node ids server-side via Graph.get_node_from_position. Paths are
    returned the same way: [[row, col], ...], Spooky's native (row, col) matrix
    convention β€” see quantum/utils/coordinates.py to convert to robotics (x, y)
    Y-up if needed.
    """
    if request.format not in ("grid", "graph"):
        raise HTTPException(
            400, f"Unknown format: {request.format}. Must be 'grid' or 'graph'."
        )

    # --- 1. Resolve map ---
    try:
        env = registry.get_map(request.map_id, format=request.format)
    except KeyError:
        raise HTTPException(404, f"Unknown map_id: {request.map_id}")
    except ValueError as e:
        raise HTTPException(400, str(e))
    except Exception as e:
        raise HTTPException(400, f"Failed to load map '{request.map_id}': {e}")

    # --- 2. Resolve solver ---
    try:
        solver = registry.get_solver(request.solver)
    except KeyError:
        raise HTTPException(400, f"Unknown solver: {request.solver}")

    # --- 3. Resolve penalties ---
    if request.penalty_set not in global_penalties_params:
        raise HTTPException(
            400,
            f"Unknown penalty_set: {request.penalty_set}. Available: {list(global_penalties_params.keys())}",
        )
    penalties = global_penalties_params[request.penalty_set]

    # --- 4. Build robot configs (single or multi) ---
    def resolve_position(pos: list[int], coordinate_format: str):
        # Graph builders index robots by node id, not (row, col) β€” RobotConfig
        # must hold the node id directly (see PathfindingProblem.from_graph_data).
        # Node ids have no coordinate frame of their own, so cartesian input isn't
        # supported in graph mode β€” convert to matrix before the position lookup
        # would be needed elsewhere, but here we just reject it outright.
        if coordinate_format == "cartesian" and request.format == "graph":
            raise HTTPException(
                400,
                "coordinate_format='cartesian' is not supported in graph mode β€” "
                "positions there resolve directly to node ids.",
            )
        if request.format != "graph":
            return tuple(pos)
        node_id = env.get_node_from_position(tuple(pos))
        if node_id is None:
            raise HTTPException(
                400, f"Position {pos} is not a node in map '{request.map_id}'"
            )
        return node_id

    robot_configs = [
        RobotConfig(
            robot_id=r.id or f"robot_{i}",
            start=resolve_position(r.start, r.coordinate_format),
            goal=resolve_position(r.goal, r.coordinate_format),
            start_time=r.start_time,
            priority=r.priority,
            safety_radius=r.safety_radius,
            coordinate_format=r.coordinate_format,
        )
        for i, r in enumerate(request.robots)
    ]

    # --- 5. Solve ---
    try:
        is_ilp = solver.solver == "ilp"
        if request.format == "graph":
            problem = pathfinding.PathfindingProblem(
                robot_configs, graph=env, T=request.T
            )
            builder = (
                GraphILPBuilder(problem, name="v1_plan")
                if is_ilp
                else GraphQUBO(problem, penalties=penalties, name="v1_plan")
            )
        else:
            problem = pathfinding.PathfindingProblem(
                robot_configs, grid=env, T=request.T
            )
            builder = (
                GridILPBuilder(problem, name="v1_plan")
                if is_ilp
                else QUBOBuilder(problem, penalties=penalties, name="v1_plan")
            )
        start_time = time.time()
        builder.build()
        solution = solver.solve(builder)
        planning_time = time.time() - start_time

        decoded = solver.decode_path(solution["solution"], problem)
        robot_paths = solver.get_robot_paths(
            decoded
        )  # {robot_num: [(i, j, t), ...] sorted by t} β€” matrix, for the visualizer below
        response_path = decoded
        if request.clip_at_goal:
            # Clip on matrix-native coords (against robot.goal) before
            # formatting, so the response stops at goal instead of showing
            # it parked there. robot_paths (visualizer, above) stays unclipped.
            clipped_robot_paths = solver.clip_paths_at_goal(robot_paths, problem)
            response_path = [
                ((i, j, t), robot_num)
                for robot_num, coords in clipped_robot_paths.items()
                for (i, j, t) in coords
            ]
        formatted_robot_paths = solver.get_robot_paths(
            solver.format_output_path(response_path, problem)
        )  # same, but each robot's path in its own coordinate_format β€” for the response
        num_to_id = {num: rid for rid, num in problem.get_robot_nums().items()}

        paths = [
            RobotPathResult(
                robot_id=num_to_id.get(robot_num, str(robot_num)),
                path=[[i, j] for (i, j, t) in coords],
                coordinate_format=problem.robots[num_to_id[robot_num]].coordinate_format,
            )
            for robot_num, coords in formatted_robot_paths.items()
        ]

        response = StatelessPlanResponse(
            paths=paths,
            cost=float(solver.total_energy(solution)),
            map_id=request.map_id,
            solver_used=request.solver,
            metrics={
                "planning_time": planning_time,
                "timestamp": datetime.datetime.now(datetime.UTC).isoformat(),
            },
        )
        if request.details:
            response.solver_details = solver.to_dict()

        if request.render and request.format == "grid":
            # The figure is one shared plot for all robots, so a single display
            # convention is used β€” the first robot's coordinate_format (the demo
            # UI sets the same value for every robot via one toggle).
            render_format = request.robots[0].coordinate_format if request.robots else "matrix"
            visualizer = QuantumRoboticsVisualizer(
                grid_size=(env.M, env.N), title=f"{request.map_id} β€” solved",
                convention="robotics" if render_format == "cartesian" else "matrix",
            )
            fig = visualizer.create_animated_plot(
                obstacles=env.obstacles,
                problem=problem,
                robot_paths={
                    num_to_id.get(num, str(num)): coords
                    for num, coords in robot_paths.items()
                },
            )
            response.figure = json.loads(fig.to_json())

        return response
    except Exception as e:
        raise HTTPException(500, f"Planning failed: {str(e)}")


if __name__ == "__main__":
    uvicorn.run(app, host="127.0.0.1", port=8000, reload=True)