public void SinglePointCrossoverApplyTest() { TestRandom random = new TestRandom(); IntegerVector parent1, parent2, expected, actual; bool exceptionFired; // The following test is not based on published examples random.Reset(); random.IntNumbers = new int[] { 3 }; parent1 = new IntegerVector(new int[] { 2, 2, 3, 5, 1 }); parent2 = new IntegerVector(new int[] { 4, 1, 3, 2, 8 }); expected = new IntegerVector(new int[] { 2, 2, 3, 2, 8 }); actual = SinglePointCrossover.Apply(random, parent1, parent2); Assert.IsTrue(Auxiliary.IntegerVectorIsEqualByPosition(actual, expected)); // The following test is not based on published examples random.Reset(); random.IntNumbers = new int[] { 2 }; parent1 = new IntegerVector(new int[] { 2, 2, 3, 5, 1, 9 }); // this parent is longer parent2 = new IntegerVector(new int[] { 4, 1, 3, 2, 8 }); exceptionFired = false; try { actual = SinglePointCrossover.Apply(random, parent1, parent2); } catch (System.ArgumentException) { exceptionFired = true; } Assert.IsTrue(exceptionFired); }
public void Should_cut_and_Produce_Child_properly() { for (int x = 0; x < 100; x++) { var parentA = new CrossoverTestModel(new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }); var parentB = new CrossoverTestModel(new[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }); var options = new EvolutionOptions() { CollectionSize = parentA.Bits.Count }; // 10 items var child = new SinglePointCrossover <CrossoverTestModel>(options).Produce(parentA, parentB); _output.WriteLine(child.ToString()); // asserts var cutPoint = child.Bits.Select((b, i) => (bit: b, index: i)).First(e => e.bit == 1).index; var bitsBeforeCutPoint = child.Bits.Where((b, i) => i < cutPoint).ToList(); var bitsAfterCutPoint = child.Bits.Where((b, i) => i >= cutPoint).ToList(); child.Bits.Should().HaveSameCount(parentA.Bits); child.Bits.Should().HaveCount(bitsBeforeCutPoint.Count + bitsAfterCutPoint.Count); bitsBeforeCutPoint.Should().AllBeEquivalentTo(0); bitsAfterCutPoint.Should().AllBeEquivalentTo(1); } }
protected IBreeding <Synapse> NewBreeding(System.Random random) { ISelection <Synapse> selector; if (roulteWheelSelection) { selector = new RouletteWheelSelection <Synapse>(random, 2); } else { selector = new EliteSelection <Synapse>(2); } var crossover = new SinglePointCrossover <Synapse>(random); var mutator = new FixedNeuralNetMutation( random, geneMutChance, mutRange); return(new BreedingBase <Synapse>( poplLne - (int)(poplLne * reinsertPart), selector, crossover, mutator)); }
private BreedingBase <int> NewBreeding( int poplLen, double partToReinsert, double geneMutChance, int mutRange) { BreedingBase <int> result; ISelection <int> selector; ICrossover <int> crossover; IMutation <int> mutator; //selector = new EliteSelection<int>(2, partToBeElites: 0.5d); selector = new RouletteWheelSelection <int>(RandomInst, 2); crossover = new SinglePointCrossover <int>(RandomInst, 1); mutator = new ES_Mutation(RandomInst, geneMutChance, mutRange); result = new BreedingBase <int>( poplLen - (int)(partToReinsert * poplLen), selector, crossover, mutator ); return(result); }
public void ItThrowsAnErrorIfTheParentsAreNotLargeEnough() { var father = new OrderedChromosome(1); var mother = new OrderedChromosome(1); var singlePointCrossover = new SinglePointCrossover(); singlePointCrossover.Execute(father, mother, GATestHelper.GetTravelingSalesmanDefaultConfiguration()); }
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()); }
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()); }
public void ItCanEnsureTheMotherAndFatherPassAtLeastOneGene() { var father = GATestHelper.GetAlphabetCharacterChromosome(); var mother = GATestHelper.GetAlphabetCharacterChromosome(); mother.Genes.Shuffle(new Random()); var singlePoint = new SinglePointCrossover(); var child = singlePoint.Execute(father, mother, GATestHelper.GetTravelingSalesmanDefaultConfiguration()); Assert.AreNotEqual(child, father); }
public void TestChromosome() { Chromosome a = new Chromosome(1000); Chromosome b = new Chromosome(1000); SinglePointCrossover spc = new SinglePointCrossover(); Chromosome c = spc.Cross(a, b); for (int i = 0; i < c.Length; i++) { Assert.IsTrue(c.GetValue(i) == a.GetValue(i) || c.GetValue(i) == b.GetValue(i)); } }
public static void RunCrossOver(List <CoupleParent> coupleParents, List <string> crossoverChildren, List <Person> population) { Random random = new Random(); int setCrossoverPoint = random.Next(0, 1); if (setCrossoverPoint == 0) { SinglePointCrossover.DoCrossover(coupleParents, crossoverChildren, crossOverPercentage, population); } else { TwoPointCrossover.DoCrossover(coupleParents, crossoverChildren, crossOverPercentage, population); } }
public void ItCanPerformACrossover() { var father = GATestHelper.GetAlphabetCharacterChromosome(); var mother = GATestHelper.GetAlphabetCharacterChromosome(); mother.Genes.Shuffle(new Random()); var singlePoint = new SinglePointCrossover(); var child = singlePoint.Execute(father, mother, GATestHelper.GetTravelingSalesmanDefaultConfiguration()); Console.Out.WriteLine("Child: " + child.ToString()); Assert.AreNotEqual(father.ToString(), child.ToString()); Assert.AreNotEqual(mother.ToString(), child.ToString()); }
private IBreeding <Synapse> NewBreeding(System.Random random) { var selector = new EliteSelection <Synapse>(2); //var selector = new RouletteWheelSelection<Synapse>(random, 2); var crossover = new SinglePointCrossover <Synapse>(random); var mutator = new FixedNeuralNetMutation( random, geneMutChance, weightMutRange); return(new BreedingBase <Synapse>( poplLen - (int)(partToReinsert * poplLen), selector, crossover, mutator)); }
static void Main(string[] args) { //var graph = new byte[,] //{ // {0, 2, 3, 1, 3}, // {2, 0, 7, 6, 9}, // {3, 7, 0, 6, 4}, // {1, 6, 6, 0, 2}, // {3, 9, 4, 2, 0}, //}; Console.WriteLine("Enter number of cities:"); var n = int.Parse(Console.ReadLine()); var random = new Random(); var graph = new byte[n, n]; InitGraph(n, graph, random); var selectionOperator = new RandomFromTheFittestSelection(Constants.CrossoverTopPercent, random); var crossoverOperator = new SinglePointCrossover(random, (childPath => new Path(childPath, graph))); var mutationOperator = new RandomSwapMutation(random, Constants.MutationChance, (childPath => new Path(childPath, graph))); var pop = new Population(selectionOperator, crossoverOperator, mutationOperator, Constants.PopulationCount); for (int i = 0; i < Constants.PopulationCount; i++) { pop.AddIndividual(new Path(GenerateRandomPath(n, random), graph)); } var k = 1; for (int i = 0; i < Constants.IterationsCount; i++) { pop.GenNextPopulation(Constants.ChildrenCountPercentage); if (i == 2 * k) { k *= 2; Console.WriteLine($"Shortest path for population {i} is: {pop.Individuals.First().Fitness}"); } } Console.WriteLine($"Shortest path for population {Constants.IterationsCount} is: {pop.Individuals.First().Fitness}"); }
private IBreeding <char> NewBreeding( Random random, int poplLen, double partToReinsert, double geneMutChance, int mutRange) { var selector = new EliteSelection <char>(2); //var selector = new RouletteWheelSelection<char>(random, 2); var crossover = new SinglePointCrossover <char>(random); var mutator = new Sh_Mutation( random, geneMutChance, mutRange, chars); return(new BreedingBase <char>( poplLen - (int)(partToReinsert * poplLen), selector, crossover, mutator)); }
public void SinglePointCrossoverApplyTest() { TestRandom random = new TestRandom(); RealVector parent1, parent2, expected, actual; bool exceptionFired; // The following test is not based on published examples random.Reset(); random.IntNumbers = new int[] { 3 }; parent1 = new RealVector(new double[] { 0.2, 0.2, 0.3, 0.5, 0.1 }); parent2 = new RealVector(new double[] { 0.4, 0.1, 0.3, 0.2, 0.8 }); expected = new RealVector(new double[] { 0.2, 0.2, 0.3, 0.2, 0.8 }); actual = SinglePointCrossover.Apply(random, parent1, parent2); Assert.IsTrue(Auxiliary.RealVectorIsAlmostEqualByPosition(actual, expected)); // The following test is not based on published examples random.Reset(); random.IntNumbers = new int[] { 2 }; parent1 = new RealVector(new double[] { 0.2, 0.2, 0.3, 0.5, 0.1, 0.9 }); // this parent is longer parent2 = new RealVector(new double[] { 0.4, 0.1, 0.3, 0.2, 0.8 }); exceptionFired = false; try { actual = SinglePointCrossover.Apply(random, parent1, parent2); } catch (System.ArgumentException) { exceptionFired = true; } Assert.IsTrue(exceptionFired); // The following test is not based on published examples random.Reset(); random.IntNumbers = new int[] { 5 }; // should not have an effect parent1 = new RealVector(new double[] { 0.2, 0.4 }); parent2 = new RealVector(new double[] { 0.6, 0.1 }); expected = new RealVector(new double[] { 0.2, 0.1 }); actual = SinglePointCrossover.Apply(random, parent1, parent2); Assert.IsTrue(Auxiliary.RealVectorIsAlmostEqualByPosition(actual, expected)); // The following test is not based on published examples }
public static PRVEncoding Apply(IRandom random, PRVEncoding parent1, PRVEncoding parent2) { return(new PRVEncoding(SinglePointCrossover.Apply(random, parent1.PriorityRulesVector, parent2.PriorityRulesVector), parent1.NrOfRules)); }