Exemple #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)
                    );
            });
        }
Exemple #2
0
        public void StreamWithInitialGenotypes()
        {
            var problem = Problem.Of(
                a => a,
                Codec.Of(
                    () => Genotype.Of(IntegerChromosome.Of(0, 1000)),
                    g => g.Gene.Allele
                    )
                );

            const int genotypeCount = 10;
            const int max           = 1000;
            var       genotypes     = IntRange.Of(1, genotypeCount)
                                      .Select(i => IntegerChromosome.Of(IntegerGene.Of(max, 0, max)))
                                      .Select(i => Genotype.Of(i))
                                      .ToImmutableSeq();

            var engine = Engine.Builder(problem).Build();

            var result = engine.Stream(genotypes)
                         .Take(1)
                         .ToBestEvolutionResult();

            long maxCount = result.GetPopulation().Count(pt => pt.GetFitness() == max);

            Assert.True(maxCount >= genotypeCount, $"{maxCount} >= {genotypeCount}");
        }
Exemple #3
0
 public static ICodec <double, DoubleGene> OfScalar(DoubleRange domain)
 {
     return(Codec.Of(
                () => Genotype.Of(DoubleChromosome.Of(domain)),
                gt => gt.GetChromosome().GetGene().Allele
                ));
 }
Exemple #4
0
 public static Builder <TGene, TAllele> Builder <TGene, TAllele>(Func <Genotype <TGene>, TAllele> ff,
                                                                 IChromosome <TGene> chromosome,
                                                                 params IChromosome <TGene>[] chromosomes)
     where TGene : IGene <TGene>
     where TAllele : IComparable <TAllele>, IConvertible
 {
     return(new Builder <TGene, TAllele>(() => Genotype.Of(chromosome, chromosomes), ff));
 }
Exemple #5
0
        public static ICodec <long, LongGene> OfScalar(LongRange domain)
        {
            NonNull(domain);

            return(Codec.Of(
                       () => Genotype.Of(LongChromosome.Of(domain)),
                       gt => gt.GetChromosome().GetGene().Allele
                       ));
        }
Exemple #6
0
 public static ICodec <TAllele, AnyGene <TAllele> > OfScalar <TAllele>(
     Func <TAllele> supplier
     )
 {
     return(Codec.Of(
                () => Genotype.Of(AnyChromosome.Of(supplier)),
                gt => gt.Gene.Allele
                ));
 }
Exemple #7
0
        public static ICodec <int, IntegerGene> OfScalar(IntRange domain)
        {
            NonNull(domain);

            return(Codec.Of(
                       () => Genotype.Of(IntegerChromosome.Of(domain)),
                       gt => gt.GetChromosome().GetGene().Allele
                       ));
        }
Exemple #8
0
        public static ICodec <IImmutableSeq <T>, BitGene> OfSubSet <T>(IImmutableSeq <T> basicSet)
        {
            Positive(basicSet.Length);

            return(Codec.Of(
                       () => Genotype.Of(BitChromosome.Of(basicSet.Length)),
                       gt => ((BitChromosome)gt.GetChromosome()).Ones().Select(i => basicSet[i]).ToImmutableSeq()
                       ));
        }
Exemple #9
0
        public static ICodec <int[], EnumGene <int> > OfPermutation(int length)
        {
            Positive(length);

            return(Codec.Of(
                       () => Genotype.Of(PermutationChromosome.OfInteger(length)),
                       gt => gt.GetChromosome().ToSeq()
                       .Select(g => g.Allele)
                       .ToArray()
                       ));
        }
Exemple #10
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())
                ));
 }
Exemple #11
0
        public static ICodec <IImmutableSeq <T>, EnumGene <T> > OfSubSet <T>(
            IImmutableSeq <T> basicSet,
            int size
            )
        {
            NonNull(basicSet);
            Base.CheckSubSet(basicSet.Length, size);

            return(Codec.Of(
                       () => Genotype.Of(PermutationChromosome.Of(basicSet, size)),
                       gt => gt.GetChromosome()
                       .Select(g => g.Allele)
                       .ToImmutableSeq()));
        }
Exemple #12
0
        public static void Main()
        {
            // 1.) Define the genotype (factory) suitable
            //     for the problem.
            Factory <Genotype <BitGene> > F = delegate()
            {
                return(Genotype.Of(BitChromosome.Of(10, 0.5)));
            };

            // 3.) Create the execution environment.
            var engine = Engine.Engine.Builder(Eval, F).Build();

            // 4.) Start the execution (evolution) and
            //     collect the result.
            var result = engine.Stream().Take(100).ToBestGenotype();

            Console.WriteLine("Hello World:\n" + result);
        }
Exemple #13
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);
        }
Exemple #14
0
        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));
        }
Exemple #15
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());
        }
            private Genotype <TGene> Mutate(Genotype <TGene> genotype, double p, IntRef alterations)
            {
                IList <IChromosome <TGene> > chromosomes =
                    new List <IChromosome <TGene> >(genotype.ToSeq());

                var random = RandomRegistry.GetRandom();
                var rd     = random.NextDouble();

                if (rd < 1 / 3.0)
                {
                    chromosomes.RemoveAt(0);
                }
                else if (rd < 2 / 3.0)
                {
                    chromosomes.Add(chromosomes[0].NewInstance());
                }

                alterations.Value +=
                    Internal.Math.random.Indexes(RandomRegistry.GetRandom(), chromosomes.Count, p)
                    .Select(i => Mutate(chromosomes, i, p))
                    .Sum();

                return(Genotype.Of(chromosomes));
            }
Exemple #17
0
        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);
        }