harbor run -t openthoughts/tasktrove-exp-flat25-stackoverflow-v2-5k-stackoverflow-0291harbor download openthoughts/tasktrove-exp-flat25-stackoverflow-v2-5k-stackoverflow-0291Question: Implement an abstract base class AgentInterface with input validation in blobrl/agents.py for RL agents
Context I’m building a reinforcement learning framework and need a robust abstract base class AgentInterface that all concrete agents can subclass. Tests drive a MOCKAgentInterface subclass, so the base class must enforce a consistent API and validate inputs reliably.
Requirements to implement
Acceptance criteria
Deliverable
Example usage (illustrative; not required to copy exactly)
# blobrl/agents.py (base)
from abc import ABC, abstractmethod
from gym import spaces
import torch
class AgentInterface(ABC):
@abstractmethod
def get_action(self, observation):
pass
@abstractmethod
def enable_exploration(self):
pass
@abstractmethod
def disable_exploration(self):
pass
@abstractmethod
def learn(self, observation, action, reward, next_observation, done) -> None:
pass
@abstractmethod
def episode_finished(self):
pass
@abstractmethod
def save(self, file_name, dire_name="."):
pass
@classmethod
@abstractmethod
def load(cls, file_name, dire_name=".", device=None):
pass
@abstractmethod
def __str__(self):
pass
def __init__(self, observation_space, action_space, device):
if not isinstance(observation_space, spaces.Space):
raise TypeError("observation_space must be a gym.spaces.Space")
if not isinstance(action_space, spaces.Space):
raise TypeError("action_space must be a gym.spaces.Space")
if device is None:
self.device = torch.device("cpu")
else:
if not isinstance(device, torch.device):
raise TypeError("device must be a torch.device or None")
self.device = device
self.observation_space = observation_space
self.action_space = action_space
# tests/example_mocks.py (conceptual)
from blobrl.agents import AgentInterface
class MOCKAgentInterface(AgentInterface):
def __init__(self, observation_space, action_space, device=None):
super().__init__(observation_space, action_space, device)
def get_action(self, observation):
return None
def enable_exploration(self): pass
def disable_exploration(self): pass
def learn(self, observation, action, reward, next_observation, done): pass
def episode_finished(self): pass
def save(self, file_name, dire_name="."): pass
@classmethod
def load(cls, file_name, dire_name=".", device=None): return cls(None, None, device)
def __str__(self): return "MOCKAgentInterface"
Notes
If someone can provide a clean, production-ready implementation that matches these constraints (and handles nested gym spaces gracefully), or point out any pitfalls in this approach, I’d greatly appreciate it.