Exemple #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_arenasParameters = input.RlResetInput;
            m_isTraining       = firstRlInput.IsTraining;
            return(initializationInput.RlInitializationInput);
        }
        /// <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
            {
                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);
            }

            var firstRlInput = input.RlInput;

            m_Command = firstRlInput.Command;
            m_EnvironmentParameters = firstRlInput.EnvironmentParameters;
            m_IsTraining            = firstRlInput.IsTraining;
            return(initializationInput.RlInitializationInput);
        }
Exemple #3
0
        /// <summary>
        /// Initializes the environment, configures it and initialized the Academy.
        /// </summary>
        private void InitializeEnvironment()
        {
            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";
                logWriter = new StreamWriter(logPath, false);
                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 += () => { };
            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();
        }
Exemple #4
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()
            InitializeAcademy();

            Communicator communicator = null;

            // BroadcastHub: 简单的Brain的列表

            // 获取所有的Brain
            var exposedBrains = broadcastHub.broadcastingBrains.Where(x => x != null).ToList();;

            // 获取受控制的LearningBrain
            var controlledBrains = broadcastHub.broadcastingBrains.Where(
                x => x != null && x is LearningBrain && broadcastHub.IsControlled(x)
                );

            // 设置Brain是Controlled
            foreach (LearningBrain brain in controlledBrains)
            {
                // 标记_isControlled = true;
                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;

                // 如果有,需要控制的LearningBrain
                // 则表示,需要TensorFlow
                // 所以,尝试连接默认端口
                if (controlledBrains.ToList().Count > 0)
                {
                    communicator = new RPCCommunicator(
                        new CommunicatorParameters
                    {
                        port = 5005
                    });
                }
            }

            // 创建Batcher
            brainBatcher = new Batcher(communicator);

            // Brain设置到Batcher
            // Batcher,是用来,连接Brain和Agent的地方
            foreach (var trainingBrain in exposedBrains)
            {
                trainingBrain.SetBatcher(brainBatcher);
            }

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

                // 创建UnityRLInitializationOutput消息
                var academyParameters = new CommunicatorObjects.UnityRLInitializationOutput();
                academyParameters.Name    = gameObject.name;
                academyParameters.Version = kApiVersion;

                // 从需要控制的Brain中
                // 获取BrainParameters
                // 填写到消息中
                foreach (var brain in exposedBrains)
                {
                    var bp = brain.brainParameters;
                    academyParameters.BrainParameters.Add(bp.ToProto(brain.name, broadcastHub.IsControlled(brain)));
                }

                // 填写EnvironmentParameters
                academyParameters.EnvironmentParameters = new CommunicatorObjects.EnvironmentParametersProto();
                foreach (var key in resetParameters.Keys)
                {
                    academyParameters.EnvironmentParameters.FloatParameters.Add(key, resetParameters[key]);
                }

                // Q: 通过Batcher发送消息?
                var pythonParameters = brainBatcher.SendAcademyParameters(academyParameters);

                Random.InitState(pythonParameters.Seed);

                // 监听Unity消息
                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();
        }
Exemple #5
0
        /// <summary>
        /// Initializes the environment, configures it and initialized the Academy.
        /// </summary>
        private void InitializeEnvironment()
        {
            m_OriginalGravity          = Physics.gravity;
            m_OriginalFixedDeltaTime   = Time.fixedDeltaTime;
            m_OriginalMaximumDeltaTime = Time.maximumDeltaTime;

            InitializeAcademy();
            ICommunicator communicator;

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

            foreach (var brain1 in controlledBrains)
            {
                var brain = (LearningBrain)brain1;
                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
                    });
                }
            }

            m_BrainBatcher = new Batcher(communicator);

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

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

                var academyParameters =
                    new CommunicatorObjects.UnityRLInitializationOutput();
                academyParameters.Name    = gameObject.name;
                academyParameters.Version = k_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 = m_BrainBatcher.SendAcademyParameters(academyParameters);
                Random.InitState(pythonParameters.Seed);
            }

            // 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.
            m_IsInference = !m_IsCommunicatorOn;

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


            // Configure the environment using the configurations provided by
            // the developer in the Editor.
            SetIsInference(!m_BrainBatcher.GetIsTraining());
            ConfigureEnvironment();
        }
Exemple #6
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 spawnPrefab            = agentSpawner.GetPrefabFor(GetAgentId());
            var spawnAgentPrefabs      = spawnPrefab.GetComponentsInChildren <Agent>();
            var spawnAgentPrefabBrains = spawnAgentPrefabs
                                         .Where(x => x.brain as LearningBrain != null)
                                         .Select(x => x.brain)
                                         .ToList();
            var spawnerEnabled      = spawnAgentPrefabBrains.Count > 0;
            var hubBrains           = broadcastHub.broadcastingBrains.Where(x => x != null).ToList();;
            var hubControlledBrains = broadcastHub.broadcastingBrains.Where(
                x => x != null && x is LearningBrain && broadcastHub.IsControlled(x));

            IEnumerable <Brain> exposedBrains =
                spawnerEnabled ? spawnAgentPrefabBrains : hubBrains;
            IEnumerable <Brain> controlledBrains = hubControlledBrains;

            if (spawnerEnabled)
            {
                controlledBrains = IsTrainingMode()
                    ? spawnAgentPrefabBrains
                    : new List <Brain>();
            }

            // 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
                    });
                }
            }
            foreach (LearningBrain brain in controlledBrains)
            {
                brain.SetToControlledExternally();
            }

            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();

            if (spawnerEnabled)
            {
                agentSpawner.SpawnSpawnableEnv(this.gameObject, GetNumAgents(), spawnPrefab);
            }
        }