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


C# IVRPProblemInstance.TourFeasible方法代码示例

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


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

示例1: ApplyManipulation

    public static void ApplyManipulation(IRandom random, PotvinEncoding individual, IVRPProblemInstance instance, bool allowInfeasible) {
      int selectedIndex = SelectRandomTourBiasedByLength(random, individual, instance);
      if (selectedIndex >= 0) {
        Tour route1 = individual.Tours[selectedIndex];

        bool performed = false;
        int customer1Position = 0;
        while (customer1Position < route1.Stops.Count) {
          performed = false;

          foreach (Tour tour in individual.Tours) {
            if (tour != route1) {
              for (int customer2Position = 0; customer2Position < tour.Stops.Count; customer2Position++) {
                int customer1 = route1.Stops[customer1Position];
                int customer2 = tour.Stops[customer2Position];

                //customer1 can be feasibly inserted at the location of customer2
                tour.Stops[customer2Position] = customer1;
                route1.Stops.RemoveAt(customer1Position);

                if (instance.TourFeasible(tour, individual)) {
                  int routeIdx, place;
                  if (FindInsertionPlace(individual,
                    customer2, selectedIndex, allowInfeasible, out routeIdx, out place)) {
                    individual.Tours[routeIdx].Stops.Insert(place, customer2);

                    //two-level exchange has been performed
                    performed = true;
                    break;
                  } else {
                    tour.Stops[customer2Position] = customer2;
                    route1.Stops.Insert(customer1Position, customer1);
                  }
                } else {
                  tour.Stops[customer2Position] = customer2;
                  route1.Stops.Insert(customer1Position, customer1);
                }
              }
            }

            if (performed)
              break;
          }

          if (!performed)
            customer1Position++;
        }
      }
    }
开发者ID:thunder176,项目名称:HeuristicLab,代码行数:49,代码来源:PotvinTwoLevelExchangeManipulator.cs

示例2: FindBetterInsertionPlace

    private static bool FindBetterInsertionPlace(
      PotvinEncoding individual, IVRPProblemInstance instance, int tour, int city, int length,
      out int insertionTour, out int insertionPlace) {
      bool insertionFound = false;
      insertionTour = -1;
      insertionPlace = 1;

      List<int> toBeDeleted = individual.Tours[tour].Stops.GetRange(city, length);
      double distance = individual.GetTourLength(individual.Tours[tour]);
      individual.Tours[tour].Stops.RemoveRange(city, length);
      double removalBenefit = distance - individual.GetTourLength(individual.Tours[tour]);

      int currentTour = 0;
      while (currentTour < individual.Tours.Count && !insertionFound) {
        int currentCity = 0;
        while (currentCity <= individual.Tours[currentTour].Stops.Count && !insertionFound) {
          distance = individual.GetTourLength(individual.Tours[currentTour]);
          individual.Tours[currentTour].Stops.InsertRange(currentCity, toBeDeleted);
          if (instance.TourFeasible(individual.Tours[currentTour], individual)) {
            double lengthIncrease =
              individual.GetTourLength(individual.Tours[currentTour]) - distance;
            if (removalBenefit > lengthIncrease) {
              insertionTour = currentTour;
              insertionPlace = currentCity;

              insertionFound = true;
            }
          }
          individual.Tours[currentTour].Stops.RemoveRange(currentCity, length);

          currentCity++;
        }
        currentTour++;
      }

      individual.Tours[tour].Stops.InsertRange(city, toBeDeleted);

      return insertionFound;
    }
开发者ID:thunder176,项目名称:HeuristicLab,代码行数:39,代码来源:PotvinLocalSearchManipulator.cs

示例3: Repair

    protected static bool Repair(IRandom random, PotvinEncoding solution, Tour newTour, IVRPProblemInstance instance, bool allowInfeasible) {
      bool success = true;

      //remove duplicates from new tour      
      for (int i = 0; i < newTour.Stops.Count; i++) {
        for (int j = 0; j < newTour.Stops.Count; j++) {
          if (newTour.Stops[i] == newTour.Stops[j] && i != j) {
            if (random.NextDouble() < 0.5)
              newTour.Stops[i] = 0;
            else
              newTour.Stops[j] = 0;
          }
        }
      }
      while (newTour.Stops.Contains(0))
        newTour.Stops.Remove(0);

      //remove duplicates from old tours
      for (int i = 0; i < newTour.Stops.Count; i++) {
        foreach (Tour tour in solution.Tours) {
          if (tour != newTour && tour.Stops.Contains(newTour.Stops[i])) {
            tour.Stops.Remove(newTour.Stops[i]);
          }
        }
      }

      //remove empty tours
      List<Tour> toBeDeleted = new List<Tour>();
      foreach (Tour tour in solution.Tours) {
        if (tour.Stops.Count == 0)
          toBeDeleted.Add(tour);
      }
      foreach (Tour tour in toBeDeleted) {
        solution.Tours.Remove(tour);
      }

      if (newTour.Stops.Count > 0 && !allowInfeasible && !instance.TourFeasible(newTour, solution))
        return false;

      //route unrouted vehicles
      success = RouteUnrouted(solution, allowInfeasible);

      return success;
    }
开发者ID:thunder176,项目名称:HeuristicLab,代码行数:44,代码来源:PotvinCrossover.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


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