public OrderedGeneticAlgorithm(GAConfiguration configuration, params Gene[] possibleValues) { Configuration = configuration; PossibleValues = possibleValues; StandardConstructorLogic(); }
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); }
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); }
public void ItAllowsAValidRetirementType() { var task = GATestHelper.GetDummyTravelingSalesmanTask(); task.RetirementStrategy = RetirementStrategy.MaxChildren; var settings = new GAConfiguration(task); }
public void ItAllowsAValidMutationType() { var task = GATestHelper.GetDummyTravelingSalesmanTask(); task.MutationStrategy = MutationStrategy.Boundary; var settings = new GAConfiguration(task); }
public void ItAllowsAValidImmigrationType() { var task = GATestHelper.GetDummyTravelingSalesmanTask(); task.ImmigrationStrategy = ImmigrationStrategy.Constant; var settings = new GAConfiguration(task); }
public void ItAllowsAValidCrossoverType() { var task = GATestHelper.GetDummyTravelingSalesmanTask(); task.CrossoverStrategy = CrossoverStrategy.AlternatingPosition; var settings = new GAConfiguration(task); }
public void ItDoesNotAllowsAnInvalidCrossoverType() { var task = GATestHelper.GetDummyTravelingSalesmanTask(); task.CrossoverStrategy = 0; var settings = new GAConfiguration(task); }
public void ItsConstructorsCanSetTheStrategies() { var task = GATestHelper.GetDummyTravelingSalesmanTask(); var settings = new GAConfiguration(task); Assert.IsTrue(settings.IsValid()); }
public override GARun Run(GAConfiguration configuration) { Configuration = configuration; Configuration.GeneSize = GetGeneSize(); GeneticAlgorithm = new UnorderedGeneticAlgorithm(Configuration, GetNewGene(new Random()).GetType()); return(GeneticAlgorithm.Run()); }
public void ItAllowsAValidParentSelectionType() { var task = GATestHelper.GetDummyTravelingSalesmanTask(); task.ParentSelectionStrategy = ParentSelectionStrategy.RouletteWheel; var settings = new GAConfiguration(task); }
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); }
public void ItDoesNotAllowsAnInvalidParentSelectionType() { var task = GATestHelper.GetDummyTravelingSalesmanTask(); task.ParentSelectionStrategy = 0; var settings = new GAConfiguration(task); }
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); } }
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); }
public void ItAllowsRetirementTypeMaxChildrenWhenRetirementMaximumIsAboveOne() { var task = GATestHelper.GetDummyTravelingSalesmanTask(); task.RetirementStrategy = RetirementStrategy.MaxChildren; task.MaxRetirement = 2; var settings = new GAConfiguration(task); }
public void ItAllowsRetirementTypeNoneWhenRetirementMaximumIsBelowZero() { var task = GATestHelper.GetDummyTravelingSalesmanTask(); task.RetirementStrategy = RetirementStrategy.None; task.MaxRetirement = 1; var settings = new GAConfiguration(task); }
public void ItThrowsAnExceptionIfRetirementTypeMaxChildrenWhenRetirementMaximumIsBelowZero() { var task = GATestHelper.GetDummyTravelingSalesmanTask(); task.RetirementStrategy = RetirementStrategy.MaxChildren; task.MaxRetirement = 0; var settings = new GAConfiguration(task); }
public void Setup(Chromosome[] pop, GAConfiguration configuration) { Configuration = configuration; Population = pop; NormalizeAllFitnessScores(); SetupSelection(); Validate(); }
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; }
public override GARun Run(GAConfiguration configuration) { Configuration = configuration; var options = GetOptions(); GeneticAlgorithm = new OrderedGeneticAlgorithm(Configuration, options); return(GeneticAlgorithm.Run()); }
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); }
public void ItsConstructorsCanSetTheStrategiesWhenUnordered() { var task = GATestHelper.GetDummyTravelingSalesmanTask(); task.MutationStrategy = MutationStrategy.Random; var settings = new GAConfiguration(task); Assert.IsTrue(settings.IsValid()); }
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); } } }
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); }
public void ItCanCopyProperties() { var randomValue = GATestHelper.GetNextDouble(); var task = GATestHelper.GetDummyTravelingSalesmanTask(); task.MutationRate = randomValue; var config = new GAConfiguration(task); Assert.AreEqual(randomValue, config.MutationRate); }
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); }
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); }
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; } }