Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10391 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 6.3 KB
RevLine 
[7445]1#region License Information
2/* HeuristicLab
[9456]3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[7445]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;
[7638]28using ICSharpCode.SharpZipLib.Zip;
[7445]29
30namespace HeuristicLab.Problems.Instances.ElloumiCTAP {
[7548]31  public class ElloumiCTAPInstanceProvider : ProblemInstanceProvider<CTAPData> {
[7466]32    public override string Name {
[7445]33      get { return "Elloumi's CTAP instances"; }
34    }
35
[7466]36    public override string Description {
[7445]37      get { return "CTAP instances published by Sourour Elloumi"; }
38    }
39
[7482]40    public override Uri WebLink {
[7445]41      get { return new Uri("http://cedric.cnam.fr/oc/TAP/TAP.html"); }
42    }
43
[7482]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
[7638]52    private const string FileName = "ElloumiCTAP";
53
[7548]54    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
[7638]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;
[7445]65
[7638]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      }
[7445]71    }
72
[7548]73    public override CTAPData LoadData(IDataDescriptor id) {
74      var descriptor = (ElloumiCTAPDataDescriptor)id;
[7638]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);
[7445]82
[7638]83          instance.Name = id.Name;
84          instance.Description = id.Description;
[7445]85
[7638]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;
[7445]94
[7638]95                instance.BestKnownAssignment = slnParser.Assignment;
96                instance.BestKnownQuality = slnParser.Quality;
97              }
98            }
[7445]99          }
[7638]100          return instance;
[7445]101        }
102      }
[7538]103    }
104
[8192]105    public override bool CanImportData {
106      get { return true; }
107    }
108    public override CTAPData ImportData(string path) {
[7538]109      var parser = new ElloumiCTAPParser();
110      parser.Parse(path);
111      var instance = Load(parser);
112      instance.Name = Path.GetFileName(path);
113      instance.Description = "Loaded from file \"" + path + "\" on " + DateTime.Now.ToString();
[7445]114      return instance;
115    }
116
[7548]117    private CTAPData Load(ElloumiCTAPParser parser) {
118      var instance = new CTAPData();
[7538]119      instance.Processors = parser.Processors;
120      instance.Tasks = parser.Tasks;
121      instance.ExecutionCosts = parser.ExecutionCosts;
122      instance.CommunicationCosts = parser.CommunicationCosts;
123      instance.MemoryRequirements = parser.MemoryRequirements;
124      instance.MemoryCapacities = parser.MemoryCapacities;
125      return instance;
126    }
127
[7445]128    private string GetPrettyName(string instanceIdentifier) {
129      return Regex.Match(instanceIdentifier, GetType().Namespace + @"\.Data\.(.*)\.dat").Groups[1].Captures[0].Value;
130    }
131
132    private string GetDescription() {
133      return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + ".";
134    }
[7638]135
136    protected virtual string GetResourceName(string fileName) {
137      return Assembly.GetExecutingAssembly().GetManifestResourceNames()
138              .Where(x => Regex.Match(x, @".*\.Data\." + fileName).Success).SingleOrDefault();
139    }
140
141    protected IEnumerable<string> GetZipContents(ZipInputStream zipFile) {
142      ZipEntry entry;
143      while ((entry = zipFile.GetNextEntry()) != null) {
144        yield return entry.Name;
145      }
146    }
[7445]147  }
148}
Note: See TracBrowser for help on using the repository browser.