Example #1
0
        private void SimulateGeneration()
        {
            tempPopulation.Clear();
            int FitToCopy   = tourPopulation.Count * TOP_SURVIVORS / 100;
            int UnfitToCopy = BOTTOM_SURVIVORS;
            int i;

            for (i = 0; i < FitToCopy; i++)
            {
                if (i != 0 && rand.Next(1, 100) <= MUTATION_PROBABILITY)
                {
                    Mutate(tourPopulation[i]);
                }
                tempPopulation.Add(tourPopulation[i]);
            }
            for (; i < tourPopulation.Count - UnfitToCopy; i++)
            {
                Breed(tourPopulation[i]);
                tempPopulation.Add(tourPopulation[i]);
            }
            for (; i < tourPopulation.Count; i++)
            {
                if (rand.Next(1, 100) <= MUTATION_PROBABILITY * 2)
                {
                    Tour C = new Tour();
                    C.AddRange(tourPopulation[i].ToArray());
                    Mutate(C);
                    tempPopulation.Add(C);
                }
            }
            tourPopulation.Clear();
            tourPopulation.AddRange(tempPopulation);
        }
Example #2
0
        private void Crossover(Tour parent1, Tour parent2, Tour child)
        {
            int unique = 0;

            child.Clear();
            int        index = 0;
            int        start = rand.Next(0, numberOfCities * 6 / 10);
            int        len   = rand.Next(numberOfCities * 3 / 10, (numberOfCities - 1 - start));
            List <int> S;

            S = parent1.GetRange(start, len);
            int i = 0;

            while (index < start)
            {
                if (!S.Contains(parent2[i]))
                {
                    child.Add(parent2[i]);
                    index++;
                }
                else
                {
                    unique++;
                }
                i++;
            }
            child.AddRange(S);
            index += len;
            while (index < parent1.Count())
            {
                if (!S.Contains(parent2[i]))
                {
                    unique++;
                    child.Add(parent2[i]);
                    index++;
                }
                else
                {
                    unique++;
                }
                i++;
            }
            if (unique == numberOfCities - len)
            {
                Mutate(child);
            }
        }