Exemplo n.º 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());
            }
        }
Exemplo n.º 2
0
        public void OfDoubleScalar(DoubleRange domain)
        {
            var codec = Codecs.OfScalar(domain);

            var gt = codec.Encoding()();

            Assert.Equal(1, gt.Length);
            Assert.Equal(1, gt.GetChromosome().Length);
            Assert.Equal(domain.Min, gt.Gene.Min);
            Assert.Equal(domain.Max, gt.Gene.Max);

            var f = codec.Decoder();

            Assert.Equal(gt.Gene.DoubleValue(), f(gt));
        }
Exemplo n.º 3
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);
        }