Пример #1
0
        public void StreamWithSerializedPopulation()
        {
            var problem = Problem.Of(
                x => Math.Cos(0.5 + Math.Sin(x)) * Math.Cos(x),
                Codecs.OfScalar(DoubleRange.Of(0.0, 2.0 * Math.PI))
                );

            var engine = Engine.Builder(problem)
                         .Optimize(Optimize.Minimum)
                         .OffspringSelector(new RouletteWheelSelector <DoubleGene, double>())
                         .Build();

            var interimResult = engine.Stream()
                                .TakeWhile(Limits.BySteadyFitness <DoubleGene, double>(10))
                                .ToBestEvolutionResult();

            using (var stream = new MemoryStream())
            {
                IFormatter formatter = new BinaryFormatter();
                formatter.Serialize(stream, interimResult);

                stream.Seek(0, SeekOrigin.Begin);
                var loadedResult = (EvolutionResult <DoubleGene, double>)formatter.Deserialize(stream);

                var result = engine.Stream(loadedResult)
                             .Take(10)
                             .ToBestEvolutionResult();

                Assert.NotEqual(0.0, result.GetBestFitness());
            }
        }
Пример #2
0
        public static void Main()
        {
            var engine = Engine.Engine
                         .Builder(
                Fitness,
                Codecs.OfScalar(DoubleRange.Of(0.0, 2.0 * Math.PI)))
                         .PopulationSize(500)
                         .Optimize(Optimize.Minimum)
                         .Alterers(
                new Mutator <DoubleGene, double>(0.03),
                new MeanAlterer <DoubleGene, double>(0.6))
                         .Build();

            var
                statistics = EvolutionStatistics.OfNumber <double>();

            var best = engine.Stream()
                       .TakeWhile(BySteadyFitness <DoubleGene, double>(7))
                       .Take(100)
                       .Peek(statistics.Accept)
                       .ToBestPhenotype();

            Console.WriteLine(statistics);
            Console.WriteLine(best);
        }
Пример #3
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())
                ));
 }
Пример #4
0
        public static IEnumerable <object[]> DoubleScalarData()
        {
            yield return(new object[] { DoubleRange.Of(0, 1) });

            yield return(new object[] { DoubleRange.Of(0, 10) });

            yield return(new object[] { DoubleRange.Of(1, 2) });

            yield return(new object[] { DoubleRange.Of(0, 100) });

            yield return(new object[] { DoubleRange.Of(10, 1000) });

            yield return(new object[] { DoubleRange.Of(1000, 10000) });
        }
Пример #5
0
        public static void Main()
        {
            var domain1 = IntRange.Of(0, 100);
            var domain2 = LongRange.Of(0, 1000000000000L);
            var domain3 = DoubleRange.Of(0, 1);

            var codec = Codec(domain1, domain2, domain3);

            var engine = Engine.Engine.Builder(F, codec).Build();

            var gt = engine.Stream().Take(100).ToBestGenotype();

            var param = codec.Decoder()(gt);

            Console.WriteLine($"Result:\t{param}");
        }
Пример #6
0
        public void InitialResult()
        {
            var problem = Problem.Of(
                x => Math.Cos(0.5 + Math.Sin(x)) * Math.Cos(x),
                Codecs.OfScalar(DoubleRange.Of(0.0, 2.0 * Math.PI))
                );

            var engine = Engine.Builder(problem)
                         .Optimize(Optimize.Minimum)
                         .OffspringSelector(new RouletteWheelSelector <DoubleGene, double>())
                         .Build();

            var interimResult = engine.Stream()
                                .TakeWhile(Limits.BySteadyFitness <DoubleGene, double>(10))
                                .ToBestEvolutionResult();

            engine.Builder()
            .Alterers(new Mutator <DoubleGene, double>()).Build()
            .Stream(interimResult);
        }
Пример #7
0
        public static void Main()
        {
            const int    μ = 5;
            const int    λ = 20;
            const double p = 0.2;

            var codec = Codecs.OfScalar(DoubleRange.Of(0, 1));

            var engine = Engine.Engine
                         .Builder(Fitness, codec)
                         .PopulationSize(λ)
                         .SurvivorsSize(0)
                         .OffspringSelector(new TruncationSelector <DoubleGene, double>(μ))
                         .Alterers(new Mutator <DoubleGene, double>(p))
                         .Build();

            Console.WriteLine(
                codec.Decode(
                    engine.Stream()
                    .Take(100)
                    .ToBestGenotype()
                    )
                );
        }