Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.Instances.VehicleRouting/3.4/CordeauFormat/CordeauParser.cs @ 10864

Last change on this file since 10864 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 6.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.IO;
25using System.Text.RegularExpressions;
26
27namespace HeuristicLab.Problems.Instances.VehicleRouting {
28  class CordeauParser {
29    private string file;
30    private Stream stream;
31    private string problemName;
32    private int cities;
33    private int depots;
34    private int vehicles;
35    private List<double> capacity;
36    private List<double> xCoord;
37    private List<double> yCoord;
38    private List<double> demand;
39    private List<double> readyTime;
40    private List<double> dueTime;
41    private List<double> serviceTime;
42
43    public int Cities {
44      get {
45        return cities;
46      }
47    }
48
49    public int Depots {
50      get {
51        return depots;
52      }
53    }
54
55    public int Vehicles {
56      get {
57        return vehicles;
58      }
59    }
60
61    public double[] Capacity {
62      get {
63        return capacity.ToArray();
64      }
65    }
66
67    public double[,] Coordinates {
68      get {
69        double[] x = xCoord.ToArray();
70        double[] y = yCoord.ToArray();
71        double[,] coord = new double[x.Length, 2];
72        for (int i = 0; i < x.Length; i++) {
73          coord[i, 0] = x[i];
74          coord[i, 1] = y[i];
75        }
76        return coord;
77      }
78    }
79
80    public double[] Demands {
81      get {
82        return demand.ToArray();
83      }
84    }
85
86    public double[] Readytimes {
87      get {
88        return readyTime.ToArray();
89      }
90    }
91
92    public double[] Duetimes {
93      get {
94        return dueTime.ToArray();
95      }
96    }
97
98    public double[] Servicetimes {
99      get {
100        return serviceTime.ToArray();
101      }
102    }
103
104    public String ProblemName {
105      get {
106        return problemName;
107      }
108    }
109
110    public CordeauParser() {
111      capacity = new List<double>();
112      xCoord = new List<double>();
113      yCoord = new List<double>();
114      demand = new List<double>();
115      readyTime = new List<double>();
116      dueTime = new List<double>();
117      serviceTime = new List<double>();
118    }
119
120    public CordeauParser(string file)
121      : this() {
122      this.file = file;
123    }
124
125    public CordeauParser(Stream stream)
126      : this() {
127      this.stream = stream;
128    }
129
130    public void Parse() {
131      string line;
132      Regex reg = new Regex(@"-?\d+(\.\d+)?");
133      MatchCollection m;
134
135      StreamReader reader;
136      if (stream != null) {
137        reader = new StreamReader(stream);
138      } else {
139        reader = new StreamReader(file);
140        problemName = Path.GetFileNameWithoutExtension(file);
141      }
142
143      using (reader) {
144        List<double> depotXcoord = new List<double>();
145        List<double> depotYcoord = new List<double>();
146        List<double> depotReadyTime = new List<double>();
147        List<double> depotDueTime = new List<double>();
148
149        List<double> routeDueTime = new List<double>();
150
151        line = reader.ReadLine();
152
153        m = reg.Matches(line);
154        if (m.Count != 4)
155          throw new InvalidDataException("File has wrong format!");
156
157        int type = int.Parse(m[0].Value);
158        if (type != 2 && type != 6)
159          throw new InvalidDataException("Unsupported instance type");
160
161        bool timeWindows = type == 6;
162        vehicles = int.Parse(m[1].Value);
163        cities = int.Parse(m[2].Value);
164        depots = int.Parse(m[3].Value);
165        line = reader.ReadLine();
166
167        for (int i = 0; i < depots; i++) {
168          m = reg.Matches(line);
169          if (m.Count != 2) { continue; }
170
171          routeDueTime.Add(double.Parse(m[0].Value, System.Globalization.CultureInfo.InvariantCulture));
172          capacity.Add(double.Parse(m[1].Value, System.Globalization.CultureInfo.InvariantCulture));
173
174          line = reader.ReadLine();
175        }
176
177        while ((line != null)) {
178          m = reg.Matches(line);
179
180          if (demand.Count < cities) {
181            xCoord.Add(double.Parse(m[1].Value, System.Globalization.CultureInfo.InvariantCulture));
182            yCoord.Add(double.Parse(m[2].Value, System.Globalization.CultureInfo.InvariantCulture));
183            demand.Add((double)int.Parse(m[4].Value, System.Globalization.CultureInfo.InvariantCulture));
184            serviceTime.Add(int.Parse(m[3].Value));
185
186            if (timeWindows) {
187              readyTime.Add(int.Parse(m[m.Count - 2].Value));
188              dueTime.Add(int.Parse(m[m.Count - 1].Value));
189            } else {
190              readyTime.Add(0);
191              dueTime.Add(double.MaxValue);
192            }
193          } else {
194            depotXcoord.Add(double.Parse(m[1].Value, System.Globalization.CultureInfo.InvariantCulture));
195            depotYcoord.Add(double.Parse(m[2].Value, System.Globalization.CultureInfo.InvariantCulture));
196
197            if (timeWindows) {
198              depotReadyTime.Add(int.Parse(m[m.Count - 2].Value));
199              depotDueTime.Add(int.Parse(m[m.Count - 1].Value));
200            } else {
201              depotReadyTime.Add(0);
202              depotDueTime.Add(double.MaxValue);
203            }
204          }
205
206          line = reader.ReadLine();
207        }
208
209        for (int i = 0; i < depotDueTime.Count; i++) {
210          if (!timeWindows) {
211            depotDueTime[i] = routeDueTime[i];
212          }
213          if (depotDueTime[i] < double.Epsilon)
214            depotDueTime[i] = double.MaxValue;
215        }
216
217        xCoord.InsertRange(0, depotXcoord);
218        yCoord.InsertRange(0, depotYcoord);
219        readyTime.InsertRange(0, depotReadyTime);
220        dueTime.InsertRange(0, depotDueTime);
221
222        List<double> originalCapacities = new List<double>(capacity);
223        capacity.Clear();
224        for (int i = 0; i < depots; i++) {
225          for (int j = 0; j < vehicles; j++) {
226            capacity.Add(originalCapacities[i]);
227          }
228        }
229        vehicles *= depots;
230      }
231    }
232  }
233}
Note: See TracBrowser for help on using the repository browser.