• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C# Potvin.PotvinEncoding类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



PotvinEncoding类属于HeuristicLab.Problems.VehicleRouting.Encodings.Potvin命名空间,在下文中一共展示了PotvinEncoding类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。

示例1: CalculateSimilarity

    public static double CalculateSimilarity(PotvinEncoding left, PotvinEncoding right) {
      if (left == null || right == null)
        throw new ArgumentException("Cannot calculate similarity because one of the provided solutions or both are null.");
      if (left == right) return 1.0;

      // extract edges from first solution
      var edges1 = new List<Tuple<int, int>>();
      foreach (Tour tour in left.Tours) {
        edges1.Add(new Tuple<int, int>(0, tour.Stops[0]));
        for (int i = 0; i < tour.Stops.Count - 1; i++)
          edges1.Add(new Tuple<int, int>(tour.Stops[i], tour.Stops[i + 1]));
        edges1.Add(new Tuple<int, int>(tour.Stops[tour.Stops.Count - 1], 0));
      }

      // extract edges from second solution
      var edges2 = new List<Tuple<int, int>>();
      foreach (Tour tour in right.Tours) {
        edges2.Add(new Tuple<int, int>(0, tour.Stops[0]));
        for (int i = 0; i < tour.Stops.Count - 1; i++)
          edges2.Add(new Tuple<int, int>(tour.Stops[i], tour.Stops[i + 1]));
        edges2.Add(new Tuple<int, int>(tour.Stops[tour.Stops.Count - 1], 0));
      }

      if (edges1.Count + edges2.Count == 0)
        throw new ArgumentException("Cannot calculate diversity because no tours exist.");

      int identicalEdges = 0;
      foreach (var edge in edges1) {
        if (edges2.Any(x => x.Equals(edge)))
          identicalEdges++;
      }

      return identicalEdges * 2.0 / (edges1.Count + edges2.Count);
    }
开发者ID:t-h-e,项目名称:HeuristicLab,代码行数:34,代码来源:VRPSimilarityCalculator.cs


示例2: GenerateMoves

    protected override PotvinPDShiftMove[] GenerateMoves(PotvinEncoding individual, IVRPProblemInstance problemInstance) {
      List<PotvinPDShiftMove> result = new List<PotvinPDShiftMove>();
      IPickupAndDeliveryProblemInstance pdp = problemInstance as IPickupAndDeliveryProblemInstance;

      int max = individual.Tours.Count;
      if (individual.Tours.Count >= problemInstance.Vehicles.Value)
        max = max - 1;

      for (int i = 0; i < individual.Tours.Count; i++) {
        for (int j = 0; j < individual.Tours[i].Stops.Count; j++) {
          for (int k = 0; k <= max; k++) {
            if (k != i) {
              int city = individual.Tours[i].Stops[j];
              if (pdp == null || pdp.GetDemand(city) >= 0) {
                PotvinPDShiftMove move = new PotvinPDShiftMove(
                  city, i, k, individual);

                result.Add(move);
              }
            }
          }
        }
      }

      return result.ToArray();
    }
开发者ID:t-h-e,项目名称:HeuristicLab,代码行数:26,代码来源:PotvinPDShiftExhaustiveMoveGenerator.cs


示例3: Apply

    public static void Apply(PotvinEncoding solution, PotvinVehicleAssignmentMove move, IVRPProblemInstance problemInstance) {
      int vehicle1 = solution.VehicleAssignment[move.Tour1];
      int vehicle2 = solution.VehicleAssignment[move.Tour2];

      solution.VehicleAssignment[move.Tour1] = vehicle2;
      solution.VehicleAssignment[move.Tour2] = vehicle1;
    }
开发者ID:thunder176,项目名称:HeuristicLab,代码行数:7,代码来源:PotvinVehicleAssignmentMoveMaker.cs


示例4: 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];

        int count = route1.Stops.Count;
        int i = 0;
        while (i < count) {
          int insertedRoute, insertedPlace;

          int city = route1.Stops[i];
          route1.Stops.Remove(city);

          if (FindInsertionPlace(individual, city, selectedIndex, allowInfeasible, out insertedRoute, out insertedPlace)) {
            individual.Tours[insertedRoute].Stops.Insert(insertedPlace, city);
          } else {
            route1.Stops.Insert(i, city);
            i++;
          }

          count = route1.Stops.Count;
        }
      }
    }
