Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2212 updated copyright year

File size: 4.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.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; }
31    public double BestKnownQuality { get; private set; }
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
52    public void Parse(string file) {
53      using (var stream = new FileStream(file, FileMode.Open, FileAccess.Read)) {
54        Parse(stream);
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>
65    public void Parse(Stream stream) {
66      var separator = new char[] { ' ', '\t' };
67      var reader = new StreamReader(stream);
68      string line = Continue(reader);
69
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]);
74
75      line = Continue(reader);
76      double bkq;
77      if (!double.TryParse(line, out bkq))
78        BestKnownQuality = double.NaN;
79      else {
80        BestKnownQuality = bkq;
81        line = Continue(reader);
82      }
83
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);
89        }
90        line = Continue(reader);
91      }
92
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);
98        }
99        line = Continue(reader);
100      }
101
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      }
109
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);
114
115      line = Continue(reader);
116
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);
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.