Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/14/14 10:04:43 (10 years ago)
Author:
pfleck
Message:

#2152: merged r10435, r10460, r10475, r10651,r10652 into stable

Location:
stable
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Problems.VehicleRouting

  • stable/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Manipulators/PotvinPairwiseTwoLevelExchangeManipulator.cs

    r9456 r10744  
    2626using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
    2727using HeuristicLab.Problems.VehicleRouting.Variants;
     28using HeuristicLab.Problems.VehicleRouting.Interfaces;
    2829
    2930namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
     
    4445    }
    4546
    46     private PotvinEncoding ReplacePair(PotvinEncoding individual, int replaced, int replacing, bool allowInfeasible) {
     47    private static PotvinEncoding ReplacePair(PotvinEncoding individual, IVRPProblemInstance instance, int replaced, int replacing, bool allowInfeasible) {
    4748      individual = individual.Clone() as PotvinEncoding;
    48       IPickupAndDeliveryProblemInstance pdp = ProblemInstance as IPickupAndDeliveryProblemInstance;
     49      IPickupAndDeliveryProblemInstance pdp = instance as IPickupAndDeliveryProblemInstance;
    4950
    5051      int replacedDest = pdp.GetPickupDeliveryLocation(replaced);
     
    7677
    7778      replacedSourceTour.Stops[replacedSourceTour.Stops.IndexOf(replacedSource)] = replacingSource;
    78       if (!allowInfeasible && !ProblemInstance.TourFeasible(replacedSourceTour, individual))
     79      if (!allowInfeasible && !instance.TourFeasible(replacedSourceTour, individual))
    7980        return null;
    8081
    8182      replacedTargetTour.Stops[replacedTargetTour.Stops.IndexOf(replacedTarget)] = replacingTarget;
    82       if (!allowInfeasible && !ProblemInstance.TourFeasible(replacedTargetTour, individual))
     83      if (!allowInfeasible && !instance.TourFeasible(replacedTargetTour, individual))
    8384        return null;
    8485
     
    9394        if (tourIdx != routeToAvoid) {
    9495          Tour tour = individual.Tours[tourIdx];
    95           VRPEvaluation eval = ProblemInstance.EvaluateTour(tour, individual);
    96           individual.InsertPair(tour, replacedSource, replacedTarget, ProblemInstance);
    97           VRPEvaluation evalNew = ProblemInstance.EvaluateTour(tour, individual);
     96          VRPEvaluation eval = instance.EvaluateTour(tour, individual);
     97          individual.InsertPair(tour, replacedSource, replacedTarget, instance);
     98          VRPEvaluation evalNew = instance.EvaluateTour(tour, individual);
    9899
    99100          double delta = evalNew.Quality - eval.Quality;
    100101
    101102          if (delta < bestQuality &&
    102               (ProblemInstance.Feasible(evalNew) || allowInfeasible)) {
     103              (instance.Feasible(evalNew) || allowInfeasible)) {
    103104            bestQuality = delta;
    104105            bestTour = tourIdx;
     
    127128    }
    128129
     130    public static PotvinEncoding ApplyManipulation(IRandom random, PotvinEncoding individual, IPickupAndDeliveryProblemInstance pdp, bool allowInfeasible) {
     131      PotvinEncoding result = null;
     132     
     133      int selectedIndex = SelectRandomTourBiasedByLength(random, individual, pdp);
     134      if (selectedIndex >= 0) {
     135        bool performed = false;
     136        Tour route1 = individual.Tours[selectedIndex];
     137
     138        if (route1.Stops.Count > 0) {
     139          //randomize customer selection
     140          Permutation perm = new Permutation(PermutationTypes.Absolute, route1.Stops.Count, random);
     141          int customer1Position = 0;
     142
     143          while (customer1Position < route1.Stops.Count) {
     144            performed = false;
     145
     146            int customer1 = route1.Stops[perm[customer1Position]];
     147            int customer2 = -1;
     148
     149            for (int i = 0; i < individual.Tours.Count; i++) {
     150              if (i != selectedIndex) {
     151                Tour tour = individual.Tours[i];
     152                for (int customer2Position = 0; customer2Position < tour.Stops.Count; customer2Position++) {
     153                  customer2 = tour.Stops[customer2Position];
     154
     155                  if (pdp.GetPickupDeliveryLocation(customer1) != customer2) {
     156                    result = ReplacePair(individual, pdp, customer2, customer1, allowInfeasible);
     157                    if (result != null) {
     158                      individual = result;
     159
     160                      route1 = individual.Tours[selectedIndex];
     161                      performed = true;
     162                      break;
     163                    }
     164                  }
     165                }
     166              }
     167
     168              if (performed) {
     169                break;
     170              }
     171            }
     172
     173            if (!performed)
     174              customer1Position++;
     175            else
     176              break;
     177          }
     178        }
     179      }
     180
     181      return result;
     182    }
     183
    129184    protected override void Manipulate(IRandom random, PotvinEncoding individual) {
    130185      bool allowInfeasible = AllowInfeasibleSolutions.Value.Value;
     
    132187
    133188      if (pdp != null) {
    134         int selectedIndex = SelectRandomTourBiasedByLength(random, individual);
    135         if (selectedIndex >= 0) {
    136           bool performed = false;
    137           Tour route1 = individual.Tours[selectedIndex];
    138 
    139           if (route1.Stops.Count > 0) {
    140             //randomize customer selection
    141             Permutation perm = new Permutation(PermutationTypes.Absolute, route1.Stops.Count, random);
    142             int customer1Position = 0;
    143 
    144             while (customer1Position < route1.Stops.Count) {
    145               performed = false;
    146 
    147               int customer1 = route1.Stops[perm[customer1Position]];
    148               int customer2 = -1;
    149 
    150               for (int i = 0; i < individual.Tours.Count; i++) {
    151                 if (i != selectedIndex) {
    152                   Tour tour = individual.Tours[i];
    153                   for (int customer2Position = 0; customer2Position < tour.Stops.Count; customer2Position++) {
    154                     customer2 = tour.Stops[customer2Position];
    155 
    156                     if (pdp.GetPickupDeliveryLocation(customer1) != customer2) {
    157                       PotvinEncoding result = ReplacePair(individual, customer2, customer1, allowInfeasible);
    158                       if (result != null) {
    159                         VRPToursParameter.ActualValue = result;
    160                         individual = result;
    161 
    162                         route1 = individual.Tours[selectedIndex];
    163                         performed = true;
    164                         break;
    165                       }
    166                     }
    167                   }
    168                 }
    169 
    170                 if (performed) {
    171                   break;
    172                 }
    173               }
    174 
    175               if (!performed)
    176                 customer1Position++;
    177               else
    178                 break;
    179             }
    180           }
    181         }
     189        PotvinEncoding result = ApplyManipulation(random, individual, pdp, allowInfeasible);
     190        if (result != null) {
     191          VRPToursParameter.ActualValue = result;
     192        }       
    182193      }
    183194    }
Note: See TracChangeset for help on using the changeset viewer.