Ejemplo n.º 1
0
 /// <inheritdoc/>
 public ISpecies CreateSpecies()
 {
     ISpecies species = new BasicSpecies();
     species.Population = this;
     Species.Add(species);
     return species;
 }
Ejemplo n.º 2
0
        /// <summary>
        ///     Create an initial population.
        /// </summary>
        /// <param name="rnd">Random number generator.</param>
        /// <param name="codec">The codec, the type of network to use.</param>
        /// <returns>The population.</returns>
        public static IPopulation InitPopulation(IGenerateRandom rnd, RBFNetworkGenomeCODEC codec)
        {
            // Create a RBF network to get the length.
            var network = new RBFNetwork(codec.InputCount, codec.RbfCount, codec.OutputCount);
            int size = network.LongTermMemory.Length;

            // Create a new population, use a single species.
            IPopulation result = new BasicPopulation(PopulationSize, new DoubleArrayGenomeFactory(size));
            var defaultSpecies = new BasicSpecies {Population = result};
            result.Species.Add(defaultSpecies);

            // Create a new population of random networks.
            for (int i = 0; i < PopulationSize; i++)
            {
                var genome = new DoubleArrayGenome(size);
                network.Reset(rnd);
                Array.Copy(network.LongTermMemory, 0, genome.Data, 0, size);
                defaultSpecies.Add(genome);
            }

            // Set the genome factory to use the double array genome.
            result.GenomeFactory = new DoubleArrayGenomeFactory(size);

            return result;
        }
Ejemplo n.º 3
0
        /// <summary>
        ///     Determine the species.
        /// </summary>
        /// <param name="genomes">The genomes to speciate.</param>
        private void SpeciateAndCalculateSpawnLevels(IList <IGenome> genomes)
        {
            double maxScore = 0;

            if (genomes.Count == 0)
            {
                throw new AIFHError("Can't speciate, the population is empty.");
            }

            IList <ISpecies> speciesCollection = _population.Species;

            if (speciesCollection.Count == 0)
            {
                throw new AIFHError("Can't speciate, there are no species.1");
            }

            // calculate compatibility between genomes and species
            AdjustCompatibilityThreshold();

            // assign genomes to species (if any exist)
            foreach (IGenome g in genomes)
            {
                ISpecies currentSpecies = null;
                IGenome  genome         = g;

                if (!Double.IsNaN(genome.AdjustedScore) &&
                    !double.IsInfinity(genome.AdjustedScore))
                {
                    maxScore = Math.Max(genome.AdjustedScore, maxScore);
                }

                foreach (ISpecies s in speciesCollection)
                {
                    double compatibility = GetCompatibilityScore(genome,
                                                                 s.Leader);

                    if (compatibility <= _compatibilityThreshold)
                    {
                        currentSpecies = s;
                        AddSpeciesMember(s, genome);
                        genome.Species = s;
                        break;
                    }
                }

                // if this genome did not fall into any existing species, create a
                // new species
                if (currentSpecies == null)
                {
                    currentSpecies = new BasicSpecies(_population, genome);
                    _population.Species.Add(currentSpecies);
                }
            }

            //
            double totalSpeciesScore = speciesCollection.Sum(species => species.CalculateShare(_owner.ScoreFunction.ShouldMinimize, maxScore));

            if (speciesCollection.Count == 0)
            {
                throw new AIFHError("Can't speciate, there are no species.2");
            }
            if (totalSpeciesScore < AIFH.DefaultPrecision)
            {
                // This should not happen much, or if it does, only in the
                // beginning.
                // All species scored zero. So they are all equally bad. Just divide
                // up the right to produce offspring evenly.
                DivideEven(speciesCollection);
            }
            else
            {
                // Divide up the number of offspring produced to the most fit
                // species.
                DivideByFittestSpecies(speciesCollection, totalSpeciesScore);
            }

            LevelOff();
        }
Ejemplo n.º 4
0
        /// <summary>
        ///     Create an initial random population of random paths through the cities.
        /// </summary>
        /// <param name="rnd">The random population.</param>
        /// <returns>The population</returns>
        private IPopulation InitPopulation(IGenerateRandom rnd)
        {
            IPopulation result = new BasicPopulation(PopulationSize, null);

            var defaultSpecies = new BasicSpecies();
            defaultSpecies.Population = result;
            for (int i = 0; i < PopulationSize; i++)
            {
                IntegerArrayGenome genome = RandomGenome(rnd);
                defaultSpecies.Add(genome);
            }
            result.GenomeFactory = new IntegerArrayGenomeFactory(_cities.Length);
            result.Species.Add(defaultSpecies);

            return result;
        }
