Skip to content

Reinforcement learning

GymnaxProblem

GymnaxProblem(env_name: str, policy: Module, episode_length: int | None = None, num_rollouts: int = 1, use_normalize_obs: bool = True, env_kwargs: dict | None = None, env_params: dict | None = None)

Bases: Problem

Gymnax Problem for Reinforcement Learning Optimization.

Evaluates the parameters of a policy network by rolling the policy out in a Gymnax environment. A solution is a PyTree of policy parameters.

Parameters:

  • env_name (str) –

    Name of the Gymnax environment, passed to gymnax.make.

  • policy (Module) –

    Flax module called as policy.apply(policy_params, obs, key) that returns the action for an observation, e.g. MLP from evosax.problems.networks.

  • episode_length (int | None, default: None ) –

    Number of environment steps per rollout. If None, the max_steps_in_episode of the environment parameters is used.

  • num_rollouts (int, default: 1 ) –

    Number of rollouts per population member. The fitness is averaged over them.

  • use_normalize_obs (bool, default: True ) –

    If True, observations are normalized with a running mean and standard deviation that are stored in the problem state and updated in every eval call.

  • env_kwargs (dict | None, default: None ) –

    Keyword arguments passed to gymnax.make.

  • env_params (dict | None, default: None ) –

    Fields that replace the default environment parameters returned by gymnax.make.

Note

Requires the optional gymnax package.

References

observation_space property

observation_space

Observation space of the environment.

action_space property

action_space

Action space of the environment.

observation_shape property

observation_shape

Observation shape of the environment.

action_shape property

action_shape

Action shape of the environment.

num_actions property

num_actions

Number of actions in the environment.

num_dims property

num_dims: int

Number of search dimensions, i.e. the total size of a solution.

init

init(key: Array) -> State

Initialize state with empty normalization statistics.

Parameters:

  • key (Array) –

    PRNG key.

Returns:

  • State –

    The initial problem state, with a counter of zero, an observation mean of zero and an observation standard deviation of one.

eval

eval(key: Array, solutions: Solution, state: State) -> tuple[Fitness, State, Metrics]

Evaluate a population of policies with environment rollouts.

Every member is rolled out num_rollouts times for episode_length steps. Rewards after the first episode end of a rollout are not counted. All members are evaluated with the same rollout keys.

Parameters:

  • key (Array) –

    PRNG key.

  • solutions (Solution) –

    Population of policy parameters, a PyTree with a leading population axis on every leaf.

  • state (State) –

    Current problem state.

Returns:

  • tuple[Fitness, State, Metrics] –

    A tuple (fitness, state, info). fitness is the episode return of each member averaged over its rollouts. It is not negated, so higher is better: negate it before passing it to tell, which minimizes. state has its counter incremented and, if use_normalize_obs, its observation statistics updated. info["env_states"] is a tuple (obs, env_state) with the observations and environment states of all steps; their leaves have the leading axes (population_size, num_rollouts, episode_length).

normalize_obs

normalize_obs(obs: PyTree, state: State) -> PyTree

Normalize observations using running statistics.

Parameters:

  • obs (PyTree) –

    PyTree of observations.

  • state (State) –

    Problem state holding the running mean and standard deviation.

Returns:

  • PyTree –

    The observations with the mean subtracted, divided by the standard deviation.

update_stats

update_stats(obs: PyTree, state: State) -> State

Update running statistics for observations using Welford's online algorithm.

This method implements a numerically stable algorithm for computing running mean and variance statistics across episodes.

Parameters:

  • obs (PyTree) –

    PyTree containing observations with shape (population_size, num_rollouts, episode_length, ...).

  • state (State) –

    Current state containing running statistics.

Returns:

  • State –

    Updated state with new observation statistics.

sample

sample(key: Array) -> Solution

Sample policy parameters.

Initializes policy on an observation sampled from the observation space.

Parameters:

  • key (Array) –

    PRNG key.

Returns:

  • Solution –

    A PyTree of policy parameters.

BraxProblem

BraxProblem(env_name: str, policy: Module, episode_length: int | None = None, num_rollouts: int = 1, use_normalize_obs: bool = True, env_kwargs: dict = {'backend': 'spring'})

Bases: Problem

Brax Problem for Reinforcement Learning Optimization.

Evaluates the parameters of a policy network by rolling the policy out in a Brax environment. A solution is a PyTree of policy parameters.

Parameters:

  • env_name (str) –

    Name of the Brax environment, passed to brax.envs.create.

  • policy (Module) –

    Flax module called as policy.apply(policy_params, obs, key) that returns the action for an observation, e.g. MLP from evosax.problems.networks.

  • episode_length (int | None, default: None ) –

    Number of environment steps per rollout, also passed to brax.envs.create. If None, the episode length of the created environment is used.

  • num_rollouts (int, default: 1 ) –

    Number of rollouts per population member. The fitness is averaged over them.

  • use_normalize_obs (bool, default: True ) –

    If True, the running mean and standard deviation used to normalize observations are updated in every eval call. If False, they keep their initial values (mean zero, standard deviation one).

  • env_kwargs (dict, default: {'backend': 'spring'} ) –

    Additional keyword arguments passed to brax.envs.create, e.g. the physics backend.

Note

Requires the optional brax package.

References

observation_shape property

observation_shape

Observation shape of the environment.

action_shape property

action_shape

Action shape of the environment.

num_dims property

num_dims: int

Number of search dimensions, i.e. the total size of a solution.

init

init(key: Array) -> State

Initialize state with empty normalization statistics.

Parameters:

  • key (Array) –

    PRNG key.

Returns:

  • State –

    The initial problem state, with a counter of zero, an observation mean of zero and an observation standard deviation of one.

eval

eval(key: Array, solutions: Population, state: State) -> tuple[Fitness, State, Metrics]

Evaluate a population of policies with environment rollouts.

Every member is rolled out num_rollouts times for episode_length steps. Rewards after the first episode end of a rollout are not counted. All members are evaluated with the same rollout keys.

Parameters:

  • key (Array) –

    PRNG key.

  • solutions (Population) –

    Population of policy parameters, a PyTree with a leading population axis on every leaf.

  • state (State) –

    Current problem state.

Returns:

  • tuple[Fitness, State, Metrics] –

    A tuple (fitness, state, info). fitness is the episode return of each member averaged over its rollouts. It is not negated, so higher is better: negate it before passing it to tell, which minimizes. state has its counter incremented and, if use_normalize_obs, its observation statistics updated. info["env_states"] holds the Brax environment states of all steps; their leaves have the leading axes (population_size, num_rollouts, episode_length).

normalize_obs

normalize_obs(obs: PyTree, state: State) -> PyTree

Normalize observations using running statistics.

Parameters:

  • obs (PyTree) –

    PyTree of observations.

  • state (State) –

    Problem state holding the running mean and standard deviation.

Returns:

  • PyTree –

    The observations with the mean subtracted, divided by the standard deviation.

update_stats

update_stats(obs: PyTree, state: State) -> State

Update running statistics for observations using Welford's online algorithm.

This method implements a numerically stable algorithm for computing running mean and variance statistics across episodes, following the Brax running statistics.

Parameters:

  • obs (PyTree) –

    PyTree containing observations with shape (population_size, num_rollouts, episode_length, ...).

  • state (State) –

    Current state containing running statistics.

Returns:

  • State –

    Updated state with new observation statistics.

sample

sample(key: Array) -> Solution

Sample policy parameters.

Initializes policy on the observation of a freshly reset environment.

Parameters:

  • key (Array) –

    PRNG key.

Returns:

  • Solution –

    A PyTree of policy parameters.