Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/20/11 13:36:49 (14 years ago)
Author:
svonolfe
Message:

Improved performance of many VRP operators by optimizing the parameter lookup (#1561)

Location:
trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Potvin/Manipulators
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Potvin/Manipulators/PotvinLocalSearchManipulator.cs

    r5445 r6449  
    4949
    5050    private bool FindBetterInsertionPlace(
    51       PotvinEncoding individual, int tour, int city, int length,
     51      PotvinEncoding individual, 
     52      DoubleArray dueTime, DoubleArray readyTime, DoubleArray serviceTime, DoubleArray demand,
     53      DoubleValue capacity, DistanceMatrix distMatrix,
     54      int tour, int city, int length,
    5255      out int insertionTour, out int insertionPlace) {
    5356      bool insertionFound = false;
     
    5659
    5760      List<int> toBeDeleted = individual.Tours[tour].Cities.GetRange(city, length);
    58       double distance = GetLength(individual.Tours[tour]);
     61      double distance = individual.Tours[tour].GetLength(distMatrix);
    5962      individual.Tours[tour].Cities.RemoveRange(city, length);
    60       double removalBenefit = distance - GetLength(individual.Tours[tour]);
     63      double removalBenefit = distance - individual.Tours[tour].GetLength(distMatrix);
    6164
    6265      int currentTour = 0;
     
    6467        int currentCity = 0;
    6568        while (currentCity <= individual.Tours[currentTour].Cities.Count && !insertionFound) {
    66           distance = GetLength(individual.Tours[currentTour]);
     69          distance = individual.Tours[currentTour].GetLength(distMatrix);
    6770          individual.Tours[currentTour].Cities.InsertRange(currentCity, toBeDeleted);
    68           if (Feasible(individual.Tours[currentTour])) {
     71          if (individual.Tours[currentTour].Feasible(dueTime, serviceTime, readyTime, demand, capacity, distMatrix)) {
    6972            double lengthIncrease =
    70               GetLength(individual.Tours[currentTour]) - distance;
     73              individual.Tours[currentTour].GetLength(distMatrix) - distance;
    7174            if (removalBenefit > lengthIncrease) {
    7275              insertionTour = currentTour;
     
    8386      }
    8487
    85       individual.Tours[tour].Cities.InsertRange(city, toBeDeleted);
     88      individual.Tours[tour].Cities.InsertRange(city, toBeDeleted); 
    8689
    8790      return insertionFound;
     
    8992
    9093    protected override void Manipulate(IRandom random, PotvinEncoding individual) {
     94      BoolValue useDistanceMatrix = UseDistanceMatrixParameter.ActualValue;
     95      DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
     96      DistanceMatrix distMatrix = VRPUtilities.GetDistanceMatrix(coordinates, DistanceMatrixParameter, useDistanceMatrix);
     97      DoubleArray dueTime = DueTimeParameter.ActualValue;
     98      DoubleArray readyTime = ReadyTimeParameter.ActualValue;
     99      DoubleArray serviceTime = ServiceTimeParameter.ActualValue;
     100      DoubleArray demand = DemandParameter.ActualValue;
     101      DoubleValue capacity = CapacityParameter.ActualValue;
     102     
    91103      //only apply to feasible individuals
    92       if (Feasible(individual)) {
     104      bool feasible = true;
     105
     106      foreach (Tour tour in individual.Tours) {
     107        if (!tour.Feasible(dueTime, serviceTime, readyTime, demand, capacity, distMatrix)) {
     108          feasible = false;
     109          break;
     110        }
     111      }
     112
     113      if (feasible) {
    93114        bool insertionFound;
    94115        int iterations = 0;
     
    103124              while (city <= individual.Tours[tour].Cities.Count - length && !insertionFound) {
    104125                int insertionTour, insertionPlace;
    105                 if (FindBetterInsertionPlace(individual, tour, city, length,
     126                if (FindBetterInsertionPlace(individual, dueTime, readyTime, serviceTime, demand, capacity, distMatrix,
     127                  tour, city, length,
    106128                 out insertionTour, out insertionPlace)) {
    107129                  insertionFound = true;
  • trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Potvin/Manipulators/PotvinManipulator.cs

    r5445 r6449  
    2525using HeuristicLab.Parameters;
    2626using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     27using HeuristicLab.Data;
    2728
    2829namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
     
    4546    protected abstract void Manipulate(IRandom random, PotvinEncoding individual);
    4647
    47     protected int SelectRandomTourBiasedByLength(IRandom random, PotvinEncoding individual) {
     48    protected static int SelectRandomTourBiasedByLength(IRandom random, PotvinEncoding individual) {
    4849      int tourIndex = -1;
    4950
     
    5152      double[] probabilities = new double[individual.Tours.Count];
    5253      for (int i = 0; i < individual.Tours.Count; i++) {
    53         probabilities[i] = 1.0 / ((double)individual.Tours[i].Cities.Count / (double)Cities);
     54        probabilities[i] = 1.0 / ((double)individual.Tours[i].Cities.Count / (double)individual.Cities);
    5455        sum += probabilities[i];
    5556      }
     
    7273    }
    7374
    74     protected bool FindInsertionPlace(PotvinEncoding individual, int city, int routeToAvoid, out int route, out int place) {
     75    protected static bool FindInsertionPlace(PotvinEncoding individual, int city, int routeToAvoid,
     76      DoubleArray dueTime, DoubleArray serviceTime, DoubleArray readyTime, DoubleArray demand,
     77      DoubleValue capacity, DistanceMatrix distMatrix,
     78      out int route, out int place) {
    7579      return individual.FindInsertionPlace(
    76         DueTimeParameter.ActualValue, ServiceTimeParameter.ActualValue, ReadyTimeParameter.ActualValue,
    77         DemandParameter.ActualValue, CapacityParameter.ActualValue, CoordinatesParameter.ActualValue,
    78         DistanceMatrixParameter, UseDistanceMatrixParameter.ActualValue,
     80        dueTime, serviceTime, readyTime,
     81        demand, capacity, distMatrix,
    7982        city, routeToAvoid, out route, out place);
    8083    }
     84     
    8185
    8286    public override IOperation Apply() {
  • trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Potvin/Manipulators/PotvinOneLevelExchangeManipulator.cs

    r5445 r6449  
    2424using HeuristicLab.Core;
    2525using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     26using HeuristicLab.Data;
    2627
    2728namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
     
    3940    public PotvinOneLevelExchangeMainpulator() : base() { }
    4041
    41     protected override void Manipulate(IRandom random, PotvinEncoding individual) {
     42    public static void Apply(IRandom random, PotvinEncoding individual,
     43     DoubleArray dueTime, DoubleArray readyTime, DoubleArray serviceTime, DoubleArray demand,
     44      DoubleValue capacity, DistanceMatrix distMatrix) {
    4245      int selectedIndex = SelectRandomTourBiasedByLength(random, individual);
    4346      Tour route1 =
     
    4851        int insertedRoute, insertedPlace;
    4952
    50         if (FindInsertionPlace(individual, route1.Cities[i], selectedIndex, out insertedRoute, out insertedPlace)) {
     53        if (FindInsertionPlace(individual, route1.Cities[i], selectedIndex,
     54          dueTime, serviceTime, readyTime, demand, capacity,
     55          distMatrix,
     56          out insertedRoute, out insertedPlace)) {
    5157          individual.Tours[insertedRoute].Cities.Insert(insertedPlace, route1.Cities[i]);
    5258          replaced.Add(route1.Cities[i]);
     
    6571        individual.Tours.Remove(route1);
    6672    }
     73
     74    protected override void Manipulate(IRandom random, PotvinEncoding individual) {
     75      BoolValue useDistanceMatrix = UseDistanceMatrixParameter.ActualValue;
     76      DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
     77      DistanceMatrix distMatrix = VRPUtilities.GetDistanceMatrix(coordinates, DistanceMatrixParameter, useDistanceMatrix);
     78      DoubleArray dueTime = DueTimeParameter.ActualValue;
     79      DoubleArray readyTime = ReadyTimeParameter.ActualValue;
     80      DoubleArray serviceTime = ServiceTimeParameter.ActualValue;
     81      DoubleArray demand = DemandParameter.ActualValue;
     82      DoubleValue capacity = CapacityParameter.ActualValue;
     83
     84      Apply(random, individual, dueTime, readyTime, serviceTime, demand, capacity, distMatrix);
     85    }
    6786  }
    6887}
  • trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Potvin/Manipulators/PotvinTwoLevelExchangeManipulator.cs

    r5445 r6449  
    2323using HeuristicLab.Core;
    2424using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     25using HeuristicLab.Data;
    2526
    2627namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
     
    3637    public PotvinTwoLevelExchangeManipulator() : base() { }
    3738
    38     protected override void Manipulate(IRandom random, PotvinEncoding individual) {
     39    public static void Apply(IRandom random, PotvinEncoding individual,
     40      DoubleArray dueTime, DoubleArray readyTime, DoubleArray serviceTime, DoubleArray demand,
     41      DoubleValue capacity, DistanceMatrix distMatrix) {
    3942      int selectedIndex = SelectRandomTourBiasedByLength(random, individual);
    40       Tour route1 = individual.Tours[selectedIndex]; 
     43      Tour route1 = individual.Tours[selectedIndex];
    4144
    4245      bool performed = false;
     
    5356              //customer1 can be feasibly inserted at the location of customer2
    5457              tour.Cities[customer2Position] = customer1;
    55               if (Feasible(tour)) {
     58              if (tour.Feasible(dueTime, serviceTime, readyTime, demand, capacity, distMatrix)) {
    5659                int routeIdx, place;
    5760                if (FindInsertionPlace(individual,
    58                   customer2, selectedIndex, out routeIdx, out place)) {
    59                     individual.Tours[routeIdx].Cities.Insert(place, customer2);
    60                     route1.Cities.RemoveAt(customer1Position);
     61                  customer2, selectedIndex,
     62                  dueTime, serviceTime, readyTime, demand, capacity,
     63                  distMatrix,
     64                  out routeIdx, out place)) {
     65                  individual.Tours[routeIdx].Cities.Insert(place, customer2);
     66                  route1.Cities.RemoveAt(customer1Position);
    6167
    62                     if (route1.Cities.Count == 0)
     68                  if (route1.Cities.Count == 0)
    6369                    individual.Tours.Remove(route1);
    6470
     
    8389      }
    8490    }
     91
     92    protected override void Manipulate(IRandom random, PotvinEncoding individual) {
     93      BoolValue useDistanceMatrix = UseDistanceMatrixParameter.ActualValue;
     94      DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
     95      DistanceMatrix distMatrix = VRPUtilities.GetDistanceMatrix(coordinates, DistanceMatrixParameter, useDistanceMatrix);
     96      DoubleArray dueTime = DueTimeParameter.ActualValue;
     97      DoubleArray readyTime = ReadyTimeParameter.ActualValue;
     98      DoubleArray serviceTime = ServiceTimeParameter.ActualValue;
     99      DoubleArray demand = DemandParameter.ActualValue;
     100      DoubleValue capacity = CapacityParameter.ActualValue;
     101
     102      Apply(random, individual, dueTime, readyTime, serviceTime, demand, capacity, distMatrix);
     103    }
    85104  }
    86105}
Note: See TracChangeset for help on using the changeset viewer.