Exemple #1
0
        public void Crossover()
        {
            var pmco = new PartiallyMatchedCrossover <int, double>(1);

            const int length   = 1000;
            var       alleles  = MutableSeq.OfLength <int>(length).Fill(Factories.Int());
            var       ialleles = alleles.ToImmutableSeq();

            var that  = alleles.Select(i => new EnumGene <int>(i, ialleles)).ToMutableSeq();
            var other = alleles.Select(i => new EnumGene <int>(i, ialleles)).ToMutableSeq();

            that.Shuffle();
            other.Shuffle();

            var thatChrom1 = new PermutationChromosome <int>(that.ToImmutableSeq());

            Assert.True(thatChrom1.IsValid, "thatChrom1 not valid");

            var otherChrom1 = new PermutationChromosome <int>(other.ToImmutableSeq());

            Assert.True(otherChrom1.IsValid, "otherChrom1 not valid");

            pmco.Crossover(that, other);

            var thatChrom2 = new PermutationChromosome <int>(that.ToImmutableSeq());

            Assert.True(thatChrom2.IsValid, "thatChrom2 not valid: " + thatChrom2.ToImmutableSeq());

            var otherChrom2 = new PermutationChromosome <int>(other.ToImmutableSeq());

            Assert.True(otherChrom2.IsValid, "otherChrom2 not valid: " + otherChrom2.ToImmutableSeq());

            Assert.False(thatChrom1.Equals(thatChrom2), "That chromosome must not be equal");
            Assert.False(otherChrom1.Equals(otherChrom2), "That chromosome must not be equal");
        }
Exemple #2
0
        public virtual void AlterProbability(int ngenes, int nchromosomes, int npopulation, double p)
        {
            var population = TestUtils.NewPermutationDoubleGenePopulation(
                ngenes, nchromosomes, npopulation
                );

            // The mutator to test.
            var crossover = new PartiallyMatchedCrossover <double, double>(p);

            long nallgenes = ngenes * nchromosomes * npopulation;
            long N         = 100;
            var  mean      = crossover.Order * npopulation * p;

            const long min    = 0;
            var        max    = nallgenes;
            var        domain = new Range <long>(min, max);

            var histogram = Histogram.OfLong(min, max, 10);
            var variance  = new LongMomentStatistics();

            for (var i = 0; i < N; ++i)
            {
                long alterations = crossover.Alter(population, 1);
                histogram.Accept(alterations);
                variance.Accept(alterations);
            }

            // Normal distribution as approximation for binomial distribution.
            // TODO: Implement test
//		assertDistribution(
//			histogram,
//			new NormalDistribution<>(domain, mean, variance.getVariance())
//		);
        }
Exemple #3
0
        public void CrossoverWithIllegalChromosome()
        {
            var pmco = new PartiallyMatchedCrossover <int, double>(1);

            const int length   = 1000;
            var       alleles  = MutableSeq.OfLength <int>(length).Fill(Factories.Int());
            var       ialleles = alleles.ToImmutableSeq();

            var that  = alleles.Select(i => new EnumGene <int>(i, ialleles)).ToMutableSeq();
            var other = alleles.Select(i => new EnumGene <int>(i, ialleles)).ToMutableSeq();

            pmco.Crossover(that, other);
        }
Exemple #4
0
        public void OfIntegerRangeLength()
        {
            var c1 = PermutationChromosome.OfInteger(IntRange.Of(0, 2000), 1000);

            Assert.True(c1.IsValid);

            var c2 = PermutationChromosome.OfInteger(IntRange.Of(0, 2000), 1000);

            Assert.True(c2.IsValid);

            var m1 = c1.ToSeq().Copy();
            var m2 = c2.ToSeq().Copy();

            AssertUnique(m1);
            AssertUnique(m2);

            var pmx = new PartiallyMatchedCrossover <int, double>(1);

            pmx.Crossover(m1, m2);
            AssertUnique(m1);
            AssertUnique(m2);
        }