浏览代码

Fix rider warnings and format code. (#2730)

/develop-gpu-test
GitHub 5 年前
当前提交
2a884e6d
共有 10 个文件被更改,包括 82 次插入76 次删除
  1. 2
      UnitySDK/Assets/ML-Agents/Editor/LearningBrainEditor.cs
  2. 6
      UnitySDK/Assets/ML-Agents/Examples/GridWorld/Scripts/GridAgent.cs
  3. 26
      UnitySDK/Assets/ML-Agents/Scripts/Academy.cs
  4. 29
      UnitySDK/Assets/ML-Agents/Scripts/Grpc/RpcCommunicator.cs
  5. 3
      UnitySDK/Assets/ML-Agents/Scripts/HeuristicBrain.cs
  6. 1
      UnitySDK/Assets/ML-Agents/Scripts/ICommunicator.cs
  7. 9
      UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/BarracudaModelParamLoader.cs
  8. 33
      UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/ModelRunner.cs
  9. 14
      UnitySDK/Assets/ML-Agents/Scripts/LearningBrain.cs
  10. 35
      UnitySDK/Assets/ML-Agents/Scripts/Timer.cs

2
UnitySDK/Assets/ML-Agents/Editor/LearningBrainEditor.cs


}
// Display all failed checks
D.logEnabled = false;
Barracuda.Model barracudaModel = null;
Model barracudaModel = null;
if (brain.model != null)
{
barracudaModel = ModelLoader.Load(brain.model.Value);

6
UnitySDK/Assets/ML-Agents/Examples/GridWorld/Scripts/GridAgent.cs


using UnityEngine;
using System.Linq;
using MLAgents;
using UnityEngine.Serialization;
[FormerlySerializedAs("m_Area")]
public GridArea m_Area;
public GridArea area;
public float timeBetweenDecisionsAtInference;
private float m_TimeSinceDecision;

// to be implemented by the developer
public override void AgentReset()
{
m_Area.AreaReset();
area.AreaReset();
}
public void FixedUpdate()

26
UnitySDK/Assets/ML-Agents/Scripts/Academy.cs


SetIsInference(!IsCommunicatorOn);
BrainDecideAction += () => { };
DestroyAction += () => { };
AgentSetStatus += i => { };
AgentResetIfDone += () => { };
AgentSendState += () => { };
AgentAct += () => { };
AgentForceReset += () => { };
BrainDecideAction += () => {};
DestroyAction += () => {};
AgentSetStatus += i => {};
AgentResetIfDone += () => {};
AgentSendState += () => {};
AgentAct += () => {};
AgentForceReset += () => {};
ConfigureEnvironment();
}

void ForcedFullReset()
{
EnvironmentReset();
AgentForceReset();
AgentForceReset?.Invoke();
m_FirstAcademyReset = true;
}

ForcedFullReset();
}
AgentSetStatus(m_StepCount);
AgentSetStatus?.Invoke(m_StepCount);
AgentResetIfDone();
AgentResetIfDone?.Invoke();
AgentSendState();
AgentSendState?.Invoke();
BrainDecideAction();
BrainDecideAction?.Invoke();
}
using (TimerStack.Instance.Scoped("AcademyStep"))

using (TimerStack.Instance.Scoped("AgentAct"))
{
AgentAct();
AgentAct?.Invoke();
}
m_StepCount += 1;

29
UnitySDK/Assets/ML-Agents/Scripts/Grpc/RpcCommunicator.cs


/// Adds the brain to the list of brains which will be sending information to External.
/// </summary>
/// <param name="brainKey">Brain key.</param>
/// <param name="brainParameters">Brain parameters needed to send to the trainer.</param>
public void SubscribeBrain(string brainKey, BrainParameters brainParameters)
{
m_HasQueried[brainKey] = false;

new CommunicatorObjects.UnityRLOutputProto.Types.ListAgentInfoProto());
if (m_CurrentUnityRlInitializationOutput == null)
{
m_CurrentUnityRlInitializationOutput = new CommunicatorObjects.UnityRLInitializationOutputProto();
m_CurrentUnityRlInitializationOutput = new UnityRLInitializationOutputProto();
}
m_CurrentUnityRlInitializationOutput.BrainParameters.Add(brainParameters.ToProto(brainKey, true));
}

