Writing an algorithm¶
MORLAX and AMOR are two instances of one pattern: PPO in which every parallel
environment is assigned a tradeoff, the policy is conditioned on that tradeoff, and the
vector reward is scalarized by it in the loss. This page states the contract a new
multi-objective trainer must meet, lists the building blocks it can reuse, and shows
how to register it with scripts/train.py.
The quickest starting point is to copy src/moplayground/moppo/amor.py, the simpler
of the two, and change the networks and the loss.
The contract¶
mm.train (from minimal_mjx) drives every algorithm through a setup function:
def setup_my_algo(train_fn_params: dict, network_factory_params: dict, resumed):
network_factory = functools.partial(make_my_networks, **network_factory_params)
train_fn = functools.partial(
my_algo.train, **dict(train_fn_params),
network_factory = network_factory,
wrap_env_fn = mop.mo_wrapper,
)
return train_fn, network_factory
mm.train then:
- records the checkpoint network config with Brax's
checkpoint.network_config, which reads the keyword arguments bound intonetwork_factory. They must be JSON-serializable, and the factory must acceptobservation_size,action_sizeandpreprocess_observations_fn; - calls
train_fn(environment=env, eval_env=eval_env, progress_fn=..., policy_params_fn=...); - expects it to return
(make_inference_fn, params, metrics).
Inside train, the algorithm must call the two callbacks:
| Callback | When | Arguments |
|---|---|---|
policy_params_fn |
Once before training and after every epoch | (current_step, make_inference_fn, params). mm.train writes params as the checkpoint <run_dir>/<step>/. |
progress_fn |
After every evaluation | (current_step, metrics). For MOProgress, metrics must hold 'reward', the per-objective episode returns of shape (num_eval_envs, num_objectives), and 'tradeoff', the tradeoffs that produced them. |
resumed is always None from scripts/train.py, which does not resume
multi-objective runs.
Building blocks¶
Everything below is shared by MORLAX and AMOR and can be reused unchanged.
Environment wrapper. mo_wrapper
vmaps the environment and adds
MultiObjectiveEpisodeWrapper
(episode length, truncation, vector-valued episode returns) and auto-reset. The
wrapped environment's state.reward has shape (num_envs, num_objectives).
Tradeoff sampling.
sample_preferences draws one tradeoff
per environment according to sampling_params, and returns an array of shape
(1, num_envs, num_objectives).
Unrolls. generate_unroll steps the
environment with a policy and a fixed batch of tradeoffs, and returns a
MultiObjectiveTransition:
the Brax transition with a vector reward and a tradeoff field.
tradeoff = sample_preferences(key_pref, it, sampling, k, warmup_frac, alpha,
num_evals, num_envs, num_objectives)[0]
policy = make_policy(params, tradeoff) # policy(obs, key) -> (action, extras)
state, data = acting.generate_unroll(
env, state, policy, tradeoff, key, unroll_length,
extra_fields=('truncation', 'episode_metrics', 'episode_done'),
)
Losses. Two ways to use the vector reward:
- Scalarize first:
rewards = jnp.sum(data.tradeoff * data.reward, axis=-1), then a standard scalar critic and GAE. This is whatcompute_morlax_lossdoes. - Vector critic: predict one value per objective and compute per-objective
advantages with
compute_mo_gae, then scalarize the advantages by the tradeoff. This is whatcompute_amor_lossdoes.
Evaluation. Evaluator rolls out
num_eval_envs episodes at tradeoffs drawn uniformly from the simplex, and returns
exactly the 'reward' and 'tradeoff' metrics MOProgress expects:
evaluator = acting.Evaluator(
eval_env = eval_env,
num_objs = num_objectives,
eval_policy_fn = make_eval_policy, # (params, tradeoffs) -> policy(obs, key)
num_eval_envs = num_eval_envs,
episode_length = episode_length,
action_repeat = action_repeat,
key = eval_key,
)
metrics = evaluator.run_evaluation(params, training_metrics)
progress_fn(current_step, metrics)
Networks. factory builds the Brax policy and value
networks. make_amor_networks shows a
factory for networks that take the tradeoff as an input, and
make_morlax_networks one that
generates network weights from it. Hypernetwork modules are in
networks.
Registering the algorithm¶
- Setup function. Add
setup_my_algo(above) next tosetup_morlaxandsetup_amorinsrc/moplayground/learning/training.py, and export it insrc/moplayground/__init__.py. - Config. Add a
my_algo_network_paramssection underlearning_params, and a matching property onMOConfig(see Adding a key). -
Training script. Add a case to
get_handle_paramsinscripts/train.py:case 'my_algo': return mop.setup_my_algo, cfg.ppo_params | cfg.sampling_params, cfg.my_algo_network_paramsget_progressalready returnsMOProgressfor every algorithm exceptppo. -
Inference. Add a case to
load_mo_policythat restores a checkpoint and returns apolicy(obs, key)with the tradeoff fixed, and toload_policyinscripts/rollout.py. For Pareto-front evaluation, pass your ownmake_policyandload_params_fntocompute_fronts, which is algorithm-agnostic. - Tests. Add the algorithm to
ALGORITHMSandNETWORK_PROPERTYintests/unit/test_configs_dir.pyand totest_dispatchintests/unit/test_scripts.py. The GPU tier (pytest -m gpu) trains a tiny run of each algorithm inALGORITHMS(tests/gpu/conftest.py) end to end.
Then set algorithm: my_algo in a config and train as usual: