Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

What is a good way to slow the agent down during testing?

Discussion in 'ML-Agents' started by Chrispresso, Sep 27, 2020.

  1. Chrispresso


    Nov 2, 2019
    I am communicating with Unity through the Python API. Right now I have a trained agent that I'd like to visually inspect during testing. If I do time.sleep(0.5) in Python before calling step() on the Unity environment, the StepCount increment by more than 1. It actually increases by 16, which makes me think StepCount is incremented somewhere else.

    If I instead don't have any sleep time in Python, and in C# do:
    Code (CSharp):
    1. public void FixedUpdate() {
    2.         WaitTimeInference();
    3.     }
    5. void WaitTimeInference() {
    6.         stepz = StepCount;
    8.         if (timeSinceDecision >= timeBetweenDecisionsAtInference) {
    9.             timeSinceDecision = 0f;
    10.             RequestDecision();
    11.         } else {
    12.             timeSinceDecision += Time.fixedDeltaTime;
    13.         }
    14. }
    16. public override void OnActionReceived(float[] vectorAction) {
    17.         numActions++;
    18. }
    then I get a desired result of actions being taken slower. Obviously here I have stepz which increments way faster than numActions.

    Is there a better way to slow the agent down for testing? Because I obviously can't really set MaxStep since that continues to update and putting time.sleep(0.5) in Python seems to still result in the StepCount incrementing elsewhere within Unity.
  2. ruoping_unity


    Unity Technologies

    Jul 10, 2020
    If you want all the behaviors and everything remain the same but only goes slower, that code snippet might not be exactly what you want. Since it makes the decision period longer but during each period the environment physics is still going on, the agent might perform differently if you train the agent normally and do inference using WaitTimeInference.

    Have you tried modifying the timeScale or fixedDeltaTime? It slows down the time in Unity so it doesn't affect the step counting.
  3. Chrispresso


    Nov 2, 2019
    It seems like when I modify timeScale it behaves as expected, but if I do what the docs suggest and also change fixedDeltaTime to be Time.fixedDeltaTime *= Time.timeScale it seems to have no affect.

    Looking at the docs, 0.02 should result in 50 calls per second. If I set Time.timeScale to 0.5, then that math makes it 0.01, or 100 calls/second. Wouldn't I want to make it Time.fixedDeltaTime /= Time.timeScale to get it to be 0.04 or 25 calls/second? Or am I misunderstanding the interraction between those variables?

    EDIT: I think the documentation is wrong. Time.timeScale seems to be the only thing you need to change. If you look under it states that it's affected by Time.timeScale.
    Last edited: Oct 4, 2020
  4. TreyK-47


    Unity Technologies

    Oct 22, 2019
    Oh! If you find what you feel is a mistake in the documentation, could you please fill out the Documentation Feedback form, which can be found at the bottom of every documentation page - both in the core platform docs & in package docs. This allows us to collate all feedback about a specific page in the same place, and act on all of it at once. :)