Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.Instances.VehicleRouting/3.4/VRPInstanceProvider.cs @ 11297

Last change on this file since 11297 was 11295, checked in by pfleck, 10 years ago

#2226:

  • Fixed VRPInstanceProvider parsing of best known solutions. If a file contains multiple solutions (Augerat A-n45-k7), only the first solution is used.
  • Fixed broken .opt files (R in Route missing): B-n57-k9, E-n76-k7, tai75a
File size: 5.8 KB
RevLine 
[7881]1#region License Information
2/* HeuristicLab
[11171]3 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[7881]4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.IO;
25using System.Linq;
26using System.Reflection;
27using System.Text.RegularExpressions;
28using ICSharpCode.SharpZipLib.Zip;
29
30namespace HeuristicLab.Problems.Instances.VehicleRouting {
[11287]31  public abstract class VRPInstanceProvider<T> : ProblemInstanceProvider<T>, IVRPInstanceProvider where T : IVRPData {
[7881]32    protected abstract string FileName { get; }
33
34    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
35      Dictionary<string, string> solutions = new Dictionary<string, string>();
36      var solutionsArchiveName = GetResourceName(FileName + @"\.opt\.zip");
37      if (!String.IsNullOrEmpty(solutionsArchiveName)) {
38        using (var solutionsZipFile = new ZipInputStream(GetType().Assembly.GetManifestResourceStream(solutionsArchiveName))) {
39          foreach (var entry in GetZipContents(solutionsZipFile))
40            solutions.Add(entry.Substring(0, entry.Length - ".opt".Length) + "." + FileName, entry);
41        }
42      }
43      var instanceArchiveName = GetResourceName(FileName + @"\.zip");
44      if (String.IsNullOrEmpty(instanceArchiveName)) yield break;
45
46      using (var instanceStream = new ZipInputStream(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;
49          yield return new VRPDataDescriptor(Path.GetFileNameWithoutExtension(entry), GetInstanceDescription(), entry, solutions.ContainsKey(solutionEntry) ? solutions[solutionEntry] : String.Empty);
50        }
51      }
52    }
53
[11286]54    public override T LoadData(IDataDescriptor id) {
[7881]55      var descriptor = (VRPDataDescriptor)id;
56      var instanceArchiveName = GetResourceName(FileName + @"\.zip");
57      using (var instancesZipFile = new ZipFile(GetType().Assembly.GetManifestResourceStream(instanceArchiveName))) {
58        var entry = instancesZipFile.GetEntry(descriptor.InstanceIdentifier);
59        var stream = instancesZipFile.GetInputStream(entry);
60        var instance = LoadData(stream);
61        if (string.IsNullOrEmpty(instance.Name)) {
62          instance.Name = Path.GetFileNameWithoutExtension(entry.ToString());
63        }
64
65        if (!String.IsNullOrEmpty(descriptor.SolutionIdentifier)) {
66          var solutionsArchiveName = GetResourceName(FileName + @"\.opt\.zip");
67          using (var solutionsZipFile = new ZipFile(GetType().Assembly.GetManifestResourceStream(solutionsArchiveName))) {
68            entry = solutionsZipFile.GetEntry(descriptor.SolutionIdentifier);
69            stream = solutionsZipFile.GetInputStream(entry);
70            LoadSolution(stream, instance);
71          }
72        }
73
74        return instance;
75      }
76    }
77
[11286]78    private static void LoadSolution(Stream stream, T instance) {
[7881]79      List<List<int>> routes = new List<List<int>>();
80
81      using (StreamReader reader = new StreamReader(stream)) {
82        String line;
83        while ((line = reader.ReadLine()) != null) {
84          if (line.StartsWith("Route")) {
85            string[] token = line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
86
87            List<int> route = new List<int>();
88
89            for (int i = 2; i < token.Length; i++) {
90              route.Add(int.Parse(token[i]) - 1);
91            }
92
93            routes.Add(route);
94          }
[11295]95
96          if (line.StartsWith("Solution")) {
97            if (routes.Any()) {
98              // Skip remaining solutions since only one "best solution" is stored
99              break;
100            }
101          }
[7881]102        }
103      }
104
105      instance.BestKnownTour = routes.Select(x => x.ToArray()).ToArray();
106    }
107
[11286]108    public static void LoadSolution(string path, T instance) {
[7881]109      using (FileStream stream = new FileStream(path, FileMode.Open)) {
110        LoadSolution(stream, instance);
111      }
112    }
113
[11286]114    protected abstract T LoadData(Stream stream);
[7881]115
[11287]116    public IVRPData Import(string vrpFile, string tourFile) {
[8192]117      var data = ImportData(vrpFile);
[7881]118      if (!String.IsNullOrEmpty(tourFile)) {
119        LoadSolution(tourFile, data);
120      }
121      return data;
122    }
123
[11287]124    public void Export(IVRPData instance, string path) {
125      ExportData((T)instance, path);
[8192]126    }
127
[7881]128    protected virtual string GetResourceName(string fileName) {
129      return Assembly.GetExecutingAssembly().GetManifestResourceNames()
130              .Where(x => Regex.Match(x, @".*\.Data\." + fileName).Success).SingleOrDefault();
131    }
132
133    protected virtual string GetInstanceDescription() {
134      return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + ".";
135    }
136
137    protected IEnumerable<string> GetZipContents(ZipInputStream zipFile) {
138      ZipEntry entry;
139      while ((entry = zipFile.GetNextEntry()) != null) {
140        yield return entry.Name;
141      }
142    }
143  }
144}
Note: See TracBrowser for help on using the repository browser.