当前位置: 首页>>代码示例>>C#>>正文


C# PotvinEncoding.InsertPair方法代码示例

本文整理汇总了C#中HeuristicLab.Problems.VehicleRouting.Encodings.Potvin.PotvinEncoding.InsertPair方法的典型用法代码示例。如果您正苦于以下问题:C# PotvinEncoding.InsertPair方法的具体用法?C# PotvinEncoding.InsertPair怎么用?C# PotvinEncoding.InsertPair使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在HeuristicLab.Problems.VehicleRouting.Encodings.Potvin.PotvinEncoding的用法示例。


在下文中一共展示了PotvinEncoding.InsertPair方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Apply

    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();
    }
开发者ID:thunder176,项目名称:HeuristicLab,代码行数:35,代码来源:PotvinPDExchangeMoveMaker.cs

示例2: Apply

    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();
    }
开发者ID:thunder176,项目名称:HeuristicLab,代码行数:60,代码来源:PotvinPDShiftMoveMaker.cs

示例3: Apply

    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();
    }
开发者ID:thunder176,项目名称:HeuristicLab,代码行数:22,代码来源:PotvinPDRearrangeMoveMaker.cs

示例4: ReplacePair

    private static PotvinEncoding ReplacePair(PotvinEncoding individual, IVRPProblemInstance instance, int replaced, int replacing, bool allowInfeasible) {
      individual = individual.Clone() as PotvinEncoding;
      IPickupAndDeliveryProblemInstance pdp = instance as IPickupAndDeliveryProblemInstance;

      int replacedDest = pdp.GetPickupDeliveryLocation(replaced);
      int replacedSource, replacedTarget;
      if (pdp.GetDemand(replaced) >= 0) {
        replacedSource = replaced;
        replacedTarget = replacedDest;
      } else {
        replacedSource = replacedDest;
        replacedTarget = replaced;
      }
      Tour replacedSourceTour = individual.Tours.Find(t => t.Stops.Contains(replacedSource));
      Tour replacedTargetTour = individual.Tours.Find(t => t.Stops.Contains(replacedTarget));

      int replacingDest = pdp.GetPickupDeliveryLocation(replacing);
      int replacingSource, replacingTarget;
      if (pdp.GetDemand(replacing) >= 0) {
        replacingSource = replacing;
        replacingTarget = replacingDest;
      } else {
        replacingSource = replacingDest;
        replacingTarget = replacing;
      }
      Tour replacingSourceTour = individual.Tours.Find(t => t.Stops.Contains(replacingSource));
      Tour replacingTargetTour = individual.Tours.Find(t => t.Stops.Contains(replacingTarget));

      replacingSourceTour.Stops.Remove(replacingSource);
      replacingTargetTour.Stops.Remove(replacingTarget);

      replacedSourceTour.Stops[replacedSourceTour.Stops.IndexOf(replacedSource)] = replacingSource;
      if (!allowInfeasible && !instance.TourFeasible(replacedSourceTour, individual))
        return null;

      replacedTargetTour.Stops[replacedTargetTour.Stops.IndexOf(replacedTarget)] = replacingTarget;
      if (!allowInfeasible && !instance.TourFeasible(replacedTargetTour, individual))
        return null;

      double bestQuality = double.MaxValue;
      int bestTour = -1;
      int bestPositionSource = -1;
      int bestPositionTarget = -1;

      int routeToAvoid = individual.Tours.IndexOf(replacingSourceTour);

      for (int tourIdx = 0; tourIdx < individual.Tours.Count; tourIdx++) {
        if (tourIdx != routeToAvoid) {
          Tour tour = individual.Tours[tourIdx];
          VRPEvaluation eval = instance.EvaluateTour(tour, individual);
          individual.InsertPair(tour, replacedSource, replacedTarget, instance);
          VRPEvaluation evalNew = instance.EvaluateTour(tour, individual);

          double delta = evalNew.Quality - eval.Quality;

          if (delta < bestQuality &&
              (instance.Feasible(evalNew) || allowInfeasible)) {
            bestQuality = delta;
            bestTour = tourIdx;
            bestPositionSource = tour.Stops.IndexOf(replacedSource);
            bestPositionTarget = tour.Stops.IndexOf(replacedTarget);
          }

          tour.Stops.Remove(replacedSource);
          tour.Stops.Remove(replacedTarget);
        }
      }

      if (bestTour != -1) {
        if (bestPositionTarget < bestPositionSource) {
          individual.Tours[bestTour].Stops.Insert(bestPositionTarget, replacedTarget);
          individual.Tours[bestTour].Stops.Insert(bestPositionSource, replacedSource);
        } else {
          individual.Tours[bestTour].Stops.Insert(bestPositionSource, replacedSource);
          individual.Tours[bestTour].Stops.Insert(bestPositionTarget, replacedTarget);
        }

        return individual;
      } else {
        return null;
      }
    }
