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.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.
|
---|
47 | Contribution for solving non linear programs with o-1 variables, application to task assignment problems in distributed systems.
|
---|
48 | PhD 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 | private CTAPData Load(ElloumiCTAPParser parser) {
|
---|
115 | var instance = new CTAPData();
|
---|
116 | instance.Processors = parser.Processors;
|
---|
117 | instance.Tasks = parser.Tasks;
|
---|
118 | instance.ExecutionCosts = parser.ExecutionCosts;
|
---|
119 | instance.CommunicationCosts = parser.CommunicationCosts;
|
---|
120 | instance.MemoryRequirements = parser.MemoryRequirements;
|
---|
121 | instance.MemoryCapacities = parser.MemoryCapacities;
|
---|
122 | return instance;
|
---|
123 | }
|
---|
124 |
|
---|
125 | private string GetPrettyName(string instanceIdentifier) {
|
---|
126 | return Regex.Match(instanceIdentifier, GetType().Namespace + @"\.Data\.(.*)\.dat").Groups[1].Captures[0].Value;
|
---|
127 | }
|
---|
128 |
|
---|
129 | private string GetDescription() {
|
---|
130 | return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + ".";
|
---|
131 | }
|
---|
132 |
|
---|
133 | protected virtual string GetResourceName(string fileName) {
|
---|
134 | return Assembly.GetExecutingAssembly().GetManifestResourceNames()
|
---|
135 | .Where(x => Regex.Match(x, @".*\.Data\." + fileName).Success).SingleOrDefault();
|
---|
136 | }
|
---|
137 |
|
---|
138 | protected IEnumerable<string> GetZipContents(ZipInputStream zipFile) {
|
---|
139 | ZipEntry entry;
|
---|
140 | while ((entry = zipFile.GetNextEntry()) != null) {
|
---|
141 | yield return entry.Name;
|
---|
142 | }
|
---|
143 | }
|
---|
144 | }
|
---|
145 | }
|
---|