from __future__ import annotations from dataclasses import asdict, dataclass, field from pathlib import Path from typing import Any, Dict, List import json @dataclass class QueueItem: ligand_id: str status: str = "active" priority: float = 0.0 hypercluster_id: int = -1 cluster_id: int = -1 times_selected: int = 0 last_score: float | None = None @dataclass class SchedulerState: queue: List[QueueItem] = field(default_factory=list) batch_history: List[Dict[str, Any]] = field(default_factory=list) round_idx: int = 0 def to_dict(self) -> Dict[str, Any]: return { "queue": [asdict(item) for item in self.queue], "batch_history": self.batch_history, "round_idx": self.round_idx, } @classmethod def from_dict(cls, payload: Dict[str, Any]) -> "SchedulerState": queue = [QueueItem(**item) for item in payload.get("queue", [])] return cls(queue=queue, batch_history=payload.get("batch_history", []), round_idx=int(payload.get("round_idx", 0))) def save(self, path: str | Path) -> Path: target = Path(path) target.parent.mkdir(parents=True, exist_ok=True) with target.open("w", encoding="utf-8") as handle: json.dump(self.to_dict(), handle, indent=2) return target @classmethod def load(cls, path: str | Path) -> "SchedulerState": source = Path(path) with source.open("r", encoding="utf-8") as handle: return cls.from_dict(json.load(handle))