Skip to content
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
5 changes: 4 additions & 1 deletion Project/ProjectSettings/EditorBuildSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@
EditorBuildSettings:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Scenes: []
m_Scenes:
- enabled: 1
path: Packages/com.unity.ml-agents/Tests/Runtime/SerializeTestScene.unity
guid: 60783bd849bd242eeb66243542762b23
m_configObjects: {}
38 changes: 36 additions & 2 deletions com.unity.ml-agents/Runtime/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using UnityEngine;
using Barracuda;
using UnityEditor;
using UnityEngine.Serialization;

namespace MLAgents
Expand Down Expand Up @@ -105,11 +106,25 @@ internal struct AgentAction
"docs/Learning-Environment-Design-Agents.md")]
[Serializable]
[RequireComponent(typeof(BehaviorParameters))]
public abstract class Agent : MonoBehaviour
public abstract class Agent : MonoBehaviour, ISerializationCallbackReceiver
{
IPolicy m_Brain;
BehaviorParameters m_PolicyFactory;

/// This code is here to make the upgrade path for users using maxStep
/// easier. We will hook into the Serialization code and make sure that
/// agentParameters.maxStep and this.maxStep are in sync.
[Serializable]
internal struct AgentParameters
{
public int maxStep;
}

[SerializeField] [HideInInspector]
internal AgentParameters agentParameters;
[SerializeField] [HideInInspector]
internal bool hasUpgradedFromAgentParameters;

/// <summary>
/// The maximum number of steps the agent takes before being done.
/// </summary>
Expand Down Expand Up @@ -186,6 +201,26 @@ void OnEnable()
LazyInitialize();
}


public void OnBeforeSerialize()
{
if (maxStep == 0 && maxStep != agentParameters.maxStep && !hasUpgradedFromAgentParameters)
{
maxStep = agentParameters.maxStep;

}
hasUpgradedFromAgentParameters = true;
}

public void OnAfterDeserialize()
{
if (maxStep == 0 && maxStep != agentParameters.maxStep && !hasUpgradedFromAgentParameters)
{
maxStep = agentParameters.maxStep;
}
hasUpgradedFromAgentParameters = true;
}

/// Helper method for the <see cref="OnEnable"/> event, created to
/// facilitate testing.
public void LazyInitialize()
Expand Down Expand Up @@ -214,7 +249,6 @@ public void LazyInitialize()
ResetData();
InitializeAgent();
InitializeSensors();

}

/// Monobehavior function that is called when the attached GameObject
Expand Down
8 changes: 8 additions & 0 deletions com.unity.ml-agents/Tests/Runtime.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions com.unity.ml-agents/Tests/Runtime/SerializationTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// using System.Collections;
// using NUnit.Framework;
// #if UNITY_EDITOR
// using UnityEditor.SceneManagement;
// using UnityEngine.TestTools;
// #endif
// using UnityEngine;
// using UnityEngine.SceneManagement;
//
// namespace Tests
// {
// [TestFixture]
// public class SerializationTest
// {
//
// [SetUp]
// public void Setup()
// {
// SceneManager.LoadScene("Packages/com.unity.ml-agents/Tests/Runtime/SerializeTestScene");
// }
//
// /// <summary>
// /// Test that the serialized agent in the scene, which has its agent parameter value serialized,
// /// properly deserializes it to Agent.maxStep.
// /// </summary>
// [UnityTest]
// public IEnumerator SerializationTestSimplePasses()
// {
// // Use the Assert class to test conditions
// var gameObjects = SceneManager.GetActiveScene().GetRootGameObjects();
// if (SceneManager.GetActiveScene().name != "SerializeTestScene")
// {
// yield return null;
// }
//
// GameObject agent = null;
// foreach (var go in gameObjects)
// {
// if (go.name == "Agent")
// {
// agent = go;
// break;
// }
// }
// Assert.NotNull(agent);
// var agentComponent = agent.GetComponent<SerializeAgent>();
// Assert.True(agentComponent.maxStep == 5000);
//
// }
// }
// }
11 changes: 11 additions & 0 deletions com.unity.ml-agents/Tests/Runtime/SerializationTest.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions com.unity.ml-agents/Tests/Runtime/SerializeAgent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;

public class SerializeAgent : Agent
{
public override float[] Heuristic()
{
return new[] {0f};
}
}
11 changes: 11 additions & 0 deletions com.unity.ml-agents/Tests/Runtime/SerializeAgent.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading