WHY it's hard: RL learns from the return Gt=∑k≥0γkrt+k. If almost all r=0, then Q(s,a)≈0 everywhere the agent has actually visited, so no action looks better than any other → no policy improvement until the goal is hit by luck. The probability of hitting it by luck decays exponentially with the number of steps required.
Step 1 — Write the shaped return.
For a trajectory s0,s1,s2,… the shaped return is
G0′=∑t=0∞γt(rt+γΦ(st+1)−Φ(st)).Why this step? By definition the return is the discounted sum of the shaped reward rt′=rt+Ft.
Step 2 — Split the sum.G0′=G0t∑γtrt+∑t=0∞γt(γΦ(st+1)−Φ(st)).Why? Sum of a sum splits linearly.
Step 3 — Telescope the shaping part. Let T=∑tγt(γΦ(st+1)−Φ(st)). Write it out:
T=(γΦ(s1)−Φ(s0))+γ(γΦ(s2)−Φ(s1))+γ2(γΦ(s3)−Φ(s2))+⋯
Group terms with Φ(s1): coefficient =γ−γ⋅1=0. Same for Φ(s2): γ2−γ2=0. Everything cancels except −Φ(s0).Why? Each Φ(st) appears with +γ⋅γt−1=γt from one term and −γt from the next — they annihilate. This is a telescoping series.
Step 4 — Collect.G0′=G0−Φ(s0).
Step 5 — Interpret. The shaped value differs from the true value by a constant that depends only on the start state, not on the actions:
V′π(s)=Vπ(s)−Φ(s),Q′π(s,a)=Qπ(s,a)−Φ(s).Why this is the punchline:argmaxaQ′(s,a)=argmaxa(Q(s,a)−Φ(s))=argmaxaQ(s,a) because Φ(s) is a constant offset over a. So the greedy/optimal policy is identical. ∎
Intrinsic motivation / curiosity: add F=η∥prediction error∥ or a count bonus F=β/N(s) so the agent is rewarded for visiting novel states → explores until it finds the real reward.
Hindsight Experience Replay (HER): if the agent aimed for goal g but reached g′, relabel the episode as if g′was the goal. Now a "failure" becomes a "success" → dense synthetic learning signal, no reward hacking.
Curriculum learning: start with goals close to the agent (dense success), gradually move them farther.
You're playing "hot or cold" but blindfolded and the friend only shouts "found it!" once you touch the hidden toy — silence otherwise. You'd wander forever. Reward shaping is your friend now whispering "warmer... warmer... colder." That helps! But there's a rule: whenever you step closer they give you a candy, and whenever you step back they take one away — exactly the candy back. So walking in circles earns you nothing; only actually finding the toy pays off. That's why we can help you and never trick you into pacing back and forth for candy.
Dekho, RL mein sabse bada problem hota hai sparse reward. Matlab agent ko reward tabhi milta hai jab woh finally goal tak pahunch jaaye — beech mein har step pe zero. To jab tak luck se goal nahi milta, agent ko koi signal hi nahi milta ki "sahi ja raha hoon ya galat". Isliye learning atak jaati hai. Ise samajhne ka easy tareeka: blindfold "hot-cold" game, jisme dost sirf "mil gaya!" bolta hai, warna chup — tum toh ghumte hi rehte ho.
Reward shaping ka idea hai ek helper reward F add kar do, jaise "goal ke paas ja rahe ho to thoda plus, door ja rahe ho to minus". Isse har step pe ek gradient mil jaata hai. Par ek khatra hai: agar F galat design kiya, to agent asli kaam chhod ke sirf F ko farm karega — jaise boat race wali agent circle mein ghoom ke checkpoints re-hit karti rehti hai. Isko reward hacking kehte hain.
Iska safe solution hai Potential-Based Reward Shaping (PBRS): F=γΦ(s′)−Φ(s) lo, jahan Φ koi bhi potential function hai (best choice Φ≈V∗). Iska magic ye hai ki jab tum kisi loop mein ghoomoge, to andar aane ka bonus, bahar jaane ke penalty se exactly cancel ho jaata hai (telescoping series). Isliye loop se koi free reward nahi milta, aur maths proof karta hai ki optimal policy same rehti hai — kyunki Q′(s,a)=Q(s,a)−Φ(s), aur Φ(s) toh action pe depend hi nahi karta.
Jab tumhare paas koi heuristic nahi hai (pata hi nahi goal kahan hai), tab curiosity bonus (naye states explore karne pe reward) ya HER (fail hui episode ka goal relabel karke success bana do) use karo. Yahi 20% cheezein 80% problems solve kar deti hain. Yaad rakho: shaping madad kar sakti hai, par PBRS form use karke hi taaki cheating na ho.