Can Yalnız
RL · 1 min read

Potential-based shaping speeds up learning without changing the optimal policy

Sample write-up. Why shaping rewards with a potential function is safe, with the one-line proof and a minimal implementation.

This is a sample entry showing the write-up layout. Replace or delete it.

Sparse rewards make exploration slow. Shaping adds a dense signal F(s,a,s′)F(s, a, s') to the environment reward, but an arbitrary FF can change which policy is optimal.

The potential-based form

Ng, Harada and Russell (1999) showed that if

F(s,a,s′)=γΦ(s′)−Φ(s)F(s, a, s') = \gamma \Phi(s') - \Phi(s)

for some potential Φ:S→R\Phi : \mathcal{S} \to \mathbb{R}, the optimal policy is unchanged. The shaping terms telescope along any trajectory, so every return shifts by the same −Φ(s0)-\Phi(s_0).

Implementation

class PotentialShaping(gym.Wrapper):
    def __init__(self, env, phi, gamma=0.99):
        super().__init__(env)
        self.phi, self.gamma = phi, gamma

    def reset(self, **kw):
        obs, info = self.env.reset(**kw)
        self._last = self.phi(obs)
        return obs, info

    def step(self, action):
        obs, r, term, trunc, info = self.env.step(action)
        p = 0.0 if term else self.phi(obs)
        r += self.gamma * p - self._last
        self._last = p
        return obs, r, term, trunc, info

Set Φ=0\Phi = 0 at terminal states, or the invariance argument breaks for episodic tasks.