Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2936_GQAPIntegration/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Instances/3.3/GeneralizedQAPLIBInstanceProvider.cs @ 16323

Last change on this file since 16323 was 15579, checked in by abeham, 7 years ago

#1614: Added the new instances as problem instance assembly

File size: 4.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.IO.Compression;
26using System.Linq;
27using System.Reflection;
28using System.Text.RegularExpressions;
29using HeuristicLab.Problems.Instances;
30using HeuristicLab.Problems.Instances.CordeauGQAP;
31
32namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Instances {
33  public class GeneralizedQAPLIBInstanceProvider : ProblemInstanceProvider<GQAPData> {
34    public override string Name {
35      get { return "Generalized QAPLIB"; }
36    }
37
38    public override string Description {
39      get { return "GQAP instances based on flow and distance matrices from QAPLIB instances."; }
40    }
41
42    public override Uri WebLink {
43      get { return null; }
44    }
45
46    public override string ReferencePublication {
47      get { return null; }
48    }
49
50    private const string FileName = "Data";
51
52    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
53      var instanceArchiveName = GetResourceName(FileName + @"\.zip");
54      if (String.IsNullOrEmpty(instanceArchiveName)) yield break;
55
56      using (var instanceStream = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName), ZipArchiveMode.Read)) {
57        foreach (var entry in instanceStream.Entries.Select(x => x.Name).OrderBy(x => x)) {
58          yield return new GeneralizedQAPLIBInstanceDescriptor(Path.GetFileNameWithoutExtension(entry), GetDescription(), entry);
59        }
60      }
61    }
62
63    public override GQAPData LoadData(IDataDescriptor id) {
64      var descriptor = (GeneralizedQAPLIBInstanceDescriptor)id;
65      var instanceArchiveName = GetResourceName(FileName + @"\.zip");
66      using (var instancesZipFile = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName), ZipArchiveMode.Read)) {
67        var entry = instancesZipFile.GetEntry(descriptor.InstanceIdentifier);
68        using (var stream = entry.Open()) {
69          var parser = new CordeauGQAPParser();
70          parser.Parse(stream);
71          var instance = Load(parser);
72
73          instance.Name = id.Name;
74          instance.Description = id.Description;
75
76          return instance;
77        }
78      }
79    }
80
81    public override bool CanImportData {
82      get { return true; }
83    }
84    public override GQAPData ImportData(string path) {
85      var parser = new CordeauGQAPParser();
86      parser.Parse(path);
87      var instance = Load(parser);
88
89      instance.Name = Path.GetFileName(path);
90      instance.Description = "Loaded from file \"" + path + "\" on " + DateTime.Now.ToString();
91
92      return instance;
93    }
94
95    private GQAPData Load(CordeauGQAPParser parser) {
96      var instance = new GQAPData();
97      instance.BestKnownQuality = parser.BestKnownQuality;
98      instance.Equipments = parser.Equipments;
99      instance.Locations = parser.Locations;
100      instance.Demands = parser.Demands;
101      instance.Capacities = parser.Capacities;
102      instance.Weights = parser.Weights;
103      instance.Distances = parser.Distances;
104      instance.InstallationCosts = parser.InstallationCosts;
105      instance.TransportationCosts = parser.TransportationCosts;
106      return instance;
107    }
108
109    private string GetDescription() {
110      return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + ".";
111    }
112
113    protected virtual string GetResourceName(string fileName) {
114      return Assembly.GetExecutingAssembly().GetManifestResourceNames()
115              .Where(x => Regex.Match(x, @".*\.Data\." + fileName).Success).SingleOrDefault();
116    }
117  }
118}
Note: See TracBrowser for help on using the repository browser.