Initial commit: Autonomous Mobile Robot (AMR) warehouse fleet simulator & OpenUSD digital twin

This commit is contained in:
om_karande
2026-10-26 12:38:52 +05:30
commit eee8d11800
12 changed files with 10986 additions and 0 deletions
+169
View File
@@ -0,0 +1,169 @@
import heapq, random
from collections import defaultdict
W, H = 20, 12
PICKUPS = [(x, y) for x in range(3, 17) for y in (4, 8)]
DROP = (19, 5)
CHARGERS = [(0, 11), (19, 11)]
STRATEGIES = ("baseline", "congestion", "optimized")
LOAD_TIME, UNLOAD_TIME = 3, 5 # seconds spent loading at a pickup / unloading at the dock
YIELD_AFTER = 15 # blocked this long -> a dodging robot backs off to a free cell
STATIONARY = ("LOADING", "UNLOADING", "CHARGING", "IDLE")
def make_grid():
g = [[0] * W for _ in range(H)]
for x in range(3, 17):
for y in (2, 3, 6, 7): # shelf rows (blocked cells)
g[y][x] = 1
return g
def manhattan(a, b):
return abs(a[0] - b[0]) + abs(a[1] - b[1])
def free_neighbors(grid, pos, occ):
out = []
for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)):
n = (pos[0] + dx, pos[1] + dy)
if 0 <= n[0] < W and 0 <= n[1] < H and not grid[n[1]][n[0]] and n not in occ:
out.append(n)
return out
def astar(grid, start, goal, extra):
pq, came, best = [(manhattan(start, goal), 0, start, None)], {}, {start: 0}
while pq:
_, g, cur, parent = heapq.heappop(pq)
if cur in came:
continue
came[cur] = parent
if cur == goal:
break
for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)):
n = (cur[0] + dx, cur[1] + dy)
if not (0 <= n[0] < W and 0 <= n[1] < H) or grid[n[1]][n[0]]:
continue
c = g + 1 + extra.get(n, 0)
if c < best.get(n, 1e9):
best[n] = c
heapq.heappush(pq, (c + manhattan(n, goal), c, n, cur))
path, cur = [], goal
while cur is not None:
path.append(cur)
cur = came[cur]
return path[::-1]
class Robot:
def __init__(self, i, pos):
self.id, self.pos, self.goal = i, pos, pos
self.path, self.stage = [], "IDLE"
self.battery, self.wait, self.blocked, self.streak, self.timer = 100.0, 0, 0, 0, 0
def priority(self):
# loaded robots beat charging-bound robots, which beat empty robots; ties broken by id
rank = 2 if self.stage == "TO_DROP" else 1 if self.stage == "TO_CHARGER" else 0
return (rank, -self.id)
def run(n, strategy, steps=3600, seed=1, record=False):
rng = random.Random(seed)
pool = [rng.choice(PICKUPS) for _ in range(5000)] # same demand for every strategy
rng2 = random.Random(seed + 1000)
grid = make_grid()
robots = [Robot(i, (0, i)) for i in range(n)]
occ = {r.pos: r for r in robots}
patience = 10 if strategy == "baseline" else 2 # how fast a robot reroutes when blocked
penalty = 0 if strategy == "baseline" else 5 # congestion-aware cost on occupied cells
done = replans = charge_steps = yields = 0
heat, frames = defaultdict(int), []
for t in range(steps):
for r in robots:
if r.stage == "IDLE":
if r.battery < 25:
r.goal = min(CHARGERS, key=lambda c: manhattan(c, r.pos))
r.stage = "TO_CHARGER"
else:
if strategy == "optimized": # scheduling: nearest of the next 5 tasks
j = min(range(5), key=lambda k: manhattan(pool[k], r.pos))
r.goal = pool.pop(j)
else: # first come, first served
r.goal = pool.pop(0)
r.stage = "TO_PICK"
r.path = astar(grid, r.pos, r.goal, {p: penalty for p in occ})[1:]
elif r.stage == "CHARGING":
charge_steps += 1
r.battery = min(100.0, r.battery + 1)
if r.battery >= 100:
r.stage = "IDLE"
elif r.stage in ("LOADING", "UNLOADING"):
r.timer -= 1
if r.timer <= 0:
if r.stage == "LOADING":
r.goal, r.stage = DROP, "TO_DROP"
r.path = astar(grid, r.pos, DROP, {})[1:]
else:
done += 1
r.stage = "IDLE"
elif r.path:
nxt = r.path[0]
# Admission control (optimized only): hold back at distance 3 from the dock
# while 2+ robots are already within 2 cells of it, so the dock never gridlocks.
if (strategy == "optimized" and r.stage == "TO_DROP"
and manhattan(r.pos, DROP) == 3
and sum(1 for o in robots if o is not r and manhattan(o.pos, DROP) <= 2) >= 2):
r.wait += 1
heat[r.pos] += 1
continue
if nxt in occ: # blocked: wait
blocker = occ[nxt]
r.blocked += 1
r.streak += 1
r.wait += 1
heat[r.pos] += 1
# Priority rule: only the lower-priority robot (or anyone blocked by a
# stationary robot) dodges; the higher-priority robot simply waits.
may_dodge = blocker.stage in STATIONARY or r.priority() < blocker.priority()
limit = YIELD_AFTER if may_dodge else YIELD_AFTER * 3
if r.streak >= limit: # deadlock recovery: back off to a free cell
free = free_neighbors(grid, r.pos, occ)
if free:
nxt2 = rng2.choice(free)
del occ[r.pos]
r.pos = nxt2
occ[nxt2] = r
r.path = astar(grid, r.pos, r.goal, {p: 50 for p in occ if p != r.pos})[1:]
r.blocked = r.streak = 0
yields += 1
continue
if may_dodge and r.blocked >= patience: # reroute around the blocker
r.path = astar(grid, r.pos, r.goal,
{p: 50 for p in occ if p != r.pos})[1:]
r.blocked = 0
replans += 1
else: # move one cell
del occ[r.pos]
r.pos = nxt
occ[nxt] = r
r.path.pop(0)
r.blocked = r.streak = 0
r.battery -= 0.05
else: # arrived
if r.stage == "TO_PICK":
r.stage, r.timer = "LOADING", LOAD_TIME
elif r.stage == "TO_DROP":
r.stage, r.timer = "UNLOADING", UNLOAD_TIME
elif r.stage == "TO_CHARGER":
r.stage = "CHARGING"
if record:
frames.append([(r.pos, r.stage, round(r.battery)) for r in robots])
total_wait = sum(r.wait for r in robots)
return {"strategy": strategy, "robots": n, "seed": seed,
"tasks_per_hour": done * 3600 / steps, "total_wait": total_wait,
"wait_per_task": total_wait / max(done, 1), "replans": replans,
"yields": yields, "charge_steps": charge_steps,
"heat": dict(heat), "frames": frames}
if __name__ == "__main__":
for s in STRATEGIES:
r = run(6, s)
print(f"{s:11s} tasks/hr={r['tasks_per_hour']:.0f} wait/task={r['wait_per_task']:.1f}s")