diff --git a/pyproject.toml b/pyproject.toml index 627c98971..5408246d0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "envtorch" -version = "0.0.0" +version = "0.1.0" requires-python = ">=3.8" dependencies = [ "torch>=1.9.0", diff --git a/src/envtorch/core/__init__.py b/src/core/__init__.py similarity index 100% rename from src/envtorch/core/__init__.py rename to src/core/__init__.py diff --git a/src/core/base.py b/src/core/base.py new file mode 100644 index 000000000..b98080286 --- /dev/null +++ b/src/core/base.py @@ -0,0 +1,45 @@ +# Abstract base classes for EnvTorch +from __future__ import annotations + +from abc import ABC, abstractmethod +from typing import Generic, TypeVar + +from .types import StepResult + +# Generic type variables +ActT = TypeVar("ActT") # Type for the action sent to the environment +ObsT = TypeVar("ObsT") # Type for the observation returned by the environment + + +class BaseEnv(ABC, Generic[ActT, ObsT]): + """ + Abstract base class for all environments. + + Each environment must implement: + - reset(): to initialize or reinitialize environment state + - step(action): to execute an action and return results + - close(): to release resources (containers, sessions, etc.) + """ + + @abstractmethod + def reset(self) -> ObsT: + """ + Resets the environment to its initial state. + + Returns: + ObsT: The initial observation after resetting. + """ + raise NotImplementedError + + @abstractmethod + def step(self, action: ActT) -> StepResult[ObsT]: + """ + Performs one logical step in the environment. + + Args: + action (ActT): The action to perform in the environment. + + Returns: + StepResult[ObsT]: The resulting observation, reward, done flag, and info. + """ + raise NotImplementedError diff --git a/src/core/types.py b/src/core/types.py new file mode 100644 index 000000000..1abb168b3 --- /dev/null +++ b/src/core/types.py @@ -0,0 +1,24 @@ +# Type definitions for EnvTorch +from dataclasses import dataclass +from typing import Any, Generic, Optional, TypeVar + +# Generic type for observations +ObsT = TypeVar("ObsT") # TypeVar for typehinting in IDEs + + +@dataclass +class StepResult(Generic[ObsT]): + """ + Represents the result of one environment step. + + Attributes: + observation: The environment's observation after the action. + reward: Scalar reward for this step (optional). + done: Whether the episode is finished. + info: Additional metadata (e.g. debug info, latency, etc.). + """ + + observation: ObsT + reward: Optional[float] = None + done: bool = False + info: Optional[dict[str, Any]] = None diff --git a/src/envs/__init__.py b/src/envs/__init__.py new file mode 100644 index 000000000..f631431b4 --- /dev/null +++ b/src/envs/__init__.py @@ -0,0 +1 @@ +# Environment implementations like CodingEnv here diff --git a/src/envtorch/envs/__init__.py b/src/envtorch/envs/__init__.py deleted file mode 100644 index 714a0f5b9..000000000 --- a/src/envtorch/envs/__init__.py +++ /dev/null @@ -1 +0,0 @@ -# Environment implementations