File size: 1,213 Bytes
cb330aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""
Simple random comparison agent.
"""

import random
from typing import Optional, Protocol

from env.models import Action, ActionPayload, Observation, StepResult

ACTION_TYPES = ("execute", "delay", "reallocate")


class SupportsEpisodeEnv(Protocol):
    def reset(self) -> Observation:
        ...

    def step(self, action: Action) -> StepResult:
        ...


def select_random_action(obs: Observation, rng: Optional[random.Random] = None) -> Action:
    rng = rng or random.Random()
    task_ids = [task.id for task in obs.tasks]
    if not task_ids:
        return Action(action_type="delay", task_id=0)

    return Action(
        action_type=rng.choice(ACTION_TYPES),
        task_id=rng.choice(task_ids),
    )


def run_episode(
    env: SupportsEpisodeEnv,
    max_steps: int = 30,
    rng: Optional[random.Random] = None,
) -> list[ActionPayload]:
    rng = rng or random.Random(42)
    obs = env.reset()
    actions_taken: list[ActionPayload] = []

    for _ in range(max_steps):
        if obs.episode_done:
            break
        action = select_random_action(obs, rng=rng)
        actions_taken.append(action.model_dump())
        obs = env.step(action).observation

    return actions_taken