Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        private Population InitPopulation()
        {
            Population pop = new Population();

            pop.Add(new Sequence(parent.BSSF));

            City[]      cities = parent.GetCities();
            City        start  = cities[0];
            GreedyMaker maker  = new GreedyMaker(cities);
            List <City> greedy = new List <City>();
            City        source = start;

            greedy.Add(source);
            while (greedy.Count < cities.Length)
            {
                var destinations = maker[source];
                foreach (var c in destinations)
                {
                    City gcity = c.Destination;
                    if (!greedy.Contains(gcity))
                    {
                        greedy.Add(gcity);
                        source = gcity;
                        break;
                    }
                }
            }
            pop.Add(new Sequence(greedy));


            while (pop.Size < PopulationSize)
            {
                City        city = start;
                List <City> gene = new List <City>();
                while (gene.Count < cities.Length)
                {
                    if (!gene.Contains(city))
                    {
                        gene.Add(city);
                    }
                    city = maker[city][(random.Next() <= GreedyPreferenceRate ?
                                        random.Next(GreedyVariance) :
                                        random.Next(maker[city].Count))].Destination;
                }
                pop.Add(new Sequence(gene));
            }

            return(pop);
        }
Exemplo n.º 3
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;
 }
Exemplo n.º 4
0
        private void CrossPopulation(ref Population pop)
        {
            Population children = new Population();

            children.Add(pop.Best);
            while (children.Size < PopulationSize)
            {
                var mom = TournamentParents(pop);
                var dad = TournamentParents(pop);

                var chillen = Cross(mom, dad);
                children.Add(chillen.Item1);
                children.Add(chillen.Item2);
            }
            pop = children;
        }
Exemplo n.º 5
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;
        }
Exemplo n.º 6
0
        private Sequence TournamentParents(Population pop)
        {
            Population tournament = new Population();

            for (int i = 0; i < TournamentSize; i++)
            {
                tournament.Add(pop.GetCandidate());
            }
            return(tournament.Best);
        }
Exemplo n.º 7
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]);
        }