Skip to content

Commit 8f14b25

Browse files
[Bug Fix] Serialization of vectorActionSpace (#5220)
* [Bug Fix] Serialization of vectorActionSpace * adding a test
1 parent 357e9d6 commit 8f14b25

File tree

6 files changed

+131
-3
lines changed

6 files changed

+131
-3
lines changed

com.unity.ml-agents/Runtime/Policies/BrainParameters.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public ActionSpec ActionSpec
8080
/// For the discrete actions: the number of branches.
8181
/// </value>
8282
[Obsolete("VectorActionSize has been deprecated, please use ActionSpec instead.")]
83+
[SerializeField]
8384
[FormerlySerializedAs("vectorActionSize")]
8485
internal int[] VectorActionSize = new[] { 1 };
8586

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

@@ -129,16 +131,14 @@ private void UpdateToActionSpec()
129131
#pragma warning disable CS0618
130132
if (!hasUpgradedBrainParametersWithActionSpec
131133
&& m_ActionSpec.NumContinuousActions == 0
132-
&& m_ActionSpec.BranchSizes == null)
134+
&& m_ActionSpec.NumDiscreteActions == 0)
133135
{
134136
if (VectorActionSpaceType == SpaceType.Continuous)
135137
{
136138
m_ActionSpec.NumContinuousActions = VectorActionSize[0];
137-
m_ActionSpec.BranchSizes = null;
138139
}
139140
if (VectorActionSpaceType == SpaceType.Discrete)
140141
{
141-
m_ActionSpec.NumContinuousActions = 0;
142142
m_ActionSpec.BranchSizes = (int[])VectorActionSize.Clone();
143143
}
144144
}

com.unity.ml-agents/Tests/Editor/Serialization.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using NUnit.Framework;
2+
using UnityEngine;
3+
using UnityEditor;
4+
using Unity.MLAgents.Policies;
5+
6+
namespace Unity.MLAgents.Tests
7+
{
8+
[TestFixture]
9+
public class TestSerialization
10+
{
11+
const string k_oldPrefabPath = "Packages/com.unity.ml-agents/Tests/Editor/TestModels/old_serialized_agent.prefab";
12+
const int k_numVecObs = 212;
13+
const int k_numContinuousActions = 39;
14+
15+
[Test]
16+
public void TestDeserialization()
17+
{
18+
var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(k_oldPrefabPath);
19+
var agent = GameObject.Instantiate(prefab);
20+
var bp = agent.GetComponent<BehaviorParameters>();
21+
Assert.AreEqual(bp.BrainParameters.ActionSpec.NumContinuousActions, k_numContinuousActions);
22+
Assert.AreEqual(bp.BrainParameters.VectorObservationSize, k_numVecObs);
23+
}
24+
25+
}
26+
}

com.unity.ml-agents/Tests/Editor/Serialization/TestLoadOldPrefab.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!1 &5381908961062339374
4+
GameObject:
5+
m_ObjectHideFlags: 0
6+
m_CorrespondingSourceObject: {fileID: 0}
7+
m_PrefabInstance: {fileID: 0}
8+
m_PrefabAsset: {fileID: 0}
9+
serializedVersion: 6
10+
m_Component:
11+
- component: {fileID: 5379309137663827240}
12+
- component: {fileID: 5414230854946179998}
13+
- component: {fileID: 8153975132613398210}
14+
m_Layer: 0
15+
m_Name: old_serialized_agent
16+
m_TagString: Untagged
17+
m_Icon: {fileID: 0}
18+
m_NavMeshLayer: 0
19+
m_StaticEditorFlags: 0
20+
m_IsActive: 1
21+
--- !u!4 &5379309137663827240
22+
Transform:
23+
m_ObjectHideFlags: 0
24+
m_CorrespondingSourceObject: {fileID: 0}
25+
m_PrefabInstance: {fileID: 0}
26+
m_PrefabAsset: {fileID: 0}
27+
m_GameObject: {fileID: 5381908961062339374}
28+
m_LocalRotation: {x: -0, y: -0, z: 0, w: 1}
29+
m_LocalPosition: {x: 0, y: 0, z: 0}
30+
m_LocalScale: {x: 1, y: 1, z: 1}
31+
m_Children: []
32+
m_Father: {fileID: 0}
33+
m_RootOrder: 0
34+
m_LocalEulerAnglesHint: {x: 0, y: 90, z: 0}
35+
--- !u!114 &5414230854946179998
36+
MonoBehaviour:
37+
m_ObjectHideFlags: 0
38+
m_CorrespondingSourceObject: {fileID: 0}
39+
m_PrefabInstance: {fileID: 0}
40+
m_PrefabAsset: {fileID: 0}
41+
m_GameObject: {fileID: 5381908961062339374}
42+
m_Enabled: 1
43+
m_EditorHideFlags: 0
44+
m_Script: {fileID: 11500000, guid: 5d1c4e0b1822b495aa52bc52839ecb30, type: 3}
45+
m_Name:
46+
m_EditorClassIdentifier:
47+
m_BrainParameters:
48+
vectorObservationSize: 212
49+
numStackedVectorObservations: 1
50+
vectorActionSize: 27000000
51+
vectorActionDescriptions: []
52+
vectorActionSpaceType: 1
53+
m_Model: {fileID: 11400000, guid: 4e86a19e012da43bfa5ab97ae8089b98, type: 3}
54+
m_InferenceDevice: 0
55+
m_BehaviorType: 0
56+
m_BehaviorName: Walker
57+
TeamId: 0
58+
m_UseChildSensors: 1
59+
m_UseChildActuators: 1
60+
m_ObservableAttributeHandling: 0
61+
--- !u!114 &8153975132613398210
62+
MonoBehaviour:
63+
m_ObjectHideFlags: 0
64+
m_CorrespondingSourceObject: {fileID: 0}
65+
m_PrefabInstance: {fileID: 0}
66+
m_PrefabAsset: {fileID: 0}
67+
m_GameObject: {fileID: 5381908961062339374}
68+
m_Enabled: 1
69+
m_EditorHideFlags: 0
70+
m_Script: {fileID: 11500000, guid: 88b6042bc9a5d4aa58d931eae49442e5, type: 3}
71+
m_Name:
72+
m_EditorClassIdentifier:
73+
agentParameters:
74+
maxStep: 0
75+
hasUpgradedFromAgentParameters: 1
76+
MaxStep: 0

com.unity.ml-agents/Tests/Editor/TestModels/old_serialized_agent.prefab.meta

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)