public void SelectChromosomes_offspringSizeEqualsParentsSizeAndGreaterThanMinSizeAndLowerThanMaxSize_Selectoffspring() { var target = new PureReinsertion(); var chromosome = Substitute.For <ChromosomeBase>(2); var population = new Population(2, 6, chromosome); var offspring = new List <IChromosome>() { chromosome, chromosome, chromosome, chromosome }; var parents = new List <IChromosome>() { Substitute.For <ChromosomeBase> (2), Substitute.For <ChromosomeBase> (2), Substitute.For <ChromosomeBase> (2), Substitute.For <ChromosomeBase> (2) }; var selected = target.SelectChromosomes(population, offspring, parents); Assert.AreEqual(4, selected.Count); Assert.AreEqual(2, selected[0].Length); Assert.AreEqual(2, selected[1].Length); Assert.AreEqual(2, selected[2].Length); Assert.AreEqual(2, selected[3].Length); }
public IReinsertion Pure() { var target = new PureReinsertion(); target.SelectChromosomes( new Population(5, 5, new TspChromosome(_numberOfCities)), new List <IChromosome> { new TspChromosome(_numberOfCities), new TspChromosome(_numberOfCities), new TspChromosome(_numberOfCities), new TspChromosome(_numberOfCities), new TspChromosome(_numberOfCities) }, _parents); return(target); }
private static IAlgoritmo CriaAlgoritmoGenetico(Dictionary <string, string[]> dict, List <string> flat, Problema problema) { int populacaoMin, populacaoMax; IPopulation population; ISelection selection; ICrossover crossover; IMutation mutation; ITermination termination; IReinsertion reinsertion; float crossoverProbability, mutationProbability; var p = dict.ValueOrDefault("p", "50,100").Split(new[] { ',' }); if (p.Length != 2 || !int.TryParse(p[0], out populacaoMin) || !int.TryParse(p[1], out populacaoMax)) { throw new ArgumentException("Faixa de população inválida."); } population = new Population(populacaoMin, populacaoMax, new CromossomoViajante(problema.Mapa.Locais.Count)); switch (dict.ValueOrDefault("s", "t")) { case "e": selection = new EliteSelection(); break; case "r": selection = new RouletteWheelSelection(); break; case "s": selection = new StochasticUniversalSamplingSelection(); break; case "t": selection = new TournamentSelection(); break; default: throw new ArgumentException("Seleção inválida."); } switch (dict.ValueOrDefault("c", "o")) { case "s": crossover = new CutAndSpliceCrossover(); break; case "c": crossover = new CycleCrossover(); break; case "o": crossover = new OrderedCrossover(); break; case "ob": crossover = new OrderBasedCrossover(); break; case "op": crossover = new OnePointCrossover(); break; case "pm": crossover = new PartiallyMappedCrossover(); break; case "p": crossover = new PositionBasedCrossover(); break; case "tpa": crossover = new ThreeParentCrossover(); break; case "tp": crossover = new TwoPointCrossover(); break; case "u": crossover = new UniformCrossover(); break; default: throw new ArgumentException("Crossover inválido."); } switch (dict.ValueOrDefault("m", "r")) { case "d": mutation = new DisplacementMutation(); break; case "f": mutation = new FlipBitMutation(); break; case "i": mutation = new InsertionMutation(); break; case "s": mutation = new PartialShuffleMutation(); break; case "r": mutation = new ReverseSequenceMutation(); break; case "t": mutation = new TworsMutation(); break; case "u": mutation = new UniformMutation(); break; default: throw new ArgumentException("Mutação inválida."); } switch (dict.ValueOrDefault("t", "s")) { case "s": termination = new FitnessStagnationTermination(); break; case "t": termination = new FitnessThresholdTermination(); break; case "g": termination = new GenerationNumberTermination(); break; default: throw new ArgumentException("Terminação inválida."); } switch (dict.ValueOrDefault("e", "e")) { case "e": reinsertion = new ElitistReinsertion(); break; case "p": reinsertion = new PureReinsertion(); break; case "u": reinsertion = new UniformReinsertion(); break; default: throw new ArgumentException("Reinserção inválida."); } if (!float.TryParse(dict.ValueOrDefault("cp", "0,75"), out crossoverProbability)) { throw new ArgumentException("Probabilidade de crossover inválida."); } if (!float.TryParse(dict.ValueOrDefault("mp", "0,25"), out mutationProbability)) { throw new ArgumentException("Probabilidade de mutação inválida."); } return(new AlgoritmoGenetico(problema, population, selection, crossover, crossoverProbability, mutation, mutationProbability, termination, reinsertion)); }