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/Crossovers
Files:
3 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)
Note: See TracChangeset for help on using the changeset viewer.