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.MLPfromevosax.problems.networks. -
episode_length(int | None, default:None) –Number of environment steps per rollout. If None, the
max_steps_in_episodeof 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
evalcall. -
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
init ¶
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 ¶
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).fitnessis the episode return of each member averaged over its rollouts. It is not negated, so higher is better: negate it before passing it totell, which minimizes.statehas its counter incremented and, ifuse_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 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 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 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.MLPfromevosax.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
evalcall. 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 physicsbackend.
Note
Requires the optional brax package.
References
init ¶
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 ¶
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).fitnessis the episode return of each member averaged over its rollouts. It is not negated, so higher is better: negate it before passing it totell, which minimizes.statehas its counter incremented and, ifuse_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 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 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 policy parameters.
Initializes policy on the observation of a freshly reset environment.
Parameters:
-
key(Array) –PRNG key.
Returns:
-
Solution–A PyTree of policy parameters.