route optimizer agent with dailygrubs ai assign

This commit is contained in:
2026-09-01 13:48:58 +05:30
parent aa4d4c6549
commit 90cdb57a38
28 changed files with 3281 additions and 135 deletions

View File

@@ -117,9 +117,12 @@ class FleetAgent(SpecializedAgent):
vehicle_id = suitable_vehicles[0]
vehicle = self._vehicles[vehicle_id]
was_available = vehicle["status"] == "available"
vehicle["status"] = "assigned"
if vehicle["hub"] in self._hub_capacity:
# Only move the counter when the availability boundary is actually
# crossed, so it can't drift out of [0, total].
if was_available and vehicle["hub"] in self._hub_capacity:
self._hub_capacity[vehicle["hub"]]["available"] -= 1
assignment_id = f"ASN-{uuid.uuid4().hex[:8].upper()}"
@@ -160,11 +163,20 @@ class FleetAgent(SpecializedAgent):
return {"status": "error", "message": f"Vehicle {vehicle_id} not found"}
vehicle = self._vehicles[vehicle_id]
was_available = vehicle["status"] == "available"
vehicle["status"] = "available"
if vehicle["hub"] in self._hub_capacity:
# Only increment when the vehicle was actually unavailable, so a
# double-release (or releasing an available vehicle) can't exceed total.
if not was_available and vehicle["hub"] in self._hub_capacity:
self._hub_capacity[vehicle["hub"]]["available"] += 1
# Close any active assignment for this vehicle so it isn't reported as
# still on the road (and stale 'active' rows don't accumulate).
for assignment in self._assignments.values():
if assignment.vehicle_id == vehicle_id and assignment.status == "active":
assignment.status = "returned"
logger.info(f"Vehicle {vehicle_id} released")
return {"status": "released", "vehicle_id": vehicle_id, "hub": vehicle["hub"]}
@@ -239,7 +251,12 @@ class FleetAgent(SpecializedAgent):
else:
self._maintenance_schedule[vehicle_id] = datetime.now() + timedelta(days=3)
self._vehicles[vehicle_id]["status"] = "maintenance"
# Taking a vehicle out of service reduces availability (only if it was
# counted as available), keeping hub capacity accurate.
vehicle = self._vehicles[vehicle_id]
if vehicle["status"] == "available" and vehicle["hub"] in self._hub_capacity:
self._hub_capacity[vehicle["hub"]]["available"] -= 1
vehicle["status"] = "maintenance"
return {
"status": "scheduled",