Ejemplo n.º 5
0
        /**
         * Create the initial random population.
         *
         * @return The population.
         */
        private IPopulation InitPopulation() {
        IPopulation result = new BasicPopulation(PlantUniverse.PopulationSize, null);

        BasicSpecies defaultSpecies = new BasicSpecies();
        defaultSpecies.Population = result;
        for (int i = 0; i < PlantUniverse.PopulationSize; i++) {
            DoubleArrayGenome genome = RandomGenome();
            defaultSpecies.Add(genome);
        }
        result.GenomeFactory = new DoubleArrayGenomeFactory(PlantUniverse.GenomeSize);
        result.Species.Add(defaultSpecies);

        return result;
    }
Ejemplo n.º 6
0
        /// <summary>
        ///     Determine the species.
        /// </summary>
        /// <param name="genomes">The genomes to speciate.</param>
        private void SpeciateAndCalculateSpawnLevels(IList<IGenome> genomes)
        {
            double maxScore = 0;

            if (genomes.Count == 0)
            {
                throw new AIFHError("Can't speciate, the population is empty.");
            }

            IList<ISpecies> speciesCollection = _population.Species;

            if (speciesCollection.Count == 0)
            {
                throw new AIFHError("Can't speciate, there are no species.1");
            }

            // calculate compatibility between genomes and species
            AdjustCompatibilityThreshold();

            // assign genomes to species (if any exist)
            foreach (IGenome g in genomes)
            {
                ISpecies currentSpecies = null;
                IGenome genome = g;

                if (!Double.IsNaN(genome.AdjustedScore)
                    && !double.IsInfinity(genome.AdjustedScore))
                {
                    maxScore = Math.Max(genome.AdjustedScore, maxScore);
                }

                foreach (ISpecies s in speciesCollection)
                {
                    double compatibility = GetCompatibilityScore(genome,
                                                                 s.Leader);

                    if (compatibility <= _compatibilityThreshold)
                    {
                        currentSpecies = s;
                        AddSpeciesMember(s, genome);
                        genome.Species = s;
                        break;
                    }
                }

                // if this genome did not fall into any existing species, create a
                // new species
                if (currentSpecies == null)
                {
                    currentSpecies = new BasicSpecies(_population, genome);
                    _population.Species.Add(currentSpecies);
                }
            }

            //
            double totalSpeciesScore = speciesCollection.Sum(species => species.CalculateShare(_owner.ScoreFunction.ShouldMinimize, maxScore));

            if (speciesCollection.Count == 0)
            {
                throw new AIFHError("Can't speciate, there are no species.2");
            }
            if (totalSpeciesScore < AIFH.DefaultPrecision)
            {
                // This should not happen much, or if it does, only in the
                // beginning.
                // All species scored zero. So they are all equally bad. Just divide
                // up the right to produce offspring evenly.
                DivideEven(speciesCollection);
            }
            else
            {
                // Divide up the number of offspring produced to the most fit
                // species.
                DivideByFittestSpecies(speciesCollection, totalSpeciesScore);
            }

            LevelOff();
        }
Ejemplo n.º 7
0
        /// <summary>
        ///     Create an initial random population.
        /// </summary>
        /// <param name="rnd">A random number generator.</param>
        /// <param name="eval">The expression evaluator.</param>
        /// <returns>The new population.</returns>
        private IPopulation InitPopulation(IGenerateRandom rnd, EvaluateExpression eval)
        {
            IPopulation result = new BasicPopulation(PopulationSize, null);

            var defaultSpecies = new BasicSpecies();
            defaultSpecies.Population = result;
            for (int i = 0; i < PopulationSize; i++)
            {
                TreeGenome genome = RandomGenome(rnd, eval);
                defaultSpecies.Add(genome);
            }
            result.GenomeFactory = new TreeGenomeFactory(eval);
            result.Species.Add(defaultSpecies);

            return result;
        }