Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GBT/HeuristicLab.Problems.Instances.ElloumiCTAP/3.3/ElloumiCTAPInstanceProvider.cs @ 12542

Last change on this file since 12542 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 6.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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;
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 ZipArchive(GetType().Assembly.GetManifestResourceStream(solutionsArchiveName), ZipArchiveMode.Read)) {
59          foreach (var entry in solutionsZipFile.Entries)
60            solutions.Add(Path.GetFileNameWithoutExtension(entry.Name) + ".dat", entry.Name);
61        }
62      }
63      var instanceArchiveName = GetResourceName(FileName + @"\.dat\.zip");
64      if (String.IsNullOrEmpty(instanceArchiveName)) yield break;
65
66      using (var instanceStream = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName), ZipArchiveMode.Read)) {
67        foreach (var entry in instanceStream.Entries.Select(x => x.Name).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 ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName), ZipArchiveMode.Read)) {
77        var entry = instancesZipFile.GetEntry(descriptor.InstanceIdentifier);
78        using (var stream = entry.Open()) {
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 ZipArchive(GetType().Assembly.GetManifestResourceStream(solutionsArchiveName), ZipArchiveMode.Read)) {
89              entry = solutionsZipFile.GetEntry(descriptor.SolutionIdentifier);
90              using (var solStream = entry.Open()) {
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 bool CanImportData {
106      get { return true; }
107    }
108    public override CTAPData ImportData(string path) {
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();
114      return instance;
115    }
116
117    private CTAPData Load(ElloumiCTAPParser parser) {
118      var instance = new CTAPData();
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
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    }
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}
Note: See TracBrowser for help on using the repository browser.