Changeset 11428


Ignore:
Timestamp:
10/08/14 16:21:42 (5 years ago)
Author:
pfleck
Message:

#2229

  • The IVRPData now specifies an optional BestKnownTourVehicleAssignment property. This can be used for solutions where a specific assignment for tours to vehicles is necessary.
  • The VRPInterpreter interpret the new BestKnownTourVehicleAssignment directly as VehicleAssignment in the created PotvinEncoding.
  • Minor refactoring in (I)VRPInstanceProvider.
Location:
trunk/sources
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.Instances.VehicleRouting/3.4/IVRPInstanceProvider.cs

    r11420 r11428  
    2020#endregion
    2121
     22
    2223namespace HeuristicLab.Problems.Instances.VehicleRouting {
    23   public interface IVRPInstanceProvider<T> {
     24  public interface IVRPInstanceProvider<TData> {
    2425    bool CanImportData { get; }
    25     T Import(string vrpFile, string tourFile);
     26    TData Import(string instancePath, string solutionPath);
    2627
    2728    bool CanExportData { get; }
    28     void Export(T instance, string path);
     29    void Export(TData instance, string path);
    2930  }
    3031}
  • trunk/sources/HeuristicLab.Problems.Instances.VehicleRouting/3.4/VRPInstanceProvider.cs

    r11420 r11428  
    2929
    3030namespace HeuristicLab.Problems.Instances.VehicleRouting {
    31   public abstract class VRPInstanceProvider<T> : ProblemInstanceProvider<T>, IVRPInstanceProvider<T> where T : IVRPData {
     31  public abstract class VRPInstanceProvider<TData> : ProblemInstanceProvider<TData>, IVRPInstanceProvider<TData> where TData : IVRPData {
    3232    protected abstract string FileName { get; }
    3333
    3434    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
    35       Dictionary<string, string> solutions = new Dictionary<string, string>();
     35      var solutions = new Dictionary<string, string>();
    3636      var solutionsArchiveName = GetResourceName(FileName + @"\.opt\.zip");
    3737      if (!String.IsNullOrEmpty(solutionsArchiveName)) {
    3838        using (var solutionsZipFile = new ZipInputStream(GetType().Assembly.GetManifestResourceStream(solutionsArchiveName))) {
    3939          foreach (var entry in GetZipContents(solutionsZipFile))
    40             solutions.Add(entry.Substring(0, entry.Length - ".opt".Length) + "." + FileName, entry);
     40            solutions.Add(Path.GetFileNameWithoutExtension(entry) + "." + FileName, entry);
    4141        }
    4242      }
     
    4646      using (var instanceStream = new ZipInputStream(GetType().Assembly.GetManifestResourceStream(instanceArchiveName))) {
    4747        foreach (var entry in GetZipContents(instanceStream).OrderBy(x => x)) {
    48           string solutionEntry = entry.Substring(0, entry.Length - ".opt".Length) + "." + FileName;
     48          string solutionEntry = Path.GetFileNameWithoutExtension(entry) + "." + FileName;
    4949          yield return new VRPDataDescriptor(Path.GetFileNameWithoutExtension(entry), GetInstanceDescription(), entry, solutions.ContainsKey(solutionEntry) ? solutions[solutionEntry] : String.Empty);
    5050        }
     
    5252    }
    5353
    54     public override T LoadData(IDataDescriptor id) {
     54    public override TData LoadData(IDataDescriptor id) {
    5555      var descriptor = (VRPDataDescriptor)id;
    5656      var instanceArchiveName = GetResourceName(FileName + @"\.zip");
     
    7676    }
    7777
    78     private static void LoadSolution(Stream stream, T instance) {
     78    #region IVRPInstanceProvider
     79    public TData Import(string vrpFile, string tourFile) {
     80      var data = ImportData(vrpFile);
     81      if (!String.IsNullOrEmpty(tourFile)) {
     82        LoadSolution(tourFile, data);
     83      }
     84      return data;
     85    }
     86
     87    public void Export(TData instance, string path) {
     88      ExportData(instance, path);
     89    }
     90    #endregion
     91
     92    protected virtual void LoadSolution(Stream stream, TData instance) {
    7993      List<List<int>> routes = new List<List<int>>();
    8094
     
    106120    }
    107121
    108     public static void LoadSolution(string path, T instance) {
     122    public void LoadSolution(string path, TData instance) {
    109123      using (FileStream stream = new FileStream(path, FileMode.Open)) {
    110124        LoadSolution(stream, instance);
     
    112126    }
    113127
    114     protected abstract T LoadData(Stream stream);
     128    protected abstract TData LoadData(Stream stream);
    115129
    116     public T Import(string vrpFile, string tourFile) {
    117       var data = ImportData(vrpFile);
    118       if (!String.IsNullOrEmpty(tourFile)) {
    119         LoadSolution(tourFile, data);
    120       }
    121       return data;
    122     }
    123 
    124     public void Export(T instance, string path) {
    125       ExportData(instance, path);
    126     }
    127 
     130    #region Helpers
    128131    protected virtual string GetResourceName(string fileName) {
    129132      return Assembly.GetExecutingAssembly().GetManifestResourceNames()
     
    141144      }
    142145    }
     146    #endregion
    143147  }
    144148}
  • trunk/sources/HeuristicLab.Problems.Instances/3.3/Types/VRP/IVRPData.cs

    r11171 r11428  
    3131    double? BestKnownQuality { get; set; }
    3232    int[][] BestKnownTour { get; set; }
     33    int[] BestKnownTourVehicleAssignment { get; set; }
    3334  }
    3435}
  • trunk/sources/HeuristicLab.Problems.Instances/3.3/Types/VRP/VRPData.cs

    r11285 r11428  
    7878    public int[][] BestKnownTour { get; set; }
    7979    /// <summary>
     80    /// Optional! Specifies the used vehicle for a given tour.
     81    /// </summary>
     82    public int[] BestKnownTourVehicleAssignment { get; set; }
     83    /// <summary>
    8084    /// Optional! The quality of the best-known solution.
    8185    /// </summary>
  • trunk/sources/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/PotvinEncoding.cs

    r11171 r11428  
    3636    public List<int> Unrouted { get; set; }
    3737
     38    // VehicleAssignment[tour] retreives the assigned vehicle for the given tour
    3839    [Storable]
    3940    public Permutation VehicleAssignment { get; private set; }
  • trunk/sources/HeuristicLab.Problems.VehicleRouting/3.4/Interpreters/VRPInterpreter.cs

    r11171 r11428  
    2020#endregion
    2121
     22using System.IO;
    2223using HeuristicLab.Data;
    2324using HeuristicLab.Problems.Instances;
     
    4445        }
    4546
     47        if (data.BestKnownTourVehicleAssignment != null) {
     48          if (data.BestKnownTourVehicleAssignment.Length != solution.VehicleAssignment.Length)
     49            throw new InvalidDataException("Number of vehicles of the best known tour does not match the number vehicles of the instance.");
     50          for (int i = 0; i < data.BestKnownTourVehicleAssignment.Length; i++)
     51            solution.VehicleAssignment[i] = data.BestKnownTourVehicleAssignment[i];
     52        }
     53
    4654        return solution;
    4755      } else {
     
    5159
    5260    protected abstract IVRPProblemInstance CreateProblemInstance();
    53    
     61
    5462    public VRPInstanceDescription Interpret(IVRPData data) {
    5563      VRPInstanceDescription result = new VRPInstanceDescription();
Note: See TracChangeset for help on using the changeset viewer.