Changeset 12395 for branches/HiveStatistics/sources/HeuristicLab.Problems.Instances.VehicleRouting/3.4/VRPInstanceProvider.cs
- Timestamp:
- 05/20/15 16:41:14 (10 years ago)
- Location:
- branches/HiveStatistics/sources
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HiveStatistics/sources
- Property svn:mergeinfo changed
-
branches/HiveStatistics/sources/HeuristicLab.Problems.Instances.VehicleRouting/3.4/VRPInstanceProvider.cs
r11205 r12395 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 4Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 23 23 using System.Collections.Generic; 24 24 using System.IO; 25 using System.IO.Compression; 25 26 using System.Linq; 26 27 using System.Reflection; 27 28 using System.Text.RegularExpressions; 28 using ICSharpCode.SharpZipLib.Zip;29 using HeuristicLab.Common; 29 30 30 31 namespace HeuristicLab.Problems.Instances.VehicleRouting { 31 public abstract class VRPInstanceProvider : ProblemInstanceProvider<VRPData>, IVRPInstanceProvider{32 public abstract class VRPInstanceProvider<TData> : ProblemInstanceProvider<TData>, IVRPInstanceProvider<TData> where TData : IVRPData { 32 33 protected abstract string FileName { get; } 33 34 34 35 public override IEnumerable<IDataDescriptor> GetDataDescriptors() { 35 Dictionary<string, string>solutions = new Dictionary<string, string>();36 var solutions = new Dictionary<string, string>(); 36 37 var solutionsArchiveName = GetResourceName(FileName + @"\.opt\.zip"); 37 38 if (!String.IsNullOrEmpty(solutionsArchiveName)) { 38 using (var solutionsZipFile = new Zip InputStream(GetType().Assembly.GetManifestResourceStream(solutionsArchiveName))) {39 foreach (var entry in GetZipContents(solutionsZipFile))40 solutions.Add( entry.Substring(0, entry.Length - ".opt".Length) + "." + FileName, entry);39 using (var solutionsZipFile = new ZipArchive(GetType().Assembly.GetManifestResourceStream(solutionsArchiveName), ZipArchiveMode.Read)) { 40 foreach (var entry in solutionsZipFile.Entries) 41 solutions.Add(Path.GetFileNameWithoutExtension(entry.Name) + "." + FileName, entry.Name); 41 42 } 42 43 } … … 44 45 if (String.IsNullOrEmpty(instanceArchiveName)) yield break; 45 46 46 using (var instanceStream = new Zip InputStream(GetType().Assembly.GetManifestResourceStream(instanceArchiveName))) {47 foreach (var entry in GetZipContents(instanceStream).OrderBy(x => x)) {48 string solutionEntry = entry.Substring(0, entry.Length - ".opt".Length) + "." + FileName;47 using (var instanceStream = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName), ZipArchiveMode.Read)) { 48 foreach (var entry in instanceStream.Entries.Select(x => x.Name).OrderBy(x => x, new NaturalStringComparer())) { 49 string solutionEntry = Path.GetFileNameWithoutExtension(entry) + "." + FileName; 49 50 yield return new VRPDataDescriptor(Path.GetFileNameWithoutExtension(entry), GetInstanceDescription(), entry, solutions.ContainsKey(solutionEntry) ? solutions[solutionEntry] : String.Empty); 50 51 } … … 52 53 } 53 54 54 public override VRPData LoadData(IDataDescriptor id) {55 public override TData LoadData(IDataDescriptor id) { 55 56 var descriptor = (VRPDataDescriptor)id; 56 57 var instanceArchiveName = GetResourceName(FileName + @"\.zip"); 57 using (var instancesZipFile = new Zip File(GetType().Assembly.GetManifestResourceStream(instanceArchiveName))) {58 using (var instancesZipFile = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName))) { 58 59 var entry = instancesZipFile.GetEntry(descriptor.InstanceIdentifier); 59 var stream = instancesZipFile.GetInputStream(entry);60 var stream = entry.Open(); 60 61 var instance = LoadData(stream); 61 62 if (string.IsNullOrEmpty(instance.Name)) { … … 65 66 if (!String.IsNullOrEmpty(descriptor.SolutionIdentifier)) { 66 67 var solutionsArchiveName = GetResourceName(FileName + @"\.opt\.zip"); 67 using (var solutionsZipFile = new Zip File(GetType().Assembly.GetManifestResourceStream(solutionsArchiveName))) {68 using (var solutionsZipFile = new ZipArchive(GetType().Assembly.GetManifestResourceStream(solutionsArchiveName))) { 68 69 entry = solutionsZipFile.GetEntry(descriptor.SolutionIdentifier); 69 stream = solutionsZipFile.GetInputStream(entry);70 stream = entry.Open(); 70 71 LoadSolution(stream, instance); 71 72 } … … 76 77 } 77 78 78 private static void LoadSolution(Stream stream, VRPData instance) { 79 #region IVRPInstanceProvider 80 public TData Import(string vrpFile, string tourFile) { 81 var data = ImportData(vrpFile); 82 if (!String.IsNullOrEmpty(tourFile)) { 83 LoadSolution(tourFile, data); 84 } 85 return data; 86 } 87 88 public void Export(TData instance, string path) { 89 ExportData(instance, path); 90 } 91 #endregion 92 93 protected virtual void LoadSolution(Stream stream, TData instance) { 94 LoadOptFile(stream, instance); 95 } 96 97 private void LoadOptFile(Stream stream, TData instance) { 79 98 List<List<int>> routes = new List<List<int>>(); 80 99 … … 93 112 routes.Add(route); 94 113 } 114 115 if (line.StartsWith("Solution")) { 116 if (routes.Any()) { 117 // Skip remaining solutions since only one "best solution" is stored 118 break; 119 } 120 } 95 121 } 96 122 } … … 99 125 } 100 126 101 public static void LoadSolution(string path, VRPData instance) { 102 using (FileStream stream = new FileStream(path, FileMode.Open)) { 103 LoadSolution(stream, instance); 127 public void LoadSolution(string path, TData instance) { 128 try { 129 using (var stream = new FileStream(path, FileMode.Open)) { 130 LoadSolution(stream, instance); 131 } 132 } 133 catch (Exception) { 134 // new stream necessary because first try already read from stream 135 using (var stream = new FileStream(path, FileMode.Open)) { 136 LoadOptFile(stream, instance); // Fallback to .opt-Format 137 } 104 138 } 105 139 } 106 140 107 protected abstract VRPData LoadData(Stream stream);141 protected abstract TData LoadData(Stream stream); 108 142 109 public IVRPData Import(string vrpFile, string tourFile) { 110 var data = ImportData(vrpFile); 111 if (!String.IsNullOrEmpty(tourFile)) { 112 LoadSolution(tourFile, data); 113 } 114 return data; 115 } 116 117 public void Export(IVRPData instance, string path) { 118 ExportData((VRPData)instance, path); 119 } 120 143 #region Helpers 121 144 protected virtual string GetResourceName(string fileName) { 122 145 return Assembly.GetExecutingAssembly().GetManifestResourceNames() … … 127 150 return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + "."; 128 151 } 129 130 protected IEnumerable<string> GetZipContents(ZipInputStream zipFile) { 131 ZipEntry entry; 132 while ((entry = zipFile.GetNextEntry()) != null) { 133 yield return entry.Name; 134 } 135 } 152 #endregion 136 153 } 137 154 }
Note: See TracChangeset
for help on using the changeset viewer.