ray.rllib.algorithms.algorithm.Algorithm#
- class ray.rllib.algorithms.algorithm.Algorithm(config: AlgorithmConfig | None = None, env=None, logger_creator: Callable[[], Logger] | None = None, **kwargs)[source]#
Bases:
Trainable
,AlgorithmBase
An RLlib algorithm responsible for optimizing one or more Policies.
Algorithms contain a WorkerSet under
self.workers
. A WorkerSet is normally composed of a single local worker (self.workers.local_worker()), used to compute and apply learning updates, and optionally one or more remote workers used to generate environment samples in parallel. WorkerSet is fault tolerant and elastic. It tracks health states for all the managed remote worker actors. As a result, Algorithm should never access the underlying actor handles directly. Instead, always access them via all the foreach APIs with assigned IDs of the underlying workers.Each worker (remotes or local) contains a PolicyMap, which itself may contain either one policy for single-agent training or one or more policies for multi-agent training. Policies are synchronized automatically from time to time using ray.remote calls. The exact synchronization logic depends on the specific algorithm used, but this usually happens from local worker to all remote workers and after each training update.
You can write your own Algorithm classes by sub-classing from
Algorithm
or any of its built-in sub-classes. This allows you to override thetraining_step
method to implement your own algorithm logic. You can find the different built-in algorithms’training_step()
methods in their respective main .py files, e.g. rllib.algorithms.dqn.dqn.py or rllib.algorithms.impala.impala.py.The most important API methods a Algorithm exposes are
train()
,evaluate()
,save()
andrestore()
.Methods
Initializes an Algorithm instance.
Adds a new (single-agent) RLModule to this Algorithm's MARLModule.
Adds a new policy to this Algorithm.
Computes an action for the specified policy on the local Worker.
Computes an action for the specified policy on the local worker.
Evaluates current policy under
evaluation_config
settings.Exports model based on export_formats.
Exports Policy checkpoint to a local directory and returns an AIR Checkpoint.
Exports policy model with given policy_id to a local directory.
Creates a new algorithm instance from a given checkpoint.
Recovers an Algorithm from a state object.
Returns configuration passed in by Tune.
Returns a default Policy class to use, given a config.
Returns the (single-agent) RLModule with
model_id
(None if ID not found).Return policy for the specified id, or None.
Return a dictionary of policy ids to weights.
Imports a model from import_file.
Imports a policy's model with given policy_id from a local h5 file.
Merges a complete Algorithm config dict with a partial override dict.
Removes a new policy from this Algorithm.
Resets trial for use with new config.
Resets configuration without restarting the trial.
Restores training state from a given model checkpoint.
Try syncing previously failed and restarted workers with local, if necessary.
Saves the current model state to a checkpoint.
Exports checkpoint to a local directory.
Set policy weights by policy id.
Implements the main
Algorithm.train()
logic.Releases all resources used by this trainable.
Runs one logical iteration of training.
Runs multiple iterations of training.
Default single iteration logic of an algorithm.
Env validator function for this Algorithm class.
Attributes
Current training iteration.
Directory of the results and checkpoints for this Trainable.
Current training iteration (same as
self.iteration
).Trial ID for the corresponding trial of this Trainable.
Trial name for the corresponding trial of this Trainable.
Resources currently assigned to the trial of this Trainable.