Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/09/10 16:24:09 (14 years ago)
Author:
svonolfe
Message:

Added Potvin encoding (#1177)

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

Legend:

Unmodified
Added
Removed
  • branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Alba/AlbaEncoding.cs

    r4365 r4376  
    2828using HeuristicLab.Problems.VehicleRouting.Encodings.General;
    2929using HeuristicLab.Problems.VehicleRouting.Interfaces;
     30using HeuristicLab.Problems.VehicleRouting.Variants;
    3031
    3132namespace HeuristicLab.Problems.VehicleRouting.Encodings.Alba {
    3233  [Item("AlbaEncoding", "Represents an Alba encoding of VRP solutions. It is implemented as described in Alba, E. and Dorronsoro, B. (2004). Solving the Vehicle Routing Problem by Using Cellular Genetic Algorithms.")]
    3334  [StorableClass]
    34   public class AlbaEncoding : PermutationEncoding {   
     35  public class AlbaEncoding : PermutationEncoding, ISingleDepotEncoding, ITimeWindowedEncoding, IHeterogenousCapacitatedEncoding {   
    3536    #region IVRPEncoding Members
    3637    public override List<Tour> GetTours() {
     
    6061    #endregion
    6162
     63    #region IHeterogenousCapacitatedEncoding Members
     64
     65    public int GetVehicleAssignment(int tour) {
     66      int vehicleAssignment = -1;
     67      Tour currentTour = GetTours()[tour];
     68
     69      int lastStop = currentTour.Stops[
     70        currentTour.Stops.Count - 1] - 1;
     71
     72      int lastStopIndex = this.IndexOf(lastStop);
     73
     74      if (lastStopIndex == this.Length - 1)
     75        vehicleAssignment = this.ProblemInstance.Vehicles.Value - 1;
     76      else
     77        vehicleAssignment = this[lastStopIndex + 1] - this.ProblemInstance.Cities.Value;
     78
     79      return vehicleAssignment;
     80    }
     81    #endregion
     82
    6283    public override IDeepCloneable Clone(HeuristicLab.Common.Cloner cloner) {
    6384      AlbaEncoding clone = new AlbaEncoding(
    64         new Permutation(this.PermutationType, this.array),
    65         (IVRPProblemInstance)cloner.Clone(ProblemInstance));
     85        new Permutation(this.PermutationType, this.array), ProblemInstance);
    6686      cloner.RegisterClonedObject(this, clone);
    6787      clone.readOnly = readOnly;
     
    103123        instance);
    104124    }
     125
     126    public static AlbaEncoding ConvertFrom(IVRPEncoding encoding, IVRPProblemInstance instance) {
     127      List<Tour> tours = encoding.GetTours();
     128
     129      int cities = 0;
     130      foreach (Tour tour in tours) {
     131        cities += tour.Stops.Count;
     132      }
     133
     134      int emptyVehicles = instance.Vehicles.Value - tours.Count;
     135
     136      int[] array = new int[cities + tours.Count + emptyVehicles - 1];
     137      int delimiter = 0;
     138      int arrayIndex = 0;
     139     
     140      foreach (Tour tour in tours) {
     141        foreach (int city in tour.Stops) {
     142          array[arrayIndex] = city - 1;
     143          arrayIndex++;
     144        }
     145
     146        if (arrayIndex != array.Length) {
     147          if (encoding is IHeterogenousCapacitatedEncoding) {
     148            array[arrayIndex] = cities + (encoding as IHeterogenousCapacitatedEncoding).GetVehicleAssignment(delimiter);
     149          } else {
     150            array[arrayIndex] = cities + delimiter;
     151          }
     152         
     153          delimiter++;
     154          arrayIndex++;
     155        }
     156      }
     157
     158      for (int i = 0; i < emptyVehicles - 1; i++) {
     159        if (encoding is IHeterogenousCapacitatedEncoding) {
     160          array[arrayIndex] = cities + (encoding as IHeterogenousCapacitatedEncoding).GetVehicleAssignment(delimiter);
     161        } else {
     162          array[arrayIndex] = cities + delimiter;
     163        }
     164
     165        delimiter++;
     166        arrayIndex++;
     167      }
     168
     169      AlbaEncoding solution = new AlbaEncoding(new Permutation(PermutationTypes.RelativeUndirected, new IntArray(array)), instance);
     170
     171      return solution;
     172    }
    105173  }
    106174}
  • branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Alba/Crossovers/AlbaCrossover.cs

    r4369 r4376  
    5353        IVRPEncoding solution = ParentsParameter.ActualValue[i];
    5454
    55         /*if (!(solution is AlbaEncoding)) {
    56           parents[i] = AlbaEncoding.ConvertFrom(solution, VehiclesParameter.ActualValue.Value,
    57             DistanceMatrixParameter);
    58         } else*/ {
     55        if (!(solution is AlbaEncoding)) {
     56          parents[i] = AlbaEncoding.ConvertFrom(solution, ProblemInstance);
     57        } else {
    5958          parents[i] = solution;
    6059        }
  • branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Alba/Manipulators/AlbaManipulator.cs

    r4369 r4376  
    6262      IVRPEncoding solution = VRPToursParameter.ActualValue;
    6363      if (!(solution is AlbaEncoding)) {
    64         //VRPToursParameter.ActualValue = AlbaEncoding.ConvertFrom(solution, VehiclesParameter.ActualValue.Value, DistanceMatrixParameter);
     64        VRPToursParameter.ActualValue = AlbaEncoding.ConvertFrom(solution, ProblemInstance);
    6565      }
    6666
  • branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Alba/Moves/AlbaMoveGenerator.cs

    r4369 r4376  
    4343      IVRPEncoding solution = VRPToursParameter.ActualValue;
    4444      if (!(solution is AlbaEncoding)) {
    45         //VRPToursParameter.ActualValue = AlbaEncoding.ConvertFrom(solution, VehiclesParameter.ActualValue.Value,
    46         //  DistanceMatrixParameter);
     45        VRPToursParameter.ActualValue = AlbaEncoding.ConvertFrom(solution, ProblemInstance);
    4746      }
    4847     
  • branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/General/TourEncoding.cs

    r4365 r4376  
    7171    protected IVRPProblemInstance ProblemInstance { get; set; }
    7272
    73     public override IDeepCloneable Clone(Cloner cloner) {
    74       TourEncoding clone = (TourEncoding)base.Clone(cloner);
    75 
    76       clone.ProblemInstance = (IVRPProblemInstance)cloner.Clone(ProblemInstance);
    77 
    78       return clone;
    79     }
    80 
    8173    public TourEncoding(IVRPProblemInstance problemInstance) {
    8274      Tours = new ItemList<Tour>();
     
    8779    [StorableConstructor]
    8880    protected TourEncoding(bool serializing)
    89       : base() {
     81      : base(serializing) {
     82    }
     83
     84    public static void ConvertFrom(IVRPEncoding encoding, TourEncoding solution, IVRPProblemInstance problemInstance) {
     85      solution.Tours = new ItemList<Tour>(encoding.GetTours());
     86    }
     87
     88    public static void ConvertFrom(List<int> route, TourEncoding solution) {
     89      solution.Tours = new ItemList<Tour>();
     90
     91      Tour tour = new Tour();
     92      for (int i = 0; i < route.Count; i++) {
     93        if (route[i] == 0) {
     94          if (tour.Stops.Count > 0) {
     95            solution.Tours.Add(tour);
     96            tour = new Tour();
     97          }
     98        } else {
     99          tour.Stops.Add(route[i]);
     100        }
     101      }
    90102    }
    91103  }
Note: See TracChangeset for help on using the changeset viewer.