forked from Unity-Technologies/ml-agents
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Develop add training area replicator (Unity-Technologies#5568)
* Added training area replicator to com.unity.ml-agents package. * Added num_areas to Unity RL Initialization proto. Added cli and config file support for num_areas. * Changed training area replicator to size grid automatically from number of areas. * Added tests for the training area replicator. * Added setup for tests for the training area replicator. * Added comments and updated create tutorial for training area replicator. * Updated CHANGELOG. * Fixed some failing tests. * Update com.unity.ml-agents/CHANGELOG.md Co-authored-by: Henry Peteet <[email protected]> * Update docs/Learning-Environment-Create-New.md Co-authored-by: Henry Peteet <[email protected]> * Update com.unity.ml-agents/Runtime/Areas/TrainingAreaReplicator.cs Co-authored-by: Henry Peteet <[email protected]> * Addressed CR comments. Co-authored-by: Miguel Alonso Jr <miguelalonsojr> Co-authored-by: Henry Peteet <[email protected]>
- Loading branch information
1 parent
f5e4a2b
commit 003e8ae
Showing
24 changed files
with
295 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
88 changes: 88 additions & 0 deletions
88
com.unity.ml-agents/Runtime/Areas/TrainingAreaReplicator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using System; | ||
using Unity.Mathematics; | ||
using UnityEngine; | ||
|
||
namespace Unity.MLAgents.Areas | ||
{ | ||
/// <summary> | ||
/// The Training Ares Replicator allows for a training area object group to be replicated dynamically during runtime. | ||
/// </summary> | ||
public class TrainingAreaReplicator : MonoBehaviour | ||
{ | ||
public GameObject baseArea; | ||
public int numAreas = 1; | ||
public float separation = 10f; | ||
|
||
int3 m_GridSize = new int3(1, 1, 1); | ||
int m_areaCount = 0; | ||
string m_TrainingAreaName; | ||
|
||
public int3 GridSize => m_GridSize; | ||
public string TrainingAreaName => m_TrainingAreaName; | ||
|
||
public void Awake() | ||
{ | ||
// Computes the Grid Size on Awake | ||
ComputeGridSize(); | ||
// Sets the TrainingArea name to the name of the base area. | ||
m_TrainingAreaName = baseArea.name; | ||
} | ||
|
||
public void OnEnable() | ||
{ | ||
// Adds the training are replicas during OnEnable to ensure they are added before the Academy begins its work. | ||
AddEnvironments(); | ||
} | ||
|
||
/// <summary> | ||
/// Computes the Grid Size for replicating the training area. | ||
/// </summary> | ||
void ComputeGridSize() | ||
{ | ||
// check if running inference, if so, use the num areas set through the component, | ||
// otherwise, pull it from the academy | ||
if (Academy.Instance.Communicator != null) | ||
numAreas = Academy.Instance.NumAreas; | ||
|
||
var rootNumAreas = Mathf.Pow(numAreas, 1.0f / 3.0f); | ||
m_GridSize.x = Mathf.CeilToInt(rootNumAreas); | ||
m_GridSize.y = Mathf.CeilToInt(rootNumAreas); | ||
var zSize = Mathf.CeilToInt((float)numAreas / (m_GridSize.x * m_GridSize.y)); | ||
m_GridSize.z = zSize == 0 ? 1 : zSize; | ||
} | ||
|
||
/// <summary> | ||
/// Adds replicas of the training area to the scene. | ||
/// </summary> | ||
/// <exception cref="UnityAgentsException"></exception> | ||
void AddEnvironments() | ||
{ | ||
if (numAreas > m_GridSize.x * m_GridSize.y * m_GridSize.z) | ||
{ | ||
throw new UnityAgentsException("The number of training areas that you have specified exceeds the size of the grid."); | ||
} | ||
|
||
for (int z = 0; z < m_GridSize.z; z++) | ||
{ | ||
for (int y = 0; y < m_GridSize.y; y++) | ||
{ | ||
for (int x = 0; x < m_GridSize.x; x++) | ||
{ | ||
if (m_areaCount == 0) | ||
{ | ||
// Skip this first area since it already exists. | ||
m_areaCount = 1; | ||
} | ||
else if (m_areaCount < numAreas) | ||
{ | ||
m_areaCount++; | ||
var area = Instantiate(baseArea, new Vector3(x * separation, y * separation, z * separation), Quaternion.identity); | ||
area.name = m_TrainingAreaName; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
11 changes: 11 additions & 0 deletions
11
com.unity.ml-agents/Runtime/Areas/TrainingAreaReplicator.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
61 changes: 61 additions & 0 deletions
61
com.unity.ml-agents/Tests/Editor/Areas/TrainingAreaReplicatorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System.Linq; | ||
using NUnit.Framework; | ||
using Unity.Mathematics; | ||
using Unity.MLAgents.Areas; | ||
using UnityEngine; | ||
|
||
namespace Unity.MLAgents.Tests.Areas | ||
{ | ||
[TestFixture] | ||
public class TrainingAreaReplicatorTests | ||
{ | ||
private TrainingAreaReplicator m_Replicator; | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
var gameObject = new GameObject(); | ||
var trainingArea = new GameObject(); | ||
trainingArea.name = "MyTrainingArea"; | ||
m_Replicator = gameObject.AddComponent<TrainingAreaReplicator>(); | ||
m_Replicator.baseArea = trainingArea; | ||
} | ||
|
||
private static object[] NumAreasCases = | ||
{ | ||
new object[] {1}, | ||
new object[] {2}, | ||
new object[] {5}, | ||
new object[] {7}, | ||
new object[] {8}, | ||
new object[] {64}, | ||
new object[] {63}, | ||
}; | ||
|
||
[TestCaseSource(nameof(NumAreasCases))] | ||
public void TestComputeGridSize(int numAreas) | ||
{ | ||
m_Replicator.numAreas = numAreas; | ||
m_Replicator.Awake(); | ||
m_Replicator.OnEnable(); | ||
var m_CorrectGridSize = int3.zero; | ||
var m_RootNumAreas = Mathf.Pow(numAreas, 1.0f / 3.0f); | ||
m_CorrectGridSize.x = Mathf.CeilToInt(m_RootNumAreas); | ||
m_CorrectGridSize.y = Mathf.CeilToInt(m_RootNumAreas); | ||
m_CorrectGridSize.z = Mathf.CeilToInt((float)numAreas / (m_CorrectGridSize.x * m_CorrectGridSize.y)); | ||
Assert.GreaterOrEqual(m_Replicator.GridSize.x * m_Replicator.GridSize.y * m_Replicator.GridSize.z, m_Replicator.numAreas); | ||
Assert.AreEqual(m_CorrectGridSize, m_Replicator.GridSize); | ||
} | ||
|
||
[Test] | ||
public void TestAddEnvironments() | ||
{ | ||
m_Replicator.numAreas = 10; | ||
m_Replicator.Awake(); | ||
m_Replicator.OnEnable(); | ||
var trainingAreas = Resources.FindObjectsOfTypeAll<GameObject>().Where(obj => obj.name == m_Replicator.TrainingAreaName); | ||
Assert.AreEqual(10, trainingAreas.Count()); | ||
|
||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
com.unity.ml-agents/Tests/Editor/Areas/TrainingAreaReplicatorTests.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.