Skip to content

RobotKpSelector: choose a controller gain with UCB1

← Bandits

A robot arm follows a trajectory with a proportional gain kp. Too low and it lags, too high and it overshoots. RobotKpSelector treats the candidate gains as the arms of a bandit: each round it picks a gain with UCB1, runs a tracking episode on a simulated arm with that gain and rewards the policy with the negated mean tracking error, so a lower error is a higher reward.

1. A first example: three gains

Three candidate gains, nine rounds. Each round runs a short episode on the simulated arm of the library.

from dense_armor.core.online.bandit import RobotKpSelector

sel = RobotKpSelector(arms=[0.5, 8.0, 30.0], period_s=0.5, n_cycles=2, seed=0)
for _ in range(9):
    sel.run_round()
print(sel.best_gain(), dict(sel.counts_))
8.0 {0.5: 3, 8.0: 3, 30.0: 3}

After the nine rounds the best gain is 8.0, and each gain was tried 3 times, because UCB1 plays every arm once before it compares them and the rounds are few. run_round() runs one episode and updates the policy, best_gain() returns the gain with the lowest mean error so far and counts_ is how many rounds each gain had.

2. What it does

Each round the policy picks a gain, runs a tracking episode with that gain on the library's SyntheticArm, and updates the bandit with the negated mean tracking error (a higher reward is a lower error). The bandit is UCB1 (see UCB1 and UCB1-Tuned).

On the benchmark of the index, UCB1 picked kp = 8.0 on all 20 seeds, with a median regret against the best fixed gain of 0.0 and a median time per decision of 0.54 s, dominated by the simulation.

Source: Auer, P., Cesa-Bianchi, N., Fischer, P. (2002). Finite-time analysis of the multiarmed bandit problem. Machine Learning 47(2-3), 235-256. UCB1, Figure 1 and Theorem 1, page 3.

3. The parameters

Parameter Meaning Default
arms the candidate gains, positive floats none
urdf_path a URDF readable by SyntheticArm test/fixtures/urdf/panda.urdf
period_s period of the reference trajectory, in seconds 0.5
rate_hz sampling rate of the reference, in Hz 50.0
n_cycles number of cycles of the reference 2
seed seed of the bandit 0

RobotKpSelector raises ValueError if arms is empty or contains a non-positive value. The methods that every policy shares are on the shared methods page.

API reference

RobotKpSelector

RobotKpSelector(
    arms: Sequence[float],
    urdf_path: str = "test/fixtures/urdf/panda.urdf",
    period_s: float = 0.5,
    rate_hz: float = 50.0,
    n_cycles: int = 2,
    seed: int = 0,
)

Bases: _PullPolicy

Choose kp among a set of values by UCB1 on a robot stream.

Each round the policy picks a gain, runs a tracking episode with that gain on the library's SyntheticArm, and updates the bandit with the negated mean tracking error (a higher reward is a lower error).

Parameters:

Name Type Description Default
arms Sequence[float]

the candidate gains, positive floats.

required
urdf_path str

a URDF readable by SyntheticArm.

'test/fixtures/urdf/panda.urdf'
period_s float

period of the reference trajectory, in seconds.

0.5
rate_hz float

sampling rate of the reference, in Hz.

50.0
n_cycles int

number of cycles of the reference.

2
seed int

seed of the bandit.

0

Raises:

Type Description
ValueError

if arms is empty or contains a non-positive value.

References

Auer, P., Cesa-Bianchi, N., Fischer, P. (2002). Finite-time analysis of the multiarmed bandit problem. Machine Learning 47(2-3), 235-256. UCB1, Figure 1 and Theorem 1, page 3.

Initialise the selector.

Parameters:

Name Type Description Default
arms Sequence[float]

the candidate gains.

required
urdf_path str

path to a URDF file.

'test/fixtures/urdf/panda.urdf'
period_s float

period of the reference trajectory.

0.5
rate_hz float

sampling rate of the reference.

50.0
n_cycles int

number of cycles.

2
seed int

seed of the bandit.

0

Raises:

Type Description
ValueError

if arms is empty or contains a non-positive value.

Source code in dense_armor/core/online/bandit/robot.py
def __init__(
    self,
    arms: Sequence[float],
    urdf_path: str = "test/fixtures/urdf/panda.urdf",
    period_s: float = 0.5,
    rate_hz: float = 50.0,
    n_cycles: int = 2,
    seed: int = 0,
) -> None:
    """Initialise the selector.

    Args:
        arms: the candidate gains.
        urdf_path: path to a URDF file.
        period_s: period of the reference trajectory.
        rate_hz: sampling rate of the reference.
        n_cycles: number of cycles.
        seed: seed of the bandit.

    Raises:
        ValueError: if ``arms`` is empty or contains a non-positive
            value.
    """
    super().__init__()
    if not arms:
        raise ValueError("arms must be non-empty")
    for a in arms:
        if float(a) <= 0.0:
            raise ValueError("gains must be positive")
    self.arms = tuple(float(a) for a in arms)
    self.urdf_path = urdf_path
    self.period_s = float(period_s)
    self.rate_hz = float(rate_hz)
    self.n_cycles = int(n_cycles)
    self.seed = int(seed)
    self.bandit_ = UCB1(arms=list(self.arms), seed=self.seed)
    self.ds_ = SyntheticArm(
        self.urdf_path,
        period_s=self.period_s,
        rate_hz=self.rate_hz,
        n_cycles=self.n_cycles,
        seed=self.seed,
    )
    self.counts_: dict[float, int] = {a: 0 for a in self.arms}
    self.rewards_: dict[float, float] = {a: 0.0 for a in self.arms}
    self.n_seen_ = 0

run_round

run_round() -> float

Choose a gain, run an episode and update the bandit.

Returns:

Type Description
float

The chosen gain.

Source code in dense_armor/core/online/bandit/robot.py
def run_round(self) -> float:
    """Choose a gain, run an episode and update the bandit.

    Returns:
        The chosen gain.
    """
    kp = float(self.bandit_.pull())
    reward = _episode(self.ds_, kp)
    self.bandit_.update(kp, reward)
    self.counts_[kp] = self.counts_.get(kp, 0) + 1
    n = self.counts_[kp]
    m = self.rewards_.get(kp, 0.0)
    self.rewards_[kp] = m + (reward - m) / n
    self.n_seen_ += 1
    return kp

best_gain

best_gain() -> float

Return the gain with the highest running mean reward.

Returns:

Type Description
float

The best gain seen so far.

Source code in dense_armor/core/online/bandit/robot.py
def best_gain(self) -> float:
    """Return the gain with the highest running mean reward.

    Returns:
        The best gain seen so far.
    """
    return max(self.arms, key=lambda a: self.rewards_.get(a, -np.inf))

Details

Choices

  • No finance example and no 3D figure on this page: the class runs a simulation of a robot arm, and one round takes about half a second, so a surface of many runs is not drawn. The behaviour on a stream of success rates is on the other pages.
  • Where it lives: dense_armor.core.online.bandit.robot.