Esempio n. 1
0
        /// <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 Parameters to be sent.</param>
        public CommunicatorObjects.UnityRLInitializationInput SendAcademyParameters(
            CommunicatorObjects.UnityRLInitializationOutput academyParameters)
        {
            CommunicatorObjects.UnityInput input;
            var initializationInput = new CommunicatorObjects.UnityInput();

            try
            {
                initializationInput = m_communicator.Initialize(
                    new CommunicatorObjects.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);
        }
Esempio n. 2
0
        /// <summary>
        /// Initializes the environment, configures it and initialized the Academy.
        /// </summary>
        private void InitializeEnvironment()
        {
            originalGravity          = Physics.gravity;
            originalFixedDeltaTime   = Time.fixedDeltaTime;
            originalMaximumDeltaTime = Time.maximumDeltaTime;

            InitializeAcademy();
            Communicator communicator = null;

            var exposedBrains    = broadcastHub.broadcastingBrains.Where(x => x != null).ToList();;
            var controlledBrains = broadcastHub.broadcastingBrains.Where(
                x => x != null && x is LearningBrain && broadcastHub.IsControlled(x));

            foreach (LearningBrain brain in controlledBrains)
            {
                brain.SetToControlledExternally();
            }

            // 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;
                if (controlledBrains.ToList().Count > 0)
                {
                    communicator = new RPCCommunicator(
                        new CommunicatorParameters
                    {
                        port = 5005
                    });
                }
            }

            brainBatcher = new Batcher(communicator);

            foreach (var trainingBrain in exposedBrains)
            {
                trainingBrain.SetBatcher(brainBatcher);
            }

            if (communicator != null)
            {
                isCommunicatorOn = true;

                var academyParameters =
                    new CommunicatorObjects.UnityRLInitializationOutput();
                academyParameters.Name    = gameObject.name;
                academyParameters.Version = kApiVersion;
                foreach (var brain in exposedBrains)
                {
                    var bp = brain.brainParameters;
                    academyParameters.BrainParameters.Add(
                        bp.ToProto(brain.name, broadcastHub.IsControlled(brain)));
                }
                academyParameters.EnvironmentParameters =
                    new CommunicatorObjects.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(".") + "/UnitySDK.log";
                using (var fs = File.Open(logPath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
                {
                    logWriter = new StreamWriter(fs);
                    logWriter.WriteLine(System.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 += () => { };
            DestroyAction     += () => { };
            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();
        }