Example #1
0
        public void ItHasADefaultName()
        {
            var chromo = new OrderedChromosome();

            Assert.AreEqual(FirstName.Kanan, chromo.FirstName);
            Assert.AreEqual(LastName.Jarrus, chromo.LastName);
        }
Example #2
0
        public void ItsConstructorDeterminesTheGeneSize()
        {
            var random       = GATestHelper.GetRandomInteger(1, 255);
            var doubleChromo = new OrderedChromosome(random);

            Assert.AreEqual(random, doubleChromo.Genes.Length);
        }
Example #3
0
        protected override Chromosome Perform(Chromosome father, Chromosome mother, GAConfiguration settings)
        {
            var geneCount      = father.Genes.Length;
            var child          = new OrderedChromosome(geneCount);
            var crossoverPoint = settings.GetRandomInteger(1, father.Genes.Length - 1);

            var seen = new HashSet <Gene>();

            for (int i = 0; i < crossoverPoint; i++)
            {
                child.Genes[i] = father.Genes[i];
                seen.Add(father.Genes[i]);
            }

            var count = 0;

            for (int i = 0; i < geneCount; i++)
            {
                if (!seen.Contains(mother.Genes[i]))
                {
                    child.Genes[crossoverPoint + count] = mother.Genes[i];
                    seen.Add(mother.Genes[i]);
                    count++;
                }
            }

            return(child);
        }
Example #4
0
        public void ItCanDetermineIfAChromosomeItShouldRetireByAge()
        {
            _config.RetirementStrategy = RetirementStrategy.MaxAge;
            var chromosome = new OrderedChromosome(8);

            chromosome.Age = 10;

            Assert.AreEqual(true, chromosome.ShouldRetire(_config));
        }
Example #5
0
        public void ItThrowsAnErrorIfTheMotherIsNull()
        {
            var father = new OrderedChromosome(GATestHelper.GetRandomInteger(16, 32));
            var mother = new OrderedChromosome(GATestHelper.GetRandomInteger(1, 8));

            var singlePointCrossover = new SinglePointCrossover();

            singlePointCrossover.Execute(father, null, GATestHelper.GetTravelingSalesmanDefaultConfiguration());
        }
Example #6
0
        public void ItThrowsAnErrorIfTheParentsAreNotLargeEnough()
        {
            var father = new OrderedChromosome(1);
            var mother = new OrderedChromosome(1);

            var singlePointCrossover = new SinglePointCrossover();

            singlePointCrossover.Execute(father, mother, GATestHelper.GetTravelingSalesmanDefaultConfiguration());
        }
Example #7
0
        public void ItDoesNotThrowErrorsIfValidationPasses()
        {
            var size   = GATestHelper.GetRandomInteger(16, 256);
            var father = new OrderedChromosome(size);
            var mother = new OrderedChromosome(size);

            var singlePointCrossover = new SinglePointCrossover();

            singlePointCrossover.Execute(father, mother, GATestHelper.GetTravelingSalesmanDefaultConfiguration());
        }
Example #8
0
        protected override Chromosome Perform(Chromosome father, Chromosome mother, GAConfiguration settings)
        {
            var geneCount = father.Genes.Length;

            DetermineCycle(father, mother);
            var child = new OrderedChromosome(mother.Genes);

            PlaceCycleInsideOfChild(father, child);

            return(child);
        }
Example #9
0
        public void ItCanDetermineIfAChromosomeItShouldRetireByChildrenSired()
        {
            _config.RetirementStrategy = RetirementStrategy.MaxChildren;
            _config.MaxRetirement      = 9;
            var chromosome = new OrderedChromosome(8);

            chromosome.Age      = 0;
            chromosome.Children = 100;

            Assert.AreEqual(true, chromosome.ShouldRetire(_config));
        }
Example #10
0
        private Chromosome[] GetZeroesAndOnesChromosomes()
        {
            var chromosomes = new Chromosome[100];

            for (int i = 0; i < 100; i++)
            {
                chromosomes[i] = new OrderedChromosome(8);
                chromosomes[i].FitnessScore = _random.Next(0, 2);
            }

            return(chromosomes);
        }
Example #11
0
        private Chromosome[] GetStepChromosomes(int offset = 0)
        {
            var chromosomes = new Chromosome[100];

            for (int i = 0; i < 100; i++)
            {
                chromosomes[i] = new OrderedChromosome(8);
                chromosomes[i].FitnessScore = i + offset;
            }

            return(chromosomes);
        }
