Example #1
0
        public static void TestSpeciateAdd(
            int popSize,
            int inputNodeCount,
            int outputNodeCount,
            double connectionsProportion,
            IDistanceMetric <double> distanceMetric,
            ISpeciationStrategy <NeatGenome <double>, double> speciationStrategy,
            IRandomSource rng,
            bool validateNearestSpecies = true)
        {
            // Create population.
            NeatPopulation <double> neatPop = CreateNeatPopulation(popSize, inputNodeCount, outputNodeCount, connectionsProportion);

            // Split the population into three.
            int popSize1 = popSize / 3;
            int popSize2 = popSize / 3;
            int popSize3 = popSize - (popSize1 + popSize2);

            var genomeList1 = neatPop.GenomeList.GetRange(0, popSize1);
            var genomeList2 = neatPop.GenomeList.GetRange(popSize1, popSize2);
            var genomeList3 = neatPop.GenomeList.GetRange(popSize1 + popSize2, popSize3);

            for (int i = 0; i < 6; i++)
            {
                int speciesCount = rng.Next(1, (neatPop.GenomeList.Count / 4) + 1);

                var fullGenomeList = new List <NeatGenome <double> >(genomeList1);

                // Invoke speciation strategy, and run tests
                var speciesArr = speciationStrategy.SpeciateAll(genomeList1, speciesCount, rng);
                ValidationTests(speciesArr, distanceMetric, speciesCount, fullGenomeList, validateNearestSpecies);

                // Add second batch of genomes, and re-run tests.
                speciationStrategy.SpeciateAdd(genomeList2, speciesArr, rng);

                fullGenomeList.AddRange(genomeList2);
                ValidationTests(speciesArr, distanceMetric, speciesCount, fullGenomeList, validateNearestSpecies);

                // Add third batch of genomes, and re-run tests.
                speciationStrategy.SpeciateAdd(genomeList3, speciesArr, rng);

                fullGenomeList.AddRange(genomeList3);
                ValidationTests(speciesArr, distanceMetric, speciesCount, fullGenomeList, validateNearestSpecies);
            }
        }
Example #2
0
        /// <summary>
        /// Initialise (or re-initialise) the population species.
        /// </summary>
        /// <param name="speciationStrategy">The speciation strategy to use.</param>
        /// <param name="speciesCount">The required number of species.</param>
        /// <param name="rng">Random source.</param>
        public void InitialiseSpecies(
            ISpeciationStrategy <NeatGenome <T>, T> speciationStrategy,
            int speciesCount,
            IRandomSource rng)
        {
            // Allocate the genomes to species.
            Species <T>[] speciesArr = speciationStrategy.SpeciateAll(this.GenomeList, speciesCount, rng);
            if (null == speciesArr || speciesArr.Length != speciesCount)
            {
                throw new Exception("Species array is null or has incorrect length.");
            }
            this.SpeciesArray = speciesArr;

            // Sort the genomes in each species. Highest fitness first, then secondary sorted by youngest genomes first.
            foreach (Species <T> species in speciesArr)
            {
                SortUtils.SortUnstable(species.GenomeList, GenomeFitnessAndAgeComparer <T> .Singleton, rng);
            }
        }
        /// <summary>
        /// Initialise the strategy.
        /// </summary>
        public void Initialise(Population <NeatGenome <T> > population)
        {
            // Check for expected population type.
            var neatPop = population as NeatPopulation <T>;

            if (null == neatPop)
            {
                throw new ArgumentException("Invalid population type; expected NeatPopulation<T>.", "population");
            }

            // Initialise species.
            var speciesArr = _speciationStrategy.SpeciateAll(population.GenomeList, _speciesCount);

            if (null == speciesArr || speciesArr.Length != _speciesCount)
            {
                throw new Exception("Species array is null or has incorrect length.");
            }

            neatPop.SpeciesArray = speciesArr;
        }
        /// <summary>
        /// Initialise (or re-initialise) the population species.
        /// </summary>
        /// <param name="speciationStrategy">The speciation strategy to use.</param>
        /// <param name="speciesCount">The required number of species.</param>
        /// <param name="genomeComparerDescending">A genome comparer for sorting by fitness in descending order.</param>
        /// <param name="rng">Random source.</param>
        public void InitialiseSpecies(
            ISpeciationStrategy <NeatGenome <T>, T> speciationStrategy,
            int speciesCount,
            IComparer <NeatGenome <T> > genomeComparerDescending,
            IRandomSource rng)
        {
            // Allocate the genomes to species.
            Species <T>[] speciesArr = speciationStrategy.SpeciateAll(this.GenomeList, speciesCount, rng);
            if (speciesArr is null || speciesArr.Length != speciesCount)
            {
                throw new Exception("Species array is null or has incorrect length.");
            }
            this.SpeciesArray = speciesArr;

            // Sort the genomes in each species by primary fitness, highest fitness first.
            // We use an unstable sort; this ensures that the order of equally fit genomes is randomized, which in turn
            // randomizes which genomes are in the subset if elite genomes that are preserved for the next generation - if lots
            // of genomes have equally high fitness.
            foreach (var species in speciesArr)
            {
                SortUtils.SortUnstable(species.GenomeList, genomeComparerDescending, rng);
            }
        }
Example #5
0
        public static void TestSpeciateAll(
            int popSize,
            int inputNodeCount,
            int outputNodeCount,
            double connectionsProportion,
            IDistanceMetric <double> distanceMetric,
            ISpeciationStrategy <NeatGenome <double>, double> speciationStrategy,
            IRandomSource rng,
            bool validateNearestSpecies = true)
        {
            // Create population.
            NeatPopulation <double> neatPop = CreateNeatPopulation(popSize, inputNodeCount, outputNodeCount, connectionsProportion);

            for (int i = 0; i < 6; i++)
            {
                int speciesCount = rng.Next(1, (neatPop.GenomeList.Count / 4) + 1);

                // Invoke speciation strategy.
                var speciesArr = speciationStrategy.SpeciateAll(neatPop.GenomeList, speciesCount, rng);

                // Perform tests.
                ValidationTests(speciesArr, distanceMetric, speciesCount, neatPop.GenomeList, validateNearestSpecies);
            }
        }