hospital-ed / agents /random_agent.py
testingaccc's picture
Upload folder using huggingface_hub
0fe00d1 verified
Raw
History Blame Contribute Delete
1.37 kB
"""Random baseline agent for the hospital environment."""
from __future__ import annotations
from typing import Optional
import numpy as np
from hospital_env import HospitalEnv
class RandomAgent:
"""Uniformly samples an action from ``Discrete(num_actions)``.
Intended purely as a baseline — serves as the lower bound against
which the heuristic and PPO agents are compared in the grader.
"""
def __init__(
self,
num_actions: int = HospitalEnv.NUM_ACTIONS,
seed: Optional[int] = None,
) -> None:
self.num_actions = int(num_actions)
self.rng = np.random.default_rng(seed)
def act(self, obs: dict) -> int:
"""Return a uniformly random action in ``[0, num_actions)``."""
return int(self.rng.integers(0, self.num_actions))
def __call__(self, obs: dict) -> int:
return self.act(obs)
def reset(self, seed: Optional[int] = None) -> None:
"""Re-seed the underlying RNG."""
self.rng = np.random.default_rng(seed)
def _main() -> None:
"""CLI entry point: grade a :class:`RandomAgent` and dump JSON."""
import json
from grader import Grader
agent = RandomAgent(seed=0)
grader = Grader(n_episodes_per_scenario=3)
scores = grader.grade(agent)
print(json.dumps(scores, indent=2))
if __name__ == "__main__":
_main()