Пример #1
0
        public NetworkSaveState[] breed(NetworkSaveState parent1, NetworkSaveState parent2, int children)
        {
            NetworkSaveState[] offspring = new NetworkSaveState[children];

            for (int childIndex = 0; childIndex < children; childIndex++)
            {
                NetworkSaveState child = parent1.deepCopy();

                for (int i = 0; i < parent2.weights.Count; i++)
                {
                    if (Utils.chance(Settings.crossoverFactor))
                    {
                        child.weights[i] = parent2.weights[i];
                    }
                }

                for (int i = 0; i < child.weights.Count; i++)
                {
                    if (Utils.chance(Settings.mutationRate))
                    {
                        child.weights[i] += Utils.rangeAroundZero(Settings.mutationMax);
                    }
                }

                offspring[childIndex] = child;
            }

            return(offspring);
        }
Пример #2
0
        public void loadState(NetworkSaveState save)
        {
            int prevNeurons  = 0;
            int index        = 0;
            int indexWeights = 0;

            layers = new Layer[save.neuronsPerLayer.Count];

            for (int i = 0; i < save.neuronsPerLayer.Count; i++)
            {
                Layer layer = new Layer(index, save.neuronsPerLayer[i], prevNeurons);

                for (int j = 0; j < layer.neurons.Length; j++)
                {
                    for (int k = 0; k < layer.neurons[j].weights.Length; k++)
                    {
                        layer.neurons[j].weights[k] = save.weights[indexWeights];

                        indexWeights++;
                    }
                }

                prevNeurons = save.neuronsPerLayer[i];
                index++;

                layers[i] = layer;
            }
        }
Пример #3
0
        public NetworkSaveState[] firstGenerationNetworks()
        {
            NetworkSaveState[] gen = new NetworkSaveState[Settings.populationSize];

            for (int i = 0; i < Settings.populationSize; i++)
            {
                Network neuralNet = new Network(Settings.inputNeurons, Settings.hiddenNeurons, Settings.ouputNeurons);

                gen[i] = neuralNet.saveState();
            }

            generations.Add(new Generation());
            return(gen);
        }
Пример #4
0
        public NetworkSaveState saveState()
        {
            NetworkSaveState save = new NetworkSaveState();

            for (int i = 0; i < layers.Length; i++)
            {
                save.neuronsPerLayer.Add(layers[i].neurons.Length);

                for (int j = 0; j < layers[i].neurons.Length; j++)
                {
                    for (int k = 0; k < layers[i].neurons[j].weights.Length; k++)
                    {
                        save.weights.Add(layers[i].neurons[j].weights[k]);
                    }
                }
            }

            return(save);
        }