Exemplo n.º 1
0
        public static AlbaLambdaInterchangeMove Apply(AlbaEncoding individual, int cities, int lambda, IRandom rand)
        {
            List <Tour> tours = individual.GetTours();

            if (tours.Count > 1)
            {
                int  route1Index = rand.Next(tours.Count);
                Tour route1      = tours[route1Index];

                int route2Index = rand.Next(tours.Count - 1);
                if (route2Index >= route1Index)
                {
                    route2Index += 1;
                }

                Tour route2 = tours[route2Index];

                int length1 = rand.Next(Math.Min(lambda + 1, route1.Stops.Count + 1));
                int index1  = rand.Next(route1.Stops.Count - length1 + 1);

                int l2Min = 0;
                if (length1 == 0)
                {
                    l2Min = 1;
                }
                int length2 = rand.Next(l2Min, Math.Min(lambda + 1, route2.Stops.Count + 1));
                int index2  = rand.Next(route2.Stops.Count - length2 + 1);

                return(new AlbaLambdaInterchangeMove(route1Index, index1, length1, route2Index, index2, length2, individual));
            }
            else
            {
                return(new AlbaLambdaInterchangeMove(0, 0, 0, 0, 0, 0, individual));
            }
        }
Exemplo n.º 2
0
        protected override void Manipulate(IRandom rand, AlbaEncoding individual)
        {
            List <Tour> tours = individual.GetTours();

            if (tours.Count > 1)
            {
                int lambda = LambdaParameter.Value.Value;

                int  route1Index = rand.Next(tours.Count);
                Tour route1      = tours[route1Index];


                int route2Index = rand.Next(tours.Count - 1);
                if (route2Index >= route1Index)
                {
                    route2Index += 1;
                }
                Tour route2 = tours[route2Index];

                int length1 = rand.Next(Math.Min(lambda + 1, route1.Stops.Count + 1));
                int index1  = rand.Next(route1.Stops.Count - length1 + 1);

                int l2Min = 0;
                if (length1 == 0)
                {
                    l2Min = 1;
                }
                int length2 = rand.Next(l2Min, Math.Min(lambda + 1, route2.Stops.Count + 1));
                int index2  = rand.Next(route2.Stops.Count - length2 + 1);

                Apply(individual, route1Index, index1, length1,
                      route2Index, index2, length2);
            }
        }
    public static AlbaIntraRouteInversionMove Apply(AlbaEncoding individual, int cities, IRandom rand) {
      int index1 = -1;
      int index2 = -1;

      List<Tour> validTours = new List<Tour>();
      foreach (Tour tour in individual.GetTours()) {
        if (tour.Stops.Count >= 4)
          validTours.Add(tour);
      }

      if (validTours.Count > 0) {
        Tour chosenTour = validTours[rand.Next(validTours.Count)];
        int currentTourStart = -1;
        for (int i = 0; i < individual.Length; i++) {
          if (individual[i] + 1 == chosenTour.Stops[0]) {
            currentTourStart = i;
            break;
          }
        }

        int currentTourEnd = currentTourStart;
        while (currentTourEnd < individual.Length &&
          individual[currentTourEnd] < cities) {
          currentTourEnd++;
        }

        int tourLength = currentTourEnd - currentTourStart;
        int a = rand.Next(tourLength - 3);
        index1 = currentTourStart + a;
        index2 = currentTourStart + rand.Next(a + 2, tourLength - 1);
      }

      return new AlbaIntraRouteInversionMove(index1, index2, individual);
    }
        protected override AlbaLambdaInterchangeMove[] GenerateMoves(AlbaEncoding individual, IVRPProblemInstance problemInstance, int lambda)
        {
            List <AlbaLambdaInterchangeMove> moves = new List <AlbaLambdaInterchangeMove>();

            List <Tour> tours = individual.GetTours();

            for (int tour1Index = 0; tour1Index < tours.Count; tour1Index++)
            {
                Tour tour1 = tours[tour1Index];
                for (int tour2Index = tour1Index + 1; tour2Index < tours.Count; tour2Index++)
                {
                    Tour tour2 = tours[tour2Index];

                    for (int length1 = 0; length1 <= Math.Min(lambda, tour1.Stops.Count); length1++)
                    {
                        for (int length2 = 0; length2 <= Math.Min(lambda, tour2.Stops.Count); length2++)
                        {
                            if (length1 != 0 || length2 != 0)
                            {
                                for (int index1 = 0; index1 < tour1.Stops.Count - length1 + 1; index1++)
                                {
                                    for (int index2 = 0; index2 < tour2.Stops.Count - length2 + 1; index2++)
                                    {
                                        moves.Add(new AlbaLambdaInterchangeMove(tour1Index, index1, length1,
                                                                                tour2Index, index2, length2, individual));
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(moves.ToArray());
        }
    public static AlbaLambdaInterchangeMove Apply(AlbaEncoding individual, int cities, int lambda, IRandom rand) {
      List<Tour> tours = individual.GetTours();

      if (tours.Count > 1) {
        int route1Index = rand.Next(tours.Count);
        Tour route1 = tours[route1Index];

        int route2Index = rand.Next(tours.Count - 1);
        if (route2Index >= route1Index)
          route2Index += 1;

        Tour route2 = tours[route2Index];

        int length1 = rand.Next(Math.Min(lambda + 1, route1.Stops.Count + 1));
        int index1 = rand.Next(route1.Stops.Count - length1 + 1);

        int l2Min = 0;
        if (length1 == 0)
          l2Min = 1;
        int length2 = rand.Next(l2Min, Math.Min(lambda + 1, route2.Stops.Count + 1));
        int index2 = rand.Next(route2.Stops.Count - length2 + 1);

        return new AlbaLambdaInterchangeMove(route1Index, index1, length1, route2Index, index2, length2, individual);
      } else {
        return new AlbaLambdaInterchangeMove(0, 0, 0, 0, 0, 0, individual);
      }
    }
    protected override void Manipulate(IRandom rand, AlbaEncoding individual) {
      int index1 = -1;
      int index2 = -1;

      List<Tour> validTours = new List<Tour>();
      foreach (Tour tour in individual.GetTours()) {
        if (tour.Stops.Count >= 4)
          validTours.Add(tour);
      }

      if (validTours.Count > 0) {
        Tour chosenTour = validTours[rand.Next(validTours.Count)];
        int currentTourStart = -1;
        for (int i = 0; i < individual.Length; i++) {
          if (individual[i] + 1 == chosenTour.Stops[0]) {
            currentTourStart = i;
            break;
          }
        }

        int currentTourEnd = currentTourStart;
        while (currentTourEnd < individual.Length &&
          individual[currentTourEnd] < ProblemInstance.Cities.Value) {
          currentTourEnd++;
        }

        int tourLength = currentTourEnd - currentTourStart;
        int a = rand.Next(tourLength - 3);
        index1 = currentTourStart + a;
        index2 = currentTourStart + rand.Next(a + 2, tourLength - 1);
      }

      Apply(individual, index1, index2);
    }
    protected override AlbaLambdaInterchangeMove[] GenerateMoves(AlbaEncoding individual, IVRPProblemInstance problemInstance, int lambda) {
      List<AlbaLambdaInterchangeMove> moves = new List<AlbaLambdaInterchangeMove>();

      List<Tour> tours = individual.GetTours();

      for (int tour1Index = 0; tour1Index < tours.Count; tour1Index++) {
        Tour tour1 = tours[tour1Index];
        for (int tour2Index = tour1Index + 1; tour2Index < tours.Count; tour2Index++) {
          Tour tour2 = tours[tour2Index];

          for (int length1 = 0; length1 <= Math.Min(lambda, tour1.Stops.Count); length1++) {
            for (int length2 = 0; length2 <= Math.Min(lambda, tour2.Stops.Count); length2++) {
              if (length1 != 0 || length2 != 0) {
                for (int index1 = 0; index1 < tour1.Stops.Count - length1 + 1; index1++) {
                  for (int index2 = 0; index2 < tour2.Stops.Count - length2 + 1; index2++) {
                    moves.Add(new AlbaLambdaInterchangeMove(tour1Index, index1, length1,
                      tour2Index, index2, length2, individual));
                  }
                }
              }
            }
          }
        }
      }

      return moves.ToArray();
    }
    public static void Apply(AlbaEncoding individual, int tour1Index, int position1, int length1,
      int tour2Index, int position2, int length2) {
      List<Tour> tours = individual.GetTours();

      Tour tour1 = tours[tour1Index];
      int tour1Start = -1;
      for (int i = 0; i < individual.Length; i++) {
        if (individual[i] == tour1.Stops[0] - 1) {
          tour1Start = i;
          break;
        }
      }

      Tour tour2 = tours[tour2Index];
      int tour2Start = -1;
      for (int i = 0; i < individual.Length; i++) {
        if (individual[i] == tour2.Stops[0] - 1) {
          tour2Start = i;
          break;
        }
      }

      AlbaEncoding original = individual.Clone() as AlbaEncoding;
      int index = 0;

      int start1 = tour1Start + position1;
      int end1 = start1 + length1;

      int start2 = tour2Start + position2;
      int end2 = start2 + length2;

      for (int i = 0; i < original.Length; i++) {
        if (index == start1) {
          if (end2 - start2 == 0)
            index = end1;
          else
            index = start2;
        } else if (index == start2) {
          if (end1 - start1 == 0)
            index = end2;
          else
            index = start1;
        } else if (index == end1) {
          index = end2;
        } else if (index == end2) {
          index = end1;
        }

        individual[i] = original[index];

        index++;
      }
    }
        public static AlbaIntraRouteInversionMove Apply(AlbaEncoding individual, int cities, IRandom rand)
        {
            int index1 = -1;
            int index2 = -1;

            List <Tour> validTours = new List <Tour>();

            foreach (Tour tour in individual.GetTours())
            {
                if (tour.Stops.Count >= 4)
                {
                    validTours.Add(tour);
                }
            }

            if (validTours.Count > 0)
            {
                Tour chosenTour       = validTours[rand.Next(validTours.Count)];
                int  currentTourStart = -1;
                for (int i = 0; i < individual.Length; i++)
                {
                    if (individual[i] + 1 == chosenTour.Stops[0])
                    {
                        currentTourStart = i;
                        break;
                    }
                }

                int currentTourEnd = currentTourStart;
                while (currentTourEnd < individual.Length &&
                       individual[currentTourEnd] < cities)
                {
                    currentTourEnd++;
                }

                int tourLength = currentTourEnd - currentTourStart;
                int a          = rand.Next(tourLength - 3);
                index1 = currentTourStart + a;
                index2 = currentTourStart + rand.Next(a + 2, tourLength - 1);
            }

            return(new AlbaIntraRouteInversionMove(index1, index2, individual));
        }
        protected override void Manipulate(IRandom rand, AlbaEncoding individual)
        {
            int index1 = -1;
            int index2 = -1;

            List <Tour> validTours = new List <Tour>();

            foreach (Tour tour in individual.GetTours())
            {
                if (tour.Stops.Count >= 4)
                {
                    validTours.Add(tour);
                }
            }

            if (validTours.Count > 0)
            {
                Tour chosenTour       = validTours[rand.Next(validTours.Count)];
                int  currentTourStart = -1;
                for (int i = 0; i < individual.Length; i++)
                {
                    if (individual[i] + 1 == chosenTour.Stops[0])
                    {
                        currentTourStart = i;
                        break;
                    }
                }

                int currentTourEnd = currentTourStart;
                while (currentTourEnd < individual.Length &&
                       individual[currentTourEnd] < ProblemInstance.Cities.Value)
                {
                    currentTourEnd++;
                }

                int tourLength = currentTourEnd - currentTourStart;
                int a          = rand.Next(tourLength - 3);
                index1 = currentTourStart + a;
                index2 = currentTourStart + rand.Next(a + 2, tourLength - 1);
            }

            Apply(individual, index1, index2);
        }
Exemplo n.º 11
0
        public static void Apply(AlbaEncoding individual, int tour1Index, int position1, int length1,
                                 int tour2Index, int position2, int length2)
        {
            List <Tour> tours = individual.GetTours();

            Tour tour1      = tours[tour1Index];
            int  tour1Start = -1;

            for (int i = 0; i < individual.Length; i++)
            {
                if (individual[i] == tour1.Stops[0] - 1)
                {
                    tour1Start = i;
                    break;
                }
            }

            Tour tour2      = tours[tour2Index];
            int  tour2Start = -1;

            for (int i = 0; i < individual.Length; i++)
            {
                if (individual[i] == tour2.Stops[0] - 1)
                {
                    tour2Start = i;
                    break;
                }
            }

            AlbaEncoding original = individual.Clone() as AlbaEncoding;
            int          index    = 0;

            int start1 = tour1Start + position1;
            int end1   = start1 + length1;

            int start2 = tour2Start + position2;
            int end2   = start2 + length2;

            for (int i = 0; i < original.Length; i++)
            {
                if (index == start1)
                {
                    if (end2 - start2 == 0)
                    {
                        index = end1;
                    }
                    else
                    {
                        index = start2;
                    }
                }
                else if (index == start2)
                {
                    if (end1 - start1 == 0)
                    {
                        index = end2;
                    }
                    else
                    {
                        index = start1;
                    }
                }
                else if (index == end1)
                {
                    index = end2;
                }
                else if (index == end2)
                {
                    index = end1;
                }

                individual[i] = original[index];

                index++;
            }
        }
    protected override void Manipulate(IRandom rand, AlbaEncoding individual) {
      List<Tour> tours = individual.GetTours();
      if (tours.Count > 1) {
        int lambda = LambdaParameter.Value.Value;

        int route1Index = rand.Next(tours.Count);
        Tour route1 = tours[route1Index];


        int route2Index = rand.Next(tours.Count - 1);
        if (route2Index >= route1Index)
          route2Index += 1;
        Tour route2 = tours[route2Index];

        int length1 = rand.Next(Math.Min(lambda + 1, route1.Stops.Count + 1));
        int index1 = rand.Next(route1.Stops.Count - length1 + 1);

        int l2Min = 0;
        if (length1 == 0)
          l2Min = 1;
        int length2 = rand.Next(l2Min, Math.Min(lambda + 1, route2.Stops.Count + 1));
        int index2 = rand.Next(route2.Stops.Count - length2 + 1);

        Apply(individual, route1Index, index1, length1,
          route2Index, index2, length2);
      }
    }