feat: initial maze-ml GA visualizer
This commit is contained in:
130
visualizer.py
Normal file
130
visualizer.py
Normal file
@@ -0,0 +1,130 @@
|
||||
import pygame
|
||||
import numpy as np
|
||||
from config import MazeConfig
|
||||
from maze import render_static
|
||||
|
||||
class Visualizer:
|
||||
def __init__(self, screen: pygame.Surface, grid: np.ndarray, cfg: MazeConfig):
|
||||
self.screen = screen
|
||||
self.grid = grid
|
||||
self.cfg = cfg
|
||||
self.maze_w = cfg.maze_cols * cfg.cell_size
|
||||
self.maze_h = cfg.maze_rows * cfg.cell_size
|
||||
self.hud_x = self.maze_w + 10
|
||||
# Pre-render static maze
|
||||
self.maze_surface = render_static(grid, cfg)
|
||||
# Trail surface with per-pixel alpha
|
||||
self.trail_surf = pygame.Surface((self.maze_w, self.maze_h), pygame.SRCALPHA)
|
||||
self.trail_surf.fill((0, 0, 0, 0))
|
||||
# Font
|
||||
pygame.font.init()
|
||||
self.font = pygame.font.SysFont("monospace", cfg.hud_font_size)
|
||||
self.font_sm = pygame.font.SysFont("monospace", cfg.hud_font_size - 4)
|
||||
self.clock = pygame.time.Clock()
|
||||
self._goal_flash = 0
|
||||
self._first_goal = False
|
||||
|
||||
def _rank_color(self, rank: int, total: int) -> tuple[int, int, int]:
|
||||
t = rank / max(1, total - 1)
|
||||
return (int(255 * t), int(255 * (1.0 - t)), 30)
|
||||
|
||||
def _fade_trails(self):
|
||||
subtract = max(1, int(255 * (1.0 - self.cfg.trail_decay)))
|
||||
fade = pygame.Surface((self.maze_w, self.maze_h), pygame.SRCALPHA)
|
||||
fade.fill((0, 0, 0, subtract))
|
||||
self.trail_surf.blit(fade, (0, 0), special_flags=pygame.BLEND_RGBA_SUB)
|
||||
|
||||
def _draw_hud(self, gen: int, best_fitness: float, step: int, alive: int, reached: int):
|
||||
# Dark sidebar background
|
||||
sidebar_rect = pygame.Rect(self.maze_w, 0, 200, self.maze_h)
|
||||
pygame.draw.rect(self.screen, (10, 10, 16), sidebar_rect)
|
||||
|
||||
lines = [
|
||||
("GEN", f"{gen:>4}"),
|
||||
("STEP", f"{step:>4}/{self.cfg.max_steps}"),
|
||||
("ALIVE", f"{alive:>4}/{self.cfg.population}"),
|
||||
("GOAL", f"{reached:>4}"),
|
||||
("BEST", f"{best_fitness:>8.1f}"),
|
||||
]
|
||||
y = 20
|
||||
accent = (0, 220, 200)
|
||||
for label, value in lines:
|
||||
lbl_surf = self.font_sm.render(label, True, (100, 100, 120))
|
||||
val_surf = self.font.render(value, True, accent)
|
||||
self.screen.blit(lbl_surf, (self.hud_x, y))
|
||||
self.screen.blit(val_surf, (self.hud_x, y + 16))
|
||||
y += 52
|
||||
|
||||
# Legend
|
||||
y += 20
|
||||
legend_label = self.font_sm.render("RANK", True, (80, 80, 100))
|
||||
self.screen.blit(legend_label, (self.hud_x, y))
|
||||
y += 18
|
||||
bar_w = 170
|
||||
for i in range(bar_w):
|
||||
t = i / bar_w
|
||||
r, g = int(255 * t), int(255 * (1 - t))
|
||||
pygame.draw.line(self.screen, (r, g, 30), (self.hud_x + bar_w - i, y), (self.hud_x + bar_w - i, y + 10))
|
||||
best_lbl = self.font_sm.render("best", True, (0, 255, 30))
|
||||
wrst_lbl = self.font_sm.render("worst", True, (255, 0, 30))
|
||||
self.screen.blit(best_lbl, (self.hud_x, y + 14))
|
||||
self.screen.blit(wrst_lbl, (self.hud_x + bar_w - 35, y + 14))
|
||||
|
||||
def render(self, agents, gen: int, step: int):
|
||||
# Sort by fitness for ranking colors
|
||||
sorted_agents = sorted(agents, key=lambda a: a.fitness)
|
||||
|
||||
# Check first goal
|
||||
reached = sum(1 for a in agents if a.reached_goal)
|
||||
if reached > 0 and not self._first_goal:
|
||||
self._first_goal = True
|
||||
self._goal_flash = 45
|
||||
|
||||
# Blit static maze
|
||||
self.screen.blit(self.maze_surface, (0, 0))
|
||||
|
||||
# Fade and draw trails
|
||||
self._fade_trails()
|
||||
for rank, agent in enumerate(sorted_agents):
|
||||
if agent.trail:
|
||||
color = self._rank_color(rank, len(sorted_agents))
|
||||
px, py = agent.trail[-1]
|
||||
pygame.draw.circle(self.trail_surf, (*color, 130), (px, py), 3)
|
||||
self.screen.blit(self.trail_surf, (0, 0))
|
||||
|
||||
# Draw agents on top
|
||||
for rank, agent in enumerate(sorted_agents):
|
||||
color = self._rank_color(rank, len(sorted_agents))
|
||||
px = agent.col * self.cfg.cell_size + self.cfg.cell_size // 2
|
||||
py = agent.row * self.cfg.cell_size + self.cfg.cell_size // 2
|
||||
radius = 5 if agent.reached_goal else 3
|
||||
pygame.draw.circle(self.screen, color, (px, py), radius)
|
||||
if agent.reached_goal:
|
||||
pygame.draw.circle(self.screen, (255, 255, 255), (px, py), radius + 2, 1)
|
||||
|
||||
# Goal flash overlay
|
||||
if self._goal_flash > 0:
|
||||
gx = (self.cfg.maze_cols - 1) * self.cfg.cell_size + self.cfg.cell_size // 2
|
||||
gy = (self.cfg.maze_rows - 1) * self.cfg.cell_size + self.cfg.cell_size // 2
|
||||
alpha = int(200 * self._goal_flash / 45)
|
||||
flash_surf = pygame.Surface((self.cfg.cell_size, self.cfg.cell_size), pygame.SRCALPHA)
|
||||
flash_surf.fill((255, 220, 0, alpha))
|
||||
self.screen.blit(flash_surf, (gx - self.cfg.cell_size//2, gy - self.cfg.cell_size//2))
|
||||
self._goal_flash -= 1
|
||||
|
||||
# HUD
|
||||
best_fit = max((a.fitness for a in agents), default=0.0)
|
||||
alive = sum(1 for a in agents if not a.reached_goal)
|
||||
self._draw_hud(gen, best_fit, step, alive, reached)
|
||||
|
||||
pygame.display.flip()
|
||||
|
||||
def reset_trails(self):
|
||||
self.trail_surf.fill((0, 0, 0, 0))
|
||||
self._first_goal = False
|
||||
|
||||
def new_maze(self, grid, cfg):
|
||||
self.grid = grid
|
||||
self.cfg = cfg
|
||||
self.maze_surface = render_static(grid, cfg)
|
||||
self.reset_trails()
|
||||
Reference in New Issue
Block a user