Beispiel #1
0
        public NeatNeuralNetwork(NeatNeuralNetwork Main)
        {
            if (Main == null)
            {
                throw new ArgumentException("A null value cannot be cloned.", "Main");
            }

            this.TopAncestorSeed = Main.TopAncestorSeed;
            TheRandomizer        = new System.Random(Main.TheRandomizer.Next());

            this.InputNeuronCount  = Main.InputNeuronCount;
            this.OutputNeuronCount = Main.OutputNeuronCount;

            AllNeurons = new OrderedDictionary(Main.AllNeurons.Count);

            foreach (DictionaryEntry aDictionaryEntry in Main.AllNeurons)
            {
                AllNeurons.Add(aDictionaryEntry.Key, new NeatNeuron(aDictionaryEntry.Value as NeatNeuron));
            }
        }
Beispiel #2
0
        private void Awake()
        {
            //Application.targetFrameRate = -1;
            MainCamera = Camera.main;

            if (TheAcademyType == AcademyType.External)
            {
                using (MemoryStream TheMemoryStream = new MemoryStream(ExternalNetworkData.bytes))
                {
                    BestNeatNeuralNetwork = new NeatNeuralNetwork(TheMemoryStream);
                }
            }
            else
            {
                BestNeatNeuralNetwork = new NeatNeuralNetwork(NeuralNetworkInputCount, NeuralNetworkOutputCount);
            }

            if (TheNeatVisualiser)
            {
                TheNeatVisualiser.VisualizeNetwork(BestNeatNeuralNetwork);
            }
        }
