Skip to content

Commit

Permalink
[Bug Fix] Serialization of vectorActionSpace (#5220)
Browse files Browse the repository at this point in the history
* [Bug Fix] Serialization of vectorActionSpace

* adding a test
  • Loading branch information
vincentpierre authored Apr 6, 2021
1 parent 357e9d6 commit 8f14b25
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 3 deletions.
6 changes: 3 additions & 3 deletions com.unity.ml-agents/Runtime/Policies/BrainParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public ActionSpec ActionSpec
/// For the discrete actions: the number of branches.
/// </value>
[Obsolete("VectorActionSize has been deprecated, please use ActionSpec instead.")]
[SerializeField]
[FormerlySerializedAs("vectorActionSize")]
internal int[] VectorActionSize = new[] { 1 };

Expand All @@ -93,6 +94,7 @@ public ActionSpec ActionSpec
/// (Deprecated) Defines if the action is discrete or continuous.
/// </summary>
[Obsolete("VectorActionSpaceType has been deprecated, please use ActionSpec instead.")]
[SerializeField]
[FormerlySerializedAs("vectorActionSpaceType")]
internal SpaceType VectorActionSpaceType = SpaceType.Discrete;

Expand Down Expand Up @@ -129,16 +131,14 @@ private void UpdateToActionSpec()
#pragma warning disable CS0618
if (!hasUpgradedBrainParametersWithActionSpec
&& m_ActionSpec.NumContinuousActions == 0
&& m_ActionSpec.BranchSizes == null)
&& m_ActionSpec.NumDiscreteActions == 0)
{
if (VectorActionSpaceType == SpaceType.Continuous)
{
m_ActionSpec.NumContinuousActions = VectorActionSize[0];
m_ActionSpec.BranchSizes = null;
}
if (VectorActionSpaceType == SpaceType.Discrete)
{
m_ActionSpec.NumContinuousActions = 0;
m_ActionSpec.BranchSizes = (int[])VectorActionSize.Clone();
}
}
Expand Down
8 changes: 8 additions & 0 deletions com.unity.ml-agents/Tests/Editor/Serialization.meta

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using NUnit.Framework;
using UnityEngine;
using UnityEditor;
using Unity.MLAgents.Policies;

namespace Unity.MLAgents.Tests
{
[TestFixture]
public class TestSerialization
{
const string k_oldPrefabPath = "Packages/com.unity.ml-agents/Tests/Editor/TestModels/old_serialized_agent.prefab";
const int k_numVecObs = 212;
const int k_numContinuousActions = 39;

[Test]
public void TestDeserialization()
{
var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(k_oldPrefabPath);
var agent = GameObject.Instantiate(prefab);
var bp = agent.GetComponent<BehaviorParameters>();
Assert.AreEqual(bp.BrainParameters.ActionSpec.NumContinuousActions, k_numContinuousActions);
Assert.AreEqual(bp.BrainParameters.VectorObservationSize, k_numVecObs);
}

}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &5381908961062339374
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 5379309137663827240}
- component: {fileID: 5414230854946179998}
- component: {fileID: 8153975132613398210}
m_Layer: 0
m_Name: old_serialized_agent
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &5379309137663827240
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5381908961062339374}
m_LocalRotation: {x: -0, y: -0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 90, z: 0}
--- !u!114 &5414230854946179998
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5381908961062339374}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5d1c4e0b1822b495aa52bc52839ecb30, type: 3}
m_Name:
m_EditorClassIdentifier:
m_BrainParameters:
vectorObservationSize: 212
numStackedVectorObservations: 1
vectorActionSize: 27000000
vectorActionDescriptions: []
vectorActionSpaceType: 1
m_Model: {fileID: 11400000, guid: 4e86a19e012da43bfa5ab97ae8089b98, type: 3}
m_InferenceDevice: 0
m_BehaviorType: 0
m_BehaviorName: Walker
TeamId: 0
m_UseChildSensors: 1
m_UseChildActuators: 1
m_ObservableAttributeHandling: 0
--- !u!114 &8153975132613398210
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5381908961062339374}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 88b6042bc9a5d4aa58d931eae49442e5, type: 3}
m_Name:
m_EditorClassIdentifier:
agentParameters:
maxStep: 0
hasUpgradedFromAgentParameters: 1
MaxStep: 0

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

0 comments on commit 8f14b25

Please sign in to comment.