Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone2/sources/HeuristicLab.Assignment.QAP/3.2/QAPParser.cs @ 6703

Last change on this file since 6703 was 1529, checked in by gkronber, 16 years ago

Moved source files of plugins AdvancedOptimizationFrontEnd ... Grid into version-specific sub-folders. #576

File size: 2.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Text;
25using System.IO;
26
27namespace HeuristicLab.Assignment.QAP {
28  public class QAPParser {
29    public int facilities;
30    public double[,] distances;
31    public double[,] weights;
32   
33    public QAPParser() {
34    }
35
36    public bool Parse(string file) {
37      try {
38        StreamReader reader = new StreamReader(file);
39        facilities = int.Parse(reader.ReadLine());
40        distances = new double[facilities, facilities];
41        weights = new double[facilities, facilities];
42        reader.ReadLine();
43        char[] delim = new char[] {' '};
44        for (int i = 0; i < facilities; i++) {
45          string valLine = reader.ReadLine();
46          string[] vals = new string[facilities];
47          string[] partVals =  valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries);
48          partVals.CopyTo(vals, 0);
49          int index = partVals.Length;
50          while (index < facilities) {
51            valLine = reader.ReadLine();
52            partVals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries);
53            partVals.CopyTo(vals, index);
54            index += partVals.Length;
55          }
56          for (int j = 0; j < facilities; j++) {
57            distances[i, j] = double.Parse(vals[j]);
58          }
59        }
60        reader.ReadLine();
61        int read = 0;
62        int k = 0;
63        while (!reader.EndOfStream) {
64          string valLine = reader.ReadLine();
65          string[] vals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries);
66          for (int j = 0; j < vals.Length; j++) {
67            if (read + j == facilities) {
68              read = 0;
69              k++;
70            }
71            weights[k, read + j] = double.Parse(vals[j]);
72          }
73          read += vals.Length;
74        }
75        return true;
76      } catch (Exception) {
77        // not good
78        return false;
79      }
80
81    }
82  }
83}
Note: See TracBrowser for help on using the repository browser.