Upload model.py
Browse files
model.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Literal, TypedDict, List
|
| 2 |
+
|
| 3 |
+
Priority = Literal["low", "normal", "high", "critical"]
|
| 4 |
+
|
| 5 |
+
class Task(TypedDict):
|
| 6 |
+
task_id: str
|
| 7 |
+
task_type: str
|
| 8 |
+
priority: Priority
|
| 9 |
+
source: str # user, schedule, sensor, system
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class ScheduledTask(TypedDict):
|
| 13 |
+
task_id: str
|
| 14 |
+
order: int
|
| 15 |
+
reason: str
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def schedule_tasks(tasks: List[Task]) -> List[ScheduledTask]:
|
| 19 |
+
"""Order tasks using simple priority and source rules.
|
| 20 |
+
|
| 21 |
+
- critical > high > normal > low
|
| 22 |
+
- user > sensor > schedule > system (within same priority)
|
| 23 |
+
"""
|
| 24 |
+
pr_order = {"critical": 0, "high": 1, "normal": 2, "low": 3}
|
| 25 |
+
src_order = {"user": 0, "sensor": 1, "schedule": 2, "system": 3}
|
| 26 |
+
|
| 27 |
+
sorted_tasks = sorted(
|
| 28 |
+
tasks,
|
| 29 |
+
key=lambda t: (
|
| 30 |
+
pr_order.get(t["priority"], 2),
|
| 31 |
+
src_order.get(t["source"], 3),
|
| 32 |
+
),
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
result: List[ScheduledTask] = []
|
| 36 |
+
for idx, t in enumerate(sorted_tasks, start=1):
|
| 37 |
+
reason = f"priority={t['priority']}, source={t['source']} -> position {idx}."
|
| 38 |
+
result.append({"task_id": t["task_id"], "order": idx, "reason": reason})
|
| 39 |
+
|
| 40 |
+
return result
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
if __name__ == "__main__":
|
| 44 |
+
sample: List[Task] = [
|
| 45 |
+
{"task_id": "T_CLEAN_KITCHEN", "task_type": "clean_room", "priority": "high", "source": "schedule"},
|
| 46 |
+
{"task_id": "T_REMIND_TRASH", "task_type": "reminder", "priority": "high", "source": "sensor"},
|
| 47 |
+
{"task_id": "T_PATROL_NIGHT", "task_type": "patrol", "priority": "normal", "source": "schedule"},
|
| 48 |
+
{"task_id": "T_STATUS", "task_type": "status_report", "priority": "low", "source": "system"},
|
| 49 |
+
{"task_id": "T_CLEAN_LIVING", "task_type": "clean_room", "priority": "critical", "source": "user"},
|
| 50 |
+
]
|
| 51 |
+
for st in schedule_tasks(sample):
|
| 52 |
+
print(st)
|