Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.Instances.VehicleRouting/3.4/Parsers/CordeauParser.cs @ 7881

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

Added VRP test instances (#1177)

File size: 6.8 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         depots = int.Parse(m[3].Value);
163         line = reader.ReadLine();
164
165         for (int i = 0; i < depots; i++) {
166           m = reg.Matches(line);
167           if (m.Count != 2) { continue; }
168
169           routeDueTime.Add(double.Parse(m[0].Value, System.Globalization.CultureInfo.InvariantCulture));
170           capacity.Add(double.Parse(m[1].Value, System.Globalization.CultureInfo.InvariantCulture));
171
172           line = reader.ReadLine();
173         }
174
175         while ((line != null)) {
176           m = reg.Matches(line);
177
178           if (m.Count >= 11) {
179             xCoord.Add(double.Parse(m[1].Value, System.Globalization.CultureInfo.InvariantCulture));
180             yCoord.Add(double.Parse(m[2].Value, System.Globalization.CultureInfo.InvariantCulture));
181             demand.Add((double)int.Parse(m[4].Value, System.Globalization.CultureInfo.InvariantCulture));
182             serviceTime.Add(int.Parse(m[3].Value));
183
184             if (timeWindows) {
185               readyTime.Add(int.Parse(m[m.Count - 2].Value));
186               dueTime.Add(int.Parse(m[m.Count - 1].Value));
187             } else {
188               readyTime.Add(0);
189               dueTime.Add(double.MaxValue);
190             }
191           } else if (m.Count >= 7) {
192             depotXcoord.Add(double.Parse(m[1].Value, System.Globalization.CultureInfo.InvariantCulture));
193             depotYcoord.Add(double.Parse(m[2].Value, System.Globalization.CultureInfo.InvariantCulture));
194
195             if (timeWindows) {
196               depotReadyTime.Add(int.Parse(m[m.Count - 2].Value));
197               depotDueTime.Add(int.Parse(m[m.Count - 1].Value));
198             } else {
199               depotReadyTime.Add(0);
200               depotDueTime.Add(double.MaxValue);
201             }
202           }
203
204           line = reader.ReadLine();
205         }
206
207         for (int i = 0; i < depotDueTime.Count; i++) {
208           if (!timeWindows) {
209             depotDueTime[i] = routeDueTime[i];
210           }
211           if (depotDueTime[i] < double.Epsilon)
212             depotDueTime[i] = double.MaxValue;
213         }
214
215         xCoord.InsertRange(0, depotXcoord);
216         yCoord.InsertRange(0, depotYcoord);
217         readyTime.InsertRange(0, depotReadyTime);
218         dueTime.InsertRange(0, depotDueTime);
219
220         cities = demand.Count;
221         depots = depotXcoord.Count;
222
223         List<double> originalCapacities = new List<double>(capacity);
224         capacity.Clear();
225         for (int i = 0; i < vehicles; i++) {
226           for (int j = 0; j < depots; j++) {
227             capacity.Add(originalCapacities[i]);
228           }
229         }
230         vehicles *= depots;
231       }
232     }
233  }
234}
Note: See TracBrowser for help on using the repository browser.