Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/29/11 15:51:56 (13 years ago)
Author:
svonolfe
Message:

Added support for multi depot CVRP instances (#1177)

Location:
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Crossovers/PotvinInsertionBasedCrossover.cs

    r6838 r6851  
    2828using HeuristicLab.Parameters;
    2929using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
     30using HeuristicLab.Problems.VehicleRouting.Interfaces;
    3031
    3132namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
     
    7475    }
    7576
    76     private double CalculateCentroidDistance(Tour t1, Tour t2, DoubleMatrix coordinates) {
     77    private double CalculateCentroidDistance(Tour t1, Tour t2, IVRPProblemInstance instance) {
    7778      double xSum = 0;
    7879      double ySum = 0;
     
    8081
    8182      for (int i = 0; i < t1.Stops.Count; i++) {
    82         xSum += coordinates[t1.Stops[i], 0];
    83         ySum += coordinates[t1.Stops[i], 1];
     83        xSum += instance.GetCoordinates(t1.Stops[i])[0];
     84        ySum += instance.GetCoordinates(t1.Stops[i])[1];
    8485      }
    8586      c1X = xSum / t1.Stops.Count;
     
    8788
    8889      for (int i = 0; i < t2.Stops.Count; i++) {
    89         xSum += coordinates[t2.Stops[i], 0];
    90         ySum += coordinates[t2.Stops[i], 1];
     90        xSum += instance.GetCoordinates(t2.Stops[i])[0];
     91        ySum += instance.GetCoordinates(t2.Stops[i])[1];
    9192      }
    9293      c2X = xSum / t1.Stops.Count;
     
    9899    }
    99100
    100     private double CalculateMeanCentroidDistance(Tour t1, IList<Tour> tours, DoubleMatrix coordinates) {
     101    private double CalculateMeanCentroidDistance(Tour t1, IList<Tour> tours, IVRPProblemInstance instance) {
    101102      double sum = 0;
    102103
    103104      for (int i = 0; i < tours.Count; i++) {
    104         sum += CalculateCentroidDistance(t1, tours[i], coordinates);
     105        sum += CalculateCentroidDistance(t1, tours[i], instance);
    105106      }
    106107
     
    108109    }
    109110
    110     private int SelectCityBiasedByNeighborDistance(IRandom random, Tour tour) {
     111    private int SelectCityBiasedByNeighborDistance(IRandom random, Tour tour, IVRPEncoding solution) {
    111112      int cityIndex = -1;
    112113
     
    120121          next = tour.Stops[i + 1];
    121122        double distance = ProblemInstance.GetDistance(
    122           tour.Stops[i], next);
     123          tour.Stops[i], next, solution);
    123124
    124125        int prev;
     
    128129          prev = tour.Stops[i - 1];
    129130        distance += ProblemInstance.GetDistance(
    130           tour.Stops[i], prev);
     131          tour.Stops[i], prev, solution);
    131132
    132133        probabilities[i] = distance;
     
    168169      for (int i = 0; i <= tour.Stops.Count; i++) {
    169170        bool feasible;
    170         double detour = ProblemInstance.GetInsertionCosts(eval, city, 0, i, out feasible);
     171        double detour = ProblemInstance.GetInsertionCosts(eval, individual, city, 0, i, out feasible);
    171172        if (feasible || allowInfeasible) {
    172173          if (place < 0 || detour < minDetour) {
     
    195196
    196197    protected override PotvinEncoding Crossover(IRandom random, PotvinEncoding parent1, PotvinEncoding parent2) {
    197       PotvinEncoding child = new PotvinEncoding(ProblemInstance);
     198      PotvinEncoding child = parent1.Clone() as PotvinEncoding;
     199      child.Tours.Clear();
    198200
    199201      bool allowInfeasible = AllowInfeasibleSolutions.Value.Value;
     
    213215        List<int> R2 = new List<int>();
    214216
    215         double r = CalculateMeanCentroidDistance(r1, parent2.Tours, ProblemInstance.Coordinates);
     217        double r = CalculateMeanCentroidDistance(r1, parent2.Tours, ProblemInstance);
    216218        foreach (Tour tour in parent2.Tours) {
    217           if (CalculateCentroidDistance(r1, tour, ProblemInstance.Coordinates) <= r) {
     219          if (CalculateCentroidDistance(r1, tour, ProblemInstance) <= r) {
    218220            R2.AddRange(tour.Stops);
    219221          }
     
    227229        int removed = random.Next(1, r1.Stops.Count + 1);
    228230        for (int i = 0; i < removed; i++) {
    229           childTour.Stops.RemoveAt(SelectCityBiasedByNeighborDistance(random, childTour));
     231          childTour.Stops.RemoveAt(SelectCityBiasedByNeighborDistance(random, childTour, child));
    230232        }
    231233
    232234        //REPAIR - add cities from R2
    233         int maxCount = random.Next(1, Math.Min(5, R2.Count));
     235        int maxCount = 1;
     236        if(R2.Count > 1)
     237          maxCount = random.Next(1, Math.Min(5, R2.Count));
    234238        int count = 0;
    235239
  • branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Crossovers/PotvinRouteBasedCrossover.cs

    r6607 r6851  
    2626using HeuristicLab.Data;
    2727using HeuristicLab.Common;
     28using HeuristicLab.Problems.VehicleRouting.Encodings.ExtendedPotvin;
    2829
    2930namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
     
    5758
    5859      child.Tours.Remove(replaced);
    59       child.Tours.Add(replacing);
     60      child.Tours.Insert(tourParent2, replacing);
     61
     62      if (parent1 is ExtendedPotvinEncoding && child is ExtendedPotvinEncoding) {
     63        Permutation vehicleAssignment = (child as ExtendedPotvinEncoding).VehicleAssignment;
     64
     65        int vehicle = vehicleAssignment[tourParent2];
     66        int vehicle2 = (parent1 as ExtendedPotvinEncoding).VehicleAssignment[tourParent1];
     67        vehicleAssignment[tourParent2] = vehicle2;
     68
     69        for (int i = 0; i < vehicleAssignment.Length; i++) {
     70          if (vehicleAssignment[i] == vehicle2 && i != tourParent2) {
     71            vehicleAssignment[i] = vehicle;
     72            break;
     73          }
     74        }       
     75      }
    6076
    6177      foreach (int city in replaced.Stops)
  • branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Crossovers/PotvinSequenceBasedCrossover.cs

    r6607 r6851  
    6767        newTour.Stops.Add(tour2.Stops[i]);
    6868
     69      int tour1Index = child.Tours.IndexOf(tour1);
    6970      child.Tours.Remove(tour1);
    70       child.Tours.Add(newTour);
     71      child.Tours.Insert(tour1Index, newTour);
    7172
    7273      foreach (int city in tour1.Stops)
  • branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Manipulators/PotvinCustomerRelocationManipulator.cs

    r6753 r6851  
    6767        for (int i = 0; i < route1.Stops.Count; i++) {
    6868          int stop = route1.Stops[i];
    69           if (ProblemInstance.Demand[stop] < 0) {
     69          if (ProblemInstance.GetDemand(stop) < 0) {
    7070            int source = pdp.PickupDeliveryLocation[stop];
    7171
     
    158158
    159159            //drive there
    160             double currentDistace = vrptw.GetDistance(start, end);
     160            double currentDistace = vrptw.GetDistance(start, end, individual);
    161161            time += currentDistace;
    162162
  • branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Moves/PickupDelivery/PDExchange/PotvinPDExchangeExhaustiveMoveGenerator.cs

    r6773 r6851  
    6262            if (k != i) {
    6363              int city1 = individual.Tours[i].Stops[j];
    64               if (pdp == null || pdp.Demand[city1] >= 0) {
     64              if (pdp == null || pdp.GetDemand(city1) >= 0) {
    6565                for (int l = 0; l < individual.Tours[k].Stops.Count; l++) {
    6666                  int city2 = individual.Tours[k].Stops[l];
    67                   if (pdp == null || pdp.Demand[city2] >= 0) {
     67                  if (pdp == null || pdp.GetDemand(city2) >= 0) {
    6868                    bool valid = pdp == null ||
    6969                      (pdp.PickupDeliveryLocation[city2] != pdp.PickupDeliveryLocation[city1] &&
  • branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Moves/PickupDelivery/PDExchange/PotvinPDExchangeSingleMoveGenerator.cs

    r6773 r6851  
    6262      IPickupAndDeliveryProblemInstance pdp = problemInstance as IPickupAndDeliveryProblemInstance;
    6363      for (int i = 1; i <= individual.Cities; i++) {
    64         if (pdp == null || pdp.Demand[i] >= 0)
     64        if (pdp == null || pdp.GetDemand(i) >= 0)
    6565          cities.Add(i);
    6666      }
     
    8484        foreach (int stop in newTour.Stops) {
    8585          if (pdp == null ||
    86             (pdp.Demand[stop] >= 0 &&
     86            (pdp.GetDemand(stop) >= 0 &&
    8787             pdp.PickupDeliveryLocation[stop] != pdp.PickupDeliveryLocation[city] &&
    8888             pdp.PickupDeliveryLocation[stop] != city &&
  • branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Moves/PickupDelivery/PDRearrange/PotvinPDRearrangeExhaustiveMoveGenerator.cs

    r6773 r6851  
    5454
    5555      for (int i = 1; i <= problemInstance.Cities.Value; i++) {
    56         if (pdp == null || pdp.Demand[i] >= 0) {
     56        if (pdp == null || pdp.GetDemand(i) >= 0) {
    5757          int tour = individual.Tours.FindIndex(t => t.Stops.Contains(i));
    5858
  • branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Moves/PickupDelivery/PDRearrange/PotvinPDRearrangeMoveMaker.cs

    r6838 r6851  
    7474          if (positionToAvoid != i || positionToAvoid2 != j || stops == 0) {
    7575            bool feasible;
    76             double targetCosts = problemInstance.GetInsertionCosts(tourEval, target, 0, j, out feasible);
     76            double targetCosts = problemInstance.GetInsertionCosts(tourEval, solution, target, 0, j, out feasible);
    7777
    7878            double costs = sourceCosts + targetCosts;
  • branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Moves/PickupDelivery/PDRearrange/PotvinPDRearrangeSingleMoveGenerator.cs

    r6773 r6851  
    6262      IPickupAndDeliveryProblemInstance pdp = problemInstance as IPickupAndDeliveryProblemInstance;
    6363      for (int i = 1; i <= individual.Cities; i++) {
    64         if(pdp == null || pdp.Demand[i] >= 0)
     64        if(pdp == null || pdp.GetDemand(i) >= 0)
    6565          cities.Add(i);
    6666      }
  • branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Moves/PickupDelivery/PDShift/PotvinPDShiftExhaustiveMoveGenerator.cs

    r6773 r6851  
    6262            if (k != i) {
    6363              int city = individual.Tours[i].Stops[j];
    64               if (pdp == null || pdp.Demand[city] >= 0) {
     64              if (pdp == null || pdp.GetDemand(city) >= 0) {
    6565                PotvinPDShiftMove move = new PotvinPDShiftMove(
    6666                  city, i, k, individual);
  • branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Moves/PickupDelivery/PDShift/PotvinPDShiftSingleMoveGenerator.cs

    r6773 r6851  
    6262      IPickupAndDeliveryProblemInstance pdp = problemInstance as IPickupAndDeliveryProblemInstance;
    6363      for (int i = 1; i <= individual.Cities; i++) {
    64         if(pdp == null || pdp.Demand[i] >= 0)
     64        if(pdp == null || pdp.GetDemand(i) >= 0)
    6565          cities.Add(i);
    6666      }
  • branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/PotvinEncoding.cs

    r6838 r6851  
    4545
    4646    [StorableConstructor]
    47     private PotvinEncoding(bool serializing)
     47    protected PotvinEncoding(bool serializing)
    4848      : base(serializing) {
    4949    }
     
    7575
    7676    public double GetTourLength(Tour tour) {
    77       return tour.GetTourLength(ProblemInstance);
     77      return tour.GetTourLength(ProblemInstance, this);
    7878    }
    7979
     
    8787        if (positionToAvoid != i) {
    8888          bool feasible;
    89           double quality = ProblemInstance.GetInsertionCosts(eval, city, 0, i, out feasible);
     89          double quality = ProblemInstance.GetInsertionCosts(eval, this, city, 0, i, out feasible);
    9090          if (place < 0 || quality < minQuality) {
    9191            place = i;
     
    115115          for (int i = 0; i <= Tours[tour].Stops.Count; i++) {
    116116            bool feasible;
    117             double detour = ProblemInstance.GetInsertionCosts(eval, city, tour, i, out feasible);
    118            
     117            double detour = ProblemInstance.GetInsertionCosts(eval, this, city, tour, i, out feasible);
     118
    119119            if (feasible || allowInfeasible) {
    120120              if (route < 0 || detour < minDetour) {
Note: See TracChangeset for help on using the changeset viewer.