예제 #1
0
        protected internal override int Crossover(IMutableSeq <TGene> that, IMutableSeq <TGene> other)
        {
            var n = Math.Min(that.Length, other.Length);
            var k = Math.Min(n, _n);

            var random = RandomRegistry.GetRandom();
            var points = k > 0 ? Base.Subset(n, k, random) : new int[0];

            MultiPointCrossover.Crossover(that, other, points);
            return(2);
        }
예제 #2
0
        public void Crossover(string stringA, string stringB, ISeq <int> points, string expectedA, string expectedB)
        {
            var a = CharSeq.ToImmutableSeq(stringA);
            var b = CharSeq.ToImmutableSeq(stringB);

            var ma = a.Copy();
            var mb = b.Copy();

            var intPoints = points.Select(i => i).ToArray();

            MultiPointCrossover.Crossover(ma, mb, intPoints);
            Assert.Equal(ToString(ma), expectedA);
            Assert.Equal(ToString(mb), expectedB);
        }
예제 #3
0
        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);
        }
예제 #4
0
        public void SinglePointCrossoverConsistency()
        {
            var a = CharSeq.ToImmutableSeq("1234567890");
            var b = CharSeq.ToImmutableSeq("ABCDEFGHIJ");

            for (var i = 0; i < a.Length + 1; ++i)
            {
                var ma1 = a.Copy();
                var mb1 = b.Copy();
                var ma2 = a.Copy();
                var mb2 = b.Copy();

                MultiPointCrossover.Crossover(ma1, mb1, new[] { i });
                SinglePointCrossover.Crossover(ma2, mb2, i);

                Assert.Equal(ma1, ma2);
                Assert.Equal(mb1, mb2);
            }
        }
예제 #5
0
        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);
            }
        }