Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Problems.Instances.QAPLIB/3.3/QAPLIBInstanceProvider.cs @ 7538

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

#1614

  • Fixed plugin dependencies
  • Updated GQAP view
  • Changed instances infrastructure
    • Changed interface types into classes
    • Removed the library specific instance classes
File size: 4.4 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<QAPInstance> {
31    public override string Name {
32      get { return "QAPLIB"; }
33    }
34
35    public override string Description {
36      get { return "Quadratic Assignment Problem Library"; }
37    }
38
39    public override Uri WebLink {
40      get { return new Uri("http://www.seas.upenn.edu/qaplib/"); }
41    }
42
43    public override string ReferencePublication {
44      get {
45        return @"R. E. Burkard, S. E. Karisch, and F. Rendl. 1997.
46QAPLIB - A Quadratic Assignment Problem Library.
47Journal of Global Optimization, 10, pp. 391-403.";
48      }
49    }
50
51    public override IEnumerable<IInstanceDescriptor> GetInstanceDescriptors() {
52      var solutions = Assembly.GetExecutingAssembly()
53        .GetManifestResourceNames()
54        .Where(x => x.EndsWith(".sln"))
55        .ToDictionary(x => Path.GetFileNameWithoutExtension(x) + ".dat", x => x);
56
57      return Assembly.GetExecutingAssembly()
58          .GetManifestResourceNames()
59          .Where(x => x.EndsWith(".dat"))
60          .OrderBy(x => x)
61          .Select(x => new QAPLIBInstanceDescriptor(GetPrettyName(x), GetDescription(), x, solutions.ContainsKey(x) ? solutions[x] : String.Empty));
62    }
63
64    public override QAPInstance LoadInstance(IInstanceDescriptor id) {
65      var descriptor = (QAPLIBInstanceDescriptor)id;
66      using (var stream = Assembly.GetExecutingAssembly()
67        .GetManifestResourceStream(descriptor.InstanceIdentifier)) {
68        var parser = new QAPLIBParser();
69        parser.Parse(stream);
70        var instance = Load(parser);
71        instance.Name = id.Name;
72        instance.Description = id.Description;
73
74        if (!String.IsNullOrEmpty(descriptor.SolutionIdentifier)) {
75          using (Stream solStream = Assembly.GetExecutingAssembly()
76            .GetManifestResourceStream(descriptor.SolutionIdentifier)) {
77            var slnParser = new QAPLIBSolutionParser();
78            slnParser.Parse(solStream, true);
79            if (slnParser.Error != null) throw slnParser.Error;
80
81            instance.BestKnownAssignment = slnParser.Assignment;
82            instance.BestKnownQuality = slnParser.Quality;
83          }
84        }
85        return instance;
86      }
87    }
88
89    public override QAPInstance LoadInstance(string path) {
90      var parser = new QAPLIBParser();
91      parser.Parse(path);
92      var instance = Load(parser);
93      instance.Name = Path.GetFileName(path);
94      instance.Description = "Loaded from file \"" + path + "\" on " + DateTime.Now.ToString();
95      return instance;
96    }
97
98    public override void SaveInstance(QAPInstance instance, string path) {
99      throw new NotSupportedException();
100    }
101
102    private QAPInstance Load(QAPLIBParser parser) {
103      var instance = new QAPInstance();
104      instance.Dimension = parser.Size;
105      instance.Distances = parser.Distances;
106      instance.Weights = parser.Weights;
107      return instance;
108    }
109
110    private string GetPrettyName(string instanceIdentifier) {
111      return Regex.Match(instanceIdentifier, GetType().Namespace + @"\.Data\.(.*)\.dat").Groups[1].Captures[0].Value;
112    }
113
114    private string GetDescription() {
115      return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + ".";
116    }
117  }
118}
Note: See TracBrowser for help on using the repository browser.