public static ICodec <double, DoubleGene> OfScalar(DoubleRange domain) { return(Codec.Of( () => Genotype.Of(DoubleChromosome.Of(domain)), gt => gt.GetChromosome().GetGene().Allele )); }
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) ); }); }
public IChromosome Clone() { DoubleChromosome cpy = new DoubleChromosome(); cpy.length = this.length; cpy.doubleValues = new Genes.DoubleGene[cpy.length]; for (int i = 0; i < cpy.length; ++i) cpy.doubleValues[i] = (Genes.DoubleGene)this.doubleValues[i].Clone(); IChromosome copy = (IChromosome)cpy; return copy; }
public void GenerationLimit(long generations) { var engine = Engine .Builder(a => a.Gene.Allele, DoubleChromosome.Of(0, 1)) .Build(); var result = engine.Stream() .TakeWhile(Limits.ByFixedGeneration <DoubleGene, double>(generations)) .ToBestEvolutionResult(); Assert.Equal(generations, result.GetTotalGenerations()); }
public void GenerationCount(long generations) { var engine = Engine .Builder(a => a.Gene.Allele, DoubleChromosome.Of(0, 1)) .Build(); var result = engine.Stream() .Take((int)generations) .ToBestEvolutionResult(); Assert.Equal(generations, result.GetTotalGenerations()); }
public void EmptyStreamCollectPhenotype() { var engine = Engine .Builder(a => a.Gene.Allele, DoubleChromosome.Of(0, 1)) .Build(); var result = engine.Stream() .Take(0) .ToBestPhenotype(); Assert.Null(result); }
private static ICodec <Tuple <int, long, double>, DoubleGene> Codec(IntRange v1Domain, LongRange v2Domain, DoubleRange v3Domain) { return(Engine.Codec.Of( () => Genotype.Of( DoubleChromosome.Of(DoubleRange.Of(v1Domain.Min, v1Domain.Max)), DoubleChromosome.Of(DoubleRange.Of(v2Domain.Min, v2Domain.Max)), DoubleChromosome.Of(v3Domain) ), gt => Tuple.Create( gt.GetChromosome(0).GetGene().IntValue(), gt.GetChromosome(0).GetGene().LongValue(), gt.GetChromosome(0).GetGene().DoubleValue()) )); }
public void ParallelStream() { var engine = Engine .Builder(a => a.Gene.Allele, DoubleChromosome.Of(0, 1)) .Build(); var result = engine .Stream() .AsParallel() .TakeWhile(Limits.ByFixedGeneration <DoubleGene, double>(1000)) .ToBestEvolutionResult(); Assert.True(result.GetTotalGenerations() >= 1000, $"Total generation must be bigger than 1000: {result.GetTotalGenerations()}"); }
public void GenotypeValidator() { const int populationSize = 100; var engine = Engine .Builder(a => a.Gene.Allele, DoubleChromosome.Of(0, 1)) .GenotypeValidator(pt => false) .PopulationSize(populationSize) .Build(); var result = engine.Stream() .Take(10) .ToBestEvolutionResult(); Assert.Equal(result.GetInvalidCount(), populationSize); }
public static void Main() { var codec = Codec.Of( () => Genotype.Of(DoubleChromosome.Of(0, 1)), gt => Player.Of(gt.Gene.DoubleValue()) ); Population <DoubleGene, double> population = null; double Fitness(Player player) { var pop = population; Player other; if (pop != null) { var index = RandomRegistry.GetRandom().NextInt(pop.Count); other = codec.Decode(pop[index].GetGenotype()); } else { other = Player.Of(0.5); } return(player.CompareTo(other)); } var engine = Engine.Engine .Builder(Fitness, codec) .Build(); var best = codec.Decode( engine.Stream() .TakeWhile(BySteadyFitness <DoubleGene, double>(50)) .Peek(er => Interlocked.Exchange(ref population, er.GetPopulation())) .ToBestGenotype()); Console.WriteLine(best.Value); }
public IChromosome GetNewChromosome() { IChromosome instance = new DoubleChromosome(this.length, new Genes.DoubleGeneFactory()); return instance; }