Ejemplo n.º 1
0
        public OrderedGeneticAlgorithm(GAConfiguration configuration, params Gene[] possibleValues)
        {
            Configuration  = configuration;
            PossibleValues = possibleValues;

            StandardConstructorLogic();
        }
Ejemplo n.º 2
0
        private GARun GetRunForExample()
        {
            var solution = new ShakespeareSolution();
            var task     = new GATask(solution);

            task.ParentSelectionStrategy = ParentSelectionStrategy.TournamentFive;
            task.MutationStrategy        = MutationStrategy.Random;
            task.CrossoverStrategy       = CrossoverStrategy.TwoPoint;
            task.ImmigrationStrategy     = ImmigrationStrategy.Dynamic;
            task.RetirementStrategy      = RetirementStrategy.None;
            task.ScoringStrategy         = ScoringStrategy.Lowest;
            task.DuplicationStrategy     = DuplicationStrategy.Prevent;

            task.PopulationSize           = 1000;
            task.MaxGenerations           = 100;
            task.CrossoverRate            = 0.659410;
            task.MutationRate             = 0.074880;
            task.ElitismRate              = 0.021880;
            task.ImmigrationRate          = 0.150480;
            task.MaxRetirement            = 0;
            task.ChildrenPerParents       = 1;
            task.RandomSeed               = 567545391;
            task.RandomPoolGenerationSeed = 22;

            var config = new GAConfiguration(task);

            var gaRun = solution.Run(config);

            return(gaRun);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
        public void ItAllowsAValidRetirementType()
        {
            var task = GATestHelper.GetDummyTravelingSalesmanTask();

            task.RetirementStrategy = RetirementStrategy.MaxChildren;
            var settings = new GAConfiguration(task);
        }
Ejemplo n.º 5
0
        public void ItAllowsAValidMutationType()
        {
            var task = GATestHelper.GetDummyTravelingSalesmanTask();

            task.MutationStrategy = MutationStrategy.Boundary;
            var settings = new GAConfiguration(task);
        }
Ejemplo n.º 6
0
        public void ItAllowsAValidImmigrationType()
        {
            var task = GATestHelper.GetDummyTravelingSalesmanTask();

            task.ImmigrationStrategy = ImmigrationStrategy.Constant;
            var settings = new GAConfiguration(task);
        }
Ejemplo n.º 7
0
        public void ItAllowsAValidCrossoverType()
        {
            var task = GATestHelper.GetDummyTravelingSalesmanTask();

            task.CrossoverStrategy = CrossoverStrategy.AlternatingPosition;
            var settings = new GAConfiguration(task);
        }
Ejemplo n.º 8
0
        public void ItDoesNotAllowsAnInvalidCrossoverType()
        {
            var task = GATestHelper.GetDummyTravelingSalesmanTask();

            task.CrossoverStrategy = 0;
            var settings = new GAConfiguration(task);
        }
Ejemplo n.º 9
0
        public void ItsConstructorsCanSetTheStrategies()
        {
            var task     = GATestHelper.GetDummyTravelingSalesmanTask();
            var settings = new GAConfiguration(task);

            Assert.IsTrue(settings.IsValid());
        }
Ejemplo n.º 10
0
 public override GARun Run(GAConfiguration configuration)
 {
     Configuration          = configuration;
     Configuration.GeneSize = GetGeneSize();
     GeneticAlgorithm       = new UnorderedGeneticAlgorithm(Configuration, GetNewGene(new Random()).GetType());
     return(GeneticAlgorithm.Run());
 }
Ejemplo n.º 11
0
        public void ItAllowsAValidParentSelectionType()
        {
            var task = GATestHelper.GetDummyTravelingSalesmanTask();

            task.ParentSelectionStrategy = ParentSelectionStrategy.RouletteWheel;
            var settings = new GAConfiguration(task);
        }
Ejemplo n.º 12
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);
        }
Ejemplo n.º 13
0
        public void ItDoesNotAllowsAnInvalidParentSelectionType()
        {
            var task = GATestHelper.GetDummyTravelingSalesmanTask();

            task.ParentSelectionStrategy = 0;
            var settings = new GAConfiguration(task);
        }
Ejemplo n.º 14
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);
            }
        }
Ejemplo n.º 15
0
        public Chromosome Execute(Chromosome father, Chromosome mother, GAConfiguration configuration)
        {
            if (father == null || mother == null)
            {
                throw new ArgumentException(INVALID_CHROMOSOME_SIZE);
            }
            if (father.Genes == null || mother.Genes == null)
            {
                throw new ArgumentException(INVALID_CHROMOSOME_SIZE);
            }
            if (father.Genes.Length != mother.Genes.Length)
            {
                throw new ArgumentException(INVALID_CHROMOSOME_SIZE);
            }
            if (father.Genes.Length <= 1)
            {
                throw new ArgumentException(INVALID_CHROMOSOME_SIZE);
            }

            father.Children++;
            mother.Children++;

            var child = Perform(father, mother, configuration);

            child.LastName  = father.LastName;
            child.FirstName = NameGenerator.GetFirstName(configuration.RandomFirstNameSeed);

            child.SetParents(father, mother);

            return(child);
        }
