Example #1
0
        protected override Chromosome Perform(Chromosome father, Chromosome mother, GAConfiguration configuration)
        {
            var geneCount = father.Genes.Length;

            var childOne = new UnorderedChromosome(geneCount);
            var childTwo = new UnorderedChromosome(geneCount);

            var crossoverPoint = configuration.GetRandomInteger(1, father.Genes.Length - 1);

            for (int i = 0; i < geneCount; i++)
            {
                if (i < crossoverPoint)
                {
                    childOne.Genes[i] = father.Genes[i];
                    childTwo.Genes[i] = mother.Genes[i];
                }
                else
                {
                    childOne.Genes[i] = mother.Genes[i];
                    childTwo.Genes[i] = father.Genes[i];
                }
            }

            if (configuration.GetNextDouble() < 0.5)
            {
                return(childOne);
            }
            else
            {
                return(childTwo);
            }
        }
Example #2
0
        public static Chromosome[] GenerateUnorderedPopulation(GAConfiguration configuration, Type unorderedGeneType)
        {
            if (configuration == null || unorderedGeneType == null)
            {
                throw new ArgumentException("Invalid parameters passed to the genome generator");
            }

            var isUnorderedGene = typeof(UnorderedGene).IsAssignableFrom(unorderedGeneType);

            if (!isUnorderedGene)
            {
                throw new ArgumentException("Can not create an unordered population from a non-unordered gene type.");
            }
            if (configuration.GeneSize <= 1)
            {
                throw new ArgumentException("Gene size must be larger than 1.");
            }
            var list = new Chromosome[configuration.PopulationSize];

            for (int i = 0; i < configuration.PopulationSize; i++)
            {
                list[i] = new UnorderedChromosome(configuration.GeneSize, unorderedGeneType, configuration.Random);

                list[i].FirstName = NameGenerator.GetFirstName(configuration.RandomFirstNameSeed);
                list[i].LastName  = NameGenerator.GetLastName(configuration.RandomLastNameSeed);
            }

            return(list);
        }
Example #3
0
        public void ItCanDetermineEquality()
        {
            var randomSeed = 22;
            var c1         = new UnorderedChromosome(10, typeof(PhraseGene), new Random(randomSeed));
            var c2         = new UnorderedChromosome(10, typeof(PhraseGene), new Random(randomSeed));

            Assert.AreEqual(true, c1 == c2);
        }
Example #4
0
        public void ItCanMakeAnUnorderedChromosome()
        {
            var random     = new Random();
            var phraseType = typeof(PhraseGene);
            var c          = new UnorderedChromosome(8, phraseType, random);

            Assert.AreEqual(8, c.Genes.Length);
        }
        private Chromosome[] GetUnorderedChromosomes(GAConfiguration config)
        {
            var chromosomes = new Chromosome[config.PopulationSize];

            for (int i = 0; i < config.PopulationSize; i++)
            {
                chromosomes[i] = new UnorderedChromosome(PhraseSolution.Shakespeare.Length, typeof(PhraseGene), _random);
            }

            return(chromosomes);
        }
Example #6
0
        public void ItCanDetermineAFitnessScore()
        {
            var random     = new Random(22);
            var length     = PhraseSolution.Shakespeare.Length;
            var chromosome = new UnorderedChromosome(length, typeof(PhraseGene), random);

            var solution = new PhraseSolution();

            var distance = solution.GetFitnessScoreFor(chromosome);

            Assert.AreNotEqual(0, distance);
        }