/// <summary> /// Initializes the environment, configures it and initializes the Academy. /// </summary> void InitializeEnvironment() { TimerStack.Instance.AddMetadata("communication_protocol_version", k_ApiVersion); TimerStack.Instance.AddMetadata("com.unity.ml-agents_version", k_PackageVersion); EnableAutomaticStepping(); SideChannelManager.RegisterSideChannel(new EngineConfigurationChannel()); SideChannelManager.RegisterSideChannel(new TrainingAnalyticsSideChannel()); m_EnvironmentParameters = new EnvironmentParameters(); m_StatsRecorder = new StatsRecorder(); // Try to launch the communicator by using the arguments passed at launch var port = ReadPortFromArgs(); if (port > 0) { Communicator = CommunicatorFactory.Create(); } if (Communicator != null) { // We try to exchange the first message with Python. If this fails, it means // no Python Process is ready to train the environment. In this case, the // environment must use Inference. bool initSuccessful = false; var communicatorInitParams = new CommunicatorInitParameters { port = port, unityCommunicationVersion = k_ApiVersion, unityPackageVersion = k_PackageVersion, name = "AcademySingleton", CSharpCapabilities = new UnityRLCapabilities() }; try { initSuccessful = Communicator.Initialize( communicatorInitParams, out var unityRlInitParameters ); if (initSuccessful) { UnityEngine.Random.InitState(unityRlInitParameters.seed); // We might have inference-only Agents, so set the seed for them too. m_InferenceSeed = unityRlInitParameters.seed; TrainerCapabilities = unityRlInitParameters.TrainerCapabilities; TrainerCapabilities.WarnOnPythonMissingBaseRLCapabilities(); } else { Debug.Log($"Couldn't connect to trainer on port {port} using API version {k_ApiVersion}. Will perform inference instead."); Communicator = null; } } catch (Exception ex) { Debug.Log($"Unexpected exception when trying to initialize communication: {ex}\nWill perform inference instead."); Communicator = null; } } if (Communicator != null) { Communicator.QuitCommandReceived += OnQuitCommandReceived; Communicator.ResetCommandReceived += OnResetCommand; } // If a communicator is enabled/provided, then we assume we are in // training mode. In the absence of a communicator, we assume we are // in inference mode. ResetActions(); }
public RpcCommunicator(CommunicatorInitParameters communicatorInitParameters) { m_CommunicatorInitParameters = communicatorInitParameters; }
/// <summary> /// Sends the initialization parameters through the Communicator. /// Is used by the academy to send initialization parameters to the communicator. /// </summary> /// <returns>Whether the connection was successful.</returns> /// <param name="initParameters">The Unity Initialization Parameters to be sent.</param> /// <param name="initParametersOut">The External Initialization Parameters received.</param> public bool Initialize(CommunicatorInitParameters initParameters, out UnityRLInitParameters initParametersOut) { #if MLA_SUPPORTED_TRAINING_PLATFORM var academyParameters = new UnityRLInitializationOutputProto { Name = initParameters.name, PackageVersion = initParameters.unityPackageVersion, CommunicationVersion = initParameters.unityCommunicationVersion, Capabilities = initParameters.CSharpCapabilities.ToProto() }; UnityInputProto input; UnityInputProto initializationInput; try { initializationInput = Initialize( initParameters.port, new UnityOutputProto { RlInitializationOutput = academyParameters }, out input ); } catch (Exception ex) { if (ex is RpcException rpcException) { switch (rpcException.Status.StatusCode) { case StatusCode.Unavailable: // This is the common case where there's no trainer to connect to. break; case StatusCode.DeadlineExceeded: // We don't currently set a deadline for connection, but likely will in the future. break; default: Debug.Log($"Unexpected gRPC exception when trying to initialize communication: {rpcException}"); break; } } else { Debug.Log($"Unexpected exception when trying to initialize communication: {ex}"); } initParametersOut = new UnityRLInitParameters(); m_Channel.ShutdownAsync().Wait(); return(false); } var pythonPackageVersion = initializationInput.RlInitializationInput.PackageVersion; var pythonCommunicationVersion = initializationInput.RlInitializationInput.CommunicationVersion; TrainingAnalytics.SetTrainerInformation(pythonPackageVersion, pythonCommunicationVersion); var communicationIsCompatible = CheckCommunicationVersionsAreCompatible( initParameters.unityCommunicationVersion, pythonCommunicationVersion ); // Initialization succeeded part-way. The most likely cause is a mismatch between the communicator // API strings, so log an explicit warning if that's the case. if (initializationInput != null && input == null) { if (!communicationIsCompatible) { Debug.LogWarningFormat( "Communication protocol between python ({0}) and Unity ({1}) have different " + "versions which make them incompatible. Python library version: {2}.", pythonCommunicationVersion, initParameters.unityCommunicationVersion, pythonPackageVersion ); } else { Debug.LogWarningFormat( "Unknown communication error between Python. Python communication protocol: {0}, " + "Python library version: {1}.", pythonCommunicationVersion, pythonPackageVersion ); } initParametersOut = new UnityRLInitParameters(); return(false); } UpdateEnvironmentWithInput(input.RlInput); initParametersOut = initializationInput.RlInitializationInput.ToUnityRLInitParameters(); // Be sure to shut down the grpc channel when the application is quitting. Application.quitting += NotifyQuitAndShutDownChannel; return(true); #else initParametersOut = new UnityRLInitParameters(); return(false); #endif }
public UnityRLInitParameters Initialize(CommunicatorInitParameters initParameters) { var academyParameters = new UnityRLInitializationOutputProto { Name = initParameters.name, PackageVersion = initParameters.unityPackageVersion, CommunicationVersion = initParameters.unityCommunicationVersion, Capabilities = initParameters.CSharpCapabilities.ToProto() }; UnityInputProto input; UnityInputProto initializationInput; try { initializationInput = Initialize( new UnityOutputProto { RlInitializationOutput = academyParameters }, out input); var pythonCommunicationVersion = initializationInput.RlInitializationInput.CommunicationVersion; var pythonPackageVersion = initializationInput.RlInitializationInput.PackageVersion; var unityCommunicationVersion = initParameters.unityCommunicationVersion; var communicationIsCompatible = CheckCommunicationVersionsAreCompatible(unityCommunicationVersion, pythonCommunicationVersion, pythonPackageVersion); // Initialization succeeded part-way. The most likely cause is a mismatch between the communicator // API strings, so log an explicit warning if that's the case. if (initializationInput != null && input == null) { if (!communicationIsCompatible) { Debug.LogWarningFormat( "Communication protocol between python ({0}) and Unity ({1}) have different " + "versions which make them incompatible. Python library version: {2}.", pythonCommunicationVersion, initParameters.unityCommunicationVersion, pythonPackageVersion ); } else { Debug.LogWarningFormat( "Unknown communication error between Python. Python communication protocol: {0}, " + "Python library version: {1}.", pythonCommunicationVersion, pythonPackageVersion ); } throw new UnityAgentsException("ICommunicator.Initialize() failed."); } } catch { var exceptionMessage = "The Communicator was unable to connect. Please make sure the External " + "process is ready to accept communication with Unity."; // Check for common error condition and add details to the exception message. var httpProxy = Environment.GetEnvironmentVariable("HTTP_PROXY"); var httpsProxy = Environment.GetEnvironmentVariable("HTTPS_PROXY"); if (httpProxy != null || httpsProxy != null) { exceptionMessage += " Try removing HTTP_PROXY and HTTPS_PROXY from the" + "environment variables and try again."; } throw new UnityAgentsException(exceptionMessage); } UpdateEnvironmentWithInput(input.RlInput); return(initializationInput.RlInitializationInput.ToUnityRLInitParameters()); }