Ejemplo n.º 1
0
        public double GetObjective(Chromosome c)
        {
            IntegerChromosome ic = (IntegerChromosome)c;

            double distance        = 0;
            int    currentPosition = 0;
            int    initialPosition = 0;

            for (int i = 0; i < ic.Genes.Length; i++)
            {
                if (i == 0)
                {
                    initialPosition = cities[ic.Genes[i]];
                    currentPosition = cities[ic.Genes[i]];
                }
                else
                {
                    distance       += Math.Abs(cities[ic.Genes[i]] - currentPosition);
                    currentPosition = cities[ic.Genes[i]];
                }
            }
            distance += Math.Abs(cities[ic.Genes[ic.Genes.Length - 1]] - initialPosition);

            return(distance);
        }
Ejemplo n.º 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}");
        }
        public void ToFloatingPoint_NoArgs_Double()
        {
            RandomizationProvider.Current = MockRepository.GenerateMock <IRandomization> ();
            RandomizationProvider.Current.Expect(r => r.GetInt(0, 3)).Return(2);
            var target = new IntegerChromosome(0, 3);
            var actual = target.ToInteger();

            Assert.AreEqual(2, actual);
        }
Ejemplo n.º 4
0
        public void ToFloatingPoint_NoArgs_Double()
        {
            RandomizationProvider.Current = Substitute.For <IRandomization> ();
            RandomizationProvider.Current.GetInt(0, 3).Returns(2);
            var target = new IntegerChromosome(0, 3);
            var actual = target.ToInteger();

            Assert.AreEqual(2, actual);
        }
Ejemplo n.º 5
0
        public static ICodec <int, IntegerGene> OfScalar(IntRange domain)
        {
            NonNull(domain);

            return(Codec.Of(
                       () => Genotype.Of(IntegerChromosome.Of(domain)),
                       gt => gt.GetChromosome().GetGene().Allele
                       ));
        }
Ejemplo n.º 6
0
        public void Constructor()
        {
            Assert.ThrowsException <ArgumentNullException>(
                () => new Specie(2, 3, 0u, null));
            var adam = new IntegerChromosome(0, 1);
            var s    = new Specie(2, 10, 5u, adam);

            Assert.AreEqual(5u, s.ID);
            Assert.AreSame(adam, s.Centroid);
        }
Ejemplo n.º 7
0
        public void ToInteger_NegativeValue_Integer()
        {
            RandomizationProvider.Current = Substitute.For <IRandomization>();
            RandomizationProvider.Current.GetInt(0, 3).Returns(-2);

            var target = new IntegerChromosome(0, 3);

            Assert.AreEqual("11111111111111111111111111111110", target.ToString());

            var actual = target.ToInteger();

            Assert.AreEqual(-2, actual);
        }
Ejemplo n.º 8
0
        public void CreateNew_NoArgs_NewInstance()
        {
            RandomizationProvider.Current = Substitute.For <IRandomization>();
            RandomizationProvider.Current.GetInt(0, 3).Returns(-2);

            var old    = new IntegerChromosome(0, 3);
            var target = old.CreateNew() as IntegerChromosome;

            Assert.AreNotSame(old, target);
            Assert.AreEqual("11111111111111111111111111111110", target.ToString());

            var actual = target.ToInteger();

            Assert.AreEqual(-2, actual);
        }
Ejemplo n.º 9
0
        public double GetObjective(Chromosome c)
        {
            IntegerChromosome ic = (IntegerChromosome)c;

            double distance = 0;    // actually distance^2
            City   currentCity;
            City   nextCity;

            currentCity = cities[ic.Genes[0]];
            for (int i = 0; i < numberOfCities + 1; i++)
            {
                //
                // The next city to be visited is given by the current gene.
                // If we've already gone through all of the genes, the next city
                // is the one from which we started.
                //
                if (i < numberOfCities)
                {
                    nextCity = cities[ic.Genes[i]];
                }
                else
                {
                    nextCity = cities[ic.Genes[0]];
                }

                //
                // Calculate the distance^2 between the currentCity and the next.
                // (the sqrt() of the distance is unnecessary)
                //
                int dx = nextCity.x - currentCity.x;    // change in x
                int dy = nextCity.y - currentCity.y;    // change in y
                int z  = (dx * dx) + (dy * dy);         // dist^2 = dx^2 + dy^2

                distance += z;

                //
                // Advance to the next city.
                //
                currentCity = nextCity;
            }

            return(distance);
        }
