public void Crossover() { var chars = CharSeq.Of("a-zA-Z"); var g1 = new CharacterChromosome(chars, 20).ToSeq(); var g2 = new CharacterChromosome(chars, 20).ToSeq(); const int rv1 = 12; RandomRegistry.Using(new ConstRandom(rv1), r => { var crossover = new SinglePointCrossover <CharacterGene, double>(); var g1C = MutableSeq.Of <CharacterGene>(g1); var g2C = MutableSeq.Of <CharacterGene>(g2); crossover.Crossover(g1C, g2C); Assert.Equal(g1C.ToImmutableSeq().SubSeq(0, rv1), g1.SubSeq(0, rv1)); Assert.Equal(g1C.SubSeq(rv1), g2.SubSeq(rv1)); Assert.NotEqual(g1C, g2); Assert.NotEqual(g2C, g1); const int rv2 = 0; RandomRegistry.Using(new ConstRandom(rv2), r2 => { var g1C2 = MutableSeq.Of <CharacterGene>(g1); var g2C2 = MutableSeq.Of <CharacterGene>(g2); crossover.Crossover(g1C2, g2C2); Assert.Equal(g1C2, g2); Assert.Equal(g2C2, g1); Assert.Equal(g1C2.SubSeq(0, rv2), g1.SubSeq(0, rv2)); Assert.Equal(g1C2.SubSeq(rv2), g2.SubSeq(rv2)); const int rv3 = 1; RandomRegistry.Using(new ConstRandom(rv3), r3 => { var g1C3 = MutableSeq.Of <CharacterGene>(g1); var g2C3 = MutableSeq.Of <CharacterGene>(g2); crossover.Crossover(g1C3, g2C3); Assert.Equal(g1C3.SubSeq(0, rv3), g1.SubSeq(0, rv3)); Assert.Equal(g1C3.SubSeq(rv3), g2.SubSeq(rv3)); var rv4 = g1.Length; RandomRegistry.Using(new ConstRandom(rv4), r4 => { var g1C4 = MutableSeq.Of <CharacterGene>(g1); var g2C4 = MutableSeq.Of <CharacterGene>(g2); crossover.Crossover(g1C4, g2C); Assert.Equal(g1C4, g1); Assert.Equal(g2C4, g2); Assert.Equal(g1C4.SubSeq(0, rv4), g1.SubSeq(0, rv4)); Assert.Equal(g1C4.SubSeq(rv4), g2.SubSeq(rv4)); }); }); }); }); }
public void CrossoverAll1() { var chars = CharSeq.Of("a-zA-Z"); var g1 = new CharacterChromosome(chars, 20).ToSeq(); var g2 = new CharacterChromosome(chars, 20).ToSeq(); var crossover = new MultiPointCrossover <CharacterGene, double>(2000); var points = new int[g1.Length]; for (var i = 0; i < points.Length; ++i) { points[i] = i; } var ms1 = g1.Copy(); var ms2 = g2.Copy(); crossover.Crossover(ms1, ms2); }
public void ReverseCrossover(int npoints) { for (var i = 1; i < 500; ++i) { var chars = CharSeq.Of("a-zA-Z"); var a = new CharacterChromosome(chars, i).ToSeq().Select(g => g.Allele).ToImmutableSeq(); var b = new CharacterChromosome(chars, i).ToSeq().Select(g => g.Allele).ToImmutableSeq(); var ma1 = a.Copy(); var mb1 = b.Copy(); var points = Base.Subset( a.Length + 1, Math.Min(npoints, a.Length + 1), new Random(1234) ); MultiPointCrossover.Crossover(ma1, mb1, points); MultiPointCrossover.Crossover(ma1, mb1, points); Assert.Equal(a, ma1); Assert.Equal(b, mb1); } }
protected override Factory <IChromosome <EnumGene <char> > > Factory() { return(() => PermutationChromosome.Of(CharSeq.Of("a-zA-Z"))); }