public void ValueOfZeroLength() { const int length = 0; var alleles = MutableSeq.OfLength <int>(length).Fill(Factories.Int()).ToImmutableSeq(); Assert.Throws <ArgumentException>(() => EnumGene.Of(alleles)); }
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 ValueOfIndexOutOfBounds2() { const int length = 100; var alleles = MutableSeq.OfLength <int>(length).Fill(Factories.Int()).ToImmutableSeq(); Assert.Throws <IndexOutOfRangeException>(() => new EnumGene <int>(-1, alleles)); }
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"); }
internal static IImmutableSeq <CharacterGene> Seq(CharSeq chars, int length) { var r = RandomRegistry.GetRandom(); return(MutableSeq.OfLength <CharacterGene>(length) .Fill(() => new CharacterGene(chars, r.Next(chars.Length))) .ToImmutableSeq()); }
public static IImmutableSeq <DoubleGene> Seq(double minimum, double maximum, int length) { var min = minimum; var max = maximum; var r = RandomRegistry.GetRandom(); return(MutableSeq.OfLength <DoubleGene>(length).Fill(() => new DoubleGene(random.NextDouble(r, min, max), minimum, maximum)).ToImmutableSeq()); }
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); }
internal static IImmutableSeq <IntegerGene> Seq(int minimum, int maximum, int length) { var min = minimum; var max = maximum; var r = RandomRegistry.GetRandom(); return(MutableSeq.OfLength <IntegerGene>(length) .Fill(() => new IntegerGene(r.NextInt(min, max), minimum, maximum)) .ToImmutableSeq()); }
public static CharacterChromosome Of(string alleles, CharSeq validChars) { var index = new IntRef(); var genes = MutableSeq.OfLength <CharacterGene>(alleles.Length).Fill( delegate() { return(CharacterGene.Of(alleles[index.Value++], validChars)); } ).ToImmutableSeq(); return(new CharacterChromosome(genes)); }
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); }
protected LongChromosome(SerializationInfo info, StreamingContext context) { var genes = MutableSeq.OfLength <LongGene>(info.GetInt32("_length")); Min = info.GetInt64("_min"); Max = info.GetInt64("_max"); for (var i = 0; i < genes.Length; ++i) { genes[i] = new LongGene(info.GetInt64("_gene_" + i), Min, Max); } Genes = genes.ToImmutableSeq(); }
internal static IImmutableSeq <LongGene> Seq( long minimum, long maximum, int length ) { Require.Positive(length); var min = minimum; var max = maximum; var r = RandomRegistry.GetRandom(); return(MutableSeq.OfLength <LongGene>(length) .Fill(() => new LongGene(random.NextLong(r, min, max), minimum, maximum)) .ToImmutableSeq()); }
public void ValueOf() { const int length = 100; var alleles = MutableSeq.OfLength <int>(length).Fill(Factories.Int()).ToImmutableSeq(); Assert.Equal(alleles.Length, length); for (var i = 0; i < alleles.Length; ++i) { Assert.Equal(alleles[i], i); } for (var i = 0; i < alleles.Length; ++i) { Assert.Equal(new EnumGene <int>(i, alleles).Allele, i); Assert.Same(new EnumGene <int>(i, alleles).GetValidAlleles(), alleles); } }
protected CharacterChromosome(SerializationInfo info, StreamingContext context) { var length = info.GetInt32("_length"); _validCharacters = (CharSeq)info.GetValue("_validCharacters", typeof(CharSeq)); var genes = MutableSeq.OfLength <CharacterGene>(length); for (var i = 0; i < length; ++i) { var gene = CharacterGene.Of( info.GetChar("_gene_" + i), _validCharacters ); genes[i] = gene; } Genes = genes.ToImmutableSeq(); }
public static Population <DoubleGene, double> Population(int ngenes, int nchromosomes, int npopulation) { var chromosomes = MutableSeq.OfLength <IChromosome <DoubleGene> >(nchromosomes); for (var i = 0; i < nchromosomes; ++i) { chromosomes[i] = DoubleChromosome.Of(0, 10, ngenes); } var genotype = new Genotype <DoubleGene>(chromosomes.ToImmutableSeq()); var population = new Population <DoubleGene, double>(npopulation); for (var i = 0; i < npopulation; ++i) { population.Add(Phenotype.Of(genotype.NewInstance(), 0, TestUtils.Ff)); } return(population); }