Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.Instances.QAPLIB/3.3/QAPLIBInstanceProvider.cs @ 8311

Last change on this file since 8311 was 8192, checked in by sforsten, 12 years ago

#1782:

  • renamed CanSave to CanExportData and SaveData to ExportData
  • added the same functionality for importing problem instance as we implemented for exporting
  • some special changes had to be made in Problems.Instances.VehicleRouting
File size: 7.9 KB
RevLine 
[7445]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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;
[7638]28using ICSharpCode.SharpZipLib.Zip;
[7445]29
30namespace HeuristicLab.Problems.Instances.QAPLIB {
[7548]31  public class QAPLIBInstanceProvider : ProblemInstanceProvider<QAPData> {
[7558]32    #region Reversed instances
33    // These instances specified their best known solution in the wrong order
34    private static HashSet<string> reversedSolutions = new HashSet<string>(new string[] {
35      "bur26a",
36      "bur26b",
37      "bur26c",
38      "bur26d",
39      "bur26e",
40      "bur26f",
41      "bur26g",
42      "bur26h",
43      "chr12a",
44      "chr12b",
45      "chr12c",
46      "chr15a",
47      "chr15b",
48      "chr15c",
49      "chr18a",
50      "chr18b",
51      "chr20a",
52      "chr20b",
53      "chr20c",
54      "chr22a",
55      "chr22b",
56      "chr25a",
57      "esc16a",
58      "esc16b",
59      "esc16c",
60      "esc16d",
61      "esc16e",
62      "esc16g",
63      "esc16h",
64      "esc16i",
65      "esc16j",
66      "esc32a",
67      "esc32e",
68      "esc32f",
69      "esc32g",
70      "had12",
71      "had14",
72      "had16",
73      "had18",
74      "had20",
75      "kra32",
76      "lipa20a",
77      "lipa30a",
78      "lipa40a",
79      "lipa50a",
80      "lipa60a",
81      "lipa70a",
82      "lipa80a",
83      "lipa90a",
84      "nug12",
85      "nug14",
86      "nug15",
87      "nug16a",
88      "nug16b",
89      "nug17",
90      "nug18",
91      "nug20",
92      "nug21",
93      "nug22",
94      "nug24",
95      "nug25",
96      "nug27",
97      "nug28",
98      "rou12",
99      "rou15",
100      "rou20",
101      "scr12",
102      "scr15",
103      "scr20",
104      "sko100a",
105      "sko100b",
106      "sko100c",
107      "sko100d",
108      "sko100e",
109      "sko100f",
110      "sko49",
111      "sko81",
112      "sko90",
113      "ste36a",
114      "ste36b",
115      "tai100a",
116      "tai100b",
117      "tai12a",
118      "tai12b",
119      "tai150b",
120      "tai15a",
121      "tai15b",
122      "tai17a",
123      "tai20a",
124      "tai20b",
125      "tai256c",
126      "tai25a",
127      "tai25b",
128      "tai30b",
129      "tai35b",
130      "tai40b",
131      "tai50a",
132      "tai50b",
133      "tai60a",
134      "tai60b",
135      "tai64c",
136      "tai80a",
137      "tai80b",
138      "wil100"
139    });
140    #endregion
141
[7466]142    public override string Name {
[7445]143      get { return "QAPLIB"; }
144    }
145
[7466]146    public override string Description {
[7445]147      get { return "Quadratic Assignment Problem Library"; }
148    }
149
[7482]150    public override Uri WebLink {
[7445]151      get { return new Uri("http://www.seas.upenn.edu/qaplib/"); }
152    }
153
[7482]154    public override string ReferencePublication {
155      get {
156        return @"R. E. Burkard, S. E. Karisch, and F. Rendl. 1997.
157QAPLIB - A Quadratic Assignment Problem Library.
158Journal of Global Optimization, 10, pp. 391-403.";
159      }
160    }
161
[7638]162    private const string FileName = "qap";
163
[7548]164    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
[7638]165      Dictionary<string, string> solutions = new Dictionary<string, string>();
166      var solutionsArchiveName = GetResourceName(FileName + @"\.sln\.zip");
167      if (!String.IsNullOrEmpty(solutionsArchiveName)) {
168        using (var solutionsZipFile = new ZipInputStream(GetType().Assembly.GetManifestResourceStream(solutionsArchiveName))) {
169          foreach (var entry in GetZipContents(solutionsZipFile))
[8180]170            solutions.Add(Path.GetFileNameWithoutExtension(entry) + ".dat", entry);
[7638]171        }
172      }
173      var instanceArchiveName = GetResourceName(FileName + @"\.dat\.zip");
174      if (String.IsNullOrEmpty(instanceArchiveName)) yield break;
[7445]175
[7638]176      using (var instanceStream = new ZipInputStream(GetType().Assembly.GetManifestResourceStream(instanceArchiveName))) {
177        foreach (var entry in GetZipContents(instanceStream).OrderBy(x => x)) {
178          yield return new QAPLIBDataDescriptor(Path.GetFileNameWithoutExtension(entry), GetDescription(), entry, solutions.ContainsKey(entry) ? solutions[entry] : String.Empty);
179        }
180      }
[7445]181    }
182
[7548]183    public override QAPData LoadData(IDataDescriptor id) {
184      var descriptor = (QAPLIBDataDescriptor)id;
[7638]185      var instanceArchiveName = GetResourceName(FileName + @"\.dat\.zip");
186      using (var instancesZipFile = new ZipFile(GetType().Assembly.GetManifestResourceStream(instanceArchiveName))) {
187        var entry = instancesZipFile.GetEntry(descriptor.InstanceIdentifier);
[7445]188
[7638]189        using (var stream = instancesZipFile.GetInputStream(entry)) {
190          var parser = new QAPLIBParser();
191          parser.Parse(stream);
192          var instance = Load(parser);
193          instance.Name = id.Name;
194          instance.Description = id.Description;
[7445]195
[7638]196          if (!String.IsNullOrEmpty(descriptor.SolutionIdentifier)) {
197            var solutionsArchiveName = GetResourceName(FileName + @"\.sln\.zip");
198            using (var solutionsZipFile = new ZipFile(GetType().Assembly.GetManifestResourceStream(solutionsArchiveName))) {
199              entry = solutionsZipFile.GetEntry(descriptor.SolutionIdentifier);
200              using (var solStream = solutionsZipFile.GetInputStream(entry)) {
201                var slnParser = new QAPLIBSolutionParser();
202                slnParser.Parse(solStream, true);
203                if (slnParser.Error != null) throw slnParser.Error;
204
205                int[] assignment = slnParser.Assignment;
206                if (reversedSolutions.Contains(instance.Name)) {
207                  assignment = (int[])slnParser.Assignment.Clone();
208                  for (int i = 0; i < assignment.Length; i++)
209                    assignment[slnParser.Assignment[i]] = i;
210                }
211                instance.BestKnownAssignment = assignment;
212                instance.BestKnownQuality = slnParser.Quality;
213              }
[7558]214            }
[7445]215          }
[7638]216          return instance;
[7445]217        }
218      }
[7538]219    }
220
[8192]221    public override bool CanImportData {
222      get { return true; }
223    }
224    public override QAPData ImportData(string path) {
[7538]225      var parser = new QAPLIBParser();
226      parser.Parse(path);
227      var instance = Load(parser);
228      instance.Name = Path.GetFileName(path);
229      instance.Description = "Loaded from file \"" + path + "\" on " + DateTime.Now.ToString();
[7445]230      return instance;
231    }
232
[7548]233    private QAPData Load(QAPLIBParser parser) {
234      var instance = new QAPData();
[7538]235      instance.Dimension = parser.Size;
236      instance.Distances = parser.Distances;
237      instance.Weights = parser.Weights;
238      return instance;
239    }
240
[7445]241    private string GetDescription() {
242      return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + ".";
243    }
[7638]244
245    protected virtual string GetResourceName(string fileName) {
246      return Assembly.GetExecutingAssembly().GetManifestResourceNames()
247              .Where(x => Regex.Match(x, @".*\.Data\." + fileName).Success).SingleOrDefault();
248    }
249
250    protected IEnumerable<string> GetZipContents(ZipInputStream zipFile) {
251      ZipEntry entry;
252      while ((entry = zipFile.GetNextEntry()) != null) {
253        yield return entry.Name;
254      }
255    }
[7445]256  }
257}
Note: See TracBrowser for help on using the repository browser.