Behavior-regulated policy gradients¶
BRPG-MAP-Elites learns how an action affects both reward and behavior. A reward critic trains on the environment reward. A separate estimator learns discounted timestep features. The actor follows a behavior compliance gradient and removes the component of the reward gradient that would oppose it.
Install the optional RL dependencies using pip install "qdpy[rl]" or
uv add "qdpy[rl]". Use a single Gymnasium environment with finite
Box bounds. The example below controls a scalar actuator. Reward favors
a positive setting, and the descriptor is the mean setting achieved
during the policy’s rollout.
import gymnasium as gym
import numpy as np
from qdpy.containers import Grid
from qdpy.rl import BRPGMAPElites, PolicyConfig, TD3Config
class Actuator(gym.Env):
observation_space = gym.spaces.Box(-1, 1, (1,), dtype=np.float32)
action_space = gym.spaces.Box(-1, 1, (1,), dtype=np.float32)
def reset(self, *, seed=None, options=None):
super().reset(seed=seed)
self.elapsed = 0
return np.zeros(1, dtype=np.float32), {}
def step(self, action):
self.elapsed += 1
reward = 1 - 0.3 * (float(action[0]) - 0.6)**2
return action.copy(), reward, False, self.elapsed == 4, {}
archive = Grid(shape=9, fitness_domain=((0, 4),),
features_domain=((-1, 1),))
search = BRPGMAPElites(
Actuator, archive, feature=lambda obs, act, next_obs, info: (float(act[0]),),
td3=TD3Config(batch_size=16, learning_starts=24, critic_hidden_sizes=(16,)),
policy=PolicyConfig((16,)), max_episode_steps=4, critic_steps=3,
behavior_steps=4, pg_steps=2, behavior_gamma=0.8, seed=11,
)
stats = search.train(192)
conditioned_actor = search.conditioned_policy()
print(stats.env_steps, stats.behavior_updates)
print(conditioned_actor.act(np.zeros(1, dtype=np.float32), [0.4]))
The feature gets the observation before the step, the physical action, the successor observation, and the step’s info. Return one coordinate per archive descriptor, within its declared bounds. The average feature vector defines the reported descriptor. For example, per-step contact indicators become the fraction of steps in contact. Use a feature whose value is available at the current transition. Include any needed history in the state or the transition info. A true terminal stops critic bootstrapping, while an episode time limit permits it.
Once the learner has enough replay, each generation runs critic_steps
reward updates, behavior_steps estimator updates, and one shared
actor update. It then proposes genetic, policy-gradient, and
actor-injection candidates. A candidate competes by its measured
descriptor and fitness. If you set an episode-level fitness callback,
it controls archive ranking. The reward critic still uses step rewards.
Use conditioned_actor.save("actor.npz") and
DescriptorConditionedPolicy.load("actor.npz") to export the shared
actor. Use search.policy_for(elite) to recover a particular archived
controller. See Repertoire adaptation, gradient search, and exploration benchmarks for a matched control task
with held-out target tracking.