Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1614

  • Added Transpose() extension method for double[,] matrices
  • Added IProblemInstanceConsumer<T> interface
  • Implemented general ProblemView which auto-detects all instances a problem can consume
  • Added ability of IProblemInstanceProvider to directly feed a consumer
  • Implemented general view for problem instance providers
  • Fixed a few bugs
File size: 4.4 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    private IProblemInstanceConsumer<ICTAPInstance> consumer;
32
33    public string Name {
34      get { return "Elloumi's CTAP instances"; }
35    }
36
37    public string Description {
38      get { return "CTAP instances published by Sourour Elloumi"; }
39    }
40
41    public Uri Link {
42      get { return new Uri("http://cedric.cnam.fr/oc/TAP/TAP.html"); }
43    }
44
45    public bool ConsumerCanBeFed {
46      get { return consumer != null; }
47    }
48
49    public void SetConsumer(IProblemInstanceConsumer consumer) {
50      if (consumer is IProblemInstanceConsumer<ICTAPInstance>)
51        this.consumer = (IProblemInstanceConsumer<ICTAPInstance>)consumer;
52      else this.consumer = null;
53    }
54
55    public void FeedConsumer(IInstanceDescriptor descriptor) {
56      consumer.LoadFrom(GetInstance(descriptor));
57    }
58
59    public IEnumerable<IInstanceDescriptor> GetInstanceDescriptors() {
60      var solutions = Assembly.GetExecutingAssembly()
61        .GetManifestResourceNames()
62        .Where(x => x.EndsWith(".sol"))
63        .ToDictionary(x => Path.GetFileNameWithoutExtension(x) + ".dat", x => x);
64
65      return Assembly.GetExecutingAssembly()
66          .GetManifestResourceNames()
67          .Where(x => x.EndsWith(".dat"))
68          .OrderBy(x => x)
69          .Select(x => new ElloumiCTAPInstanceDescriptor(GetPrettyName(x), GetDescription(), x, solutions.ContainsKey(x) ? solutions[x] : String.Empty));
70    }
71
72    public ICTAPInstance GetInstance(IInstanceDescriptor id) {
73      var descriptor = (ElloumiCTAPInstanceDescriptor)id;
74      var instance = new ElloumiCTAPInstance();
75      using (var stream = Assembly.GetExecutingAssembly()
76        .GetManifestResourceStream(descriptor.InstanceIdentifier)) {
77        ElloumiCTAPParser datParser = new ElloumiCTAPParser();
78        datParser.Parse(stream);
79        if (datParser.Error != null) throw datParser.Error;
80        instance.ExecutionCosts = datParser.ExecutionCosts;
81        instance.CommunicationCosts = datParser.CommunicationCosts;
82        instance.MemoryRequirements = datParser.MemoryRequirements;
83        instance.MemoryCapacities = datParser.MemoryCapacities;
84
85        instance.Name = id.Name;
86        instance.Description = id.Description;
87
88        if (!String.IsNullOrEmpty(descriptor.SolutionIdentifier)) {
89          using (Stream solStream = Assembly.GetExecutingAssembly()
90            .GetManifestResourceStream(descriptor.SolutionIdentifier)) {
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    private string GetPrettyName(string instanceIdentifier) {
104      return Regex.Match(instanceIdentifier, GetType().Namespace + @"\.Data\.(.*)\.dat").Groups[1].Captures[0].Value;
105    }
106
107    private string GetDescription() {
108      return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + ".";
109    }
110  }
111}
Note: See TracBrowser for help on using the repository browser.