Ejemplo n.º 16
0
        public void ItAllowsRetirementTypeMaxChildrenWhenRetirementMaximumIsAboveOne()
        {
            var task = GATestHelper.GetDummyTravelingSalesmanTask();

            task.RetirementStrategy = RetirementStrategy.MaxChildren;
            task.MaxRetirement      = 2;
            var settings = new GAConfiguration(task);
        }
Ejemplo n.º 17
0
        public void ItAllowsRetirementTypeNoneWhenRetirementMaximumIsBelowZero()
        {
            var task = GATestHelper.GetDummyTravelingSalesmanTask();

            task.RetirementStrategy = RetirementStrategy.None;
            task.MaxRetirement      = 1;
            var settings = new GAConfiguration(task);
        }
Ejemplo n.º 18
0
        public void ItThrowsAnExceptionIfRetirementTypeMaxChildrenWhenRetirementMaximumIsBelowZero()
        {
            var task = GATestHelper.GetDummyTravelingSalesmanTask();

            task.RetirementStrategy = RetirementStrategy.MaxChildren;
            task.MaxRetirement      = 0;
            var settings = new GAConfiguration(task);
        }
Ejemplo n.º 19
0
 public void Setup(Chromosome[] pop, GAConfiguration configuration)
 {
     Configuration = configuration;
     Population    = pop;
     NormalizeAllFitnessScores();
     SetupSelection();
     Validate();
 }
Ejemplo n.º 20
0
 public void Configure(IMvxPluginConfiguration configuration)
 {
     if (!(configuration is GAConfiguration))
     {
         throw new System.Exception("Configuration does not appear to be a valid GAConfiguration.");
     }
     _Configuration = (GAConfiguration)configuration;
 }
Ejemplo n.º 21
0
        public override GARun Run(GAConfiguration configuration)
        {
            Configuration = configuration;

            var options = GetOptions();

            GeneticAlgorithm = new OrderedGeneticAlgorithm(Configuration, options);
            return(GeneticAlgorithm.Run());
        }
Ejemplo n.º 22
0
        public void ItCanCopyObjects()
        {
            var task   = GATestHelper.GetDummyTravelingSalesmanTask();
            var config = new GAConfiguration(task);

            Assert.AreEqual(task.MutationStrategy, config.MutationStrategy);
            Assert.AreEqual(task.CrossoverStrategy, config.CrossoverStrategy);
            Assert.AreEqual(task.ParentSelectionStrategy, config.ParentSelectionStrategy);
        }
Ejemplo n.º 23
0
        private void SetConfiguration()
        {
            _configuration = GATestHelper.GetTravelingSalesmanDefaultConfiguration();
            _configuration.DuplicationStrategy = DuplicationStrategy.Prevent;
            _configuration.PopulationSize      = 10;
            _configuration.MaxRetirement       = 2;
            _configuration.MaxGenerations      = 5;

            _exampleGenes = Array.ConvertAll(GATestHelper.GetTravelingSalesmanChromosome().Genes, option => (TravelingSalesmanGene)option);
        }
Ejemplo n.º 24
0
        public void ItsConstructorsCanSetTheStrategiesWhenUnordered()
        {
            var task = GATestHelper.GetDummyTravelingSalesmanTask();

            task.MutationStrategy = MutationStrategy.Random;

            var settings = new GAConfiguration(task);

            Assert.IsTrue(settings.IsValid());
        }
Ejemplo n.º 25
0
 public void Mutate(Chromosome chromosome, GAConfiguration settings)
 {
     for (int i = 0; i < chromosome.Genes.Length; i++)
     {
         if (settings.GetNextDouble() < settings.MutationRate)
         {
             Perform(settings, chromosome, i);
         }
     }
 }
Ejemplo n.º 26
0
        public void ItCopiesByValue()
        {
            var task   = GATestHelper.GetDummyTravelingSalesmanTask();
            var config = new GAConfiguration(task);

            var value = GATestHelper.GetRandomInteger(1, int.MaxValue - 1);

            task.MutationRate = value;
            Assert.AreNotEqual(value, config.MutationRate);
        }
Ejemplo n.º 27
0
        public void ItCanCopyProperties()
        {
            var randomValue = GATestHelper.GetNextDouble();

            var task = GATestHelper.GetDummyTravelingSalesmanTask();

            task.MutationRate = randomValue;
            var config = new GAConfiguration(task);

            Assert.AreEqual(randomValue, config.MutationRate);
        }
Ejemplo n.º 28
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);
        }
Ejemplo n.º 29
0
        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);
        }
Ejemplo n.º 30
0
        private void DetermineCrossoverPoints(int numberOfGenes, GAConfiguration configuration)
        {
            _firstCrossoverPoint  = configuration.GetRandomInteger(1, numberOfGenes - 1);
            _secondCrossoverPoint = configuration.GetRandomInteger(1, numberOfGenes - 1, _firstCrossoverPoint);

            if (_firstCrossoverPoint > _secondCrossoverPoint)
            {
                var temp = _firstCrossoverPoint;
                _firstCrossoverPoint  = _secondCrossoverPoint;
                _secondCrossoverPoint = temp;
            }
        }