public void InitializeCoreBrain(MLAgents.Batcher brainBatcher)
 {
     Debug.Assert(trainer != null && trainerInterface != null, "Please specify a trainer in the Trainer field of your Brain!");
     trainerInterface.Initialize();
 }
Exemplo n.º 2
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();
        }
        /// Loads the tensorflow graph model to generate a TFGraph object
        public void InitializeCoreBrain(MLAgents.Batcher brainBatcher)
        {
#if ENABLE_TENSORFLOW
#if UNITY_ANDROID && !UNITY_EDITOR
// This needs to ba called only once and will raise an exception if
// there are multiple internal brains
            try{
                TensorFlowSharp.Android.NativeBinding.Init();
            }
            catch {
            }
#endif
            if ((brainBatcher == null) ||
                (!broadcast))
            {
                this.brainBatcher = null;
            }
            else
            {
                this.brainBatcher = brainBatcher;
                this.brainBatcher.SubscribeBrain(brain.gameObject.name);
            }

            if (graphModel != null)
            {
                graph = new TFGraph();

                graph.Import(graphModel.bytes);

                session = new TFSession(graph);

                // TODO: Make this a loop over a dynamic set of graph inputs

                if ((graphScope.Length > 1) && (graphScope[graphScope.Length - 1] != '/'))
                {
                    graphScope = graphScope + '/';
                }

                if (graph[graphScope + BatchSizePlaceholderName] != null)
                {
                    hasBatchSize = true;
                }

                if ((graph[graphScope + RecurrentInPlaceholderName] != null) &&
                    (graph[graphScope + RecurrentOutPlaceholderName] != null))
                {
                    hasRecurrent = true;
                    var runner = session.GetRunner();
                    runner.Fetch(graph[graphScope + "memory_size"][0]);
                    var networkOutput = runner.Run()[0].GetValue();
                    memorySize = (int)networkOutput;
                }

                if (graph[graphScope + VectorObservationPlacholderName] != null)
                {
                    hasState = true;
                }

                if (graph[graphScope + PreviousActionPlaceholderName] != null)
                {
                    hasPrevAction = true;
                }
                if (graph[graphScope + "value_estimate"] != null)
                {
                    hasValueEstimate = true;
                }
                if (graph[graphScope + ActionMaskPlaceholderName] != null)
                {
                    hasMaskedActions = true;
                }
            }


            observationMatrixList = new List <float[, , , ]>();
            texturesHolder        = new List <Texture2D>();
#endif
        }
Exemplo n.º 4
0
        /// <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();
        }