Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.Instances.CordeauGQAP/3.3/CordeauGQAPParser.cs @ 13963

Last change on this file since 13963 was 12009, checked in by ascheibe, 10 years ago

#2212 updated copyright year

File size: 4.8 KB
RevLine 
[7505]1#region License Information
2/* HeuristicLab
[12009]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[7505]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.Globalization;
24using System.IO;
25
26namespace HeuristicLab.Problems.Instances.CordeauGQAP {
27  public class CordeauGQAPParser {
28    public int Equipments { get; private set; }
29    public int Locations { get; private set; }
30    public double TransportationCosts { get; private set; }
[7879]31    public double BestKnownQuality { get; private set; }
[7505]32    public double[,] Weights { get; private set; }
33    public double[,] Distances { get; private set; }
34    public double[,] InstallationCosts { get; private set; }
35    public double[] Demands { get; private set; }
36    public double[] Capacities { get; private set; }
37
38    public CordeauGQAPParser() {
39      Reset();
40    }
41
42    public void Reset() {
43      Equipments = 0;
44      Locations = 0;
45      Weights = null;
46      Distances = null;
47      InstallationCosts = null;
48      Demands = null;
49      Capacities = null;
50    }
51
[7538]52    public void Parse(string file) {
[7505]53      using (var stream = new FileStream(file, FileMode.Open, FileAccess.Read)) {
[7538]54        Parse(stream);
[7505]55      }
56    }
57
58    /// <summary>
59    /// Reads from the given stream data which is expected to be in the Cordeau's GQAP format.
60    /// </summary>
61    /// <remarks>
62    /// The stream is not closed or disposed. The caller has to take care of that.
63    /// </remarks>
64    /// <param name="stream">The stream to read data from.</param>
[7538]65    public void Parse(Stream stream) {
[7505]66      var separator = new char[] { ' ', '\t' };
[7538]67      var reader = new StreamReader(stream);
68      string line = Continue(reader);
[7505]69
[7538]70      var info = line.Split(separator, StringSplitOptions.RemoveEmptyEntries);
71      Equipments = int.Parse(info[0]);
72      Locations = int.Parse(info[1]);
73      TransportationCosts = double.Parse(info[2]);
[7505]74
[7538]75      line = Continue(reader);
[7879]76      double bkq;
77      if (!double.TryParse(line, out bkq))
78        BestKnownQuality = double.NaN;
79      else {
80        BestKnownQuality = bkq;
81        line = Continue(reader);
82      }
[7505]83
[7538]84      Weights = new double[Equipments, Equipments];
85      for (int i = 0; i < Equipments; i++) {
86        var weightsPerEquipment = line.Split(separator, StringSplitOptions.RemoveEmptyEntries);
87        for (int j = 0; j < Equipments; j++) {
88          Weights[i, j] = double.Parse(weightsPerEquipment[j], CultureInfo.InvariantCulture.NumberFormat);
[7505]89        }
[7538]90        line = Continue(reader);
91      }
[7505]92
[7538]93      Distances = new double[Locations, Locations];
94      for (int i = 0; i < Locations; i++) {
95        var distancePerLocation = line.Split(separator, StringSplitOptions.RemoveEmptyEntries);
96        for (int j = 0; j < Locations; j++) {
97          Distances[i, j] = double.Parse(distancePerLocation[j], CultureInfo.InvariantCulture.NumberFormat);
[7505]98        }
[7538]99        line = Continue(reader);
100      }
[7505]101
[7538]102      InstallationCosts = new double[Equipments, Locations];
103      for (int i = 0; i < Equipments; i++) {
104        var costsPerEquipment = line.Split(separator, StringSplitOptions.RemoveEmptyEntries);
105        for (int j = 0; j < Locations; j++)
106          InstallationCosts[i, j] = double.Parse(costsPerEquipment[j], CultureInfo.InvariantCulture.NumberFormat);
107        line = Continue(reader);
108      }
[7505]109
[7538]110      Demands = new double[Equipments];
111      var demandsPerEquipment = line.Split(separator, StringSplitOptions.RemoveEmptyEntries);
112      for (int i = 0; i < Equipments; i++)
113        Demands[i] = double.Parse(demandsPerEquipment[i], CultureInfo.InvariantCulture.NumberFormat);
[7505]114
[7538]115      line = Continue(reader);
[7505]116
[7538]117      Capacities = new double[Locations];
118      string[] capacityPerLocation = line.Split(separator, StringSplitOptions.RemoveEmptyEntries);
119      for (int i = 0; i < Locations; i++)
120        Capacities[i] = double.Parse(capacityPerLocation[i], CultureInfo.InvariantCulture.NumberFormat);
[7505]121    }
122
123    private static string Continue(StreamReader reader) {
124      string line = String.Empty;
125      while (String.IsNullOrEmpty(line) && !reader.EndOfStream) {
126        line = reader.ReadLine();
127      }
128      return line;
129    }
130  }
131}
Note: See TracBrowser for help on using the repository browser.