Ejemplo n.º 10
0
        void Example()
        {
            var i = new IntegerChromosome(3, 3);

            // 31 is the least significant bit
            // 0 is sign bit
            i.FlipGene(31);
            Console.WriteLine("I3: " + i.ToInteger());


            var array = new int[1];
            // most significant bit is the right most bit.
            // Last bit is sign bit
            var genes    = new bool[] { false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false };
            var bitArray = new BitArray(genes);

            bitArray.CopyTo(array, 0);

            Console.WriteLine(array[0]);
        }
        public static void ClassInit(TestContext tc)
        {
            var chrom = new IntegerChromosome(0, 1);

            Graph1 = new GraphChromosome(chrom, chrom, 2);
            Graph1.AddVertex(0u);
            Graph1.AddVertex(2u);
            Graph1.AddEdge(0u, 2u);
            Graph1.AddEdge(2u, 0u);
            Graph2 = new GraphChromosome(chrom, chrom, 4);
            Graph2.AddVertex(0u);
            Graph2.AddVertex(1u);
            Graph2.AddVertex(2u);
            Graph2.AddVertex(3u);
            Graph2.AddEdge(0u, 2u);
            Graph2.AddEdge(1u, 2u);
            Graph2.AddEdge(2u, 3u);

            nVertMismatch = 2;
            nEdgeMismatch = 3;
        }
Ejemplo n.º 12
0
        public GeneticAlgorithm GetGA(int seed, int termination)
        {
            RandomizationProvider.Current = new FastRandomRandomizationWithSeed();
            FastRandomRandomizationWithSeed.setSeed(seed);

            var selection  = new EliteSelection();
            var crossover  = new OnePointCrossover();
            var mutation   = new FlipBitMutation();
            var fitness    = new IntegerMaximizationFitness();
            var chromosome = new IntegerChromosome(0, 9);
            var population = new Population(2, 10, chromosome);

            var ga = new GeneticAlgorithm(population, fitness, selection, crossover, mutation)
            {
                Termination = new GenerationNumberTermination(termination)
            };

            ga.Start();

            return(ga);
        }
Ejemplo n.º 13
0
        public void Integer()
        {
            var target = new IntegerChromosome(0, 10);

            target.Clone();
            target.CompareTo(new IntegerChromosome(0, 10));
            target.CreateNew();
            var x = target.Fitness;

            target.FlipGene(0);
            target.GenerateGene(0);
            target.GetGene(0);
            target.GetGenes();
            target.GetHashCode();
            var y = target.Length;

            target.ReplaceGene(0, new Gene(false));
            target.ReplaceGenes(0, new Gene[] { new Gene(false), new Gene(true) });
            target.Resize(20);
            target.ToInteger();
            target.ToString();
        }
Ejemplo n.º 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));
        }
Ejemplo n.º 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());
        }
        public void FlipGene_Index_ValueFlip()
        {
            RandomizationProvider.Current = Substitute.For <IRandomization>();
            RandomizationProvider.Current.GetInt(0, 3).Returns(-2);

            var target = new IntegerChromosome(0, 3);

            Assert.AreEqual("11111111111111111111111111111110", target.ToString());

            target.FlipGene(0);
            Assert.AreEqual("01111111111111111111111111111110", target.ToString());

            target.FlipGene(1);
            Assert.AreEqual("00111111111111111111111111111110", target.ToString());

            target.FlipGene(10);
            Assert.AreEqual("00111111110111111111111111111110", target.ToString());

            target.FlipGene(31);
            Assert.AreEqual("00111111110111111111111111111111", target.ToString());

            target.FlipGene(30);
            Assert.AreEqual("00111111110111111111111111111101", target.ToString());
        }
Ejemplo n.º 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);
        }