/// <summary> /// Sends the academy parameters through the Communicator. /// Is used by the academy to send the AcademyParameters to the communicator. /// </summary> /// <returns>The External Initialization Parameters received.</returns> /// <param name="academyParameters">The Unity Initialization Paramters to be sent.</param> public UnityRLInitializationInput SendAcademyParameters( UnityRLInitializationOutput academyParameters) { UnityInput input; var initializationInput = new UnityInput(); try { initializationInput = m_communicator.Initialize( new UnityOutput { RlInitializationOutput = academyParameters }, out input); } catch { throw new UnityAgentsException( "The Communicator was unable to connect. Please make sure the External " + "process is ready to accept communication with Unity."); } var firstRlInput = input.RlInput; m_command = firstRlInput.Command; m_environmentParameters = firstRlInput.EnvironmentParameters; m_isTraining = firstRlInput.IsTraining; return(initializationInput.RlInitializationInput); }
/// <summary> /// Initializes the environment, configures it and initialized the Academy. /// </summary> private void InitializeEnvironment() { // Retrieve Brain and initialize Academy var brains = GetBrains(gameObject); InitializeAcademy(); Communicator communicator = null; // Try to launch the communicator by usig the arguments passed at launch try { communicator = new RPCCommunicator( new CommunicatorParameters { port = ReadArgs() }); } // If it fails, we check if there are any external brains in the scene // If there are : Launch the communicator on the default port // If there arn't, there is no need for a communicator and it is set // to null catch { communicator = null; var externalBrain = brains.FirstOrDefault(b => b.brainType == BrainType.External); if (externalBrain != null) { communicator = new RPCCommunicator( new CommunicatorParameters { port = 5005 }); } } brainBatcher = new Batcher(communicator); // Initialize Brains and communicator (if present) foreach (var brain in brains) { brain.InitializeBrain(this, brainBatcher); } if (communicator != null) { isCommunicatorOn = true; var academyParameters = new UnityRLInitializationOutput(); academyParameters.Name = gameObject.name; academyParameters.Version = kApiVersion; foreach (var brain in brains) { var bp = brain.brainParameters; academyParameters.BrainParameters.Add( Batcher.BrainParametersConvertor( bp, brain.gameObject.name, (BrainTypeProto) brain.brainType)); } academyParameters.EnvironmentParameters = new EnvironmentParametersProto(); foreach (var key in resetParameters.Keys) { academyParameters.EnvironmentParameters.FloatParameters.Add( key, resetParameters[key] ); } var pythonParameters = brainBatcher.SendAcademyParameters(academyParameters); Random.InitState(pythonParameters.Seed); Application.logMessageReceived += HandleLog; logPath = Path.GetFullPath(".") + "/unity-environment.log"; logWriter = new StreamWriter(logPath, false); logWriter.WriteLine(DateTime.Now.ToString()); logWriter.WriteLine(" "); logWriter.Close(); } // 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. isInference = !isCommunicatorOn; BrainDecideAction += () => { }; AgentSetStatus += (m, d, i) => { }; AgentResetIfDone += () => { }; AgentSendState += () => { }; AgentAct += () => { }; AgentForceReset += () => { }; // Configure the environment using the configurations provided by // the developer in the Editor. SetIsInference(!brainBatcher.GetIsTraining()); ConfigureEnvironment(); }