Example #1
0
    public bool IsEqual(Tour tour) {
      bool equal = (tour != null) && (tour.Stops.Count == Stops.Count);
      int index = 0;

      while (equal && index < Stops.Count) {
        equal = equal && tour.Stops[index] == Stops[index];
        index++;
      }

      return equal;
    }
    public static void GuidedRelocateMove(PotvinEncoding initiator, PotvinEncoding guide, IRandom random) {
      List<int> cities = new List<int>();
      foreach (Tour tour in initiator.Tours) {
        foreach (int city in tour.Stops) {
          Tour guideTour = guide.Tours.First(t => t.Stops.Contains(city));
          if (guide.Tours.IndexOf(guideTour) != initiator.Tours.IndexOf(tour)) {
            cities.Add(city);
          }
        }
      }

      if (cities.Count == 0) {
        RelocateMove(initiator, random);
      } else {
        int city = cities[random.Next(cities.Count)];
        Tour tour = initiator.Tours.First(t => t.Stops.Contains(city));
        tour.Stops.Remove(city);

        Tour guideTour = guide.Tours.First(t => t.Stops.Contains(city));
        int guideTourIndex = guide.Tours.IndexOf(guideTour);

        if (guideTourIndex < initiator.Tours.Count) {
          Tour tour2 = initiator.Tours[guideTourIndex];

          int guideIndex = guideTour.Stops.IndexOf(city);
          if (guideIndex == 0) {
            tour2.Stops.Insert(0, city);
          } else {
            int predecessor = guideTour.Stops[guideIndex - 1];
            int initIndex = tour2.Stops.IndexOf(predecessor);
            if (initIndex != -1) {
              tour2.Stops.Insert(initIndex + 1, city);
            } else {
              if (guideIndex == guideTour.Stops.Count - 1) {
                tour2.Stops.Insert(tour2.Stops.Count, city);
              } else {
                int sucessor = guideTour.Stops[guideIndex + 1];
                initIndex = tour2.Stops.IndexOf(sucessor);
                if (initIndex != -1) {
                  tour2.Stops.Insert(initIndex, city);
                } else {
                  tour2.Stops.Insert(random.Next(tour2.Stops.Count + 1), city);
                }
              }
            }
          }
        } else {
          Tour tour2 = new Tour();
          tour2.Stops.Add(city);
          initiator.Tours.Add(tour2);
        }

        if (tour.Stops.Count == 0)
          initiator.Tours.Remove(tour);
      }
    }
 private static int MatchingCities(Tour tour1, Tour tour2) {
   return tour1.Stops.Intersect(tour2.Stops).Count();
 }
Example #4
0
 protected Tour(Tour original, Cloner cloner)
   : base(original, cloner) {
   this.Stops = new List<int>(original.Stops);
 }
        public static void GuidedRelocateMove(PotvinEncoding initiator, PotvinEncoding guide, IRandom random)
        {
            List <int> cities = new List <int>();

            foreach (Tour tour in initiator.Tours)
            {
                foreach (int city in tour.Stops)
                {
                    Tour guideTour = guide.Tours.First(t => t.Stops.Contains(city));
                    if (guide.Tours.IndexOf(guideTour) != initiator.Tours.IndexOf(tour))
                    {
                        cities.Add(city);
                    }
                }
            }

            if (cities.Count == 0)
            {
                RelocateMove(initiator, random);
            }
            else
            {
                int  city = cities[random.Next(cities.Count)];
                Tour tour = initiator.Tours.First(t => t.Stops.Contains(city));
                tour.Stops.Remove(city);

                Tour guideTour      = guide.Tours.First(t => t.Stops.Contains(city));
                int  guideTourIndex = guide.Tours.IndexOf(guideTour);

                if (guideTourIndex < initiator.Tours.Count)
                {
                    Tour tour2 = initiator.Tours[guideTourIndex];

                    int guideIndex = guideTour.Stops.IndexOf(city);
                    if (guideIndex == 0)
                    {
                        tour2.Stops.Insert(0, city);
                    }
                    else
                    {
                        int predecessor = guideTour.Stops[guideIndex - 1];
                        int initIndex   = tour2.Stops.IndexOf(predecessor);
                        if (initIndex != -1)
                        {
                            tour2.Stops.Insert(initIndex + 1, city);
                        }
                        else
                        {
                            if (guideIndex == guideTour.Stops.Count - 1)
                            {
                                tour2.Stops.Insert(tour2.Stops.Count, city);
                            }
                            else
                            {
                                int sucessor = guideTour.Stops[guideIndex + 1];
                                initIndex = tour2.Stops.IndexOf(sucessor);
                                if (initIndex != -1)
                                {
                                    tour2.Stops.Insert(initIndex, city);
                                }
                                else
                                {
                                    tour2.Stops.Insert(random.Next(tour2.Stops.Count + 1), city);
                                }
                            }
                        }
                    }
                }
                else
                {
                    Tour tour2 = new Tour();
                    tour2.Stops.Add(city);
                    initiator.Tours.Add(tour2);
                }

                if (tour.Stops.Count == 0)
                {
                    initiator.Tours.Remove(tour);
                }
            }
        }
 private static int MatchingCities(Tour tour1, Tour tour2)
 {
     return(tour1.Stops.Intersect(tour2.Stops).Count());
 }
Example #7
0
 protected Tour(Tour original, Cloner cloner)
     : base(original, cloner)
 {
     this.Stops = new List <int>(original.Stops);
 }