public void TestGaussianMutator() { RandomGenerator rnd = RandomGenerator.GetInstance(); ListGenotype<FloatGene> genotype = new ListGenotype<FloatGene>(10000); GaussianBaseMutator<ListGenotype<FloatGene>, int> baseMutator = new GaussianBaseMutator<ListGenotype<FloatGene>, int>(0.1); List<ListGenotype<FloatGene>> parents = new List<ListGenotype<FloatGene>>(); parents.Add(genotype); IList<Individual<ListGenotype<FloatGene>, int>> individuals = Individual<ListGenotype<FloatGene>, int>.FromGenotypes(parents); IList<Individual<ListGenotype<FloatGene>, int>> offspring = baseMutator.Apply(individuals); Assert.AreEqual(offspring.Count, 1); ListGenotype<FloatGene> child1 = offspring.First().Genotype; int changes = 0; for (int i = 0; i < 5; i++) { if (Math.Abs(child1[i].Value - i) > double.Epsilon) changes++; } Assert.LessOrEqual(changes, 1000); }
public void TestIndividualConstructors() { ListGenotype<FloatGene> genotype1 = new ListGenotype<FloatGene>(new[] {new FloatGene(1)}); ListGenotype<FloatGene> genotype2 = new ListGenotype<FloatGene>(new[] {new FloatGene(2)}); Individual<ListGenotype<FloatGene>, int> individual1 = new Individual<ListGenotype<FloatGene>, int>(genotype1); Individual<ListGenotype<FloatGene>, int> individual2 = new Individual<ListGenotype<FloatGene>, int>(genotype2, 30); Assert.AreEqual(1, individual1.Genotype.Count); Assert.AreEqual(1, individual2.Genotype.Count); Assert.False(individual1.HasFitnessAssigned); Assert.True(individual2.HasFitnessAssigned); int a; Assert.Throws<InvalidOperationException>(() => a = individual1.Fitness); Assert.AreEqual(30, individual2.Fitness); ListGenotype<FloatGene> genotype3 = individual1.Genotype; Assert.AreSame(individual1.Genotype, genotype3); IList<Individual<ListGenotype<FloatGene>, int>> individuals = Individual<ListGenotype<FloatGene>, int>.FromGenotypes(new[] {genotype1, genotype2, genotype3}); Assert.AreSame(individuals[0].Genotype, genotype1); Assert.AreSame(individuals[1].Genotype, genotype2); Assert.AreSame(individuals[2].Genotype, genotype3); }
public void BasicTest() { RandomGenerator.GetInstance().RandomSource = new MockRandomSource(new List<int>(), new List<double> {0.350, 0.623, 0.311, 0.511, 0.999}); ListGenotype<FloatGene> genotype1 = new ListGenotype<FloatGene>(10000); ListGenotype<FloatGene> genotype2 = new ListGenotype<FloatGene>(20000); ListGenotype<FloatGene> genotype3 = new ListGenotype<FloatGene>(30000); List<Individual<ListGenotype<FloatGene>, double>> individuals = new List<Individual<ListGenotype<FloatGene>, double>> { new Individual<ListGenotype<FloatGene>, double>(genotype1, 300), new Individual<ListGenotype<FloatGene>, double>(genotype2, 200), new Individual<ListGenotype<FloatGene>, double>(genotype3, 500) }; RouletteWheelSelector<ListGenotype<FloatGene>> selector = new RouletteWheelSelector<ListGenotype<FloatGene>>(5); IList<Individual<ListGenotype<FloatGene>, double>> selection = selector.Apply(individuals); Assert.AreEqual(5, selection.Count); Assert.AreSame(individuals[2], selection[0]); Assert.AreSame(individuals[0], selection[1]); Assert.AreSame(individuals[2], selection[2]); Assert.AreSame(individuals[0], selection[3]); Assert.AreSame(individuals[1], selection[4]); Assert.AreNotSame(individuals[0], selection[0]); Assert.AreNotSame(individuals[1], selection[0]); }
public void TestClone() { ListGenotype<FloatGene> genotype = new ListGenotype<FloatGene>(5); ListGenotype<FloatGene> genotype2 = genotype.Clone(); Assert.IsTrue(genotype.All(g1 => genotype2.All(g2 => !ReferenceEquals(g1, g2) && g1.Equals(g2)))); }
private double FitnessFunction(ListGenotype<FloatGene> genotype) { double squaredDistance = 0; for (int i = 1; i < genotype.Count; i++) { City city = Cities[(int) genotype[i].Value]; City previousCity = Cities[(int) genotype[i - 1].Value]; squaredDistance += Math.Pow(city.Position.X - previousCity.Position.X, 2) + Math.Pow(city.Position.Y - previousCity.Position.Y, 2); } return 1/squaredDistance; }
public void TestConstructors() { ListGenotype<FloatGene> genotype = new ListGenotype<FloatGene>(5); Assert.AreEqual(genotype.Count, 5); ListGenotype<FloatGene> genotype2 = new ListGenotype<FloatGene>(genotype); Assert.AreEqual(genotype2.Count, 5); Assert.AreNotSame(genotype,genotype2); for (int i = 0; i < genotype.Count; i++) { Assert.AreSame(genotype[i],genotype2[i]); } }
public void TestMultipointCrossover() { RandomGenerator rnd = RandomGenerator.GetInstance(); ListGenotype<FloatGene> genotype = new ListGenotype<FloatGene>(new[] {new FloatGene(1), new FloatGene(2), new FloatGene(3), new FloatGene(4), new FloatGene(5)}); ListGenotype<FloatGene> genotype2 = new ListGenotype<FloatGene>(new[] {new FloatGene(1.5), new FloatGene(2.5), new FloatGene(3.5), new FloatGene(4.5), new FloatGene(5.5)}); MultipointCrossover<ListGenotype<FloatGene>, FloatGene, int> crossover = new MultipointCrossover<ListGenotype<FloatGene>, FloatGene, int>(3); List<Individual<ListGenotype<FloatGene>, int>> offspring = crossover.Apply( Individual<ListGenotype<FloatGene>, int>.FromGenotypes(new List<ListGenotype<FloatGene>> { genotype, genotype2 })).ToList(); Assert.AreEqual(offspring.Count, 2); foreach (Individual<ListGenotype<FloatGene>, int> child in offspring) { bool previousWasInteger = Math.Abs(child.Genotype[0].Value - (int) child.Genotype[0].Value) < double.Epsilon; int cuts = 0; foreach (FloatGene gene in child.Genotype) { bool currentIsInteger = Math.Abs(gene.Value - (int) gene.Value) < double.Epsilon; if (currentIsInteger != previousWasInteger) cuts++; previousWasInteger = currentIsInteger; } Assert.AreEqual(3, cuts); } }