Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.Instances.VehicleRouting/3.4/CordeauFormat/CordeauParser.cs @ 7891

Last change on this file since 7891 was 7891, checked in by svonolfe, 12 years ago

Fixed issues in Cordeau parser (#1177)

File size: 6.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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): this() {
121      this.file = file;
122    }
123
124     public CordeauParser(Stream stream)
125       : this() {
126      this.stream = stream;
127    }
128
129     public void Parse() {
130       string line;
131       Regex reg = new Regex(@"-?\d+(\.\d+)?");
132       MatchCollection m;
133
134       StreamReader reader;
135       if (stream != null) {
136         reader = new StreamReader(stream);
137       } else {
138         reader = new StreamReader(file);
139         problemName = Path.GetFileNameWithoutExtension(file);
140       }
141
142       using (reader) {
143         List<double> depotXcoord = new List<double>();
144         List<double> depotYcoord = new List<double>();
145         List<double> depotReadyTime = new List<double>();
146         List<double> depotDueTime = new List<double>();
147         
148         List<double> routeDueTime = new List<double>();
149
150         line = reader.ReadLine();
151
152         m = reg.Matches(line);
153         if (m.Count != 4)
154           throw new InvalidDataException("File has wrong format!");
155
156         int type = int.Parse(m[0].Value);
157         if(type != 2 && type != 6)
158           throw new InvalidDataException("Unsupported instance type");
159
160         bool timeWindows = type == 6;
161         vehicles = int.Parse(m[1].Value);
162         cities = int.Parse(m[2].Value);
163         depots = int.Parse(m[3].Value);
164         line = reader.ReadLine();
165
166         for (int i = 0; i < depots; i++) {
167           m = reg.Matches(line);
168           if (m.Count != 2) { continue; }
169
170           routeDueTime.Add(double.Parse(m[0].Value, System.Globalization.CultureInfo.InvariantCulture));
171           capacity.Add(double.Parse(m[1].Value, System.Globalization.CultureInfo.InvariantCulture));
172
173           line = reader.ReadLine();
174         }
175
176         while ((line != null)) {
177           m = reg.Matches(line);
178
179           if (demand.Count < cities) {
180             xCoord.Add(double.Parse(m[1].Value, System.Globalization.CultureInfo.InvariantCulture));
181             yCoord.Add(double.Parse(m[2].Value, System.Globalization.CultureInfo.InvariantCulture));
182             demand.Add((double)int.Parse(m[4].Value, System.Globalization.CultureInfo.InvariantCulture));
183             serviceTime.Add(int.Parse(m[3].Value));
184
185             if (timeWindows) {
186               readyTime.Add(int.Parse(m[m.Count - 2].Value));
187               dueTime.Add(int.Parse(m[m.Count - 1].Value));
188             } else {
189               readyTime.Add(0);
190               dueTime.Add(double.MaxValue);
191             }
192           } else {
193             depotXcoord.Add(double.Parse(m[1].Value, System.Globalization.CultureInfo.InvariantCulture));
194             depotYcoord.Add(double.Parse(m[2].Value, System.Globalization.CultureInfo.InvariantCulture));
195
196             if (timeWindows) {
197               depotReadyTime.Add(int.Parse(m[m.Count - 2].Value));
198               depotDueTime.Add(int.Parse(m[m.Count - 1].Value));
199             } else {
200               depotReadyTime.Add(0);
201               depotDueTime.Add(double.MaxValue);
202             }
203           }
204
205           line = reader.ReadLine();
206         }
207
208         for (int i = 0; i < depotDueTime.Count; i++) {
209           if (!timeWindows) {
210             depotDueTime[i] = routeDueTime[i];
211           }
212           if (depotDueTime[i] < double.Epsilon)
213             depotDueTime[i] = double.MaxValue;
214         }
215
216         xCoord.InsertRange(0, depotXcoord);
217         yCoord.InsertRange(0, depotYcoord);
218         readyTime.InsertRange(0, depotReadyTime);
219         dueTime.InsertRange(0, depotDueTime);
220
221         List<double> originalCapacities = new List<double>(capacity);
222         capacity.Clear();
223         for (int i = 0; i < depots; i++) {
224           for (int j = 0; j < vehicles; j++) {
225             capacity.Add(originalCapacities[i]);
226           }
227         }
228         vehicles *= depots;
229       }
230     }
231  }
232}
Note: See TracBrowser for help on using the repository browser.