protected override Factory <EvolutionResult <DoubleGene, double> > Factory() { return(() => { var random = RandomRegistry.GetRandom(); return EvolutionResult.Of( random.NextBoolean() ? Optimize.Maximum : Optimize.Minimum, new Population <DoubleGene, double>(100) .Fill(() => Phenotype.Of( Genotype.Of(DoubleChromosome.Of(0, 1)), 1, a => a.Gene.Allele), 100 ), random.NextInt(1000), random.NextInt(1000), EvolutionDurations.Of( TimeSpan.FromMilliseconds(random.NextInt(1000000)), TimeSpan.FromMilliseconds(random.NextInt(1000000)), TimeSpan.FromMilliseconds(random.NextInt(1000000)), TimeSpan.FromMilliseconds(random.NextInt(1000000)), TimeSpan.FromMilliseconds(random.NextInt(1000000)), TimeSpan.FromMilliseconds(random.NextInt(1000000)), TimeSpan.FromMilliseconds(random.NextInt(1000000)) ), random.NextInt(100), random.NextInt(100), random.NextInt(100) ); }); }
private static EvolutionResult <IntegerGene, int> NewResult(Optimize opt, int value) { const int length = 1000; Func <Genotype <IntegerGene>, int> F = delegate(Genotype <IntegerGene> gt) { return(gt.Gene.Allele); }; var pop = new Population <IntegerGene, int>(length); for (var i = 0; i < length; ++i) { var gt = Genotype.Of(IntegerChromosome.Of( IntegerGene.Of(value, 0, length) )); pop.Add(Phenotype.Of(gt, 1, F)); } Shuffle(pop, RandomRegistry.GetRandom()); return(EvolutionResult.Of(opt, pop, 0, 0, EvolutionDurations.Zero, 0, 0, 0)); }
public void BestWorstPhenotype() { const int length = 100; Func <Genotype <IntegerGene>, int> F = delegate(Genotype <IntegerGene> gt) { return(gt.Gene.Allele); }; var population = new Population <IntegerGene, int>(length); for (var i = 0; i < length; ++i) { var gt = Genotype.Of(IntegerChromosome.Of( IntegerGene.Of(i, 0, length) )); population.Add(Phenotype.Of(gt, 1, F)); } Shuffle(population, RandomRegistry.GetRandom()); var maxResult = EvolutionResult.Of( Optimize.Maximum, population, 0, 0, EvolutionDurations.Zero, 0, 0, 0 ); Assert.Equal(length - 1, maxResult.GetBestFitness()); Assert.Equal(0, maxResult.GetWorstFitness()); var minResult = EvolutionResult.Of( Optimize.Minimum, population, 0, 0, EvolutionDurations.Zero, 0, 0, 0 ); Assert.Equal(0, minResult.GetBestFitness()); Assert.Equal(length - 1, minResult.GetWorstFitness()); }
public void CompareTo() { const int length = 100; Func <Genotype <IntegerGene>, int> F = delegate(Genotype <IntegerGene> gt) { return(gt.Gene.Allele); }; var small = new Population <IntegerGene, int>(length); for (var i = 0; i < length; ++i) { var gt = Genotype.Of(IntegerChromosome.Of( IntegerGene.Of(i, 0, length) )); small.Add(Phenotype.Of(gt, 1, F)); } Shuffle(small, RandomRegistry.GetRandom()); var big = new Population <IntegerGene, int>(length); for (var i = 0; i < length; ++i) { var gt = Genotype.Of(IntegerChromosome.Of( IntegerGene.Of(i + length, 0, length) )); big.Add(Phenotype.Of(gt, 1, F)); } Shuffle(big, RandomRegistry.GetRandom()); var smallMaxResult = EvolutionResult.Of( Optimize.Maximum, small, 0, 0, EvolutionDurations.Zero, 0, 0, 0 ); var bigMaxResult = EvolutionResult.Of( Optimize.Maximum, big, 0, 0, EvolutionDurations.Zero, 0, 0, 0 ); Assert.True(smallMaxResult.CompareTo(bigMaxResult) < 0); Assert.True(bigMaxResult.CompareTo(smallMaxResult) > 0); Assert.True(smallMaxResult.CompareTo(smallMaxResult) == 0); Assert.True(bigMaxResult.CompareTo(bigMaxResult) == 0); var smallMinResult = EvolutionResult.Of( Optimize.Minimum, small, 0, 0, EvolutionDurations.Zero, 0, 0, 0 ); var bigMinResult = EvolutionResult.Of( Optimize.Minimum, big, 0, 0, EvolutionDurations.Zero, 0, 0, 0 ); Assert.True(smallMinResult.CompareTo(bigMinResult) > 0); Assert.True(bigMinResult.CompareTo(smallMinResult) < 0); Assert.True(smallMinResult.CompareTo(smallMinResult) == 0); Assert.True(bigMinResult.CompareTo(bigMinResult) == 0); }