switch (command)
{
case CommandProto.Quit:
{
QuitCommandReceived?.Invoke();
return;
}
{
QuitCommandReceived?.Invoke();
return;
}
{
ResetCommandReceived?.Invoke(environmentParametersProto.ToEnvironmentResetParameters());
return;
}
{
ResetCommandReceived?.Invoke(environmentParametersProto.ToEnvironmentResetParameters());
return;
}
{
return;
}
{
return;
}
}
}

/// sent once all the brains that were part of initialization have tried
/// to send information.
/// </summary>
/// <param name="key">Batch Key.</param>
/// <param name="brainKey">Batch Key.</param>
/// <param name="agents">Agent info.</param>
public void PutObservations(string brainKey, ICollection<Agent> agents)
{

/// </summary>
void SendBatchedMessageHelper()
{
var message = new CommunicatorObjects.UnityOutputProto
var message = new UnityOutputProto
{
RlOutput = m_CurrentUnityRlOutput,
};

3
UnitySDK/Assets/ML-Agents/Scripts/HeuristicBrain.cs


using UnityEngine;
using UnityEngine.Serialization;
using Debug = System.Diagnostics.Debug;
#if UNITY_EDITOR
using UnityEditor;
#endif

if ((cDecision != null) && decision == null)
{
decision = CreateInstance(cDecision) as Decision;
Debug.Assert(decision != null,
"Expected Decision instance type: " + cDecision + " to be non-null.");
decision.brainParameters = brainParameters;
}
}

1
UnitySDK/Assets/ML-Agents/Scripts/ICommunicator.cs


/// <param name="key">A key to identify which actions to get</param>
/// <returns></returns>
Dictionary<Agent, AgentAction> GetActions(string key);
}
public interface IBatchedDecisionMaker : IDisposable

9
UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/BarracudaModelParamLoader.cs


/// </summary>
public class BarracudaModelParamLoader
{
private enum ModelActionType
{
Unknown,

);
failedModelChecks.AddRange(
CheckInputTensorPresence(model, brainParameters, memorySize, isContinuous)
);
);
;
;
);
);
);
);
return failedModelChecks;
}

33
UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/ModelRunner.cs


using System.Collections.Generic;
using System.Collections.Generic;
using UnityEngine;
using Barracuda;
using UnityEngine.Profiling;

private NNModel m_Model;
private InferenceDevice m_InferenceDevice;
private Model m_BarracudaModel;
private IWorker m_Engine;
private bool m_Verbose = false;
private string[] m_OutputNames;

/// Initializes the Brain with the Model that it will use when selecting actions for
/// the agents
/// </summary>
/// <param name="model"> The Barracuda model to load
/// <param name="brainParameters"> The parameters of the Brain used to generate the
/// placeholder tensors
/// <param name="inferenceDevice"> Inference execution device. CPU is the fastest
/// option for most of ML Agents models.
/// <param name="model"> The Barracuda model to load </param>
/// <param name="brainParameters"> The parameters of the Brain used to generate the
/// placeholder tensors </param>
/// <param name="inferenceDevice"> Inference execution device. CPU is the fastest
/// option for most of ML Agents models. </param>
/// <param name="seed"> The seed that will be used to initialize the RandomNormal
/// and Multinomial objects used when running inference.</param>
/// <exception cref="UnityAgentsException">Throws an error when the model is null

