Very simple environment with continuous action space fails to learn effectively with PPO

efysk: <p>I will post a link to the minimal reproduction code, but the env is really simple.<br>
(link: <a href="https://gist.github.com/Demonstrandum/697feb20701343704d0e2e667a96fdbd" rel="noopener nofollow ugc">Minimal PPO training environment (gist.github.com)</a>)</p>
<p>A flat 2D world [0,1]×[0,1] has an agent and a target destination it needs to get to.<br>
Thus the observation space is continuous and has shape (4,) {x0,y0,x1,y1}.<br>
The action space is essentially a direction to step in, and how large of a step to make.<br>
Neither of these actions compound with the previous actions, they stand on their own.<br>
Both actions are in the range [-1,1].  Thus the action space is continuous and has shape (2,).<br>
Location is clipped at the world boundaries to be within [0,1] as well.</p>
<p>I’ve tried running the exact same PPO method to train the BipedalWalker-v3 gym env, and it performs extremely well and steadily improves the average reward until the environment is essentially solved.</p>
<p>However, switching over to my environment, it performs abysmally.<br>
After 1 million time steps (much longer than the bipedalwalker needs for good results) it only reaches the destination less than half the time, and even then, most of them look like it reached it by accident.</p>
<p>The other half the time it will just burrow itself in a corner, or try to go through the world border even though it’s clipped and doesn’t get anywhere.</p>
<p>I’d think my environment is dramatically simpler and easier to learn, so why does it not work at all?</p>
<p>I’ve tested it can go in all direction, and always reach its target well within the step limit if it wanted to. Episode is truncated after 500 time steps with a -10 reward, +10 reward for reaching the target, and otherwise, the reward is just -dist where dist is the distance from the agent to the target.</p>
<p>Even a random sample (random walk) reaches the target relatively often.</p>
<p>What is wrong with my environment / approach?</p>

J_Johnson: <p>In the case of the bipedal walker, it’s objective is to stand and take steps, irrespective of the direction. And so it doesn’t have any visual information about the surrounding environment as an input. Or any concept of location.</p>
<p>But in your example, it seems you want the agent to reach a physical location.</p>
<p>What information about the surrounding environment is given to your model?</p>

efysk: <p>The observation space is the four x,y coordinates of both the agent and the target, that’s what it knows about it’s environment.<br>
And the reward like I said is just the negative distance between it and the target, so the agent would want to close the gap as quick as possible I’d assume.</p>

J_Johnson: <pre><code class="lang-auto"># Distance from target, reward is simply negtive distance.
        target_dist = float(norm(self._agent_location - self._destination))
        reward: float = -target_dist
</code></pre>
<p>I think this might be an incorrect formation of the reward, in your case. Ideally, you want to reward the model for <em>decreasing</em> the distance to the target. This means you should calculate the <em>delta between the start distance and the end distance</em> on that step.</p>
<p>Consider this, when the model is far from the target, the current function is communicating to the model it needs to change parameters ALOT(likely resulting in exploding gradients). But how did it’s actions on that time step determine where it would start? While, when it is very close to the target, it’s changing the parameters very little. And there is no correlation between the actions it took on that time step and the total distance it just happens to be on that time step.</p>
<p>But if you give the model the difference between the <code>distance_start - distance_end</code> of each step, you then have a reward correlated to the actions taken that step.</p>

efysk: <p>First I want to say thank you for your help <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<p>That reward was just the one I saw being used in a similar (using DQN) model that’s even a bit more sophisticated: <a href="https://cs.stanford.edu/people/karpathy/reinforcejs/puckworld.html" rel="noopener nofollow ugc">PuckWorld (stanford.edu)</a></p>
<p>I previously tried normalizing the distance based on the initial distance, I also tried what you said:</p>
<pre><code class="lang-python">        prev_target_dist = float(norm(self._last_location - self._destination))
        target_dist = float(norm(self._agent_location - self._destination))
        reward: float = prev_target_dist - target_dist
</code></pre>
<p>and still it performs erratically even after 1,000,000 steps.  I feel there must be something else flawed in my env.</p>
<p>I also tried controlling the agent with manual keyboard input and found nothing inconsistent about its movement based on the action values.</p>

