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 to the environment reward, but an arbitrary can change which policy is optimal.
The potential-based form
Ng, Harada and Russell (1999) showed that if
for some potential , the optimal policy is unchanged. The shaping terms telescope along any trajectory, so every return shifts by the same .
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 at terminal states, or the invariance argument breaks for episodic tasks.