Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Problems.Instances.CordeauGQAP/3.3/CordeauGQAPParser.cs @ 7505

Last change on this file since 7505 was 7505, checked in by abeham, 12 years ago

#1614

  • added instances of Cordeau et al. as given by L. Moccia
  • added operators for tabu search
File size: 5.0 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    public Exception Error { 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      Error = null;
51    }
52
53    public bool Parse(string file) {
54      using (var stream = new FileStream(file, FileMode.Open, FileAccess.Read)) {
55        return Parse(stream);
56      }
57    }
58
59    /// <summary>
60    /// Reads from the given stream data which is expected to be in the Cordeau's GQAP format.
61    /// </summary>
62    /// <remarks>
63    /// The stream is not closed or disposed. The caller has to take care of that.
64    /// </remarks>
65    /// <param name="stream">The stream to read data from.</param>
66    /// <returns>True if the file was successfully read or false otherwise.</returns>
67    public bool Parse(Stream stream) {
68      Error = null;
69      var separator = new char[] { ' ', '\t' };
70      try {
71        StreamReader reader = new StreamReader(stream);
72        string line = Continue(reader);
73
74        var info = line.Split(separator, StringSplitOptions.RemoveEmptyEntries);
75        Equipments = int.Parse(info[0]);
76        Locations = int.Parse(info[1]);
77        TransportationCosts = double.Parse(info[2]);
78
79        line = Continue(reader);
80
81        Weights = new double[Equipments, Equipments];
82        for (int i = 0; i < Equipments; i++) {
83          var weightsPerEquipment = line.Split(separator, StringSplitOptions.RemoveEmptyEntries);
84          for (int j = 0; j < Equipments; j++) {
85            Weights[i, j] = double.Parse(weightsPerEquipment[j], CultureInfo.InvariantCulture.NumberFormat);
86          }
87          line = Continue(reader);
88        }
89
90        Distances = new double[Locations, Locations];
91        for (int i = 0; i < Locations; i++) {
92          var distancePerLocation = line.Split(separator, StringSplitOptions.RemoveEmptyEntries);
93          for (int j = 0; j < Locations; j++) {
94            Distances[i, j] = double.Parse(distancePerLocation[j], CultureInfo.InvariantCulture.NumberFormat);
95          }
96          line = Continue(reader);
97        }
98
99        InstallationCosts = new double[Equipments, Locations];
100        for (int i = 0; i < Equipments; i++) {
101          var costsPerEquipment = line.Split(separator, StringSplitOptions.RemoveEmptyEntries);
102          for (int j = 0; j < Locations; j++)
103            InstallationCosts[i, j] = double.Parse(costsPerEquipment[j], CultureInfo.InvariantCulture.NumberFormat);
104          line = Continue(reader);
105        }
106
107        Demands = new double[Equipments];
108        var demandsPerEquipment = line.Split(separator, StringSplitOptions.RemoveEmptyEntries);
109        for (int i = 0; i < Equipments; i++)
110          Demands[i] = double.Parse(demandsPerEquipment[i], CultureInfo.InvariantCulture.NumberFormat);
111
112        line = Continue(reader);
113
114        Capacities = new double[Locations];
115        string[] capacityPerLocation = line.Split(separator, StringSplitOptions.RemoveEmptyEntries);
116        for (int i = 0; i < Locations; i++)
117          Capacities[i] = double.Parse(capacityPerLocation[i], CultureInfo.InvariantCulture.NumberFormat);
118
119        return true;
120      } catch (Exception e) {
121        Error = e;
122        return false;
123      }
124    }
125
126    private static string Continue(StreamReader reader) {
127      string line = String.Empty;
128      while (String.IsNullOrEmpty(line) && !reader.EndOfStream) {
129        line = reader.ReadLine();
130      }
131      return line;
132    }
133  }
134}
Note: See TracBrowser for help on using the repository browser.