Esempio n. 1
0
        //Run a generation given a particular fitness simulation, this simulation must implement NNPopulationSimulation
        public void RunGeneration(Type simulationType, bool showSimulation, BreedingFunction function)
        {
            //Calculate the fitnesses of a given simiulation, this simulation must implement NNPopulationSimulation
            latestFitness = CalculateFitness(simulationType, showSimulation);

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

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

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

            //Mutate the new population
            Mutate(Params.probabilityOfWeightMutationIfChosenToMutate,
                  (int)(((double)population.Count) * Params.minimumProportionOfMutationsPerPopulationMember),
                  (int)(((double)population.Count) * Params.maximumProportionOfMutationsPerPopulationMember),
                  Params.rangeOfMutationPerturbation);
        }
Esempio n. 2
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. 3
0
        internal static void FillListFromBreedingPop(List <T> nextGen, List <T> BreedingPop, int targetSize, BreedingFunction func)
        {
            int elitismNum = (int)(OptoGlobals.ElitismPercent * targetSize);

            while (nextGen.Count < targetSize)
            {
                int j = OptoGlobals.RNG.Next(0, elitismNum), k = OptoGlobals.RNG.Next(0, BreedingPop.Count);
                while (k == j)
                {
                    k = OptoGlobals.RNG.Next(0, BreedingPop.Count);
                }
                foreach (T newGuy in func(nextGen[j], BreedingPop[k]))
                {
                    nextGen.Add(newGuy);
                }
            }
            while (nextGen.Count > targetSize)
            {
                nextGen.RemoveAt(nextGen.Count - 1);
            }
        }