Exemplo n.º 1
0
        public void SwapIntIntMSeqInt(IMutableSeq <BitGene> seq)
        {
            for (var start = 0; start < seq.Length - 3; ++start)
            {
                var random    = new Random();
                var other     = NewSeq(seq.Length);
                var otherCopy = NewSeq(seq.Length);
                for (var j = 0; j < other.Length; ++j)
                {
                    other[j]     = BitGene.Of(random.NextBoolean());
                    otherCopy[j] = other[j];
                }

                var       copy       = seq.ToArray(new BitGene[0]);
                var       end        = start + 2;
                const int otherStart = 1;

                seq.Swap(start, end, other, otherStart);

                for (var j = start; j < end; ++j)
                {
                    Assert.Equal(seq[j], otherCopy[j + otherStart - start]);
                }
                for (var j = 0; j < end - start; ++j)
                {
                    Assert.Equal(other[j + otherStart], copy[j + start]);
                }
            }
        }
Exemplo n.º 2
0
 public static IMutableSeq <T> Shuffle <T>(this IMutableSeq <T> seq, Random randomSource)
 {
     for (var j = seq.Length - 1; j > 0; --j)
     {
         seq.Swap(j, randomSource.NextInt(j + 1));
     }
     return(seq);
 }
Exemplo n.º 3
0
        protected override int Mutate(IMutableSeq <TGene> genes, double p)
        {
            var random = RandomRegistry.GetRandom();

            if (genes.Length > 1)
            {
                return(Internal.Math.random.Indexes(random, genes.Length, p)
                       .Peek(i => genes.Swap(i, random.NextInt(genes.Length)))
                       .Count());
            }
            return(0);
        }
        protected internal override int Crossover(IMutableSeq <EnumGene <TGene> > that, IMutableSeq <EnumGene <TGene> > other)
        {
            if (that.Length != other.Length)
            {
                throw new ArgumentException($"Required chromosomes with same length: {that.Length} != {other.Length}");
            }

            if (that.Length >= 2)
            {
                var random = RandomRegistry.GetRandom();
                var points = Base.Subset(that.Length, 2, random);

                that.Swap(points[0], points[1], other, points[0]);
                Repair(that, other, points[0], points[1]);
                Repair(other, that, points[0], points[1]);
            }

            return(1);
        }
Exemplo n.º 5
0
        public void SwapIntInt(IMutableSeq <BitGene> seq)
        {
            for (var i = 0; i < seq.Length - 3; ++i)
            {
                var copy = seq.ToArray(new BitGene[0]);
                var j    = i + 2;
                var vi   = seq[i];
                var vj   = seq[j];

                seq.Swap(i, j);

                Assert.Equal(vj, seq[i]);
                Assert.Equal(vi, seq[j]);
                for (var k = 0; k < seq.Length; ++k)
                {
                    if (k != i && k != j)
                    {
                        Assert.Equal(copy[k], seq[k]);
                    }
                }
            }
        }