浏览代码

Communicator factory (#4965)

/develop/gail-srl-hack
GitHub 4 年前
当前提交
0ccca975
共有 4 个文件被更改,包括 54 次插入41 次删除
  1. 8
      com.unity.ml-agents/Runtime/Academy.cs
  2. 49
      com.unity.ml-agents/Runtime/Communicator/RpcCommunicator.cs
  3. 35
      com.unity.ml-agents/Runtime/Communicator/CommunicatorFactory.cs
  4. 3
      com.unity.ml-agents/Runtime/Communicator/CommunicatorFactory.cs.meta

8
com.unity.ml-agents/Runtime/Academy.cs


var port = ReadPortFromArgs();
if (port > 0)
{
Communicator = new RpcCommunicator(
new CommunicatorInitParameters
{
port = port
}
);
Communicator = CommunicatorFactory.Create();
}
if (Communicator != null)

bool initSuccessful = false;
var communicatorInitParams = new CommunicatorInitParameters
{
port = port,
unityCommunicationVersion = k_ApiVersion,
unityPackageVersion = k_PackageVersion,
name = "AcademySingleton",

49
com.unity.ml-agents/Runtime/Communicator/RpcCommunicator.cs


# if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_STANDALONE_LINUX
#if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_STANDALONE_LINUX
#endif
#if UNITY_EDITOR
using UnityEditor;
#endif

Dictionary<string, ActionSpec> m_UnsentBrainKeys = new Dictionary<string, ActionSpec>();
#if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_STANDALONE_LINUX
#endif
/// The communicator parameters sent at construction
CommunicatorInitParameters m_CommunicatorInitParameters;
/// <param name="communicatorInitParameters">Communicator parameters.</param>
public RpcCommunicator(CommunicatorInitParameters communicatorInitParameters)
public RpcCommunicator()
m_CommunicatorInitParameters = communicatorInitParameters;
#region Initialization
#region Initialization
internal static bool CheckCommunicationVersionsAreCompatible(
string unityCommunicationVersion,

try
{
initializationInput = Initialize(
initParameters.port,
new UnityOutputProto
{
RlInitializationOutput = academyParameters

SendCommandEvent(rlInput.Command);
}
UnityInputProto Initialize(UnityOutputProto unityOutput, out UnityInputProto unityInput)
UnityInputProto Initialize(int port, UnityOutputProto unityOutput, out UnityInputProto unityInput)
#if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_STANDALONE_LINUX
var channel = new Channel(
"localhost:" + m_CommunicatorInitParameters.port,
ChannelCredentials.Insecure);
var channel = new Channel($"localhost:{port}", ChannelCredentials.Insecure);
m_Client = new UnityToExternalProto.UnityToExternalProtoClient(channel);
var result = m_Client.Exchange(WrapMessage(unityOutput, 200));

QuitCommandReceived?.Invoke();
}
return result.UnityInput;
#else
throw new UnityAgentsException("You cannot perform training on this platform.");
#endif
#endregion
#endregion
#region Destruction
#region Destruction
/// <summary>
/// Close the communicator gracefully on both sides of the communication.

#if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_STANDALONE_LINUX
if (!m_IsOpen)
{
return;

{
// ignored
}
#else
throw new UnityAgentsException(
"You cannot perform training on this platform.");
#endif
#endregion
#endregion
#region Sending Events
#region Sending Events
void SendCommandEvent(CommandProto command)
{

}
}
#endregion
#endregion
#region Sending and retreiving data
#region Sending and retreiving data
public void DecideBatch()
{

/// <param name="unityOutput">The UnityOutput to be sent.</param>
UnityInputProto Exchange(UnityOutputProto unityOutput)
{
#if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_STANDALONE_LINUX
if (!m_IsOpen)
{
return null;

QuitCommandReceived?.Invoke();
return null;
}
#else
throw new UnityAgentsException(
"You cannot perform training on this platform.");
#endif
}
/// <summary>

}
}
#endregion
#endregion
#if UNITY_EDITOR
/// <summary>

#endif
}
}
#endif // UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_STANDALONE_LINUX

35
com.unity.ml-agents/Runtime/Communicator/CommunicatorFactory.cs


namespace Unity.MLAgents
{
/// <summary>
/// Factory class for an ICommunicator instance. This is used to the <see cref="Academy"/> at startup.
/// By default, on desktop platforms, an ICommunicator will be created and attempt to connect
/// to a trainer. This behavior can be prevented by setting <see cref="CommunicatorFactory.Enabled"/> to false
/// *before* the <see cref="Academy"/> is initialized.
/// </summary>
public static class CommunicatorFactory
{
static bool s_Enabled = true;
/// <summary>
/// Whether or not an ICommunicator instance will be created when the <see cref="Academy"/> is initialized.
/// Changing this has no effect after the <see cref="Academy"/> has already been initialized.
/// </summary>
public static bool Enabled
{
get => s_Enabled;
set => s_Enabled = value;
}
internal static ICommunicator Create()
{
#if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_STANDALONE_LINUX
if (s_Enabled)
{
return new RpcCommunicator();
}
#endif
// Non-desktop or disabled
return null;
}
}
}

3
com.unity.ml-agents/Runtime/Communicator/CommunicatorFactory.cs.meta


fileFormatVersion: 2
guid: 0b604cddc07e4484a2cdaba630a971ea
timeCreated: 1613617949
正在加载...
取消
保存