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); }
protected Histogram <double> Distribution( ISelector <DoubleGene, double> selector, Optimize opt, int populationCount, int loops ) { Func <Genotype <DoubleGene>, double> ff = gt => gt.Gene.Allele; Factory <Phenotype <DoubleGene, double> > ptf = () => Phenotype.Of(Genotype.Of(DoubleChromosome.Of(Min, Max)), 1, ff); return(Enumerable.Range(0, loops).AsParallel().Select(j => { var hist = Histogram.OfDouble(Min, Max, ClassCount); var population = Enumerable.Range(0, populationCount) .Select(i => ptf()) .ToPopulation(); var selectionCount = (int)(populationCount / SelectionFraction); foreach (var pt in selector.Select(population, selectionCount, opt) .Select(pt => pt.GetGenotype().Gene.Allele)) { hist.Accept(pt); } return hist; }).ToDoubleHistogram(Min, Max, ClassCount)); }
public void Select(int size, int count, Optimize opt) { Func <Genotype <DoubleGene>, double> ff = gt => gt.Gene.Allele; Func <Phenotype <DoubleGene, double> > F = delegate() { return(Phenotype.Of(Genotype.Of(DoubleChromosome.Of(0.0, 1000.0)), 1, ff)); }; var population = Enumerable.Range(0, size) .Select(i => F()) .ToPopulation(); var selection = Selector().Select(population, count, opt); if (size == 0) { Assert.Empty(selection); } else { Assert.Equal(count, selection.Count); } foreach (var pt in selection) { Assert.True( population.Contains(pt), $"Population doesn't contain {pt}." ); } }
public Phenotype <TGene, TAllele> NewInstance( long generation, Func <Genotype <TGene>, TAllele> function, Func <TAllele, TAllele> scaler ) { return(Phenotype.Of(_genotype, generation, function, scaler)); }
protected override Factory <Population <DoubleGene, double> > Factory() { return(() => { var gt = Genotype.Of(DoubleChromosome.Of(0, 1)); return new Population <DoubleGene, double>(100) .Fill(() => Phenotype.Of(gt.NewInstance(), 1, Ff, Fs), 100); }); }
public void SelectNegativeCountArgument() { Func <Genotype <DoubleGene> > F = delegate() { return(Genotype.Of(new DoubleChromosome(0.0, 1.0))); }; var population = new Population <DoubleGene, double>(2); for (int i = 0, n = 2; i < n; ++i) { population.Add(Phenotype.Of(F(), 12, TestUtils.Ff)); } Assert.Throws <ArgumentOutOfRangeException>(() => Selector().Select(population, -1, Optimize.Maximum)); }
public void SelectionPerformance(int size, int count, Optimize opt) { Func <Genotype <DoubleGene>, double> ff = g => g.Gene.Allele; Func <Phenotype <DoubleGene, double> > F = delegate() { return(Phenotype.Of(Genotype.Of(DoubleChromosome.Of(0.0, 100.0)), 1, ff)); }; RandomRegistry.Using(new Random(543455), r => { var population = Enumerable.Range(0, size) .Select(i => F()) .ToPopulation(); ISelector <DoubleGene, double> selector = Selector(); if (!(selector is MonteCarloSelector <DoubleGene, double>)) { var monteCarloSelectionSum = new MonteCarloSelector <DoubleGene, double>() .Select(population, count, opt) .Select(p => p.GetFitness()) .Sum(); var selectionSum = selector .Select(population, count, opt) .Select(p => p.GetFitness()) .Sum(); if (opt == Optimize.Maximum) { Assert.True( selectionSum > monteCarloSelectionSum, $"{selectionSum} <= {monteCarloSelectionSum}"); } else { Assert.True( selectionSum < monteCarloSelectionSum, $"{selectionSum} >= {monteCarloSelectionSum}"); } } }); }
public void Empty() { var genotype = Genotype.Of( Enumerable.Range(0, 10) .Select(i => DoubleChromosome.Of(0, 10, 10)) .ToImmutableSeq() ); var pop = Population.Empty <DoubleGene, bool>(); Assert.True(0 == pop.Count); Assert.True(pop.IsEmpty); pop.Add(Phenotype.Of(genotype, 1, chromosomes => true)); Assert.True(1 == pop.Count); Assert.False(pop.IsEmpty); }
public void SelectMinimum() { Func <Genotype <IntegerGene>, int> Ff = delegate(Genotype <IntegerGene> gt) { return(gt.GetChromosome().ToSeq().Select(g => g.IntValue()).Sum()); }; Func <Genotype <IntegerGene> > Gtf = delegate() { return(Genotype.Of(IntegerChromosome.Of(0, 100, 10))); }; var population = Enumerable.Range(0, 50).Select(i => Phenotype.Of(Gtf(), 50, Ff)).ToPopulation(); var selector = new StochasticUniversalSelector <IntegerGene, int>(); var selection = selector.Select(population, 50, Optimize.Minimum); }
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); }
public void WorstIndividual() { const int size = 20; var population = new Population <DoubleGene, int>(size); for (var i = 0; i < size; ++i) { var gene = DoubleGene.Of(i, 0, size + 10); var ch = DoubleChromosome.Of(gene); var gt = Genotype.Of(ch); var pt = Phenotype.Of(gt, 1, g => g.Gene.IntValue()); population.Add(pt); } var selector = new TruncationSelector <DoubleGene, int>(5); var selected = selector.Select(population, 10, Optimize.Minimum); foreach (var pt in selected) { Assert.True(pt.GetFitness() < 5); } }
public void Maximize() { RandomRegistry.Using(new Random(), r => { Func <Genotype <IntegerGene>, int> ff = g => g.GetChromosome().GetGene().Allele; Phenotype <IntegerGene, int> F() { return(Phenotype.Of(Genotype.Of(IntegerChromosome.Of(0, 100)), 1, ff)); } var population = Enumerable.Range(0, 1000) .Select(i => F()) .ToPopulation(); var selector = new RouletteWheelSelector <IntegerGene, int>(); var p = selector.Probabilities(population, 100, Optimize.Maximum); Assert.True(ProbabilitySelector.Sum2One(p), p + " != 1"); }); }
protected override Factory <Phenotype <DoubleGene, double> > Factory() { return(() => Phenotype.Of(_genotype(), 0, _ff).Evaluate()); }
public void Insert(int index, Phenotype <TGene, TAllele> item) { _population.Insert(index, item); }
public int IndexOf(Phenotype <TGene, TAllele> item) { return(_population.IndexOf(item)); }
public bool Remove(Phenotype <TGene, TAllele> item) { return(_population.Remove(item)); }
public bool Contains(Phenotype <TGene, TAllele> item) { return(_population.Contains(item)); }
public void Add(Phenotype <TGene, TAllele> item) { _population.Add(item); }
public static Phenotype <DoubleGene, double> NewDoublePhenotype(double value) { return(Phenotype.Of(Genotype.Of( DoubleChromosome.Of(DoubleGene.Of(value, 0, 10))), 0, Ff ).Evaluate()); }
public Phenotype <TGene, TAllele> NewInstance(Genotype <TGene> genotype, long generation) { return(Phenotype.Of(genotype, generation, _function, _scaler)); }
public int CompareTo(Phenotype <TGene, TAllele> other) { return(GetFitness().CompareTo(other.GetFitness())); }
private static Phenotype <DoubleGene, double> Pt(double value) { return(Phenotype.Of(Genotype.Of(DoubleChromosome.Of(DoubleGene.Of(value, 0, 10))), 0, Ff, Fs)); }
private static Phenotype <DoubleGene, double> CreatePhenotype(double value) { return(Phenotype.Of(Genotype.Of(DoubleChromosome.Of(DoubleGene.Of(value, 0, 10))), 0, gt => gt.Gene.Allele)); }