Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7558 was 7558, checked in by abeham, 12 years ago

#1782: trunk integration of problem instance development

  • Adapted TSP and QAP to use the new feature
  • Moved the TSPLIB importer dialog from the TSP plugin to the TSPLIB instances plugin (created a view for that provider)
  • Created it as a default view for IHeuristicOptimizationProblem in order not to interfere with other problems do not yet work with this
File size: 6.6 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;
28
29namespace HeuristicLab.Problems.Instances.QAPLIB {
30  public class QAPLIBInstanceProvider : ProblemInstanceProvider<QAPData> {
31    #region Reversed instances
32    // These instances specified their best known solution in the wrong order
33    private static HashSet<string> reversedSolutions = new HashSet<string>(new string[] {
34      "bur26a",
35      "bur26b",
36      "bur26c",
37      "bur26d",
38      "bur26e",
39      "bur26f",
40      "bur26g",
41      "bur26h",
42      "chr12a",
43      "chr12b",
44      "chr12c",
45      "chr15a",
46      "chr15b",
47      "chr15c",
48      "chr18a",
49      "chr18b",
50      "chr20a",
51      "chr20b",
52      "chr20c",
53      "chr22a",
54      "chr22b",
55      "chr25a",
56      "esc16a",
57      "esc16b",
58      "esc16c",
59      "esc16d",
60      "esc16e",
61      "esc16g",
62      "esc16h",
63      "esc16i",
64      "esc16j",
65      "esc32a",
66      "esc32e",
67      "esc32f",
68      "esc32g",
69      "had12",
70      "had14",
71      "had16",
72      "had18",
73      "had20",
74      "kra32",
75      "lipa20a",
76      "lipa30a",
77      "lipa40a",
78      "lipa50a",
79      "lipa60a",
80      "lipa70a",
81      "lipa80a",
82      "lipa90a",
83      "nug12",
84      "nug14",
85      "nug15",
86      "nug16a",
87      "nug16b",
88      "nug17",
89      "nug18",
90      "nug20",
91      "nug21",
92      "nug22",
93      "nug24",
94      "nug25",
95      "nug27",
96      "nug28",
97      "rou12",
98      "rou15",
99      "rou20",
100      "scr12",
101      "scr15",
102      "scr20",
103      "sko100a",
104      "sko100b",
105      "sko100c",
106      "sko100d",
107      "sko100e",
108      "sko100f",
109      "sko49",
110      "sko81",
111      "sko90",
112      "ste36a",
113      "ste36b",
114      "tai100a",
115      "tai100b",
116      "tai12a",
117      "tai12b",
118      "tai150b",
119      "tai15a",
120      "tai15b",
121      "tai17a",
122      "tai20a",
123      "tai20b",
124      "tai256c",
125      "tai25a",
126      "tai25b",
127      "tai30b",
128      "tai35b",
129      "tai40b",
130      "tai50a",
131      "tai50b",
132      "tai60a",
133      "tai60b",
134      "tai64c",
135      "tai80a",
136      "tai80b",
137      "wil100"
138    });
139    #endregion
140
141    public override string Name {
142      get { return "QAPLIB"; }
143    }
144
145    public override string Description {
146      get { return "Quadratic Assignment Problem Library"; }
147    }
148
149    public override Uri WebLink {
150      get { return new Uri("http://www.seas.upenn.edu/qaplib/"); }
151    }
152
153    public override string ReferencePublication {
154      get {
155        return @"R. E. Burkard, S. E. Karisch, and F. Rendl. 1997.
156QAPLIB - A Quadratic Assignment Problem Library.
157Journal of Global Optimization, 10, pp. 391-403.";
158      }
159    }
160
161    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
162      var solutions = Assembly.GetExecutingAssembly()
163        .GetManifestResourceNames()
164        .Where(x => x.EndsWith(".sln"))
165        .ToDictionary(x => Path.GetFileNameWithoutExtension(x) + ".dat", x => x);
166
167      return Assembly.GetExecutingAssembly()
168          .GetManifestResourceNames()
169          .Where(x => x.EndsWith(".dat"))
170          .OrderBy(x => x)
171          .Select(x => new QAPLIBDataDescriptor(GetPrettyName(x), GetDescription(), x, solutions.ContainsKey(x) ? solutions[x] : String.Empty));
172    }
173
174    public override QAPData LoadData(IDataDescriptor id) {
175      var descriptor = (QAPLIBDataDescriptor)id;
176      using (var stream = Assembly.GetExecutingAssembly()
177        .GetManifestResourceStream(descriptor.InstanceIdentifier)) {
178        var parser = new QAPLIBParser();
179        parser.Parse(stream);
180        var instance = Load(parser);
181        instance.Name = id.Name;
182        instance.Description = id.Description;
183
184        if (!String.IsNullOrEmpty(descriptor.SolutionIdentifier)) {
185          using (Stream solStream = Assembly.GetExecutingAssembly()
186            .GetManifestResourceStream(descriptor.SolutionIdentifier)) {
187            var slnParser = new QAPLIBSolutionParser();
188            slnParser.Parse(solStream, true);
189            if (slnParser.Error != null) throw slnParser.Error;
190
191            int[] assignment = slnParser.Assignment;
192            if (reversedSolutions.Contains(instance.Name)) {
193              assignment = (int[])slnParser.Assignment.Clone();
194              for (int i = 0; i < assignment.Length; i++)
195                assignment[slnParser.Assignment[i]] = i;
196            }
197            instance.BestKnownAssignment = assignment;
198            instance.BestKnownQuality = slnParser.Quality;
199          }
200        }
201        return instance;
202      }
203    }
204
205    public override QAPData LoadData(string path) {
206      var parser = new QAPLIBParser();
207      parser.Parse(path);
208      var instance = Load(parser);
209      instance.Name = Path.GetFileName(path);
210      instance.Description = "Loaded from file \"" + path + "\" on " + DateTime.Now.ToString();
211      return instance;
212    }
213
214    public override void SaveData(QAPData instance, string path) {
215      throw new NotSupportedException();
216    }
217
218    private QAPData Load(QAPLIBParser parser) {
219      var instance = new QAPData();
220      instance.Dimension = parser.Size;
221      instance.Distances = parser.Distances;
222      instance.Weights = parser.Weights;
223      return instance;
224    }
225
226    private string GetPrettyName(string instanceIdentifier) {
227      return Regex.Match(instanceIdentifier, GetType().Namespace + @"\.Data\.(.*)\.dat").Groups[1].Captures[0].Value;
228    }
229
230    private string GetDescription() {
231      return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + ".";
232    }
233  }
234}
Note: See TracBrowser for help on using the repository browser.