Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6854 was 6854, checked in by svonolfe, 13 years ago

Added support for the multi depot CVRP with time windows (#1177)

File size: 5.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.VehicleRouting.Parsers {
28  class CordeauParser {
29    private string file;
30    private string problemName;
31    private int cities;
32    private int depots;
33    private int vehicles;
34    private List<double> capacity;
35    private List<double> xCoord;
36    private List<double> yCoord;
37    private List<double> demand;
38    private List<double> readyTime;
39    private List<double> dueTime;
40    private List<double> serviceTime;
41
42    public int Cities {
43      get {
44        return cities;
45      }
46    }
47
48    public int Depots {
49      get {
50        return depots;
51      }
52    }
53
54    public int Vehicles {
55      get {
56        return vehicles;
57      }
58    }
59
60    public double[] Capacity {
61      get {
62        return capacity.ToArray();
63      }
64    }
65
66    public double[,] Coordinates {
67      get {
68        double[] x = xCoord.ToArray();
69        double[] y = yCoord.ToArray();
70        double[,] coord = new double[x.Length, 2];
71        for (int i = 0; i < x.Length; i++) {
72          coord[i, 0] = x[i];
73          coord[i, 1] = y[i];
74        }
75        return coord;
76      }
77    }
78
79    public double[] Demands {
80      get {
81        return demand.ToArray();
82      }
83    }
84
85    public double[] Readytimes {
86      get {
87        return readyTime.ToArray();
88      }
89    }
90
91    public double[] Duetimes {
92      get {
93        return dueTime.ToArray();
94      }
95    }
96
97    public double[] Servicetimes {
98      get {
99        return serviceTime.ToArray();
100      }
101    }
102
103    public String ProblemName {
104      get {
105        return problemName;
106      }
107    }
108
109    public CordeauParser(string file) {
110      this.file = file;
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 void Parse() {
121      string line;
122      Regex reg = new Regex(@"-?\d+(\.\d+)?");
123      MatchCollection m;
124
125      StreamReader reader = new StreamReader(file);
126
127      problemName = Path.GetFileNameWithoutExtension(file);
128      line = reader.ReadLine();
129
130      m = reg.Matches(line);
131      if (m.Count != 4)
132        throw new InvalidDataException("File has wrong format!");
133
134      bool timeWindows = int.Parse(m[0].Value) == 6;
135      vehicles = int.Parse(m[3].Value);
136      line = reader.ReadLine();
137
138      for (int i = 0; i < vehicles; i++) {
139        m = reg.Matches(line);
140        if (m.Count != 2) { continue; }
141
142        capacity.Add(double.Parse(m[1].Value, System.Globalization.CultureInfo.InvariantCulture));
143
144        line = reader.ReadLine();
145      }
146
147      List<double> depotXcoord = new List<double>();
148      List<double> depotYcoord = new List<double>();
149      List<double> depotReadyTime = new List<double>();
150      List<double> depotDueTime = new List<double>();
151
152      while ((line != null)) {
153        m = reg.Matches(line);
154
155        if (m.Count >= 11) {
156          xCoord.Add(double.Parse(m[1].Value, System.Globalization.CultureInfo.InvariantCulture));
157          yCoord.Add(double.Parse(m[2].Value, System.Globalization.CultureInfo.InvariantCulture));
158          demand.Add((double)int.Parse(m[4].Value, System.Globalization.CultureInfo.InvariantCulture));
159
160          if (timeWindows) {
161            serviceTime.Add(int.Parse(m[3].Value));
162            readyTime.Add(int.Parse(m[m.Count - 2].Value));
163            dueTime.Add(int.Parse(m[m.Count - 1].Value));
164          } else {
165            serviceTime.Add(0);
166            readyTime.Add(0);
167            dueTime.Add(double.MaxValue);
168          }
169        } else if (m.Count >= 7) {
170          depotXcoord.Add(double.Parse(m[1].Value, System.Globalization.CultureInfo.InvariantCulture));
171          depotYcoord.Add(double.Parse(m[2].Value, System.Globalization.CultureInfo.InvariantCulture));
172
173          if (timeWindows) {
174            depotReadyTime.Add(int.Parse(m[m.Count - 2].Value));
175            depotDueTime.Add(int.Parse(m[m.Count - 1].Value));
176          } else {
177            depotReadyTime.Add(0);
178            depotDueTime.Add(double.MaxValue);
179          }
180        }
181               
182        line = reader.ReadLine();
183      }
184
185      xCoord.InsertRange(0, depotXcoord);
186      yCoord.InsertRange(0, depotYcoord);
187      readyTime.InsertRange(0, depotReadyTime);
188      dueTime.InsertRange(0, depotDueTime);
189
190      cities = demand.Count;
191      depots = depotXcoord.Count;
192
193      vehicles *= depots;
194      List<double> originalCapacities = new List<double>(capacity);
195      for (int i = 1; i < depots; i++) {
196        capacity.InsertRange(0, originalCapacities);
197      }
198    }
199
200    internal string OnReset() {
201      throw new NotImplementedException();
202    }
203  }
204}
Note: See TracBrowser for help on using the repository browser.