Example #1
0
 public static Population CreateRandomPopulation(List<City> availableCities, int populationSize)
 {
     Population p = new Population();
     for (int i = 0; i < populationSize; i++)
         p.Add(Chromosome.CreateRandomChromosome(availableCities));
     return p;
 }
Example #2
0
        public void Population_GetBestChromosome()
        {
            List<City> cities = new List<City>();
            cities.Add(new City(1, 1, "A"));
            cities.Add(new City(2, 2, "B"));
            cities.Add(new City(3, 3, "C"));
            cities.Add(new City(4, 4, "D"));
            cities.Add(new City(5, 5, "E"));

            List<City> cities2 = new List<City>();
            cities2.Add(new City(1, 1, "A"));
            cities2.Add(new City(5, 5, "B"));
            cities2.Add(new City(3, 3, "C"));
            cities2.Add(new City(4, 4, "D"));
            cities2.Add(new City(2, 2, "E"));

            Population p = new Population();
            Chromosome c = new Chromosome(cities);
            Chromosome c2 = new Chromosome(cities2);

            p.Add(c);
            p.Add(c2);

            Chromosome best = p.GetBestChromosome();

            Assert.AreEqual(c, best);
        }
Example #3
0
        public void Population_Create()
        {
            Population p = new Population();

            Assert.IsNotNull(p.Chromosomes);
            Assert.AreEqual<int>(0, p.Chromosomes.Count);
        }
Example #4
0
        private Population GenerateNewGeneration(int populationSize, Population pop)
        {
            currentGeneration = currentGeneration + 1;

            Population nextGeneration = new Population();
            for (int i = 0; i < populationSize; i++)
            {
                ChromosomePair parents = GetHighestRankedChromosomes(pop.Chromosomes);

                Chromosome child;
                if (ShouldPerformCrossover())
                    child = parents.CreateCrossoverChild();
                else
                    child = parents.CreateChild();

                if (ShouldMutate())
                    child.Mutate();

                nextGeneration.Add(child);
            }

            if (currentGeneration < generations)
                nextGeneration = GenerateNewGeneration(populationSize, nextGeneration);

            return nextGeneration;
        }
Example #5
0
        public void Population_AddChromoSome()
        {
            List<City> cities = new List<City>();
            cities.Add(new City(1, 1, "A"));

            Population p = new Population();
            Chromosome c = new Chromosome(cities);
            p.Add(c);

            Assert.AreEqual(c, p.Chromosomes[0]);
        }
Example #6
0
        public Population elite(int n)
        {
            List<Tour> best = new List<Tour>();
            Population tmp = new Population(p);

            for (int i = 0; i < n; ++i)
            {
                best.Add( tmp.findBest() );
                tmp = new Population( tmp.p.Except(best).ToList() );
            }

            return new Population(best);
        }