forked from UNN/2026-rff_mp
stats
созданы классы оркестратора и сбора статистики
This commit is contained in:
parent
451ae4ce4b
commit
80d51b3f93
|
|
@ -196,8 +196,42 @@ class AStarStrategy(PathFindingStrategy):
|
||||||
return []
|
return []
|
||||||
|
|
||||||
class SearchStats:
|
class SearchStats:
|
||||||
|
def __init__(self, time_ms: float, visited_cells: int, path_length: int):
|
||||||
|
self.time_ms = time_ms
|
||||||
|
self.visited_cells = visited_cells
|
||||||
|
self.path_length = path_length
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f"Stats(time={self.time_ms:.2f}ms, visited={self.visited_cells}, path_len={self.path_length})"
|
||||||
|
|
||||||
class MazeSolver:
|
class MazeSolver:
|
||||||
|
def __init__(self, maze: Maze, strategy: PathFindingStrategy):
|
||||||
|
self.maze = maze
|
||||||
|
self.strategy = strategy
|
||||||
|
self.observers = []
|
||||||
|
|
||||||
|
def set_strategy(self, strategy: PathFindingStrategy):
|
||||||
|
self.strategy = strategy
|
||||||
|
|
||||||
|
def attach(self, observer):
|
||||||
|
self.observers.append(observer)
|
||||||
|
|
||||||
|
def detach(self, observer):
|
||||||
|
self.observers.remove(observer)
|
||||||
|
|
||||||
|
def notify(self, event: str, data: Any = None):
|
||||||
|
for obs in self.observers:
|
||||||
|
obs.update(event, data)
|
||||||
|
|
||||||
|
def solve(self) -> Tuple[List[Cell], SearchStats]:
|
||||||
|
start_time = time.perf_counter()
|
||||||
|
path = self.strategy.find_path(self.maze, self.maze.start, self.maze.exit)
|
||||||
|
end_time = time.perf_counter()
|
||||||
|
elapsed_ms = (end_time - start_time) * 1000.0
|
||||||
|
visited_cells = getattr(self.strategy, 'last_visited', len(path) if path else 0)
|
||||||
|
stats = SearchStats(elapsed_ms, visited_cells, len(path))
|
||||||
|
self.notify("solved", {"path": path, "stats": stats})
|
||||||
|
return path, stats
|
||||||
|
|
||||||
class Observer(ABC):
|
class Observer(ABC):
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user