Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.Instances.CordeauGQAP/3.3/CordeauGQAPInstanceProvider.cs @ 11504

Last change on this file since 11504 was 11170, checked in by ascheibe, 10 years ago

#2115 updated copyright year in stable branch

File size: 4.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.CordeauGQAP {
31  public class CordeauGQAPInstanceProvider : ProblemInstanceProvider<GQAPData> {
32    public override string Name {
33      get { return "Cordeau et al. GQAP instances"; }
34    }
35
36    public override string Description {
37      get { return "GQAP instances published by Cordeau, Gaudioso, Laporte, and Moccia."; }
38    }
39
40    public override Uri WebLink {
41      get { return null; }
42    }
43
44    public override string ReferencePublication {
45      get {
46        return @"Cordeau, J.-F., Gaudioso, M., Laporte, G., Moccia, L. 2006.
47A memetic heuristic for the generalized quadratic assignment problem.
48INFORMS Journal on Computing, 18, pp. 433–443.";
49      }
50    }
51
52    private const string FileName = "CordeauGQAP";
53
54    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
55      var instanceArchiveName = GetResourceName(FileName + @"\.zip");
56      if (String.IsNullOrEmpty(instanceArchiveName)) yield break;
57
58      using (var instanceStream = new ZipInputStream(GetType().Assembly.GetManifestResourceStream(instanceArchiveName))) {
59        foreach (var entry in GetZipContents(instanceStream).OrderBy(x => x)) {
60          yield return new CordeauGQAPDataDescriptor(Path.GetFileNameWithoutExtension(entry), GetDescription(), entry);
61        }
62      }
63    }
64
65    public override GQAPData LoadData(IDataDescriptor id) {
66      var descriptor = (CordeauGQAPDataDescriptor)id;
67      var instanceArchiveName = GetResourceName(FileName + @"\.zip");
68      using (var instancesZipFile = new ZipFile(GetType().Assembly.GetManifestResourceStream(instanceArchiveName))) {
69        var entry = instancesZipFile.GetEntry(descriptor.InstanceIdentifier);
70        using (var stream = instancesZipFile.GetInputStream(entry)) {
71          var parser = new CordeauGQAPParser();
72          parser.Parse(stream);
73          var instance = Load(parser);
74
75          instance.Name = id.Name;
76          instance.Description = id.Description;
77
78          return instance;
79        }
80      }
81    }
82
83    public override bool CanImportData {
84      get { return true; }
85    }
86    public override GQAPData ImportData(string path) {
87      var parser = new CordeauGQAPParser();
88      parser.Parse(path);
89      var instance = Load(parser);
90
91      instance.Name = Path.GetFileName(path);
92      instance.Description = "Loaded from file \"" + path + "\" on " + DateTime.Now.ToString();
93
94      return instance;
95    }
96
97    private GQAPData Load(CordeauGQAPParser parser) {
98      var instance = new GQAPData();
99      instance.BestKnownQuality = parser.BestKnownQuality;
100      instance.Equipments = parser.Equipments;
101      instance.Locations = parser.Locations;
102      instance.Demands = parser.Demands;
103      instance.Capacities = parser.Capacities;
104      instance.Weights = parser.Weights;
105      instance.Distances = parser.Distances;
106      instance.InstallationCosts = parser.InstallationCosts;
107      instance.TransportationCosts = parser.TransportationCosts;
108      return instance;
109    }
110
111    private string GetDescription() {
112      return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + ".";
113    }
114
115    protected virtual string GetResourceName(string fileName) {
116      return Assembly.GetExecutingAssembly().GetManifestResourceNames()
117              .Where(x => Regex.Match(x, @".*\.Data\." + fileName).Success).SingleOrDefault();
118    }
119
120    protected IEnumerable<string> GetZipContents(ZipInputStream zipFile) {
121      ZipEntry entry;
122      while ((entry = zipFile.GetNextEntry()) != null) {
123        yield return entry.Name;
124      }
125    }
126  }
127}
Note: See TracBrowser for help on using the repository browser.