Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1614

  • updated problem instance provider
File size: 3.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 : IProblemInstanceProvider<IQAPInstance> {
31    public string Name {
32      get { return "QAPLIB"; }
33    }
34
35    public string Description {
36      get { return "Quadratic Assignment Problem Library"; }
37    }
38
39    public Uri Link {
40      get { return new Uri("http://www.seas.upenn.edu/qaplib/"); }
41    }
42
43    public IEnumerable<IInstanceDescriptor> GetInstanceDescriptors() {
44      var solutions = Assembly.GetExecutingAssembly()
45        .GetManifestResourceNames()
46        .Where(x => x.EndsWith(".sln"))
47        .ToDictionary(x => Path.GetFileNameWithoutExtension(x) + ".dat", x => x);
48
49      return Assembly.GetExecutingAssembly()
50          .GetManifestResourceNames()
51          .Where(x => x.EndsWith(".dat"))
52          .OrderBy(x => x)
53          .Select(x => new QAPLIBInstanceDescriptor(GetPrettyName(x), GetDescription(), x, solutions.ContainsKey(x) ? solutions[x] : String.Empty));
54    }
55
56    public IQAPInstance GetInstance(IInstanceDescriptor id) {
57      var descriptor = (QAPLIBInstanceDescriptor)id;
58      var instance = new QAPLIBInstance();
59      using (var stream = Assembly.GetExecutingAssembly()
60        .GetManifestResourceStream(descriptor.InstanceIdentifier)) {
61        QAPLIBParser datParser = new QAPLIBParser();
62        datParser.Parse(stream);
63        if (datParser.Error != null) throw datParser.Error;
64        instance.Distances = datParser.Distances;
65        instance.Weights = datParser.Weights;
66
67        instance.Name = id.Name;
68        instance.Description = id.Description;
69
70        if (!String.IsNullOrEmpty(descriptor.SolutionIdentifier)) {
71          using (Stream solStream = Assembly.GetExecutingAssembly()
72            .GetManifestResourceStream(descriptor.SolutionIdentifier)) {
73            QAPLIBSolutionParser slnParser = new QAPLIBSolutionParser();
74            slnParser.Parse(solStream, true);
75            if (slnParser.Error != null) throw slnParser.Error;
76
77            instance.BestKnownAssignment = slnParser.Assignment;
78            instance.BestKnownQuality = slnParser.Quality;
79          }
80        }
81      }
82      return instance;
83    }
84
85    private string GetPrettyName(string instanceIdentifier) {
86      return Regex.Match(instanceIdentifier, GetType().Namespace + @"\.Data\.(.*)\.dat").Groups[1].Captures[0].Value;
87    }
88
89    private string GetDescription() {
90      return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + ".";
91    }
92  }
93}
Note: See TracBrowser for help on using the repository browser.