public static void Apply(PotvinEncoding solution, PotvinPDShiftMove move, IVRPProblemInstance problemInstance) {
      bool newTour = false;

      if (move.Tour >= solution.Tours.Count) {
        solution.Tours.Add(new Tour());
        newTour = true;
      }
      Tour tour = solution.Tours[move.Tour];

      Tour oldTour = solution.Tours.Find(t => t.Stops.Contains(move.City));
      oldTour.Stops.Remove(move.City);

      if (problemInstance is IPickupAndDeliveryProblemInstance) {
        IPickupAndDeliveryProblemInstance pdp = problemInstance as IPickupAndDeliveryProblemInstance;

        int location = pdp.GetPickupDeliveryLocation(move.City);
        Tour oldTour2 = solution.Tours.Find(t => t.Stops.Contains(location));
        oldTour2.Stops.Remove(location);

        solution.InsertPair(tour, move.City, location, problemInstance);
      } else {
        int place = solution.FindBestInsertionPlace(tour, move.City);
        tour.Stops.Insert(place, move.City);
      }

      if (newTour) {
        List<int> vehicles = new List<int>();
        for (int i = move.Tour; i < problemInstance.Vehicles.Value; i++) {
          vehicles.Add(solution.GetVehicleAssignment(i));
        }

        double bestQuality = double.MaxValue;
        int bestVehicle = -1;

        int originalVehicle = solution.GetVehicleAssignment(move.Tour);
        foreach (int vehicle in vehicles) {
          solution.VehicleAssignment[move.Tour] = vehicle;

          double quality = problemInstance.EvaluateTour(tour, solution).Quality;
          if (quality < bestQuality) {
            bestQuality = quality;
            bestVehicle = vehicle;
          }
        }

        solution.VehicleAssignment[move.Tour] = originalVehicle;

        int index = -1;
        for (int i = move.Tour; i < solution.VehicleAssignment.Length; i++) {
          if (solution.VehicleAssignment[i] == bestVehicle) {
            index = i;
            break;
          }
        }
        solution.VehicleAssignment[index] = originalVehicle;
        solution.VehicleAssignment[move.Tour] = bestVehicle;
      }

      solution.Repair();
    }
