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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.IO;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Reflection;
|
---|
27 | using System.Text.RegularExpressions;
|
---|
28 | using ICSharpCode.SharpZipLib.Zip;
|
---|
29 |
|
---|
30 | namespace 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.
|
---|
47 | A memetic heuristic for the generalized quadratic assignment problem.
|
---|
48 | INFORMS 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 GQAPData LoadData(string path) {
|
---|
84 | var parser = new CordeauGQAPParser();
|
---|
85 | parser.Parse(path);
|
---|
86 | var instance = Load(parser);
|
---|
87 |
|
---|
88 | instance.Name = Path.GetFileName(path);
|
---|
89 | instance.Description = "Loaded from file \"" + path + "\" on " + DateTime.Now.ToString();
|
---|
90 |
|
---|
91 | return instance;
|
---|
92 | }
|
---|
93 |
|
---|
94 | public override void SaveData(GQAPData instance, string path) {
|
---|
95 | throw new NotSupportedException();
|
---|
96 | }
|
---|
97 |
|
---|
98 | private GQAPData Load(CordeauGQAPParser parser) {
|
---|
99 | var instance = new GQAPData();
|
---|
100 | instance.BestKnownQuality = parser.BestKnownQuality;
|
---|
101 | instance.Equipments = parser.Equipments;
|
---|
102 | instance.Locations = parser.Locations;
|
---|
103 | instance.Demands = parser.Demands;
|
---|
104 | instance.Capacities = parser.Capacities;
|
---|
105 | instance.Weights = parser.Weights;
|
---|
106 | instance.Distances = parser.Distances;
|
---|
107 | instance.InstallationCosts = parser.InstallationCosts;
|
---|
108 | instance.TransportationCosts = parser.TransportationCosts;
|
---|
109 | return instance;
|
---|
110 | }
|
---|
111 |
|
---|
112 | private string GetDescription() {
|
---|
113 | return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + ".";
|
---|
114 | }
|
---|
115 |
|
---|
116 | protected virtual string GetResourceName(string fileName) {
|
---|
117 | return Assembly.GetExecutingAssembly().GetManifestResourceNames()
|
---|
118 | .Where(x => Regex.Match(x, @".*\.Data\." + fileName).Success).SingleOrDefault();
|
---|
119 | }
|
---|
120 |
|
---|
121 | protected IEnumerable<string> GetZipContents(ZipInputStream zipFile) {
|
---|
122 | ZipEntry entry;
|
---|
123 | while ((entry = zipFile.GetNextEntry()) != null) {
|
---|
124 | yield return entry.Name;
|
---|
125 | }
|
---|
126 | }
|
---|
127 | }
|
---|
128 | }
|
---|