Skip to content

Don't inherit from Academy, remove virtual methods #3184

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 25 commits into from
Jan 10, 2020

Conversation

chriselion
Copy link
Contributor

@chriselion chriselion commented Jan 8, 2020

Make Academy non-abstract, and replace all child classes of it.

  • Remove all virtual methods of Academy.
  • All scenes now use an Academy instead of the child class
  • Some scenes used their Academy to store things like agent speeds or materials. These were converted to *Settings.cs files instead.
  • ProjectSettingsOverrides handles exposing gravity as a reset parameter, as well as a few advanced physics settings used by Walker and Crawler scenes, and resets these values when stopped. This was added to most of the scenes (not everything had an override for gravity)
  • Remove the saved gravity and framerate variables from Academy.

Suggested replacements for the Academy virtual methods:

  • InitializeAcademy -> MonoBehaviour.OnAwake
  • AcademyStep -> MonoBehaviour.Update or FixedUpdate
  • AcademyReset -> subscribe to Academy.OnEnvironmentReset
  • OnDestroy -> MonoBehaviour.OnDestroy or subscribe to Academy.DestroyAction


// Override
Physics.gravity *= gravityMultiplier;
Time.fixedDeltaTime = fixedDeltaTime;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most scenes only care about gravity, but Crawler and Walker override these deltaTime and solver iterations settings.

Physics.defaultSolverIterations = solverIterations;
Physics.defaultSolverVelocityIterations = solverVelocityIterations;

var academy = FindObjectOfType<Academy>();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will eventually be replaced with singleton access.

Copy link
Contributor

@vincentpierre vincentpierre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it a lot so far. (Not approving only because it is WIP)

@@ -8,7 +8,7 @@ public override void InitializeAcademy()
Monitor.verticalOffset = 1f;
Physics.defaultSolverIterations = 12;
Physics.defaultSolverVelocityIterations = 12;
Time.fixedDeltaTime = 0.01333f; // (75fps). default is .2 (60fps)
Time.fixedDeltaTime = 0.01333f; // (75fps). default is .02 (60fps)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<opinion>
I do think we should avoid changing those in our environments.
</opinion>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know the history of them; since the scenes that override them are using ragdolls, I assume it was to improve stability. It might all change with the physx changes anyway.

int m_OriginalSolverIterations;
int m_OriginalSolverVelocityIterations;

public float gravityMultiplier = 1.0f;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we should have gravity multiplier or gravity setting (with default at -9.81)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of the scenes today use a multiplier (but FloatPropertiesChannel use the negative of the value)

@@ -0,0 +1,52 @@
using UnityEngine;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is SimulationSettingsOverrides more appropriate?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is directly modifying what unity calls ProjectSettings, maybe ProjectSettingsOverrides?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That works for me too. Since this will only be used in our scenes, I'm less worried about the naming or making sure that it handles all possible cases.

@surfnerd
Copy link
Contributor

surfnerd commented Jan 8, 2020

Seems ok to me.

@chriselion
Copy link
Contributor Author

Diffs aren't very useful, so here's how Soccer "Academy" GameObject looks now
image

@chriselion chriselion changed the title WIP settings override for scenes Don't inherit from Academy Jan 9, 2020
Assert.AreEqual(0, aca.GetStepCount());
Assert.AreEqual(0, aca.GetEpisodeCount());
Assert.AreEqual(0, aca.GetTotalStepCount());
Assert.AreEqual(null, aca.FloatProperties);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestions for other things to check before and after initialization?

@@ -39,23 +45,11 @@ namespace MLAgents
/// </remarks>
[HelpURL("https://github.com/Unity-Technologies/ml-agents/blob/master/" +
"docs/Learning-Environment-Design-Academy.md")]
public abstract class Academy : MonoBehaviour
public class Academy : MonoBehaviour
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we make this sealed or is that overkill?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would clearly communicate that the Academy is not to be subclassed anymore. It makes sense to add sealed to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doh, forgot to add this. If for some reason, I don't get the singleton implemented before the next release, I'll seal it for that.

@chriselion chriselion changed the title Don't inherit from Academy Don't inherit from Academy, remove virtual methods Jan 9, 2020
Copy link
Contributor

@vincentpierre vincentpierre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a no brainer to me

var academy = FindObjectOfType<Academy>();
academy.LazyInitialization();

academy.FloatProperties.RegisterCallback("gravity", f => { Physics.gravity = new Vector3(0, -f, 0); });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like there's no UnRegisterCallback on FloatProperties at the moment. This could be an issue when Academy is a singleton and scenes are switched.

@chriselion chriselion merged commit f389e85 into master Jan 10, 2020
@delete-merged-branch delete-merged-branch bot deleted the develop-settings-override branch January 10, 2020 19:26
@MikeWise2718
Copy link

So if I have something I need to do every time there is an Academy Step, should I be using Settings.Update or Settings.FixedUpdate? The design seems to indicate both of them are okay, but they are not really equivalent, so I am not sure what to do.

Hmm, looking at the old code, AcademyStep was called by EnvironmentStep which was called by FixedUpdate, so I am going with that.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 15, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants