Exemplo n.º 1
0
        public void BuildFromDNA(string dna)
        {
            string[] parts = dna.Split(new[] { "||" }, StringSplitOptions.None);

            int inputCount            = int.Parse(parts[0]);
            int outputCount           = int.Parse(parts[1]);
            int depth                 = 0;
            int hiddenNeuronsPerLayer = 0;

            if (parts.Length > 2)
            {
                depth = int.Parse(parts[2]);
            }
            if (parts.Length > 3)
            {
                hiddenNeuronsPerLayer = int.Parse(parts[3]);
            }

            InputLayer = new NeuronsLayer(0, inputCount);

            InputLayer.BuildDendrites(1, 1);

            HiddenLayers = Enumerable.Range(1, depth).Select(i => new NeuronsLayer(i, hiddenNeuronsPerLayer)).ToList();

            OutputLayer = new NeuronsLayer(1 + depth, outputCount);

            HiddenLayers.ForEach(h => h.BuildDendrites(h == HiddenLayers.First() ? inputCount : hiddenNeuronsPerLayer, 0));

            OutputLayer.BuildDendrites(hiddenNeuronsPerLayer, 0);

            if (parts.Length > 4)
            {
                int weightCounter = 4;

                foreach (NeuronsLayer nl in HiddenLayers)
                {
                    foreach (Neuron n in nl.Neurons)
                    {
                        foreach (Dendrite d in n.Dendrites)
                        {
                            d.Weight = double.Parse(parts[weightCounter++]);
                        }
                    }
                }
                foreach (Neuron n in OutputLayer.Neurons)
                {
                    foreach (Dendrite d in n.Dendrites)
                    {
                        d.Weight = double.Parse(parts[weightCounter++]);
                    }
                }
            }
            AllLayers = new List <NeuronsLayer>();
            AllLayers.Add(InputLayer);
            AllLayers.AddRange(HiddenLayers);
            AllLayers.Add(OutputLayer);
        }
 private void Generate()
 {
     if (HiddenLayers.Count != 0)
     {
         HiddenLayers.First().Connect(InputLayer.Neurons, true);
         OutputLayer.Connect(HiddenLayers.Last().Neurons, true);
         if (HiddenLayers.Count > 1)
         {
             for (int i = 0; i < HiddenLayers.Count - 1; i++)
             {
                 HiddenLayers[i + 1].Connect(HiddenLayers[i].Neurons, true);
             }
         }
     }
 }