开发者ID:t-h-e,项目名称:HeuristicLab,代码行数:82,代码来源:PotvinPairwiseTwoLevelExchangeManipulator.cs

示例5: PairwiseMove

    public static bool PairwiseMove(PotvinEncoding individual, IVRPProblemInstance instance, int city, bool allowInfeasible) {
      bool success;

      IPickupAndDeliveryProblemInstance pdp = instance as IPickupAndDeliveryProblemInstance;

      if (pdp != null) {
        Tour route1 = individual.Tours.Find(t => t.Stops.Contains(city));
        int i = route1.Stops.IndexOf(city);

        int dest = pdp.GetPickupDeliveryLocation(city);
        Tour destRoute = individual.Tours.Find(t => t.Stops.Contains(dest));
        int j = destRoute.Stops.IndexOf(dest);

        route1.Stops.Remove(city);
        destRoute.Stops.Remove(dest);

        int routeToAvoid = -1;
        if (route1 == destRoute)
          routeToAvoid = individual.Tours.IndexOf(route1);

        int source, target;
        if (instance.GetDemand(city) >= 0) {
          source = city;
          target = dest;
        } else {
          source = dest;
          target = city;
        }

        double bestQuality = double.MaxValue;
        int bestTour = -1;
        int bestPositionSource = -1;
        int bestPositionTarget = -1;

        for (int tourIdx = 0; tourIdx < individual.Tours.Count; tourIdx++) {
          if (tourIdx != routeToAvoid) {
            Tour tour = individual.Tours[tourIdx];
            VRPEvaluation eval = instance.EvaluateTour(tour, individual);
            individual.InsertPair(tour, source, target, instance);
            VRPEvaluation evalNew = instance.EvaluateTour(tour, individual);

            double delta = evalNew.Quality - eval.Quality;

            if (delta < bestQuality &&
               (instance.Feasible(evalNew) || allowInfeasible)) {
              bestQuality = delta;
              bestTour = tourIdx;
              bestPositionSource = tour.Stops.IndexOf(source);
              bestPositionTarget = tour.Stops.IndexOf(target);
            }

            tour.Stops.Remove(source);
            tour.Stops.Remove(target);
          }
        }

        if (bestTour >= 0) {
          if (bestPositionTarget < bestPositionSource) {
            individual.Tours[bestTour].Stops.Insert(bestPositionTarget, target);
            individual.Tours[bestTour].Stops.Insert(bestPositionSource, source);
          } else {
            individual.Tours[bestTour].Stops.Insert(bestPositionSource, source);
            individual.Tours[bestTour].Stops.Insert(bestPositionTarget, target);
          }

          success = true;
        } else {
          if (j < i) {
            destRoute.Stops.Insert(j, dest);
            route1.Stops.Insert(i, city);
          } else {
            route1.Stops.Insert(i, city);
            destRoute.Stops.Insert(j, dest);
          }

          success = false;
        }
      } else {
        success = false;
      }

      return success;
    }
开发者ID:t-h-e,项目名称:HeuristicLab,代码行数:83,代码来源:PotvinPairwiseOneLevelExchangeManipulator.cs


注:本文中的HeuristicLab.Problems.VehicleRouting.Encodings.Potvin.PotvinEncoding.InsertPair方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。