Skip to content

Commit e86a327

Browse files
committed
add pygame visualization
1 parent b127758 commit e86a327

1 file changed

Lines changed: 91 additions & 0 deletions

File tree

src/envs/gui/run_example.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Create cluster
2+
from time import sleep
3+
4+
import pygame
5+
6+
from src.cluster.implementation.metric_based import MetricClusterCreator
7+
from src.envs import BasicClusterEnv
8+
from src.envs.actions import EnvironmentAction
9+
from src.envs.gui.cluster.metric_cluster_gui import ClusterMetricRenderer
10+
from src.envs.utils.info_builders.base import BaceClusterInformationExtractor
11+
from src.envs.utils.observation_extractors.metric_observation_extractor import (
12+
MetricClusterObservationCreator,
13+
)
14+
from src.envs.utils.reward_caculators.base import DifferentInPendingJobsRewardCaculator
15+
from src.scheduler.random_scheduler import RandomScheduler
16+
17+
cluster = MetricClusterCreator.generate_default(
18+
n_machines=6, n_jobs=20, n_resources=3, n_ticks=50, is_offline=True, seed=42
19+
)
20+
21+
env = BasicClusterEnv(
22+
cluster,
23+
DifferentInPendingJobsRewardCaculator(),
24+
BaceClusterInformationExtractor(),
25+
MetricClusterObservationCreator(),
26+
)
27+
28+
with ClusterMetricRenderer(
29+
display_size=(1400, 900), # Larger window
30+
cell_size=10,
31+
machine_spacing=10,
32+
) as renderer:
33+
current_obs, current_info = env.reset()
34+
print(current_obs["machines"].shape[-1])
35+
terminated, truncated = False, False
36+
37+
scheduler = RandomScheduler(cluster.is_allocation_possible)
38+
clock = pygame.time.Clock()
39+
40+
print("Starting cluster simulation...")
41+
print(f"Total jobs: {len(cluster._jobs)}")
42+
print(f"Total machines: {len(cluster._machines)}")
43+
44+
step_count = 0
45+
46+
while not terminated:
47+
# Render current state
48+
renderer.render(current_info, current_obs)
49+
50+
# Handle pygame events
51+
for event in pygame.event.get():
52+
if event.type == pygame.QUIT:
53+
print("User closed window")
54+
break
55+
56+
machines = cluster._machines
57+
jobs = cluster._jobs
58+
59+
# Log current status
60+
status_counts = {}
61+
for job in jobs:
62+
status_name = job.status.name
63+
status_counts[status_name] = status_counts.get(status_name, 0) + 1
64+
65+
print(f"Step {step_count} - Job statuses: {status_counts}")
66+
67+
# Schedule next action
68+
schedule_result = scheduler.schedule(machines, jobs)
69+
70+
if schedule_result is None:
71+
# No allocation possible, skip action
72+
action = EnvironmentAction(
73+
True, (-1, -1)
74+
) # Or however your env expects "no-op" action
75+
print(f"Step {step_count}: No allocation possible, skipping")
76+
else:
77+
machine_idx, job_idx = schedule_result
78+
action = EnvironmentAction(False, (machine_idx, job_idx))
79+
print(
80+
f"Step {step_count}: Scheduling Job {job_idx} to Machine {machine_idx}"
81+
)
82+
83+
current_obs, reward, terminated, truncated, current_info = env.step(action)
84+
85+
print(f"Step {step_count}: Reward = {reward:.2f}")
86+
87+
# Control frame rate
88+
clock.tick(2) # 2 FPS for slower visualization
89+
90+
step_count += 1
91+
renderer.render(current_info, current_obs)

0 commit comments

Comments
 (0)