Beispiel #3
0
        public void SubmitAgentReward(double Reward, NeatAgent TheAgent)
        {
            if (MainCamera && RenderStateUpdates != MainCamera.enabled)
            {
                MainCamera.enabled = RenderStateUpdates;
            }

            CurrentIteration++;

            if (ShouldSaveNetworkInfo && CurrentIteration % SaveIterationInterval == 0)
            {
                SaveNetwork();
            }

            if (ShouldPreviewOnly)
            {
                TheAgent.TheNeatNeuralNetwork = new NeatNeuralNetwork(BestNeatNeuralNetwork);
            }
            else
            {
                if (Reward > double.MinValue && Reward > BestReward)
                {
                    BestReward            = Reward;
                    BestNeatNeuralNetwork = TheAgent.TheNeatNeuralNetwork;

                    if (VisualizeNetwork)
                    {
                        TheNeatVisualiser.VisualizeNetwork(BestNeatNeuralNetwork);
                    }
                }

                NeatNeuralNetwork NewNetwork = new NeatNeuralNetwork(BestNeatNeuralNetwork);
                NewNetwork.Mutate(UsePointNodeMutation: false);
                TheAgent.TheNeatNeuralNetwork = NewNetwork;
            }
        }
 public void VisualizeNetwork(NeatNeuralNetwork BestNeuralNetwork)
 {
     StartCoroutine(VisualizeNetworkEnum(BestNeuralNetwork));
 }
        IEnumerator VisualizeNetworkEnum(NeatNeuralNetwork BestNeuralNetwork)
        {
            foreach (Transform aNeuron in NeuronsParent)
            {
                Destroy(aNeuron.gameObject);
            }

            yield return(null);

            OrderedDictionary Neurons = BestNeuralNetwork.NetworkNeurons;

            if (DebugInfo)
            {
                StringBuilder SB = new StringBuilder();
                foreach (DictionaryEntry aDictionaryEntry in Neurons)
                {
                    SB.Append("Neuron: ");
                    SB.Append((uint)aDictionaryEntry.Key);
                    foreach (NeatNeuralNetwork.INeatConnection anINeatConnection in (aDictionaryEntry.Value as NeatNeuralNetwork.INeatNeuron).NeuronIncomingConnections)
                    {
                        SB.Append("\n\tConnection: [Weight: ");
                        SB.Append(anINeatConnection.ConnectionWeight);
                        SB.Append(", OtherUID: ");
                        SB.Append(anINeatConnection.OtherNeuronUniqueID);
                        SB.Append("]");
                    }
                    SB.AppendLine();
                }
                print(SB);
            }

            int i = 0;

            for (i = 0; i < BestNeuralNetwork.InputNeuronCount + 1; i++)
            {
                uint NeuronUID = (Neurons[i] as NeatNeuralNetwork.INeatNeuron).UniqueID;

                RectTransform CurrentNeuron = Instantiate <GameObject>(NeuronPrefab, NeuronsParent).GetComponent <RectTransform>();
                CurrentNeuron.name = NeuronUID.ToString();
                CurrentNeuron.GetComponent <RectTransform>().anchoredPosition = new Vector2(-CurrentNeuron.rect.x - RectT.rect.width / 2,
                                                                                            (RectT.rect.height / 2) - (RectT.rect.height / (BestNeuralNetwork.InputNeuronCount + 1f)) * (i + 0.5f));

                if (i == BestNeuralNetwork.InputNeuronCount)
                {
                    CurrentNeuron.Find("BiasText").gameObject.SetActive(true);
                }
                else
                {
                    CurrentNeuron.Find("InputText").gameObject.SetActive(true);
                }
            }

            for (; i < BestNeuralNetwork.InputNeuronCount + BestNeuralNetwork.HiddenNeuronCount + 1; i++)
            {
                uint NeuronUID = (Neurons[i] as NeatNeuralNetwork.INeatNeuron).UniqueID;

                Transform CurrentNeuron = Instantiate <GameObject>(NeuronPrefab, NeuronsParent).transform;

                CurrentNeuron.name = NeuronUID.ToString();
                CurrentNeuron.GetComponent <RectTransform>().anchoredPosition = new Vector2(
                    (RectT.rect.width / (BestNeuralNetwork.HiddenNeuronCount + 1)) * (i - BestNeuralNetwork.InputNeuronCount) - (RectT.rect.width / 2),
                    (((float)(new System.Random((int)NeuronUID)).NextDouble() * 2) - 1) * (RectT.rect.height / 3));
            }

            for (; i < BestNeuralNetwork.TotalNeuronCount; i++)
            {
                uint NeuronUID = (Neurons[i] as NeatNeuralNetwork.INeatNeuron).UniqueID;

                RectTransform CurrentNeuron = Instantiate <GameObject>(NeuronPrefab, NeuronsParent).GetComponent <RectTransform>();
                CurrentNeuron.name = NeuronUID.ToString();
                CurrentNeuron.transform.Find("OutputText").gameObject.SetActive(true);

                CurrentNeuron.GetComponent <RectTransform>().anchoredPosition = new Vector2(CurrentNeuron.rect.x + RectT.rect.width / 2,
                                                                                            (RectT.rect.height / 2) - (RectT.rect.height / (BestNeuralNetwork.OutputNeuronCount + 1f)) *
                                                                                            (i - BestNeuralNetwork.TotalNeuronCount + BestNeuralNetwork.OutputNeuronCount + 1f));
            }

            foreach (Transform aConnection in LinesParent)
            {
                Destroy(aConnection.gameObject);
            }

            for (i = 0; i < BestNeuralNetwork.TotalNeuronCount; i++)
            {
                NeatNeuralNetwork.INeatNeuron TheNeatNeuron = Neurons[i] as NeatNeuralNetwork.INeatNeuron;
                uint NeuronUID = TheNeatNeuron.UniqueID;

                ReadOnlyCollection <NeatNeuralNetwork.INeatConnection> IncomingConnections = TheNeatNeuron.NeuronIncomingConnections;
                for (int j = 0; j < IncomingConnections.Count; j++)
                {
                    NeatNeuralNetwork.INeatConnection CurrentConnection = IncomingConnections[j];
                    CreateConnection(NeuronUID, CurrentConnection.OtherNeuronUniqueID,
                                     (float)CurrentConnection.ConnectionWeight,
                                     CurrentConnection.Disabled);
                }
            }
        }