Skip to content

allow ending the episode for MaxStepsReached #4453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions com.unity.ml-agents/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ and this project adheres to
- Enabled C# formatting using `dotnet-format`. (#4362)
- GridSensor was added to the com.unity.ml-agents.extensions package. Thank you
to Jaden Travnik from Eidos Montreal for the contribution! (#4399)
- Added `Agent.EpisodeInterrupted()`, which can be used to reset the agent when
it has reached a user-determined maximum number of steps. This behaves similarly
to `Agent.EndEpsiode()` but has a slightly different effect on training (#4453).
#### ml-agents / ml-agents-envs / gym-unity (Python)
- Experimental PyTorch support has been added. Use `--torch` when running `mlagents-learn`, or add
`framework: pytorch` to your trainer configuration (under the behavior name) to enable it.
Expand Down
35 changes: 33 additions & 2 deletions com.unity.ml-agents/Runtime/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ public void LazyInitialize()
enum DoneReason
{
/// <summary>
/// The <see cref="EndEpisode"/> method was called.
/// The episode was ended manually by calling <see cref="EndEpisode"/>.
/// </summary>
DoneCalled,

Expand Down Expand Up @@ -691,10 +691,41 @@ void UpdateRewardStats()
/// <summary>
/// Sets the done flag to true and resets the agent.
/// </summary>
/// <remarks>
/// This should be used when the episode can no longer continue, such as when the Agent
/// reaches the goal or fails at the task.
/// </remarks>
/// <seealso cref="OnEpisodeBegin"/>
/// <seealso cref="EpisodeInterrupted"/>
public void EndEpisode()
{
NotifyAgentDone(DoneReason.DoneCalled);
EndEpisodeAndReset(DoneReason.DoneCalled);
}

/// <summary>
/// Indicate that the episode is over but not due to the "fault" of the Agent.
/// This has the same end result as calling <see cref="EndEpisode"/>, but has a
/// slightly different effect on training.
/// </summary>
/// <remarks>
/// This should be used when the episode could continue, but has gone on for
/// a sufficient number of steps.
/// </remarks>
/// <seealso cref="OnEpisodeBegin"/>
/// <seealso cref="EndEpisode"/>
public void EpisodeInterrupted()
{
EndEpisodeAndReset(DoneReason.MaxStepReached);
}


/// <summary>
/// Internal method to end the episode and reset the Agent.
/// </summary>
/// <param name="reason"></param>
void EndEpisodeAndReset(DoneReason reason)
{
NotifyAgentDone(reason);
_AgentReset();
}

Expand Down