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


C# Potvin.PotvinEncoding类代码示例

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


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


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