Beispiel #1
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);
            }
        }