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

            StandardConstructorLogic();
        }
Esempio 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);
        }
Esempio 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);
        }
Esempio n. 4
0
        public void ItAllowsAValidRetirementType()
        {
            var task = GATestHelper.GetDummyTravelingSalesmanTask();

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

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

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

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

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

            Assert.IsTrue(settings.IsValid());
        }
Esempio 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());
 }
Esempio n. 11
0
        public void ItAllowsAValidParentSelectionType()
        {
            var task = GATestHelper.GetDummyTravelingSalesmanTask();

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

            task.ParentSelectionStrategy = 0;
            var settings = new GAConfiguration(task);
        }
Esempio 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);
            }
        }
Esempio 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);
        }
Esempio n. 16
0
        public void ItAllowsRetirementTypeMaxChildrenWhenRetirementMaximumIsAboveOne()
        {
            var task = GATestHelper.GetDummyTravelingSalesmanTask();

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

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

            task.RetirementStrategy = RetirementStrategy.MaxChildren;
            task.MaxRetirement      = 0;
            var settings = new GAConfiguration(task);
        }
Esempio n. 19
0
 public void Setup(Chromosome[] pop, GAConfiguration configuration)
 {
     Configuration = configuration;
     Population    = pop;
     NormalizeAllFitnessScores();
     SetupSelection();
     Validate();
 }
Esempio 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;
 }
Esempio n. 21
0
        public override GARun Run(GAConfiguration configuration)
        {
            Configuration = configuration;

            var options = GetOptions();

            GeneticAlgorithm = new OrderedGeneticAlgorithm(Configuration, options);
            return(GeneticAlgorithm.Run());
        }
Esempio 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);
        }
        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);
        }
Esempio n. 24
0
        public void ItsConstructorsCanSetTheStrategiesWhenUnordered()
        {
            var task = GATestHelper.GetDummyTravelingSalesmanTask();

            task.MutationStrategy = MutationStrategy.Random;

            var settings = new GAConfiguration(task);

            Assert.IsTrue(settings.IsValid());
        }
Esempio 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);
         }
     }
 }
Esempio 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);
        }
Esempio 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);
        }
Esempio 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);
        }
Esempio 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);
        }
Esempio 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;
            }
        }