예제 #1
0
        public static NeuroNet Crossover(NeuroNet mother, NeuroNet father)
        {
            NeuroNet child = InitChild(mother, father);

            for (int i = 0; i < child.Weights.Length; i++)
            {
                for (int j = 0; j < child.Weights[i].Length; j++)
                {
                    for (int k = 0; k < child.Weights[i][j].Length; k++)
                    {
                        if (!(k >= mother.Weights[i][j].Length && mother.Weights[i][j].Length < father.Weights[i][j].Length) &&
                            ((k >= father.Weights[i][j].Length && mother.Weights[i][j].Length > father.Weights[i][j].Length) || NeuroHelper.RandomNext(0, 100) > 50))
                        {
                            child.Weights[i][j][k] = mother.Weights[i][j][k];
                        }
                        else
                        {
                            child.Weights[i][j][k] = father.Weights[i][j][k];
                        }
                    }

                    if (!(j >= mother.Biases[i].Length && mother.Biases[i].Length < father.Biases[i].Length) &&
                        ((j >= father.Biases[i].Length && mother.Biases[i].Length > father.Biases[i].Length) || NeuroHelper.RandomNext(0, 100) > 50))
                    {
                        child.Biases[i][j] = mother.Biases[i][j];
                    }
                    else
                    {
                        child.Biases[i][j] = father.Biases[i][j];
                    }
                }
            }

            return(child);
        }
예제 #2
0
        public NeuroNet(NeuroNet copy)
        {
            this.layers = new int[copy.layers.Length];
            for (int i = 0; i < this.layers.Length; i++)
            {
                this.layers[i] = copy.layers[i];
            }

            this.InitNeurons();
            this.InitWeightsAndBiases(copy.Weights, copy.Biases);
        }
예제 #3
0
        public static void Save(NeuroNet neuroNet, string fileName)
        {
            Genome genome            = new Genome(neuroNet);
            string stringifiedGenome = "";

            stringifiedGenome += genome.LayerCount + seperator.ToString();
            stringifiedGenome += genome.NeuronsPerLayer.ToString('_') + seperator;
            stringifiedGenome += genome.Weights.ToString('_') + seperator;
            stringifiedGenome += genome.Biases.ToString('_') + seperator;

            using (StreamWriter streamWriter = new StreamWriter(SAVE_DIRECTORY + fileName + ".genome"))
            {
                streamWriter.Write(stringifiedGenome);
                streamWriter.Flush();
                streamWriter.Close();
            }
        }
예제 #4
0
        public Genome(NeuroNet neuroNet)
        {
            this.LayerCount = neuroNet.LayerCount;

            int[] neuronsPerLayer = new int[LayerCount];
            for (int i = 0; i < neuronsPerLayer.Length; i++)
            {
                neuronsPerLayer[i] = neuroNet.Neurons[i].Length;
            }
            this.NeuronsPerLayer = neuronsPerLayer;

            List <float> Weights = new List <float>();

            for (int i = 1; i < this.LayerCount; i++)
            {
                int index = i - 1;
                for (int j = 0; j < neuroNet.Weights[index].Length; j++)
                {
                    for (int k = 0; k < neuroNet.Weights[index][j].Length; k++)
                    {
                        Weights.Add(neuroNet.Weights[index][j][k]);
                    }
                }
            }
            this.Weights = Weights.ToArray();

            List <float> Biases = new List <float>();

            for (int i = 1; i < this.LayerCount; i++)
            {
                int index = i - 1;
                for (int j = 0; j < neuroNet.Biases[index].Length; j++)
                {
                    Biases.Add(neuroNet.Biases[index][j]);
                }
            }
            this.Biases = Biases.ToArray();
        }
예제 #5
0
        private static NeuroNet InitChild(NeuroNet mother, NeuroNet father)
        {
            int[] fetchedLayers = new int[Math.Max(mother.layers.Length, father.layers.Length)];
            for (int i = 0; i < fetchedLayers.Length; i++)
            {
                if (mother.layers[i] >= father.layers[i])
                {
                    fetchedLayers[i] = mother.layers[i];
                }
                else
                {
                    fetchedLayers[i] = father.layers[i];
                }
            }

            NeuroNet child = new NeuroNet
            {
                layers = fetchedLayers
            };

            child.InitNeurons();
            child.InitWeightsAndBiases();
            return(child);
        }