Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2701_MemPRAlgorithm/HeuristicLab.Problems.Instances.DIMACS/3.3/GcolInstanceProvider.cs @ 16189

Last change on this file since 16189 was 14471, checked in by abeham, 8 years ago

#2701:

  • Updated GraphColoringProblem and Problems.Instances
    • Added new fitness function from literature
    • Added DIMACS benchmark instances
  • Updated LinearLinkageEncoding
    • Added HammingSimilarityCalculator
File size: 6.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.DIMACS {
31  public class GcolInstanceProvider : ProblemInstanceProvider<GCPData> {
32
33    public override string Name {
34      get { return "DIMACS Graph Coloring"; }
35    }
36
37    public override string Description {
38      get { return "Graph Coloring problem instance library"; }
39    }
40
41    public override Uri WebLink {
42      get { return new Uri("https://turing.cs.hbg.psu.edu/txn131/graphcoloring.html"); }
43    }
44
45    public override string ReferencePublication {
46      get {
47        return string.Empty;
48      }
49    }
50
51    protected virtual string FileName { get { return "col"; } }
52
53    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
54      var instanceArchiveName = GetResourceName(FileName + @"\.zip");
55      if (String.IsNullOrEmpty(instanceArchiveName)) yield break;
56
57      using (var instanceStream = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName), ZipArchiveMode.Read)) {
58        foreach (var entry in instanceStream.Entries.Select(x => x.Name).OrderBy(x => x)) {
59          yield return new GcolDataDescriptor(Path.GetFileNameWithoutExtension(entry), GetDescription(), entry);
60        }
61      }
62    }
63
64    public override GCPData LoadData(IDataDescriptor id) {
65      var descriptor = (GcolDataDescriptor)id;
66      var instanceArchiveName = GetResourceName(FileName + @"\.zip");
67      using (var instancesZipFile = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName), ZipArchiveMode.Read)) {
68        var entry = instancesZipFile.GetEntry(descriptor.InstanceIdentifier);
69
70        using (var stream = entry.Open()) {
71          var parser = new Parser();
72          parser.Parse(stream);
73          var instance = Load(parser);
74          instance.Name = id.Name;
75          instance.Description = id.Description;
76          int bestknown;
77          if (bkq.TryGetValue(instance.Name, out bestknown))
78            instance.BestKnownColors = bestknown;
79          return instance;
80        }
81      }
82    }
83
84    public override bool CanImportData {
85      get { return true; }
86    }
87    public override GCPData ImportData(string path) {
88      var parser = new Parser();
89      parser.Parse(path);
90      var instance = Load(parser);
91      instance.Name = Path.GetFileName(path);
92      instance.Description = "Loaded from file \"" + path + "\" on " + DateTime.Now.ToString();
93      return instance;
94    }
95
96    private GCPData Load(Parser parser) {
97      var instance = new GCPData();
98      instance.Nodes = parser.Nodes;
99      var adjacencies = new int[parser.Edges, 2];
100      var i = 0;
101      foreach (var a in parser.AdjacencyList) {
102        adjacencies[i, 0] = a.Item1 - 1;
103        adjacencies[i, 1] = a.Item2 - 1;
104        i++;
105      }
106      instance.Adjacencies = adjacencies;
107      return instance;
108    }
109
110    private string GetDescription() {
111      return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + ".";
112    }
113
114    protected virtual string GetResourceName(string fileName) {
115      return Assembly.GetExecutingAssembly().GetManifestResourceNames()
116              .Where(x => Regex.Match(x, @".*\.Data\." + fileName).Success).SingleOrDefault();
117    }
118
119    private Dictionary<string, int> bkq = new Dictionary<string, int> {
120      { "fpsol2.i.1", 65 },
121      { "fpsol2.i.2", 30 },
122      { "fpsol2.i.3", 30 },
123      { "inithx.i.1", 54 },
124      { "inithx.i.2", 31 },
125      { "inithx.i.3", 31 },
126      { "le450_5a", 5 },
127      { "le450_5b", 5 },
128      { "le450_5c", 5 },
129      { "le450_5d", 5 },
130      { "le450_15a", 15 },
131      { "le450_15b", 15 },
132      { "le450_15c", 15 },
133      { "le450_15d", 15 },
134      { "le450_25a", 25 },
135      { "le450_25b", 25 },
136      { "le450_25c", 25 },
137      { "le450_25d", 25 },
138      { "mulsol.i.1", 49 },
139      { "mulsol.i.2", 31 },
140      { "mulsol.i.3", 31 },
141      { "mulsol.i.4", 31 },
142      { "mulsol.i.5", 31 },
143      { "zeroin.i.1", 49 },
144      { "zeroin.i.2", 30 },
145      { "zeroin.i.3", 30 },
146      { "anna", 11 },
147      { "david", 11 },
148      { "homer", 13 },
149      { "huck", 11 },
150      { "jean", 10 },
151      { "games120", 9 },
152      { "miles250", 8 },
153      { "miles500", 20 },
154      { "miles750", 31 },
155      { "miles1000", 42 },
156      { "miles1500", 73 },
157      { "queen5_5", 5 },
158      { "queen6_6", 7 },
159      { "queen7_7", 7 },
160      { "queen8_8", 9 },
161      { "queen8_12", 12 },
162      { "queen9_9", 10 },
163      { "queen11_11", 11 },
164      { "queen13_13", 13 },
165      { "myciel3", 4 },
166      { "myciel4", 5 },
167      { "myciel5", 6 },
168      { "myciel6", 7 },
169      { "myciel7", 8 },
170      { "mugg88_1", 4 },
171      { "mugg88_25", 4 },
172      { "mugg100_1", 4 },
173      { "mugg100_25", 4 },
174      { "1-Insertions_4", 4 },
175      { "2-Insertions_3", 4 },
176      { "2-Insertions_4", 4 },
177      { "3-Insertions_3", 4 },
178      { "4-Insertions_3", 3 },
179      { "qg.order30", 30 },
180      { "qg.order40", 40 },
181      { "qg.order60", 60 },
182      { "qg.order100", 100 }
183    };
184  }
185}
Note: See TracBrowser for help on using the repository browser.