Skip to main content

Environments

The Environment class defines the world in which agents operate. It provides observations to agents, processes their actions, manages state, and determines rewards. Creating custom environments is essential for training and evaluating agents.

Environment Class

The Environment class is defined in neurenix/agent/environment.py and provides the base interface for all environments.

Constructor

Parameters: None (base class)

Properties

state

Get the current state of the environment (returns a copy).
Returns: Dict[str, Any] - Copy of the current environment state

agents

Get the agents registered with the environment (returns a copy).
Returns: Dict[str, Any] - Dictionary mapping agent IDs to agents

Core Methods

reset()

Reset the environment to its initial state.
Returns: Dict[str, Any] - Initial state of the environment

step(actions)

Apply actions to the environment and update its state. This method must be implemented by subclasses.
Parameters:
  • actions (Dict[str, Any]): Dictionary mapping agent IDs to their actions
Returns: Dict[str, Any] - Dictionary containing:
  • rewards (dict): Dictionary mapping agent IDs to their rewards
  • done (bool): Whether the episode is complete
  • info (dict): Additional information
Raises: NotImplementedError if not overridden in subclass

observe(agent)

Get an observation of the environment for a specific agent. This method must be implemented by subclasses.
Parameters:
  • agent (Any): The agent requesting the observation
Returns: Dict[str, Any] - Observation for the agent Raises: NotImplementedError if not overridden in subclass

register_agent(agent)

Register an agent with the environment.
Parameters:
  • agent (Any): The agent to register
Returns: None

unregister_agent(agent_id)

Unregister an agent from the environment.
Parameters:
  • agent_id (str): ID of the agent to unregister
Returns: None

Creating Custom Environments

Grid World Environment

Create a simple grid-based environment:

Continuous Environment

Create an environment with continuous state and action spaces:

Multi-Agent Resource Collection

Create an environment where agents collect resources:

Best Practices

1. Override _get_initial_state()

Define your environment’s initial state:

2. Return Proper Step Results

Always return a dictionary with rewards, done, and info:

3. Agent-Specific Observations

Provide observations tailored to each agent’s perspective:

4. Handle Agent Registration

Register agents if your environment needs to track them:

5. Maintain State Immutability

The state property returns a copy for safety:

API Reference

Environment

Source: neurenix/agent/environment.py:7

See Also