예제 #2
0
        public static void Apply(PotvinEncoding solution, PotvinPDRearrangeMove move, IVRPProblemInstance problemInstance)
        {
            Tour tour     = solution.Tours[move.Tour];
            int  position = tour.Stops.IndexOf(move.City);

            IPickupAndDeliveryProblemInstance pdp = problemInstance as IPickupAndDeliveryProblemInstance;

            if (pdp != null)
            {
                int  location  = pdp.GetPickupDeliveryLocation(move.City);
                Tour tour2     = solution.Tours.Find(t => t.Stops.Contains(location));
                int  position2 = tour2.Stops.IndexOf(location);

                tour.Stops.Remove(move.City);
                tour2.Stops.Remove(location);

                solution.InsertPair(tour, move.City, location, problemInstance, position, position2);
            }
            else
            {
                tour.Stops.Remove(move.City);
                int place = solution.FindBestInsertionPlace(tour, move.City, position);
                tour.Stops.Insert(place, move.City);
            }

            solution.Repair();
        }
    public static void Apply(PotvinEncoding solution, PotvinPDExchangeMove move, IVRPProblemInstance problemInstance) {
      if (move.Tour >= solution.Tours.Count)
        solution.Tours.Add(new Tour());
      Tour tour = solution.Tours[move.Tour];

      Tour oldTour = solution.Tours.Find(t => t.Stops.Contains(move.City));
      oldTour.Stops.Remove(move.City);

      if (problemInstance is IPickupAndDeliveryProblemInstance) {
        IPickupAndDeliveryProblemInstance pdp = problemInstance as IPickupAndDeliveryProblemInstance;

        int location = pdp.GetPickupDeliveryLocation(move.City);
        Tour oldTour2 = solution.Tours.Find(t => t.Stops.Contains(location));
        oldTour2.Stops.Remove(location);

        location = pdp.GetPickupDeliveryLocation(move.Replaced);
        oldTour2 = solution.Tours.Find(t => t.Stops.Contains(location));

        oldTour2.Stops.Remove(location);
        tour.Stops.Remove(move.Replaced);

        solution.InsertPair(tour, move.City, pdp.GetPickupDeliveryLocation(move.City), problemInstance);
        solution.InsertPair(oldTour, move.Replaced, pdp.GetPickupDeliveryLocation(move.Replaced), problemInstance);
      } else {
        tour.Stops.Remove(move.Replaced);

        int place = solution.FindBestInsertionPlace(tour, move.City);
        tour.Stops.Insert(place, move.City);

        place = solution.FindBestInsertionPlace(oldTour, move.Replaced);
        oldTour.Stops.Insert(place, move.Replaced);
      }

      solution.Repair();
    }
        public static void Apply(PotvinEncoding solution, PotvinPDExchangeMove move, IVRPProblemInstance problemInstance)
        {
            if (move.Tour >= solution.Tours.Count)
            {
                solution.Tours.Add(new Tour());
            }
            Tour tour = solution.Tours[move.Tour];

            Tour oldTour = solution.Tours.Find(t => t.Stops.Contains(move.City));

            oldTour.Stops.Remove(move.City);

            if (problemInstance is IPickupAndDeliveryProblemInstance)
            {
                IPickupAndDeliveryProblemInstance pdp = problemInstance as IPickupAndDeliveryProblemInstance;

                int  location = pdp.GetPickupDeliveryLocation(move.City);
                Tour oldTour2 = solution.Tours.Find(t => t.Stops.Contains(location));
                oldTour2.Stops.Remove(location);

                location = pdp.GetPickupDeliveryLocation(move.Replaced);
                oldTour2 = solution.Tours.Find(t => t.Stops.Contains(location));

                oldTour2.Stops.Remove(location);
                tour.Stops.Remove(move.Replaced);

                solution.InsertPair(tour, move.City, pdp.GetPickupDeliveryLocation(move.City), problemInstance);
                solution.InsertPair(oldTour, move.Replaced, pdp.GetPickupDeliveryLocation(move.Replaced), problemInstance);
            }
            else
            {
                tour.Stops.Remove(move.Replaced);

                int place = solution.FindBestInsertionPlace(tour, move.City);
                tour.Stops.Insert(place, move.City);

                place = solution.FindBestInsertionPlace(oldTour, move.Replaced);
                oldTour.Stops.Insert(place, move.Replaced);
            }

            solution.Repair();
        }
    protected override int Improve(PotvinEncoding solution) {
      int evaluatedSolutions = 0;

      var rand = RandomParameter.ActualValue;
      var instance = ProblemInstance;
      int sampleSize = SampleSizeParameter.Value.Value;
      int attempts = ImprovementAttemptsParameter.Value.Value;
      int customers = instance.Cities.Value;

      // store city-to-tour assignment and position of the city within the tour
      var tours = new Dictionary<int, Tour>();
      var position = new Dictionary<int, int>();
      foreach (Tour tour in solution.Tours) {
        for (int stop = 0; stop < tour.Stops.Count; stop++) {
          int city = tour.Stops[stop];
          tours[city] = tour;
          position[city] = stop;
        }
      }

      for (int attempt = 0; attempt < attempts; attempt++) {
        for (int sample = 0; sample < sampleSize; sample++) {
          int chosenCust = 1 + rand.Next(customers);
          var custTour = tours[chosenCust];

          double beforeQuality = instance.EvaluateTour(custTour, solution).Quality;
          evaluatedSolutions++;

          custTour.Stops.RemoveAt(position[chosenCust]);
          int place = solution.FindBestInsertionPlace(custTour, chosenCust);
          evaluatedSolutions += custTour.Stops.Count;
          custTour.Stops.Insert(place, chosenCust);

          if (place != position[chosenCust]) {
            double afterQuality = instance.EvaluateTour(custTour, solution).Quality;
            if (afterQuality > beforeQuality) {
              // revert move
              custTour.Stops.RemoveAt(place);
              custTour.Stops.Insert(position[chosenCust], chosenCust);
            } else {
              // accept move and update positions of the cities within the tour
              for (int stop = 0; stop < custTour.Stops.Count; stop++) {
                int city = custTour.Stops[stop];
                position[city] = stop;
              }
              break;
            }
          }
        }
      }

      return evaluatedSolutions;
    }
    public static void Apply(PotvinEncoding solution, PotvinCustomerRelocationMove move, IVRPProblemInstance problemInstance) {
      if (move.Tour >= solution.Tours.Count)
        solution.Tours.Add(new Tour());
      Tour tour = solution.Tours[move.Tour];

      Tour oldTour = solution.Tours.Find(t => t.Stops.Contains(move.City));
      oldTour.Stops.Remove(move.City);
      /*if (oldTour.Stops.Count == 0)
        solution.Tours.Remove(oldTour);*/

      int place = solution.FindBestInsertionPlace(tour, move.City);
      tour.Stops.Insert(place, move.City);

      solution.Repair();
    }
        public static void Apply(PotvinEncoding solution, PotvinCustomerRelocationMove move, IVRPProblemInstance problemInstance)
        {
            if (move.Tour >= solution.Tours.Count)
            {
                solution.Tours.Add(new Tour());
            }
            Tour tour = solution.Tours[move.Tour];

            Tour oldTour = solution.Tours.Find(t => t.Stops.Contains(move.City));

            oldTour.Stops.Remove(move.City);

            /*if (oldTour.Stops.Count == 0)
             * solution.Tours.Remove(oldTour);*/

            int place = solution.FindBestInsertionPlace(tour, move.City);

            tour.Stops.Insert(place, move.City);

            solution.Repair();
        }
    public static void Apply(PotvinEncoding solution, PotvinPDRearrangeMove move, IVRPProblemInstance problemInstance) {
      Tour tour = solution.Tours[move.Tour];
      int position = tour.Stops.IndexOf(move.City);

      IPickupAndDeliveryProblemInstance pdp = problemInstance as IPickupAndDeliveryProblemInstance;
      if (pdp != null) {
        int location = pdp.GetPickupDeliveryLocation(move.City);
        Tour tour2 = solution.Tours.Find(t => t.Stops.Contains(location));
        int position2 = tour2.Stops.IndexOf(location);

        tour.Stops.Remove(move.City);
        tour2.Stops.Remove(location);

        solution.InsertPair(tour, move.City, location, problemInstance, position, position2);
      } else {
        tour.Stops.Remove(move.City);
        int place = solution.FindBestInsertionPlace(tour, move.City, position);
        tour.Stops.Insert(place, move.City);
      }

      solution.Repair();
    }
    public static PotvinEncoding CreateSolution(IVRPProblemInstance instance, IRandom random, bool adhereTimeWindows) {
      PotvinEncoding result = new PotvinEncoding(instance);

      IPickupAndDeliveryProblemInstance pdp = instance as IPickupAndDeliveryProblemInstance;

      List<int> customers = new List<int>();
      for (int i = 1; i <= instance.Cities.Value; i++)
        if (pdp == null || pdp.GetDemand(i) >= 0)
          customers.Add(i);

      customers.Sort((city1, city2) => {
        double angle1 = CalculateAngleToDepot(instance, city1);
        double angle2 = CalculateAngleToDepot(instance, city2);

        return angle1.CompareTo(angle2);
      });

      Tour currentTour = new Tour();
      result.Tours.Add(currentTour);

      int j = random.Next(customers.Count);
      for (int i = 0; i < customers.Count; i++) {
        int index = (i + j) % customers.Count;

        int stopIdx = 0;
        if (currentTour.Stops.Count > 0)
          stopIdx = result.FindBestInsertionPlace(currentTour, customers[index]);
        currentTour.Stops.Insert(stopIdx, customers[index]);

        if (pdp != null) {
          stopIdx = result.FindBestInsertionPlace(currentTour, pdp.GetPickupDeliveryLocation(customers[index]));
          currentTour.Stops.Insert(stopIdx, pdp.GetPickupDeliveryLocation(customers[index]));
        }

        CVRPEvaluation evaluation = instance.EvaluateTour(currentTour, result) as CVRPEvaluation;
        if (result.Tours.Count < instance.Vehicles.Value &&
          ((adhereTimeWindows && !instance.Feasible(evaluation)) || ((!adhereTimeWindows) && evaluation.Overload > double.Epsilon))) {
          currentTour.Stops.Remove(customers[index]);
          if (pdp != null)
            currentTour.Stops.Remove(pdp.GetPickupDeliveryLocation(customers[index]));

          if (currentTour.Stops.Count == 0)
            result.Tours.Remove(currentTour);
          currentTour = new Tour();
          result.Tours.Add(currentTour);

          currentTour.Stops.Add(customers[index]);
          if (pdp != null) {
            currentTour.Stops.Add(pdp.GetPickupDeliveryLocation(customers[index]));
          }
        }
      }

      if (currentTour.Stops.Count == 0)
        result.Tours.Remove(currentTour);

      return result;
    }