Example #12
0
        private Chromosome GetRandomNamedChromosome()
        {
            var chromo = new OrderedChromosome();

            var lastName = NameGenerator.GetLastName(_random);

            while (_lastNamesUsed.Contains(lastName))
            {
                lastName = NameGenerator.GetLastName(_random);
            }

            chromo.FirstName = NameGenerator.GetFirstName(_random);
            chromo.LastName  = lastName;

            return(chromo);
        }
        public void ItCanDetermineObjectsAreEqualByReference()
        {
            var data = GetSampleDataset();

            var chromosomeOne = new OrderedChromosome(data);

            var tempdata = data[0];

            data[0] = data[1];
            data[1] = tempdata;

            var chromosomeTwo = new OrderedChromosome(data);

            var areEqual = chromosomeOne.Genes[0] == chromosomeTwo.Genes[1];

            Assert.AreEqual(chromosomeOne.Genes[0], chromosomeTwo.Genes[1]);
            Console.Out.WriteLine("");
        }
Example #14
0
        public static Chromosome[] GenerateOrderedPopulation(GAConfiguration configuration, params Gene[] possibleValues)
        {
            if (possibleValues == null || possibleValues.Length <= 1 || configuration == null)
            {
                throw new ArgumentException("Invalid parameters passed to the genome generator");
            }

            var list = new Chromosome[configuration.PopulationSize];

            for (int i = 0; i < configuration.PopulationSize; i++)
            {
                possibleValues.Shuffle(configuration.RandomPool);
                list[i] = new OrderedChromosome((Gene[])possibleValues.Clone());

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

            return(list);
        }
Example #15
0
        public List <Chromosome> DetermineChildren(Chromosome father, Chromosome mother, int firstCrossover, int secondCrossover)
        {
            _firstCrossoverPoint  = firstCrossover;
            _secondCrossoverPoint = secondCrossover;

            var parent1Genes          = father.Genes;
            var parent1MappingSection = parent1Genes.Skip(_firstCrossoverPoint).Take((_secondCrossoverPoint - _firstCrossoverPoint) + 1).ToArray();

            var parent2Genes          = mother.Genes;
            var parent2MappingSection = parent2Genes.Skip(_firstCrossoverPoint).Take((_secondCrossoverPoint - _firstCrossoverPoint) + 1).ToArray();

            var offspring1 = new OrderedChromosome(father.Genes);
            var offspring2 = new OrderedChromosome(mother.Genes);

            offspring2.ReplaceGenes(_firstCrossoverPoint, parent1MappingSection);
            offspring1.ReplaceGenes(_firstCrossoverPoint, parent2MappingSection);

            var length = father.Genes.Length;

            for (int i = 0; i < length; i++)
            {
                if (i >= _firstCrossoverPoint && i <= _secondCrossoverPoint)
                {
                    continue;
                }

                var geneForUffspring1 = GetGeneNotInMappingSection(parent1Genes[i], parent2MappingSection, parent1MappingSection);
                offspring1.ReplaceGenes(i, geneForUffspring1);

                var geneForoffspring2 = GetGeneNotInMappingSection(parent2Genes[i], parent1MappingSection, parent2MappingSection);
                offspring2.ReplaceGenes(i, geneForoffspring2);
            }

            var children = new List <Chromosome>();

            children.Add(offspring1);
            children.Add(offspring2);
            return(children);
        }
        protected override Chromosome Perform(Chromosome father, Chromosome mother, GAConfiguration settings)
        {
            var geneCount = father.Genes.Length;
            var child     = new OrderedChromosome(geneCount);

            var seen = new HashSet <Gene>();

            for (int i = 0; i < geneCount; i++)
            {
                if (IsOdd(i))
                {
                    for (int k = 0; k < geneCount; k++)
                    {
                        if (!seen.Contains(mother.Genes[k]))
                        {
                            child.Genes[i] = mother.Genes[k];
                            seen.Add(child.Genes[i]);
                            break;
                        }
                    }
                }
                else
                {
                    for (int k = 0; k < geneCount; k++)
                    {
                        if (!seen.Contains(father.Genes[k]))
                        {
                            child.Genes[i] = father.Genes[k];
                            seen.Add(child.Genes[i]);
                            break;
                        }
                    }
                }
            }

            return(child);
        }
Example #17
0
        public void ItCanOutputGenesToCommaDelimitedStringWhenEmpty()
        {
            var ExampleGeneChromo = new OrderedChromosome();

            Assert.AreEqual("", ExampleGeneChromo.ToString());
        }