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
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ void OverrideModel()
var nnModel = GetModelForBehaviorName(bp.behaviorName);
Debug.Log($"Overriding behavior {bp.behaviorName} for agent with model {nnModel?.name}");
// This might give a null model; that's better because we'll fall back to the Heuristic
m_Agent.GiveModel($"Override_{bp.behaviorName}", nnModel, InferenceDevice.CPU);
m_Agent.GiveModel($"Override_{bp.behaviorName}", nnModel);

}
}
Expand Down
10 changes: 3 additions & 7 deletions Project/Assets/ML-Agents/Examples/Soccer/Scripts/AgentSoccer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@ public enum Team
[HideInInspector]
public Rigidbody agentRb;
SoccerSettings m_SoccerSettings;
Renderer m_AgentRenderer;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andrewcoh A few unused variables from soccer. Just want to make sure there's no plans to bring these back.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I missed this. Good to remove.

BehaviorParameters m_BP;
BehaviorParameters m_BehaviorParameters;
Vector3 m_Transform;

public override void InitializeAgent()
{
base.InitializeAgent();
m_BP = gameObject.GetComponent<BehaviorParameters>();
if (m_BP.m_TeamID == (int)Team.Blue)
m_BehaviorParameters = gameObject.GetComponent<BehaviorParameters>();
if (m_BehaviorParameters.m_TeamID == (int)Team.Blue)
{
team = Team.Blue;
m_Transform = new Vector3(transform.position.x - 4f, .5f, transform.position.z);
Expand All @@ -44,7 +43,6 @@ public override void InitializeAgent()
team = Team.Purple;
m_Transform = new Vector3(transform.position.x + 4f, .5f, transform.position.z);
}
m_AgentRenderer = GetComponentInChildren<Renderer>();
m_SoccerSettings = FindObjectOfType<SoccerSettings>();
agentRb = GetComponent<Rigidbody>();
agentRb.maxAngularVelocity = 500;
Expand All @@ -65,8 +63,6 @@ public void MoveAgent(float[] act)
var dirToGo = Vector3.zero;
var rotateDir = Vector3.zero;

var action = Mathf.FloorToInt(act[0]);

m_KickPower = 0f;

var forwardAxis = (int)act[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,9 @@ public class SoccerFieldArea : MonoBehaviour
public GameObject goalTextUI;
[HideInInspector]
public bool canResetBall;
Material m_GroundMaterial;
Renderer m_GroundRenderer;

SoccerSettings m_SoccerSettings;

void Awake()
{
m_SoccerSettings = FindObjectOfType<SoccerSettings>();
m_GroundRenderer = centerPitch.GetComponent<Renderer>();
m_GroundMaterial = m_GroundRenderer.material;
canResetBall = true;
if (goalTextUI) { goalTextUI.SetActive(false); }
ballRb = ball.GetComponent<Rigidbody>();
Expand Down
26 changes: 13 additions & 13 deletions com.unity.ml-agents/Runtime/Academy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ public class Academy : IDisposable
const int k_EditorTrainingPort = 5004;

// Lazy initializer pattern, see https://csharpindepth.com/articles/singleton#lazy
static Lazy<Academy> lazy = new Lazy<Academy>(() => new Academy());
static Lazy<Academy> s_Lazy = new Lazy<Academy>(() => new Academy());

public static bool IsInitialized
{
get { return lazy.IsValueCreated; }
get { return s_Lazy.IsValueCreated; }
}

public static Academy Instance { get { return lazy.Value; } }
public static Academy Instance { get { return s_Lazy.Value; } }

public IFloatProperties FloatProperties;

Expand Down Expand Up @@ -108,30 +108,30 @@ public bool IsCommunicatorOn

// Signals to all the Agents at each environment step so they can use
// their Policy to decide on their next action.
internal event System.Action DecideAction;
internal event Action DecideAction;

// Signals to all the listeners that the academy is being destroyed
internal event System.Action DestroyAction;
internal event Action DestroyAction;

// Signals to all the agents at each environment step along with the
// Academy's maxStepReached, done and stepCount values. The agents rely
// on this event to update their own values of max step reached and done
// in addition to aligning on the step count of the global episode.
internal event System.Action<int> AgentSetStatus;
internal event Action<int> AgentSetStatus;

// Signals to all the agents at each environment step so they can send
// their state to their Policy if they have requested a decision.
internal event System.Action AgentSendState;
internal event Action AgentSendState;

// Signals to all the agents at each environment step so they can act if
// they have requested a decision.
internal event System.Action AgentAct;
internal event Action AgentAct;

// Signals to all the agents each time the Academy force resets.
internal event System.Action AgentForceReset;
internal event Action AgentForceReset;

// Signals that the Academy has been reset by the training process
public event System.Action OnEnvironmentReset;
public event Action OnEnvironmentReset;

AcademyFixedUpdateStepper m_FixedUpdateStepper;
GameObject m_StepperObject;
Expand Down Expand Up @@ -273,13 +273,13 @@ void InitializeEnvironment()
//environment must use Inference.
try
{
var unityRLInitParameters = Communicator.Initialize(
var unityRlInitParameters = Communicator.Initialize(
new CommunicatorInitParameters
{
version = k_ApiVersion,
name = "AcademySingleton",
});
UnityEngine.Random.InitState(unityRLInitParameters.seed);
UnityEngine.Random.InitState(unityRlInitParameters.seed);
}
catch
{
Expand Down Expand Up @@ -471,7 +471,7 @@ public void Dispose()
m_Initialized = false;

// Reset the Lazy instance
lazy = new Lazy<Academy>(() => new Academy());
s_Lazy = new Lazy<Academy>(() => new Academy());
}
}
}
1 change: 0 additions & 1 deletion com.unity.ml-agents/Runtime/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ public abstract class Agent : MonoBehaviour
/// List of sensors used to generate observations.
/// Currently generated from attached SensorComponents, and a legacy VectorSensor
/// </summary>
[FormerlySerializedAs("m_Sensors")]
internal List<ISensor> sensors;

/// <summary>
Expand Down
12 changes: 4 additions & 8 deletions com.unity.ml-agents/Runtime/DecisionRequester.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using Barracuda;
using MLAgents.Sensor;
using UnityEngine.Serialization;

namespace MLAgents
{
Expand All @@ -24,11 +20,11 @@ public class DecisionRequester : MonoBehaviour
[Tooltip("Whether or not Agent decisions should start at a random offset.")]
public bool offsetStep;

private Agent m_Agent;
private int offset;
Agent m_Agent;
int m_Offset;
public void Awake()
{
offset = offsetStep ? gameObject.GetInstanceID() : 0;
m_Offset = offsetStep ? gameObject.GetInstanceID() : 0;
m_Agent = gameObject.GetComponent<Agent>();
Academy.Instance.AgentSetStatus += MakeRequests;
}
Expand All @@ -43,7 +39,7 @@ void OnDestroy()

void MakeRequests(int count)
{
if ((count + offset) % DecisionPeriod == 0)
if ((count + m_Offset) % DecisionPeriod == 0)
{
m_Agent?.RequestDecision();
}
Expand Down
4 changes: 2 additions & 2 deletions com.unity.ml-agents/Runtime/EpisodeIdCounter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ namespace MLAgents
{
internal static class EpisodeIdCounter
{
private static int Counter;
static int s_Counter;
public static int GetEpisodeId()
{
return Counter++;
return s_Counter++;
}
}
}
1 change: 0 additions & 1 deletion com.unity.ml-agents/Runtime/Grpc/GrpcExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using Google.Protobuf;
using Google.Protobuf.Collections;
using MLAgents.CommunicatorObjects;
using MLAgents.Sensor;
using UnityEngine;
Expand Down
12 changes: 7 additions & 5 deletions com.unity.ml-agents/Runtime/Grpc/RpcCommunicator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ public void DecideBatch()
/// Sends the observations of one Agent.
/// </summary>
/// <param name="behaviorName">Batch Key.</param>
/// <param name="agent">Agent info.</param>
/// <param name="info">Agent info.</param>
/// <param name="sensors">Sensors that will produce the observations</param>
public void PutObservations(string behaviorName, AgentInfo info, List<ISensor> sensors)
{
# if DEBUG
Expand Down Expand Up @@ -465,13 +466,14 @@ void UpdateSentBrainParameters(UnityRLInitializationOutputProto output)
/// <param name="sideChannel"> The side channel to be registered.</param>
public void RegisterSideChannel(SideChannel sideChannel)
{
if (m_SideChannels.ContainsKey(sideChannel.ChannelType()))
var channelType = sideChannel.ChannelType();
if (m_SideChannels.ContainsKey(channelType))
{
throw new UnityAgentsException(string.Format(
"A side channel with type index {} is already registered. You cannot register multiple " +
"side channels of the same type."));
"A side channel with type index {0} is already registered. You cannot register multiple " +
"side channels of the same type.", channelType));
}
m_SideChannels.Add(sideChannel.ChannelType(), sideChannel);
m_SideChannels.Add(channelType, sideChannel);
}

/// <summary>
Expand Down
3 changes: 0 additions & 3 deletions com.unity.ml-agents/Runtime/ICommunicator.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using MLAgents.CommunicatorObjects;
using MLAgents.Sensor;

namespace MLAgents
Expand Down Expand Up @@ -44,7 +43,6 @@ internal struct UnityRLInputParameters
/// <summary>
/// Delegate for handling reset parameter updates sent from the communicator.
/// </summary>
/// <param name="resetParams"></param>
internal delegate void ResetCommandHandler();

/// <summary>
Expand Down Expand Up @@ -122,7 +120,6 @@ internal interface ICommunicator : IDisposable
/// <param name="brainKey">Batch Key.</param>
/// <param name="info">Agent info.</param>
/// <param name="sensors">The list of ISensors of the Agent.</param>
/// <param name="action">The action that will be called once the next AgentAction is ready.</param>
void PutObservations(string brainKey, AgentInfo info, List<ISensor> sensors);

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions com.unity.ml-agents/Runtime/InferenceBrain/ApplierImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public void Apply(TensorProxy tensorProxy, IEnumerable<int> actionIds, Dictionar
var memorySize = (int)tensorProxy.shape[tensorProxy.shape.Length - 1];
foreach (int agentId in actionIds)
{
List<float> memory = null;
List<float> memory;
if (!m_Memories.TryGetValue(agentId, out memory)
|| memory.Count < memorySize)
{
Expand Down Expand Up @@ -243,7 +243,7 @@ public void Apply(TensorProxy tensorProxy, IEnumerable<int> actionIds, Dictionar

foreach (int agentId in actionIds)
{
List<float> memory = null;
List<float> memory;
if (!m_Memories.TryGetValue(agentId, out memory)
|| memory.Count < memorySize * m_MemoriesCount)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public void Generate(TensorProxy tensorProxy, int batchSize, IEnumerable<AgentIn
/// </summary>
internal class RecurrentInputGenerator : TensorGenerator.IGenerator
{
private readonly ITensorAllocator m_Allocator;
readonly ITensorAllocator m_Allocator;
Dictionary<int, List<float>> m_Memories;

public RecurrentInputGenerator(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public interface IApplier
/// </param>
/// <param name="actionIds"> List of Agents Ids that will be updated using the tensor's data</param>
/// <param name="lastActions"> Dictionary of AgentId to Actions to be updated</param>
/// </param>
void Apply(TensorProxy tensorProxy, IEnumerable<int> actionIds, Dictionary<int, float[]> lastActions);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public void InitializeObservations(List<ISensor> sensors, ITensorAllocator alloc
/// <param name="tensors"> Enumerable of tensors that will be modified.</param>
/// <param name="currentBatchSize"> The number of agents present in the current batch
/// </param>
/// <param name="agents"> List of Agents that contains the
/// <param name="infos"> List of AgentsInfos and Sensors that contains the
/// data that will be used to modify the tensors</param>
/// <exception cref="UnityAgentsException"> One of the tensor does not have an
/// associated generator.</exception>
Expand Down
2 changes: 1 addition & 1 deletion com.unity.ml-agents/Runtime/Policy/BarracudaPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal class BarracudaPolicy : IPolicy
{
protected ModelRunner m_ModelRunner;

private int m_AgentId;
int m_AgentId;

/// <summary>
/// Sensor shapes for the associated Agents. All Agents must have the same shapes for their Sensors.
Expand Down
15 changes: 8 additions & 7 deletions com.unity.ml-agents/Runtime/Policy/BehaviorParameters.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Barracuda;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;

namespace MLAgents
{
Expand All @@ -13,7 +13,7 @@ namespace MLAgents
public class BehaviorParameters : MonoBehaviour
{
[Serializable]
private enum BehaviorType
enum BehaviorType
{
Default,
HeuristicOnly,
Expand All @@ -37,11 +37,12 @@ private enum BehaviorType
string m_BehaviorName = "My Behavior";
[HideInInspector]
[SerializeField]
public int m_TeamID = 0;
public int m_TeamID;
Copy link
Contributor Author

@chriselion chriselion Feb 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still public with m_ prefixes. Seems like it needs to be mutable at runtime so it should stay public (or have an accessor)

[FormerlySerializedAs("m_useChildSensors")]
[HideInInspector]
[SerializeField]
[Tooltip("Use all Sensor components attached to child GameObjects of this Agent.")]
bool m_useChildSensors = true;
bool m_UseChildSensors = true;

public BrainParameters brainParameters
{
Expand All @@ -50,7 +51,7 @@ public BrainParameters brainParameters

public bool useChildSensors
{
get { return m_useChildSensors; }
get { return m_UseChildSensors; }
}

public string behaviorName
Expand Down Expand Up @@ -93,13 +94,13 @@ internal IPolicy GeneratePolicy(Func<float[]> heuristic)
}

public void GiveModel(
string behaviorName,
string newBehaviorName,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Argument name shadowed the behaviorName property

NNModel model,
InferenceDevice inferenceDevice = InferenceDevice.CPU)
{
m_Model = model;
m_InferenceDevice = inferenceDevice;
m_BehaviorName = behaviorName;
m_BehaviorName = newBehaviorName;
}
}
}
3 changes: 2 additions & 1 deletion com.unity.ml-agents/Runtime/Policy/IPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ internal interface IPolicy : IDisposable
/// will make the decision at a later time to allow possible
/// batching of requests.
/// </summary>
/// <param name="agent"></param>
/// <param name="info"></param>
/// <param name="sensors"></param>
void RequestDecision(AgentInfo info, List<ISensor> sensors);

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions com.unity.ml-agents/Runtime/Sensor/RayPerceptionSensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ public virtual SensorCompressionType GetCompressionType()
/// <param name="transform">Transform of the GameObject</param>
/// <param name="castType">Whether to perform the casts in 2D or 3D.</param>
/// <param name="perceptionBuffer">Output array of floats. Must be (num rays) * (num tags + 2) in size.</param>
/// <param name="layerMask">Filtering options for the casts</param>
/// <param name="debugInfo">Optional debug information output, only used by RayPerceptionSensor.</param>
///
public static void PerceiveStatic(float unscaledRayLength,
Expand Down
2 changes: 1 addition & 1 deletion com.unity.ml-agents/Runtime/Sensor/SensorShapeValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace MLAgents.Sensor
{
internal class SensorShapeValidator
{
private List<int[]> m_SensorShapes;
List<int[]> m_SensorShapes;

/// <summary>
/// Check that the List Sensors are the same shape as the previous ones.
Expand Down
Loading