예제 #10
0
        public static PotvinEncoding CreateSolution(IVRPProblemInstance instance, IRandom random, bool adhereTimeWindows)
        {
            PotvinEncoding result = new PotvinEncoding(instance);

            IPickupAndDeliveryProblemInstance pdp = instance as IPickupAndDeliveryProblemInstance;

            List <int> customers = new List <int>();

            for (int i = 1; i <= instance.Cities.Value; i++)
            {
                if (pdp == null || pdp.GetDemand(i) >= 0)
                {
                    customers.Add(i);
                }
            }

            customers.Sort((city1, city2) => {
                double angle1 = CalculateAngleToDepot(instance, city1);
                double angle2 = CalculateAngleToDepot(instance, city2);

                return(angle1.CompareTo(angle2));
            });

            Tour currentTour = new Tour();

            result.Tours.Add(currentTour);

            int j = random.Next(customers.Count);

            for (int i = 0; i < customers.Count; i++)
            {
                int index = (i + j) % customers.Count;

                int stopIdx = 0;
                if (currentTour.Stops.Count > 0)
                {
                    stopIdx = result.FindBestInsertionPlace(currentTour, customers[index]);
                }
                currentTour.Stops.Insert(stopIdx, customers[index]);

                if (pdp != null)
                {
                    stopIdx = result.FindBestInsertionPlace(currentTour, pdp.GetPickupDeliveryLocation(customers[index]));
                    currentTour.Stops.Insert(stopIdx, pdp.GetPickupDeliveryLocation(customers[index]));
                }

                CVRPEvaluation evaluation = instance.EvaluateTour(currentTour, result) as CVRPEvaluation;
                if (result.Tours.Count < instance.Vehicles.Value &&
                    ((adhereTimeWindows && !instance.Feasible(evaluation)) || ((!adhereTimeWindows) && evaluation.Overload > double.Epsilon)))
                {
                    currentTour.Stops.Remove(customers[index]);
                    if (pdp != null)
                    {
                        currentTour.Stops.Remove(pdp.GetPickupDeliveryLocation(customers[index]));
                    }

                    if (currentTour.Stops.Count == 0)
                    {
                        result.Tours.Remove(currentTour);
                    }
                    currentTour = new Tour();
                    result.Tours.Add(currentTour);

                    currentTour.Stops.Add(customers[index]);
                    if (pdp != null)
                    {
                        currentTour.Stops.Add(pdp.GetPickupDeliveryLocation(customers[index]));
                    }
                }
            }

            if (currentTour.Stops.Count == 0)
            {
                result.Tours.Remove(currentTour);
            }

            return(result);
        }
        public static void Apply(PotvinEncoding solution, PotvinPDShiftMove move, IVRPProblemInstance problemInstance)
        {
            bool newTour = false;

            if (move.Tour >= solution.Tours.Count)
            {
                solution.Tours.Add(new Tour());
                newTour = true;
            }
            Tour tour = solution.Tours[move.Tour];

            Tour oldTour = solution.Tours.Find(t => t.Stops.Contains(move.City));

            oldTour.Stops.Remove(move.City);

            if (problemInstance is IPickupAndDeliveryProblemInstance)
            {
                IPickupAndDeliveryProblemInstance pdp = problemInstance as IPickupAndDeliveryProblemInstance;

                int  location = pdp.GetPickupDeliveryLocation(move.City);
                Tour oldTour2 = solution.Tours.Find(t => t.Stops.Contains(location));
                oldTour2.Stops.Remove(location);

                solution.InsertPair(tour, move.City, location, problemInstance);
            }
            else
            {
                int place = solution.FindBestInsertionPlace(tour, move.City);
                tour.Stops.Insert(place, move.City);
            }

            if (newTour)
            {
                List <int> vehicles = new List <int>();
                for (int i = move.Tour; i < problemInstance.Vehicles.Value; i++)
                {
                    vehicles.Add(solution.GetVehicleAssignment(i));
                }

                double bestQuality = double.MaxValue;
                int    bestVehicle = -1;

                int originalVehicle = solution.GetVehicleAssignment(move.Tour);
                foreach (int vehicle in vehicles)
                {
                    solution.VehicleAssignment[move.Tour] = vehicle;

                    double quality = problemInstance.EvaluateTour(tour, solution).Quality;
                    if (quality < bestQuality)
                    {
                        bestQuality = quality;
                        bestVehicle = vehicle;
                    }
                }

                solution.VehicleAssignment[move.Tour] = originalVehicle;

                int index = -1;
                for (int i = move.Tour; i < solution.VehicleAssignment.Length; i++)
                {
                    if (solution.VehicleAssignment[i] == bestVehicle)
                    {
                        index = i;
                        break;
                    }
                }
                solution.VehicleAssignment[index]     = originalVehicle;
                solution.VehicleAssignment[move.Tour] = bestVehicle;
            }

            solution.Repair();
        }