Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2972_PDPRowSelect/HeuristicLab.Problems.Instances.DIMACS/3.3/GcolInstanceProvider.cs @ 16713

Last change on this file since 16713 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 7.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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 GcolParser();
72          parser.Parse(stream);
73          var instance = Load(parser);
74          instance.Name = id.Name;
75          instance.Description += Environment.NewLine + 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 GcolParser();
89      parser.Parse(path);
90      var instance = Load(parser);
91      instance.Name = Path.GetFileName(path);
92      instance.Description += Environment.NewLine + "Loaded from file \"" + path + "\" on " + DateTime.Now.ToString();
93      return instance;
94    }
95
96    private GCPData Load(GcolParser parser) {
97      var instance = new GCPData();
98      instance.Description = parser.Comments;
99      instance.Nodes = parser.Nodes;
100      var adjacencies = new int[parser.Edges, 2];
101      var i = 0;
102      foreach (var a in parser.AdjacencyList) {
103        adjacencies[i, 0] = a.Item1;
104        adjacencies[i, 1] = a.Item2;
105        i++;
106      }
107      instance.Adjacencies = adjacencies;
108      return instance;
109    }
110
111    public override bool CanExportData { get { return true; } }
112    public override void ExportData(GCPData instance, string path) {
113      using (var stream = new StreamWriter(File.Create(path)) { AutoFlush = true }) {
114        stream.WriteLine("c " + instance.Name);
115        foreach (var comment in instance.Description.Split(new[] { Environment.NewLine }, StringSplitOptions.None)) {
116          var c = comment;
117          if (!c.StartsWith("c ")) c = "c " + comment;
118          stream.WriteLine(c);
119        }
120        var edges = instance.Adjacencies.GetLength(0);
121        stream.WriteLine("p edge " + instance.Nodes + " " + edges);
122        for (var i = 0; i < edges; i++) {
123          stream.WriteLine("e " + (instance.Adjacencies[i, 0] + 1) + " " + (instance.Adjacencies[i, 1] + 1));
124        }
125      }
126    }
127
128    private string GetDescription() {
129      return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + ".";
130    }
131
132    protected virtual string GetResourceName(string fileName) {
133      return Assembly.GetExecutingAssembly().GetManifestResourceNames()
134              .Where(x => Regex.Match(x, @".*\.Data\." + fileName).Success).SingleOrDefault();
135    }
136
137    private Dictionary<string, int> bkq = new Dictionary<string, int> {
138      { "fpsol2.i.1", 65 },
139      { "fpsol2.i.2", 30 },
140      { "fpsol2.i.3", 30 },
141      { "inithx.i.1", 54 },
142      { "inithx.i.2", 31 },
143      { "inithx.i.3", 31 },
144      { "le450_5a", 5 },
145      { "le450_5b", 5 },
146      { "le450_5c", 5 },
147      { "le450_5d", 5 },
148      { "le450_15a", 15 },
149      { "le450_15b", 15 },
150      { "le450_15c", 15 },
151      { "le450_15d", 15 },
152      { "le450_25a", 25 },
153      { "le450_25b", 25 },
154      { "le450_25c", 25 },
155      { "le450_25d", 25 },
156      { "mulsol.i.1", 49 },
157      { "mulsol.i.2", 31 },
158      { "mulsol.i.3", 31 },
159      { "mulsol.i.4", 31 },
160      { "mulsol.i.5", 31 },
161      { "zeroin.i.1", 49 },
162      { "zeroin.i.2", 30 },
163      { "zeroin.i.3", 30 },
164      { "anna", 11 },
165      { "david", 11 },
166      { "homer", 13 },
167      { "huck", 11 },
168      { "jean", 10 },
169      { "games120", 9 },
170      { "miles250", 8 },
171      { "miles500", 20 },
172      { "miles750", 31 },
173      { "miles1000", 42 },
174      { "miles1500", 73 },
175      { "queen5_5", 5 },
176      { "queen6_6", 7 },
177      { "queen7_7", 7 },
178      { "queen8_8", 9 },
179      { "queen8_12", 12 },
180      { "queen9_9", 10 },
181      { "queen11_11", 11 },
182      { "queen13_13", 13 },
183      { "myciel3", 4 },
184      { "myciel4", 5 },
185      { "myciel5", 6 },
186      { "myciel6", 7 },
187      { "myciel7", 8 },
188      { "mugg88_1", 4 },
189      { "mugg88_25", 4 },
190      { "mugg100_1", 4 },
191      { "mugg100_25", 4 },
192      { "1-Insertions_4", 4 },
193      { "2-Insertions_3", 4 },
194      { "2-Insertions_4", 4 },
195      { "3-Insertions_3", 4 },
196      { "4-Insertions_3", 3 },
197      { "qg.order30", 30 },
198      { "qg.order40", 40 },
199      { "qg.order60", 60 },
200      { "qg.order100", 100 }
201    };
202  }
203}
Note: See TracBrowser for help on using the repository browser.