Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Problems.Instances.ElloumiCTAP/3.3/ElloumiCTAPInstanceProvider.cs @ 7445

Last change on this file since 7445 was 7445, checked in by abeham, 12 years ago

#1614

  • updated problem instance provider
File size: 3.9 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;
28
29namespace HeuristicLab.Problems.Instances.ElloumiCTAP {
30  public class ElloumiCTAPInstanceProvider : IProblemInstanceProvider<ICTAPInstance> {
31    public string Name {
32      get { return "Elloumi's CTAP instances"; }
33    }
34
35    public string Description {
36      get { return "CTAP instances published by Sourour Elloumi"; }
37    }
38
39    public Uri Link {
40      get { return new Uri("http://cedric.cnam.fr/oc/TAP/TAP.html"); }
41    }
42
43    public IEnumerable<IInstanceDescriptor> GetInstanceDescriptors() {
44      var solutions = Assembly.GetExecutingAssembly()
45        .GetManifestResourceNames()
46        .Where(x => x.EndsWith(".sol"))
47        .ToDictionary(x => Path.GetFileNameWithoutExtension(x) + ".dat", x => x);
48
49      return Assembly.GetExecutingAssembly()
50          .GetManifestResourceNames()
51          .Where(x => x.EndsWith(".dat"))
52          .OrderBy(x => x)
53          .Select(x => new ElloumiCTAPInstanceDescriptor(GetPrettyName(x), GetDescription(), x, solutions.ContainsKey(x) ? solutions[x] : String.Empty));
54    }
55
56    public ICTAPInstance GetInstance(IInstanceDescriptor id) {
57      var descriptor = (ElloumiCTAPInstanceDescriptor)id;
58      var instance = new ElloumiCTAPInstance();
59      using (var stream = Assembly.GetExecutingAssembly()
60        .GetManifestResourceStream(descriptor.InstanceIdentifier)) {
61        ElloumiCTAPParser datParser = new ElloumiCTAPParser();
62        datParser.Parse(stream);
63        if (datParser.Error != null) throw datParser.Error;
64        instance.ExecutionCosts = datParser.ExecutionCosts;
65        instance.CommunicationCosts = datParser.CommunicationCosts;
66        instance.MemoryRequirements = datParser.MemoryRequirements;
67        instance.MemoryCapacities = datParser.MemoryCapacities;
68
69        instance.Name = id.Name;
70        instance.Description = id.Description;
71
72        if (!String.IsNullOrEmpty(descriptor.SolutionIdentifier)) {
73          using (Stream solStream = Assembly.GetExecutingAssembly()
74            .GetManifestResourceStream(descriptor.SolutionIdentifier)) {
75            ElloumiCTAPSolutionParser slnParser = new ElloumiCTAPSolutionParser();
76            slnParser.Parse(solStream, instance.MemoryRequirements.Length);
77            if (slnParser.Error != null) throw slnParser.Error;
78
79            instance.BestKnownAssignment = slnParser.Assignment;
80            instance.BestKnownQuality = slnParser.Quality;
81          }
82        }
83      }
84      return instance;
85    }
86
87    private string GetPrettyName(string instanceIdentifier) {
88      return Regex.Match(instanceIdentifier, GetType().Namespace + @"\.Data\.(.*)\.dat").Groups[1].Captures[0].Value;
89    }
90
91    private string GetDescription() {
92      return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + ".";
93    }
94  }
95}
Note: See TracBrowser for help on using the repository browser.