Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.Instances.DIMACS/3.3/Parser.cs @ 15050

Last change on this file since 15050 was 15050, checked in by abeham, 7 years ago

#2736:

  • Integrated graph coloring (GCP) into trunk
  • Additional Changes to branch version:
    • Added parameter BestKnownColors in addition to BestKnownQuality
    • Added a best colors for the default instance
    • Adapted Analyze() to output best found solution and fixed tracking of "Best Solution Colors" and "Best Solution Conflicts"
File size: 2.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2017 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;
25
26namespace HeuristicLab.Problems.Instances.DIMACS {
27  public class Parser {
28    public int Nodes { get; private set; }
29    public int Edges { get; private set; }
30    public ICollection<Tuple<int, int>> AdjacencyList { get { return edges; } }
31    private HashSet<Tuple<int, int>> edges;
32
33    public Parser() {
34      Reset();
35    }
36
37    public void Reset() {
38      Nodes = 0;
39      Edges = 0;
40      edges = new HashSet<Tuple<int, int>>();
41    }
42
43    public void Parse(string file) {
44      using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read)) {
45        Parse(stream);
46      }
47    }
48
49    /// <summary>
50    /// Reads from the given stream data which is expected to be in the QAPLIB format.
51    /// </summary>
52    /// <remarks>
53    /// The stream is not closed or disposed. The caller has to take care of that.
54    /// </remarks>
55    /// <param name="stream">The stream to read data from.</param>
56    /// <returns>True if the file was successfully read or false otherwise.</returns>
57    public void Parse(Stream stream) {
58      char[] delim = new char[] { ' ', '\t' };
59      var reader = new StreamReader(stream);
60      var line = reader.ReadLine().Trim();
61      // skip comments
62      while (line.StartsWith("c", StringComparison.InvariantCultureIgnoreCase)) line = reader.ReadLine().Trim();
63
64      // p edge NODES EDGES
65      var split = line.Split(delim, StringSplitOptions.RemoveEmptyEntries);
66      Nodes = int.Parse(split[2]);
67      do {
68        line = reader.ReadLine();
69        if (string.IsNullOrEmpty(line)) break;
70        // e XX YY
71        split = line.Split(delim, StringSplitOptions.RemoveEmptyEntries);
72        var src = int.Parse(split[1]);
73        var tgt = int.Parse(split[2]);
74        Tuple<int, int> e = null;
75        if (src < tgt) e = Tuple.Create(src, tgt);
76        else if (src > tgt) e = Tuple.Create(tgt, src);
77        else continue; // src == tgt
78        if (edges.Add(e)) Edges++;
79      } while (!reader.EndOfStream);
80    }
81  }
82}
Note: See TracBrowser for help on using the repository browser.