mpcrl.optim.GradientBasedOptimizer#

class mpcrl.optim.GradientBasedOptimizer(learning_rate, hook='on_update', max_percentage_update=inf, bound_consistency=False)[source]#

Bases: BaseOptimizer, Generic[LrType]

Base class for first- and second-order gradient-based optimization algorithms.

Parameters:
learning_ratefloat or array or mpcrl.core.schedulers.Scheduler

The learning rate of the optimizer. It can be:

  • a float, in case the learning rate must stay constant and is the same for all learnable parameters

  • an array, in case the learning rate must stay constant but is different for each parameter (should have the same size as the number of learnable parameters)

  • a mpcrl.core.schedulers.Scheduler, in case the learning rate can vary during the learning process (usually, it is set to decay). See the hook argument for more details on when this scheduler is stepped.

hook{“on_update”, “on_episode_end”, “on_timestep_end”}, optional

Specifies when to step the optimizer’s learning rate’s scheduler to decay its value. This allows to vary the rate over the learning iterations. The options are:

  • "on_update" steps the learning rate after each agent’s update

  • "on_episode_end" steps the learning rate after each episode’s end

  • "on_timestep_end" steps the learning rate after each env’s timestep.

By default, "on_update" is selected.

max_percentage_updatefloat, optional

A positive float that specifies the maximum percentage change the learnable parameters can experience in each update. For example, max_percentage_update=0.5 means that the parameters can be updated by up to 50% of their current value. By default, it is set to +inf.

bound_consistencybool, optional

A boolean that, if True, forces the learnable parameters to lie in their bounds when updated. This is done via numpy.clip. Only beneficial if numerical issues arise during updates, e.g., due to the QP solver not being able to guarantee bounds.

Methods

set_learnable_parameters(pars)

Makes the optimization class aware of the dictionary of the learnable parameters whose values are to be updated.

step(*_, **__)

Steps/decays the learning rate according to its scheduler.

update(gradient[, hessian])

Computes the gradient-based update of the learnable parameters dictated by the current RL algorithm.

Attributes

hook

Gets the hook to which the scheduler is attached to, i.e., when to step the learning rate's scheduler to decay its value.

order

Gets the order of the optimizer: 1 for first-order, 2 for second-order.

property hook: str | None#

Gets the hook to which the scheduler is attached to, i.e., when to step the learning rate’s scheduler to decay its value.

Returns:
optional str

The hook to which the scheduler is attached to. Can be None in case no hook is needed (e.g., a scheduler was not passed as learning_rate).

property order: Literal[1, 2]#

Gets the order of the optimizer: 1 for first-order, 2 for second-order.

Returns:
1 or 2

The order of the optimizer.

set_learnable_parameters(pars)#

Makes the optimization class aware of the dictionary of the learnable parameters whose values are to be updated.

Parameters:
pars:class`mpcrl.LearnableParametersDict`

The dictionary of the learnable parameters.

Return type:

None

step(*_, **__)[source]#

Steps/decays the learning rate according to its scheduler.

Return type:

None

update(gradient, hessian=None)[source]#

Computes the gradient-based update of the learnable parameters dictated by the current RL algorithm.

Parameters:
gradient1D array

The gradient of the learnable parameters.

hessian2D array, optional

The hessian of the learnable parameters. When the optimizer is firt-order, it is expected to be None since it is unused. When the optimizer is second-order, it is expected to be a 2D array.

Returns:
statusstr, optional

An optional string containing the status of the update, e.g., the status of the QP solver, if used.

Return type:

Optional[str]

Examples using mpcrl.optim.GradientBasedOptimizer#

Off-policy Q-learning

Off-policy Q-learning

On-policy Deterministic Policy Gradient

On-policy Deterministic Policy Gradient

On-policy Q-learning

On-policy Q-learning