public static Population <EnumGene <double>, double> NewPermutationDoubleGenePopulation(int ngenes, int nchromosomes, int npopulation) { var random = new Random(122343); var alleles = MutableSeq.OfLength <double>(ngenes); for (var i = 0; i < ngenes; ++i) { alleles[i] = random.NextDouble() * 10; } var ialleles = alleles.ToImmutableSeq(); var chromosomes = MutableSeq.OfLength <PermutationChromosome <double> >(nchromosomes); for (var i = 0; i < nchromosomes; ++i) { chromosomes[i] = PermutationChromosome.Of(ialleles); } var genotype = new Genotype <EnumGene <double> >(chromosomes.Cast <IChromosome <EnumGene <double> > >().ToImmutableSeq()); var population = new Population <EnumGene <double>, double>(npopulation); for (var i = 0; i < npopulation; ++i) { population.Add(Phenotype.Of(genotype.NewInstance(), 0, gt => gt.Gene.Allele)); } return(population); }
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"); }
public void InvalidChromosome() { var alleles = ImmutableSeq.Of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); var gene = new EnumGene <int>(3, alleles); var genes = MutableSeq.OfLength <EnumGene <int> >(10).Fill(() => gene).ToImmutableSeq(); var chromosome = new PermutationChromosome <int>(genes); Assert.False(chromosome.IsValid); }
public void OfIntegerLength() { var c = PermutationChromosome.OfInteger(100); var genes = c.GetValidAlleles().Copy(); var genesOrdered = genes.OrderBy(g => g).ToList(); for (var i = 0; i < c.Length; ++i) { Assert.Equal(i, genesOrdered[i]); } }
public override void IsValid() { var alleles = Enumerable.Range(0, 100).ToImmutableSeq(); var genes = Base.Subset(100, 10).Select(i => EnumGene.Of(i, alleles)).ToImmutableSeq(); var ch = new PermutationChromosome <int>(genes); Assert.True(ch.IsValid); Assert.Equal(10, ch.Length); }
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); }
public override IChromosome <EnumGene <T> > NewInstance() { return(PermutationChromosome.Of(_validAlleles, Length)); }
protected override Factory <IChromosome <EnumGene <int> > > Factory() { return(() => PermutationChromosome.OfInteger(100)); }
protected override Factory <IChromosome <EnumGene <char> > > Factory() { return(() => PermutationChromosome.Of(CharSeq.Of("a-zA-Z"))); }