Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.Instances.ElloumiCTAP/3.3/ElloumiCTAPInstanceProvider.cs @ 8103

Last change on this file since 8103 was 7638, checked in by sforsten, 13 years ago

#1782: Modified instance providers QAPLIB, ElloumiCTAP, CordeauGQAP to use a zip archive instead of individual embedded files

File size: 6.3 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;
28using ICSharpCode.SharpZipLib.Zip;
29
30namespace HeuristicLab.Problems.Instances.ElloumiCTAP {
31  public class ElloumiCTAPInstanceProvider : ProblemInstanceProvider<CTAPData> {
32    public override string Name {
33      get { return "Elloumi's CTAP instances"; }
34    }
35
36    public override string Description {
37      get { return "CTAP instances published by Sourour Elloumi"; }
38    }
39
40    public override Uri WebLink {
41      get { return new Uri("http://cedric.cnam.fr/oc/TAP/TAP.html"); }
42    }
43
44    public override string ReferencePublication {
45      get {
46        return @"Elloumi, S. 1991.
47Contribution for solving non linear programs with o-1 variables, application to task assignment problems in distributed systems.
48PhD Thesis. Conservatoire National des Arts et Métiers, Paris.";
49      }
50    }
51
52    private const string FileName = "ElloumiCTAP";
53
54    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
55      Dictionary<string, string> solutions = new Dictionary<string, string>();
56      var solutionsArchiveName = GetResourceName(FileName + @"\.sol\.zip");
57      if (!String.IsNullOrEmpty(solutionsArchiveName)) {
58        using (var solutionsZipFile = new ZipInputStream(GetType().Assembly.GetManifestResourceStream(solutionsArchiveName))) {
59          foreach (var entry in GetZipContents(solutionsZipFile))
60            solutions.Add(Path.GetFileNameWithoutExtension(entry) + ".dat", entry);
61        }
62      }
63      var instanceArchiveName = GetResourceName(FileName + @"\.dat\.zip");
64      if (String.IsNullOrEmpty(instanceArchiveName)) yield break;
65
66      using (var instanceStream = new ZipInputStream(GetType().Assembly.GetManifestResourceStream(instanceArchiveName))) {
67        foreach (var entry in GetZipContents(instanceStream).OrderBy(x => x)) {
68          yield return new ElloumiCTAPDataDescriptor(Path.GetFileNameWithoutExtension(entry), GetDescription(), entry, solutions.ContainsKey(entry) ? solutions[entry] : String.Empty);
69        }
70      }
71    }
72
73    public override CTAPData LoadData(IDataDescriptor id) {
74      var descriptor = (ElloumiCTAPDataDescriptor)id;
75      var instanceArchiveName = GetResourceName(FileName + @"\.dat\.zip");
76      using (var instancesZipFile = new ZipFile(GetType().Assembly.GetManifestResourceStream(instanceArchiveName))) {
77        var entry = instancesZipFile.GetEntry(descriptor.InstanceIdentifier);
78        using (var stream = instancesZipFile.GetInputStream(entry)) {
79          var parser = new ElloumiCTAPParser();
80          parser.Parse(stream);
81          var instance = Load(parser);
82
83          instance.Name = id.Name;
84          instance.Description = id.Description;
85
86          if (!String.IsNullOrEmpty(descriptor.SolutionIdentifier)) {
87            var solutionsArchiveName = GetResourceName(FileName + @"\.sol\.zip");
88            using (var solutionsZipFile = new ZipFile(GetType().Assembly.GetManifestResourceStream(solutionsArchiveName))) {
89              entry = solutionsZipFile.GetEntry(descriptor.SolutionIdentifier);
90              using (var solStream = solutionsZipFile.GetInputStream(entry)) {
91                ElloumiCTAPSolutionParser slnParser = new ElloumiCTAPSolutionParser();
92                slnParser.Parse(solStream, instance.MemoryRequirements.Length);
93                if (slnParser.Error != null) throw slnParser.Error;
94
95                instance.BestKnownAssignment = slnParser.Assignment;
96                instance.BestKnownQuality = slnParser.Quality;
97              }
98            }
99          }
100          return instance;
101        }
102      }
103    }
104
105    public override CTAPData LoadData(string path) {
106      var parser = new ElloumiCTAPParser();
107      parser.Parse(path);
108      var instance = Load(parser);
109      instance.Name = Path.GetFileName(path);
110      instance.Description = "Loaded from file \"" + path + "\" on " + DateTime.Now.ToString();
111      return instance;
112    }
113
114    public override void SaveData(CTAPData instance, string path) {
115      throw new NotSupportedException();
116    }
117
118    private CTAPData Load(ElloumiCTAPParser parser) {
119      var instance = new CTAPData();
120      instance.Processors = parser.Processors;
121      instance.Tasks = parser.Tasks;
122      instance.ExecutionCosts = parser.ExecutionCosts;
123      instance.CommunicationCosts = parser.CommunicationCosts;
124      instance.MemoryRequirements = parser.MemoryRequirements;
125      instance.MemoryCapacities = parser.MemoryCapacities;
126      return instance;
127    }
128
129    private string GetPrettyName(string instanceIdentifier) {
130      return Regex.Match(instanceIdentifier, GetType().Namespace + @"\.Data\.(.*)\.dat").Groups[1].Captures[0].Value;
131    }
132
133    private string GetDescription() {
134      return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + ".";
135    }
136
137    protected virtual string GetResourceName(string fileName) {
138      return Assembly.GetExecutingAssembly().GetManifestResourceNames()
139              .Where(x => Regex.Match(x, @".*\.Data\." + fileName).Success).SingleOrDefault();
140    }
141
142    protected IEnumerable<string> GetZipContents(ZipInputStream zipFile) {
143      ZipEntry entry;
144      while ((entry = zipFile.GetNextEntry()) != null) {
145        yield return entry.Name;
146      }
147    }
148  }
149}
Note: See TracBrowser for help on using the repository browser.