Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/01/12 15:15:22 (12 years ago)
Author:
abeham
Message:

#1614

  • Fixed plugin dependencies
  • Updated GQAP view
  • Changed instances infrastructure
    • Changed interface types into classes
    • Removed the library specific instance classes
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/GeneralizedQAP/HeuristicLab.Problems.Instances.QAPLIB/3.3/QAPLIBParser.cs

    r7445 r7538  
    2828    public double[,] Distances { get; private set; }
    2929    public double[,] Weights { get; private set; }
    30     public Exception Error { get; private set; }
    3130
    3231    public QAPLIBParser() {
     
    3837      Distances = null;
    3938      Weights = null;
    40       Error = null;
    4139    }
    4240
    43     public bool Parse(string file) {
     41    public void Parse(string file) {
    4442      using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read)) {
    45         return Parse(stream);
     43        Parse(stream);
    4644      }
    4745    }
     
    5553    /// <param name="stream">The stream to read data from.</param>
    5654    /// <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 = null;
    65         char[] delim = new char[] { ' ' };
    66         int read = 0;
    67         int k = 0;
    68         while (k < Size) {
    69           if (reader.EndOfStream) throw new InvalidDataException("Reached end of stream while reading first matrix.");
    70           valLine = reader.ReadLine();
    71           while (String.IsNullOrWhiteSpace(valLine)) valLine = reader.ReadLine();
    72           string[] vals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries);
    73           foreach (string val in vals) {
    74             Weights[k, read++] = double.Parse(val);
    75             if (read == Size) {
    76               read = 0;
    77               k++;
    78             }
     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[] { ' ' };
     61
     62      Distances = ParseMatrix(reader, delim);
     63      Weights = 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          Distances[k, read++] = double.Parse(val);
     76          if (read == Size) {
     77            read = 0;
     78            k++;
    7979          }
    8080        }
    81 
    82         read = 0;
    83         k = 0;
    84 
    85         while (k < Size) {
    86           if (reader.EndOfStream) throw new InvalidDataException("Reached end of stream while reading second matrix.");
    87           valLine = reader.ReadLine();
    88           while (String.IsNullOrWhiteSpace(valLine)) valLine = reader.ReadLine();
    89           string[] vals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries);
    90           foreach (string val in vals) {
    91             Distances[k, read++] = double.Parse(val);
    92             if (read == Size) {
    93               read = 0;
    94               k++;
    95             }
    96           }
    97         }
    98         return true;
    99       } catch (Exception e) {
    100         Error = e;
    101         return false;
    10281      }
     82      return result;
    10383    }
    10484  }
Note: See TracChangeset for help on using the changeset viewer.