Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.Instances.QAPLIB/3.3/QAPLIBParser.cs @ 17507

Last change on this file since 17507 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 2.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.IO;
24
25namespace 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; }
30
31    public QAPLIBParser() {
32      Reset();
33    }
34
35    public void Reset() {
36      Size = 0;
37      Distances = null;
38      Weights = null;
39    }
40
41    public void Parse(string file) {
42      using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read)) {
43        Parse(stream);
44      }
45    }
46
47    /// <summary>
48    /// Reads from the given stream data which is expected to be in the QAPLIB format.
49    /// </summary>
50    /// <remarks>
51    /// The stream is not closed or disposed. The caller has to take care of that.
52    /// </remarks>
53    /// <param name="stream">The stream to read data from.</param>
54    /// <returns>True if the file was successfully read or false otherwise.</returns>
55    public void Parse(Stream stream) {
56      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' };
61
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;
83    }
84  }
85}
Note: See TracBrowser for help on using the repository browser.