Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.Instances.CordeauGQAP/3.3/CordeauGQAPParser.cs @ 7786

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

#1614

  • Fixed plugin dependencies
  • Updated GQAP view
  • Changed instances infrastructure
    • Changed interface types into classes
    • Removed the library specific instance classes
File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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[,] Weights { get; private set; }
32    public double[,] Distances { get; private set; }
33    public double[,] InstallationCosts { get; private set; }
34    public double[] Demands { get; private set; }
35    public double[] Capacities { get; private set; }
36
37    public CordeauGQAPParser() {
38      Reset();
39    }
40
41    public void Reset() {
42      Equipments = 0;
43      Locations = 0;
44      Weights = null;
45      Distances = null;
46      InstallationCosts = null;
47      Demands = null;
48      Capacities = null;
49    }
50
51    public void Parse(string file) {
52      using (var stream = new FileStream(file, FileMode.Open, FileAccess.Read)) {
53        Parse(stream);
54      }
55    }
56
57    /// <summary>
58    /// Reads from the given stream data which is expected to be in the Cordeau's GQAP format.
59    /// </summary>
60    /// <remarks>
61    /// The stream is not closed or disposed. The caller has to take care of that.
62    /// </remarks>
63    /// <param name="stream">The stream to read data from.</param>
64    public void Parse(Stream stream) {
65      var separator = new char[] { ' ', '\t' };
66      var reader = new StreamReader(stream);
67      string line = Continue(reader);
68
69      var info = line.Split(separator, StringSplitOptions.RemoveEmptyEntries);
70      Equipments = int.Parse(info[0]);
71      Locations = int.Parse(info[1]);
72      TransportationCosts = double.Parse(info[2]);
73
74      line = Continue(reader);
75
76      Weights = new double[Equipments, Equipments];
77      for (int i = 0; i < Equipments; i++) {
78        var weightsPerEquipment = line.Split(separator, StringSplitOptions.RemoveEmptyEntries);
79        for (int j = 0; j < Equipments; j++) {
80          Weights[i, j] = double.Parse(weightsPerEquipment[j], CultureInfo.InvariantCulture.NumberFormat);
81        }
82        line = Continue(reader);
83      }
84
85      Distances = new double[Locations, Locations];
86      for (int i = 0; i < Locations; i++) {
87        var distancePerLocation = line.Split(separator, StringSplitOptions.RemoveEmptyEntries);
88        for (int j = 0; j < Locations; j++) {
89          Distances[i, j] = double.Parse(distancePerLocation[j], CultureInfo.InvariantCulture.NumberFormat);
90        }
91        line = Continue(reader);
92      }
93
94      InstallationCosts = new double[Equipments, Locations];
95      for (int i = 0; i < Equipments; i++) {
96        var costsPerEquipment = line.Split(separator, StringSplitOptions.RemoveEmptyEntries);
97        for (int j = 0; j < Locations; j++)
98          InstallationCosts[i, j] = double.Parse(costsPerEquipment[j], CultureInfo.InvariantCulture.NumberFormat);
99        line = Continue(reader);
100      }
101
102      Demands = new double[Equipments];
103      var demandsPerEquipment = line.Split(separator, StringSplitOptions.RemoveEmptyEntries);
104      for (int i = 0; i < Equipments; i++)
105        Demands[i] = double.Parse(demandsPerEquipment[i], CultureInfo.InvariantCulture.NumberFormat);
106
107      line = Continue(reader);
108
109      Capacities = new double[Locations];
110      string[] capacityPerLocation = line.Split(separator, StringSplitOptions.RemoveEmptyEntries);
111      for (int i = 0; i < Locations; i++)
112        Capacities[i] = double.Parse(capacityPerLocation[i], CultureInfo.InvariantCulture.NumberFormat);
113    }
114
115    private static string Continue(StreamReader reader) {
116      string line = String.Empty;
117      while (String.IsNullOrEmpty(line) && !reader.EndOfStream) {
118        line = reader.ReadLine();
119      }
120      return line;
121    }
122  }
123}
Note: See TracBrowser for help on using the repository browser.