Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ScatterSearch (trunk integration)/HeuristicLab.Problems.Instances.QAPLIB/3.3/QAPLIBInstanceProvider.cs @ 7787

Last change on this file since 7787 was 7638, checked in by sforsten, 13 years ago

#1782: Modified instance providers QAPLIB, ElloumiCTAP, CordeauGQAP to use a zip archive instead of individual embedded files

File size: 8.0 KB
Line 
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;
28using ICSharpCode.SharpZipLib.Zip;
29
30namespace HeuristicLab.Problems.Instances.QAPLIB {
31  public class QAPLIBInstanceProvider : ProblemInstanceProvider<QAPData> {
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
142    public override string Name {
143      get { return "QAPLIB"; }
144    }
145
146    public override string Description {
147      get { return "Quadratic Assignment Problem Library"; }
148    }
149
150    public override Uri WebLink {
151      get { return new Uri("http://www.seas.upenn.edu/qaplib/"); }
152    }
153
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
162    private const string FileName = "qap";
163
164    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
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))
170              solutions.Add(Path.GetFileNameWithoutExtension(entry) + ".dat", entry);
171        }
172      }
173      var instanceArchiveName = GetResourceName(FileName + @"\.dat\.zip");
174      if (String.IsNullOrEmpty(instanceArchiveName)) yield break;
175
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      }
181    }
182
183    public override QAPData LoadData(IDataDescriptor id) {
184      var descriptor = (QAPLIBDataDescriptor)id;
185      var instanceArchiveName = GetResourceName(FileName + @"\.dat\.zip");
186      using (var instancesZipFile = new ZipFile(GetType().Assembly.GetManifestResourceStream(instanceArchiveName))) {
187        var entry = instancesZipFile.GetEntry(descriptor.InstanceIdentifier);
188
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;
195
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              }
214            }
215          }
216          return instance;
217        }
218      }
219    }
220
221    public override QAPData LoadData(string path) {
222      var parser = new QAPLIBParser();
223      parser.Parse(path);
224      var instance = Load(parser);
225      instance.Name = Path.GetFileName(path);
226      instance.Description = "Loaded from file \"" + path + "\" on " + DateTime.Now.ToString();
227      return instance;
228    }
229
230    public override void SaveData(QAPData instance, string path) {
231      throw new NotSupportedException();
232    }
233
234    private QAPData Load(QAPLIBParser parser) {
235      var instance = new QAPData();
236      instance.Dimension = parser.Size;
237      instance.Distances = parser.Distances;
238      instance.Weights = parser.Weights;
239      return instance;
240    }
241
242    private string GetDescription() {
243      return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + ".";
244    }
245
246    protected virtual string GetResourceName(string fileName) {
247      return Assembly.GetExecutingAssembly().GetManifestResourceNames()
248              .Where(x => Regex.Match(x, @".*\.Data\." + fileName).Success).SingleOrDefault();
249    }
250
251    protected IEnumerable<string> GetZipContents(ZipInputStream zipFile) {
252      ZipEntry entry;
253      while ((entry = zipFile.GetNextEntry()) != null) {
254        yield return entry.Name;
255      }
256    }
257  }
258}
Note: See TracBrowser for help on using the repository browser.