J_Johnson: <p>Thanks for trying that. I had a chance to run your code with the above change. I made a few modifications to your code overall:</p>
<ol>
<li>Added a way to track the average number of steps per episode;</li>
<li>Reduced the reward for finding the target from 10 —&gt; 0.2;</li>
<li>I increased the learning rate by a factor of 10x.</li>
</ol>
<p>Here is the result from tracking:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/a/ea94c5cc3cfb5da7c01530b311efcd9759e03d4e.png" data-download-href="https://discuss.pytorch.org/uploads/default/ea94c5cc3cfb5da7c01530b311efcd9759e03d4e" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/e/a/ea94c5cc3cfb5da7c01530b311efcd9759e03d4e_2_690x381.png" alt="image" data-base62-sha1="xtctgpQcDK65LRHlNmqTGYN8qaG" width="690" height="381" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/e/a/ea94c5cc3cfb5da7c01530b311efcd9759e03d4e_2_690x381.png, https://discuss.pytorch.org/uploads/default/original/3X/e/a/ea94c5cc3cfb5da7c01530b311efcd9759e03d4e.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/e/a/ea94c5cc3cfb5da7c01530b311efcd9759e03d4e.png 2x" data-dominant-color="F6F7F9"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">741×410 20.1 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>It appears to me to be improving.</p>
<p>The new metrics were added with the following lines of code:</p>
<pre><code class="lang-auto">...
step = 0
episode = 0

steps_memory = torch.tensor([]) # new line
steps_save = np.empty((0,2)) # new line
while step &lt; TRAINING_LEN:
...
        if step % SAVE_FREQ == 0:
            ppo_agent.save('./ppo_agent_checkpoint.pth')

       # new code below
        if term|trunc:
            steps_memory = torch.cat([steps_memory[-100:],torch.tensor([t])])
            avg_steps = torch.mean(steps_memory)
            step_save = np.array([[episode,avg_steps]])
            steps_save = np.concatenate([steps_save, step_save])
        # new code above
        if term: print(f'\nAchieved goal (Eps: {episode}; in {t} steps) (Reward: {reward} / {total_reward}) (Avg Steps: {avg_steps})')
...
print('')

# new code below
rows = ["{},{}".format(i, j) for i, j in steps_save]
text = "\n".join(rows)

with open('steps_save.csv', 'w') as f:
    f.write(text)

# new code above

wait: str = input('run sim? (y/n): ')
...
</code></pre>
<p>With that said, I think you could improve your approach with the following changes:</p>
<ol>
<li>Change the Action Space to be a total of 5, where 1 thru 3 are a probability of rotating 10 degrees left, not rotating, or rotating 10 degrees right(get the max value for the action chosen); and the other two the probability for whether to move forward a fixed amount in the orientation after the rotation action has been applied(i.e. move vs. don’t);</li>
<li>The above assumes you change the rotation to be additive;</li>
<li>You’ll also need to include orientation into the observation state, since it would be additive;</li>
<li>Update the reward function to add one more component: pi - abs(agent_orientation - target_angle_from_agent); multiply this by an alpha to ensure it’s equivalent to the distance reward;</li>
</ol>
<p>Alternatively, you could do the same as Stanford in your link and make the action space 5 values, where 1 - 4 is a probability of left, up, right, or down. And the final value would be a scale of 0-1 for the velocity(granted, they included velocity, and acceleration in their function; likely three values where 0 is decelerate by a scalar, 1 is no acceleration, and 2 is accelerate by a scalar). When you frame the problem in probabilities, it tends to be easier for networks to pick up on a best solution more quickly.</p>

efysk: <p>Thank you very much for taking the time to run my code and modify it.</p>
<p>I suppose I need to be more sensitive about the rewards in the future.</p>
<p>May I also ask if I can avoid discretizing the actions such as “rotate 5 deg” as opposed to “rotate x degree” with x given by the model, while still framing it as a set of probabilities?  Maybe interpolate between the two most likely rotation wrt their probabilities?<br>
In case it isn’t convenient to divide them into discrete units.<br>
I don’t believe the BipedalWalker’s actions are discretized like that, could that be an improvement?</p>

J_Johnson: <p>You can set it up however you like and the model will learn something, just as in your current setup.</p>
<p>Just speaking from experience, models tend to converge faster with discrete values, where the model is just deciding the probability for a given action choice.</p>
<p>In the real world, you can always reduce your timestep, with the only limitation being compute speed. But with a smaller model, the compute speed is faster, so that’s the trade off. If you cut the discrete value into a small enough size, it looks and acts continuous on larger time frames.</p>