Example #1
0
        private CitiesSequence CrossBreed(CitiesSequence s1, CitiesSequence s2)
        {
            int[] bounds = new int[2];

            bounds[0] = bounds[1] = _random.Next(s1.Cities.Count); // probably should be -1
            while (bounds[0] == bounds[1])
            {
                bounds[1] = _random.Next(s1.Cities.Count);
            }
            bounds = bounds.OrderBy(e => e).ToArray();

            var availableCities = s1.Cities.ToList();
            var usedCities      = new List <City>();

            var newSequence = new City[s1.Cities.Count];

            for (int i = 0; i < s1.Cities.Count; i++)
            {
                // todo: check >= bound
                if (i < bounds[0] || i >= bounds[1])
                {
                    newSequence[i] = s1.Cities[i];
                    availableCities.Remove(s1.Cities[i]);
                    usedCities.Add(s1.Cities[i]);
                }
            }

            // fill unused cities between bounds
            for (int i = bounds[0]; i < bounds[1]; i++)
            {
                var tryCity = s2.Cities[i];
                if (usedCities.Contains(tryCity))
                {
                    newSequence[i] = availableCities.First();
                    availableCities.RemoveAt(0);
                    usedCities.Add(newSequence[i]);
                }
                else
                {
                    newSequence[i] = tryCity;
                    availableCities.Remove(tryCity);
                    usedCities.Add(tryCity);
                }
            }

            var result = new CitiesSequence();

            result.Cities.AddRange(newSequence);
            result.CalculateCost();

            return(result);
        }
Example #2
0
        private void RandomMutation(CitiesSequence sequence)
        {
            // todo: make constant
            if (_random.NextDouble() > 0.05)
            {
                int i1, i2;
                i1 = i2 = _random.Next(sequence.Cities.Count);

                while (i1 == i2)
                {
                    i2 = _random.Next(sequence.Cities.Count);
                }

                var temp = sequence.Cities[i1];
                sequence.Cities[i1] = sequence.Cities[i2];
                sequence.Cities[i2] = temp;
            }

            sequence.CalculateCost();
        }