Ejemplo n.º 1
0
        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)
                    );
            });
        }
Ejemplo n.º 2
0
 public static ICodec <double, DoubleGene> OfScalar(DoubleRange domain)
 {
     return(Codec.Of(
                () => Genotype.Of(DoubleChromosome.Of(domain)),
                gt => gt.GetChromosome().GetGene().Allele
                ));
 }
Ejemplo n.º 3
0
        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());
        }
Ejemplo n.º 4
0
        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());
        }
Ejemplo n.º 5
0
        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);
        }
Ejemplo n.º 6
0
 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())
                ));
 }
Ejemplo n.º 7
0
        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()}");
        }
Ejemplo n.º 8
0
        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);
        }
Ejemplo n.º 9
0
        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);
        }