开发者ID:thunder176,项目名称:HeuristicLab,代码行数:25,代码来源:PotvinOneLevelExchangeManipulator.cs


示例5: 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


示例6: PotvinCustomerRelocationMove

    public PotvinCustomerRelocationMove(int city, int oldTour, int tour, PotvinEncoding individual) {
      City = city;
      OldTour = oldTour;
      Tour = tour;

      this.Individual = individual.Clone() as PotvinEncoding;
    }
开发者ID:t-h-e,项目名称:HeuristicLab,代码行数:7,代码来源:PotvinCustomerRelocationMove.cs


示例7: PotvinPDShiftMove

    public PotvinPDShiftMove(int city, int oldTour, int tour, PotvinEncoding individual) {
      City = city;
      OldTour = oldTour;
      Tour = tour;

      this.Individual = individual.Clone() as PotvinEncoding;
    }
开发者ID:t-h-e,项目名称:HeuristicLab,代码行数:7,代码来源:PotvinPDShiftMove.cs


示例8: Apply

    public static PotvinPDShiftMove Apply(PotvinEncoding individual, IVRPProblemInstance problemInstance, IRandom rand) {
      List<int> cities = new List<int>();

      IPickupAndDeliveryProblemInstance pdp = problemInstance as IPickupAndDeliveryProblemInstance;
      for (int i = 1; i <= individual.Cities; i++) {
        if (pdp == null || pdp.GetDemand(i) >= 0)
          cities.Add(i);
      }

      if (cities.Count >= 1) {
        int city = cities[rand.Next(cities.Count)];
        Tour oldTour = individual.Tours.Find(t => t.Stops.Contains(city));
        int oldTourIndex = individual.Tours.IndexOf(oldTour);

        int max = individual.Tours.Count;
        if (individual.Tours.Count >= problemInstance.Vehicles.Value)
          max = max - 1;

        int newTourIndex = rand.Next(max);
        if (newTourIndex >= oldTourIndex)
          newTourIndex++;

        return new PotvinPDShiftMove(city, oldTourIndex, newTourIndex, individual);
      } else {
        return null;
      }
    }
开发者ID:thunder176,项目名称:HeuristicLab,代码行数:27,代码来源:PotvinPDShiftSingleMoveGenerator.cs


示例9: 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


示例10: SelectRandomTourBiasedByLength

    protected static int SelectRandomTourBiasedByLength(IRandom random, PotvinEncoding individual, IVRPProblemInstance instance) {
      int tourIndex = -1;

      double sum = 0.0;
      double[] probabilities = new double[individual.Tours.Count];
      for (int i = 0; i < individual.Tours.Count; i++) {
        probabilities[i] = 1.0 / ((double)individual.Tours[i].Stops.Count / (double)instance.Cities.Value);
        sum += probabilities[i];
      }

      for (int i = 0; i < probabilities.Length; i++)
        probabilities[i] = probabilities[i] / sum;

      double rand = random.NextDouble();
      double cumulatedProbabilities = 0.0;
      int index = 0;
      while (tourIndex == -1 && index < probabilities.Length) {
        if (cumulatedProbabilities <= rand && rand <= cumulatedProbabilities + probabilities[index])
          tourIndex = index;

        cumulatedProbabilities += probabilities[index];
        index++;
      }

      return tourIndex;
    }
开发者ID:thunder176,项目名称:HeuristicLab,代码行数:26,代码来源:PotvinManipulator.cs


示例11: PotvinTwoOptStarMove

    public PotvinTwoOptStarMove(int tour1, int x1, int tour2, int x2, PotvinEncoding individual) {
      Tour1 = tour1;
      X1 = x1;
      Tour2 = tour2;
      X2 = x2;

      this.Individual = individual.Clone() as PotvinEncoding;
    }
开发者ID:t-h-e,项目名称:HeuristicLab,代码行数:8,代码来源:PotvinTwoOptStarMove.cs


示例12: PotvinPDExchangeMove

    public PotvinPDExchangeMove(int city, int oldTour, int tour, int replaced, PotvinEncoding individual) {
      City = city;
      OldTour = oldTour;
      Tour = tour;
      Replaced = replaced;

      this.Individual = individual.Clone() as PotvinEncoding;
    }
开发者ID:t-h-e,项目名称:HeuristicLab,代码行数:8,代码来源:PotvinPDExchangeMove.cs


示例13: CreateSolution

    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;
    }
