PIXELBANKv9.1.0
Menu

Compute the PPO clipped surrogate objective.

The PPO clip objective for a single sample: LCLIP=min⁡(rt⋅At,clip(rt,1−ϵ,1+ϵ)⋅At)L_{CLIP} = \min(r_t \cdot A_t, \text{clip}(r_t, 1-\epsilon, 1+\epsilon) \cdot A_t)

where r_t = π_θ(a|s) / π_old(a|s) is the probability ratio, A_t is the advantage, and ε is the clip range.

Input:

  • Line 1: epsilon (clip range)
  • Line 2: N (number of samples)
  • Next N lines: ratio advantage

Output: Average clipped objective, rounded to 4 decimal places.

Example:

Input:
0.2
3
1.5 1.0
0.8 -0.5
1.0 2.0
Output:
1.1833
Reasoning:
  • The clip range ϵ\epsilon is set to 0.20.2, which means the clipped ratio will be between 1−ϵ=0.81-\epsilon = 0.8 and 1+ϵ=1.21+\epsilon = 1.2.
  • For each sample, we calculate the clipped surrogate objective LCLIPL_{CLIP}:
    • For the first sample, rt=1.5r_t = 1.5, At=1.0A_t = 1.0, so LCLIP=min⁡(1.5⋅1.0,clip(1.5,0.8,1.2)⋅1.0)=min⁡(1.5,1.2⋅1.0)=1.2L_{CLIP} = \min(1.5 \cdot 1.0, \text{clip}(1.5, 0.8, 1.2) \cdot 1.0) = \min(1.5, 1.2 \cdot 1.0) = 1.2.
    • For the second sample, rt=0.8r_t = 0.8, At=−0.5A_t = -0.5, so LCLIP=min⁡(0.8⋅−0.5,clip(0.8,0.8,1.2)⋅−0.5)=min⁡(−0.4,0.8⋅−0.5)=−0.4L_{CLIP} = \min(0.8 \cdot -0.5, \text{clip}(0.8, 0.8, 1.2) \cdot -0.5) = \min(-0.4, 0.8 \cdot -0.5) = -0.4.
    • For the third sample, rt=1.0r_t = 1.0, At=2.0A_t = 2.0, so LCLIP=min⁡(1.0⋅2.0,clip(1.0,0.8,1.2)⋅2.0)=min⁡(2.0,1.0⋅2.0)=2.0L_{CLIP} = \min(1.0 \cdot 2.0, \text{clip}(1.0, 0.8, 1.2) \cdot 2.0) = \min(2.0, 1.0 \cdot 2.0) = 2.0.
  • We then calculate the average clipped objective: (1.2+(−0.4)+2.0)/3=1.0+0.1833=1.1833(1.2 + (-0.4) + 2.0) / 3 = 1.0 + 0.1833 = 1.1833.
  • The final output is 1.18331.1833, rounded to 4 decimal places.

Constraints:

  • 0.1 <= epsilon <= 0.3
  • 1 <= N <= 50
  • The objective should be MAXIMIZED (we output positive for good updates)
  • Round to 4 decimal places
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.