Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/09/16 15:56:22 (7 years ago)
Author:
abeham
Message:

#2701:

  • Updated GraphColoringProblem and Problems.Instances
    • Added new fitness function from literature
    • Added DIMACS benchmark instances
  • Updated LinearLinkageEncoding
    • Added HammingSimilarityCalculator
Location:
branches/MemPRAlgorithm/HeuristicLab.Problems.Instances.DIMACS
Files:
2 added
1 copied

Legend:

Unmodified
Added
Removed
  • branches/MemPRAlgorithm/HeuristicLab.Problems.Instances.DIMACS/3.3/Parser.cs

    r14429 r14471  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using System.IO;
    2425
    25 namespace HeuristicLab.Problems.Instances.QAPLIB {
    26   public class QAPLIBParser {
    27     public int Size { get; private set; }
    28     public double[,] Distances { get; private set; }
    29     public double[,] Weights { get; private set; }
     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;
    3032
    31     public QAPLIBParser() {
     33    public Parser() {
    3234      Reset();
    3335    }
    3436
    3537    public void Reset() {
    36       Size = 0;
    37       Distances = null;
    38       Weights = null;
     38      Nodes = 0;
     39      Edges = 0;
     40      edges = new HashSet<Tuple<int, int>>();
    3941    }
    4042
     
    5456    /// <returns>True if the file was successfully read or false otherwise.</returns>
    5557    public void Parse(Stream stream) {
     58      char[] delim = new char[] { ' ', '\t' };
    5659      var reader = new StreamReader(stream);
    57       Size = int.Parse(reader.ReadLine());
    58       Distances = new double[Size, Size];
    59       Weights = new double[Size, Size];
    60       char[] delim = new char[] { ' ', '\t' };
     60      var line = reader.ReadLine().Trim();
     61      // skip comments
     62      while (line.StartsWith("c", StringComparison.InvariantCultureIgnoreCase)) line = reader.ReadLine().Trim();
    6163
    62       Weights = ParseMatrix(reader, delim);
    63       Distances = ParseMatrix(reader, delim);
    64     }
    65 
    66     private double[,] ParseMatrix(StreamReader reader, char[] delim) {
    67       int read = 0, k = 0;
    68       double[,] result = new double[Size, Size];
    69       while (k < Size) {
    70         if (reader.EndOfStream) throw new InvalidDataException("Reached end of stream while reading second matrix.");
    71         string valLine = reader.ReadLine();
    72         while (String.IsNullOrWhiteSpace(valLine)) valLine = reader.ReadLine();
    73         string[] vals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries);
    74         foreach (string val in vals) {
    75           result[k, read++] = double.Parse(val);
    76           if (read == Size) {
    77             read = 0;
    78             k++;
    79           }
    80         }
    81       }
    82       return result;
     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);
    8380    }
    8481  }
Note: See TracChangeset for help on using the changeset viewer.