Esempio n. 1
0
        private int startPopulationSize; //The starting population size of the genetic algorithm

        #endregion Fields

        #region Constructors

        public NNPopulation(int startingPopulationSize, int[] neuralNetworkLayerSizes, Type simulationType)
        {
            //Populate the input variables
            networkStructure = neuralNetworkLayerSizes;
            startPopulationSize = startingPopulationSize;

            //Generate the hidden layer structure from the input network structure
            int[] hiddenLayers = new int[neuralNetworkLayerSizes.Length - 2];
            for (int i = 0; i < hiddenLayers.Length; i++)
                hiddenLayers[i] = neuralNetworkLayerSizes[i + 1];
            hiddenLayerStructure = hiddenLayers;

            //Create the population list
            population = new List<NN>();

            //Generate nerual networks for the genetic algorithm to use to compete
            for (int i = 0; i < startingPopulationSize; i++)
                population.Add(new NN(neuralNetworkLayerSizes[0], neuralNetworkLayerSizes[neuralNetworkLayerSizes.Length - 1], neuralNetworkLayerSizes.Length - 2, hiddenLayers));

            this.simulationType = simulationType;

            this.showSimulation = true;

            this.breedingFunction = BreedingFunction.Crossover;

            this.numberOfGenerations = 0;

            //Create an instance of a NNPopulationSimulation of the specified type
            simulation = (NNPopulationSimulation)Activator.CreateInstance(simulationType, new object[] { population, numFitnessSimulationIterations, numberOfElites * numOfCopiesOfElites });
        }
Esempio n. 2
0
        public void Update(GameTime gameTime)
        {
            if (showSimulation)
                //Show the simulation (the simulation may choose to not perform any operations on this call
                simulation.ShowSimulation();

            simulation.Update(gameTime);

            if (simulation.IsSimulationComplete())
            {
                numberOfGenerations++;

                if (showSimulation)
                    //Close the simulation if it requires that
                    simulation.CloseSimulation();

                latestFitness = simulation.GetSimulationResults();

                KeyValuePair<int, double>[] sortedFitnessList = GetSortedFitnessList(latestFitness);

                //Select the breeding couples given their fitness
                Tuple<int, int>[] breedingCouples = SelectCouplesToBreed(sortedFitnessList);

                //Breed the selected couples
                if (breedingFunction == BreedingFunction.Average)
                    population = BreedAverage(breedingCouples, sortedFitnessList);
                else if (breedingFunction == BreedingFunction.Crossover)
                    population = BreedCrossover(breedingCouples, sortedFitnessList);
                else if (breedingFunction == BreedingFunction.PickEach)
                    population = BreedPickEach(breedingCouples, sortedFitnessList);

                //Mutate the new population
                Mutate(probabilityOfWeightMutationIfChosenToMutate, (int)(((double)population.Count) * minimumProportionOfMutationsPerPopulationMember), (int)(((double)population.Count) * maximumProportionOfMutationsPerPopulationMember), rangeOfMutationPerturbation);

                //Create an instance of a NNPopulationSimulation of the specified type
                simulation = (NNPopulationSimulation)Activator.CreateInstance(simulationType, new object[] { population, numFitnessSimulationIterations, numberOfElites * numOfCopiesOfElites });
            }
        }