from abc import ABC, abstractmethod import re from typing import Optional, List, Tuple, Any, Dict class BaseEnv(ABC): """ Abstract base class for all environments. The class needs to handle text-based input, input may be invalid - Environment will track the total reward for the trajectory """ def __init__(self): pass @abstractmethod def reset(self, seed=None, **kwargs) -> Any: """ Reset the environment. NOTE: the environment should be same for the same seed Returns: rendered environment """ pass @abstractmethod def step(self, action) -> Tuple[Any, float, bool, Dict]: """ Execute one step in the environment. NOTE should also handle predefined invalid action (0) Args: action: Action to take, must be in action space, or default invalid action Returns: observation (rendered environment), reward, done, info """ pass # below are optional methods def render(self, mode: str = 'text') -> Any: """Render the environment. Optional method.""" pass def compute_reward(self, action, **kwargs) -> float: """Compute reward for the action.""" pass def close(self): """Close the environment.""" pass class BaseDiscreteActionEnv(BaseEnv, ABC): """ Abstract base class for environments with discrete action spaces This class provides common functionality for environments like FrozenLakeEnv and SokobanEnv. """ @abstractmethod def step(self, action: int) -> Tuple[Any, float, bool, Dict]: """ Execute one step in the environment. Args: action: Action to take, must be in action space, or default invalid action Returns: observation (rendered environment), reward, done, info """ pass @abstractmethod def get_all_actions(self) -> List[int]: """Get list of all valid actions.""" pass class BaseLanguageBasedEnv(BaseEnv, ABC): """ Abstract base class for environments with language-based action space environment This class provides common functionality for environments like countdown from TinyZero """ @abstractmethod def step(self, action: str) -> Tuple[Any, float, bool, Dict]: """ Execute one step in the environment. Args: action: Action to take, must be in action space, or default invalid action Returns: observation (rendered environment), reward, done, info """ pass class BaseEnvConfig(ABC): """ Abstract base class for environment configurations. """ def __init__(self): self.invalid_act = "" self.invalid_act_score = 0