开发者ID:t-h-e,项目名称:HeuristicLab,代码行数:58,代码来源:IterativeInsertionCreator.cs


示例14: GenerateMoves

    protected override PotvinPDShiftMove[] GenerateMoves(PotvinEncoding individual, IVRPProblemInstance problemInstance) {
      List<PotvinPDShiftMove> result = new List<PotvinPDShiftMove>();

      PotvinPDShiftMove move = Apply(individual, ProblemInstance, RandomParameter.ActualValue);
      if (move != null)
        result.Add(move);

      return result.ToArray();
    }
开发者ID:thunder176,项目名称:HeuristicLab,代码行数:9,代码来源:PotvinPDShiftSingleMoveGenerator.cs


示例15: GenerateMoves

    protected override PotvinCustomerRelocationMove[] GenerateMoves(PotvinEncoding individual, IVRPProblemInstance problemInstance) {
      List<PotvinCustomerRelocationMove> result = new List<PotvinCustomerRelocationMove>();

      for (int i = 0; i < SampleSizeParameter.ActualValue.Value; i++) {
        result.Add(PotvinCustomerRelocationSingleMoveGenerator.Apply(individual, ProblemInstance, RandomParameter.ActualValue));
      }

      return result.ToArray();
    }
开发者ID:t-h-e,项目名称:HeuristicLab,代码行数:9,代码来源:PotvinCustomerRelocationMultiMoveGenerator.cs


示例16: GenerateMoves

    protected override PotvinVehicleAssignmentMove[] GenerateMoves(PotvinEncoding individual, IVRPProblemInstance problemInstance) {
      List<PotvinVehicleAssignmentMove> result = new List<PotvinVehicleAssignmentMove>();

      for (int i = 0; i < SampleSizeParameter.ActualValue.Value; i++) {
        var move = PotvinVehicleAssignmentSingleMoveGenerator.Apply(individual, ProblemInstance, RandomParameter.ActualValue);
        if (move != null)
          result.Add(move);
      }

      return result.ToArray();
    }
开发者ID:thunder176,项目名称:HeuristicLab,代码行数:11,代码来源:PotvinVehicleAssignmentMultiMoveGenerator.cs


示例17: Improve

    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;
    }
开发者ID:t-h-e,项目名称:HeuristicLab,代码行数:53,代码来源:VRPIntraRouteImprovementOperator.cs


示例18: GenerateMoves

    protected override PotvinVehicleAssignmentMove[] GenerateMoves(PotvinEncoding individual, IVRPProblemInstance problemInstance) {
      List<PotvinVehicleAssignmentMove> result = new List<PotvinVehicleAssignmentMove>();

      for (int i = 0; i < individual.Tours.Count; i++) {
        for (int j = i + 1; j < problemInstance.Vehicles.Value; j++) {
          result.Add(new PotvinVehicleAssignmentMove(i, j, individual));
        }

      }

      return result.ToArray();
    }
开发者ID:thunder176,项目名称:HeuristicLab,代码行数:12,代码来源:PotvinVehicleAssignmentExhaustiveMoveGenerator.cs


示例19: FindRoute

    protected static Tour FindRoute(PotvinEncoding solution, int city) {
      Tour found = null;

      foreach (Tour tour in solution.Tours) {
        if (tour.Stops.Contains(city)) {
          found = tour;
          break;
        }
      }

      return found;
    }
开发者ID:thunder176,项目名称:HeuristicLab,代码行数:12,代码来源:PotvinCrossover.cs


示例20: Apply

    public static PotvinVehicleAssignmentMove Apply(PotvinEncoding individual, IVRPProblemInstance problemInstance, IRandom rand) {
      if (individual.Tours.Count > 1) {
        int tour1 = rand.Next(individual.Tours.Count);
        int tour2 = rand.Next(problemInstance.Vehicles.Value);

        while (tour2 == tour1)
          tour2 = rand.Next(problemInstance.Vehicles.Value);

        return new PotvinVehicleAssignmentMove(tour1, tour2, individual);
      } else {
        return null;
      }
    }
开发者ID:t-h-e,项目名称:HeuristicLab,代码行数:13,代码来源:PotvinVehicleAssignmentSingleMoveGenerator.cs



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# HipChat.HipChatClient类代码示例发布时间:2022-05-26
下一篇:
C# RealVectorEncoding.RealVector类代码示例发布时间:2022-05-26
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap