Free cookie consent management tool by TermsFeed Policy Generator

source: branches/QAP/HeuristicLab.Problems.QuadraticAssignment/3.3/Parsers/QAPLIBParser.cs @ 5838

Last change on this file since 5838 was 5838, checked in by abeham, 13 years ago

#1330

  • Renamed the DistanceMatrix parameter to Distances
  • Renamed the SwapMove to Swap2Move and renamed operators accordingly
  • Integrated changes in HeuristicLab.Analysis.Views regarding description text box
File size: 3.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.QuadraticAssignment {
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    public Exception Error { get; private set; }
31
32    public QAPLIBParser() {
33      Reset();
34    }
35
36    public void Reset() {
37      Size = 0;
38      Distances = null;
39      Weights = null;
40      Error = null;
41    }
42
43    public bool Parse(string file) {
44      using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read)) {
45        return 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 bool Parse(Stream stream) {
58      Error = null;
59      try {
60        StreamReader reader = new StreamReader(stream);
61        Size = int.Parse(reader.ReadLine());
62        Distances = new double[Size, Size];
63        Weights = new double[Size, Size];
64        string valLine = reader.ReadLine();
65        char[] delim = new char[] { ' ' };
66        for (int i = 0; i < Size; i++) {
67          if (i > 0 || String.IsNullOrWhiteSpace(valLine))
68            valLine = reader.ReadLine();
69          string[] vals = new string[Size];
70          string[] partVals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries);
71          partVals.CopyTo(vals, 0);
72          int index = partVals.Length;
73          while (index < Size) {
74            valLine = reader.ReadLine();
75            partVals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries);
76            partVals.CopyTo(vals, index);
77            index += partVals.Length;
78          }
79          for (int j = 0; j < Size; j++) {
80            Distances[i, j] = double.Parse(vals[j]);
81          }
82        }
83        valLine = reader.ReadLine();
84        int read = 0;
85        int k = 0;
86        while (!reader.EndOfStream) {
87          if (read > 0 || String.IsNullOrWhiteSpace(valLine))
88            valLine = reader.ReadLine();
89          string[] vals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries);
90          for (int j = 0; j < vals.Length; j++) {
91            if (read + j == Size) {
92              read = 0;
93              k++;
94            }
95            Weights[k, read + j] = double.Parse(vals[j]);
96          }
97          read += vals.Length;
98        }
99        return true;
100      } catch (Exception e) {
101        Error = e;
102        return false;
103      }
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.