InferenceDevice inferenceDevice = InferenceDevice.CPU,
int seed = 0)
{
Model barracudaModel;
m_Model = model;
m_InferenceDevice = inferenceDevice;
m_TensorAllocator = new TensorCachingAllocator();

D.logEnabled = m_Verbose;
m_BarracudaModel = ModelLoader.Load(model.Value);
barracudaModel = ModelLoader.Load(model.Value);
m_Engine = BarracudaWorkerFactory.CreateWorker(executionDevice, m_BarracudaModel, m_Verbose);
m_Engine = BarracudaWorkerFactory.CreateWorker(executionDevice, barracudaModel, m_Verbose);
m_BarracudaModel = null;
barracudaModel = null;
m_InferenceInputs = BarracudaModelParamLoader.GetInputTensors(m_BarracudaModel);
m_OutputNames = BarracudaModelParamLoader.GetOutputNames(m_BarracudaModel);
m_TensorGenerator = new TensorGenerator(brainParameters, seed, m_TensorAllocator, m_BarracudaModel);
m_TensorApplier = new TensorApplier(brainParameters, seed, m_TensorAllocator, m_BarracudaModel);
m_InferenceInputs = BarracudaModelParamLoader.GetInputTensors(barracudaModel);
m_OutputNames = BarracudaModelParamLoader.GetOutputNames(barracudaModel);
m_TensorGenerator = new TensorGenerator(brainParameters, seed, m_TensorAllocator, barracudaModel);
m_TensorApplier = new TensorApplier(brainParameters, seed, m_TensorAllocator, barracudaModel);
private Dictionary<string, Tensor> PrepareBarracudaInputs(IEnumerable<TensorProxy> infInputs)
private static Dictionary<string, Tensor> PrepareBarracudaInputs(IEnumerable<TensorProxy> infInputs)
foreach (var inp in m_InferenceInputs)
foreach (var inp in infInputs)
{
inputs[inp.name] = inp.data;
}

public void Dispose()
{
if (m_Engine != null)

14
UnitySDK/Assets/ML-Agents/Scripts/LearningBrain.cs


using UnityEngine;
using Barracuda;
using MLAgents.InferenceBrain;
namespace MLAgents
{

m_BatchedDecisionMaker = communicator;
communicator?.SubscribeBrain(name, brainParameters);
LazyInitialize();
}
/// <inheritdoc />

var comm = aca?.Communicator;
ICommunicator comm = null;
if (aca != null)
{
comm = aca.Communicator;
}
SetCommunicator(comm);
if (aca == null || comm != null)
{

/// <inheritdoc />
protected override void DecideAction()
{
if (m_BatchedDecisionMaker != null)
{
m_BatchedDecisionMaker?.PutObservations(name, m_Agents);
return;
}
m_BatchedDecisionMaker?.PutObservations(name, m_Agents);
}
public void OnDisable()

35
UnitySDK/Assets/ML-Agents/Scripts/Timer.cs


// Compile with: csc CRefTest.cs -doc:Results.xml
using System.Collections.ObjectModel;
using System.IO;
using UnityEngine.Profiling;
using System.Runtime.Serialization;

/// <summary>
/// Number of total ticks elapsed for this node.
/// </summary>
long m_TotalTicks = 0;
long m_TotalTicks;
long m_TickStart = 0;
long m_TickStart;
int m_NumCalls = 0;
int m_NumCalls;
/// <summary>
/// The total recorded ticks for the timer node, plus the currently elapsed ticks

{
get
{
long currentTicks = m_TotalTicks;
var currentTicks = m_TotalTicks;
if (m_TickStart != 0)
{
currentTicks += (System.DateTime.Now.Ticks - m_TickStart);

/// <returns></returns>
public string DebugGetTimerString(string parentName = "", int level = 0)
{
string indent = new string(' ', 2 * level); // TODO generalize
string shortName = (level == 0) ? m_FullName : m_FullName.Replace(parentName + s_Separator, "");
string timerString = "";
var indent = new string(' ', 2 * level); // TODO generalize
var shortName = (level == 0) ? m_FullName : m_FullName.Replace(parentName + s_Separator, "");
string timerString;
if (level == 0)
{
timerString = $"{shortName}(root)\n";

timerString = $"{indent}{shortName}\t\traw={TotalSeconds} rawCount={m_NumCalls}\n";
}
// TODO use stringbuilder? might be overkill since this is only debugging code?
// TODO use StringBuilder? might be overkill since this is only debugging code?
foreach (TimerNode c in m_Children.Values)
foreach (var c in m_Children.Values)
{
timerString += c.DebugGetTimerString(m_FullName, level + 1);
}

/// <summary>
/// A "stack" of timers that allows for lightweight hierarchical profiling of long-running processes.
/// <example>
///
/// <code>
/// for (int i=0; i<5; i++)
/// for (int i=0; i&lt;5; i++)
/// {
/// using(myTimer.Scoped("bar"))
/// {

/// }
/// </code>
/// </example>
/// </summary>
/// <remarks>
/// This implements the Singleton pattern (solution 4) as described in

{
private static readonly TimerStack instance = new TimerStack();
private static readonly TimerStack k_Instance = new TimerStack();
Stack<TimerNode> m_Stack;
TimerNode m_RootNode;

public static TimerStack Instance
{
get { return instance; }
get { return k_Instance; }
}
public TimerNode RootNode

private void Push(string name)
{
TimerNode current = m_Stack.Peek();
TimerNode next = current.GetChild(name);
var current = m_Stack.Peek();
var next = current.GetChild(name);
m_Stack.Push(next);
next.Begin();
}

正在加载...
取消
保存