Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.Instances.VehicleRouting/3.4/Parsers/SolomonParser.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: 4.2 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 SolomonParser {
29    private string file;
30    private Stream stream;
31    private string problemName;
32    private int cities;
33    private int vehicles;
34    private 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 Vehicles {
49      get {
50        return vehicles;
51      }
52    }
53
54    public double Capacity {
55      get {
56        return capacity;
57      }
58    }
59
60    public double[,] Coordinates {
61      get {
62        double[] x = xCoord.ToArray();
63        double[] y = yCoord.ToArray();
64        double[,] coord = new double[x.Length, 2];
65        for (int i = 0; i < x.Length; i++) {
66          coord[i, 0] = x[i];
67          coord[i, 1] = y[i];
68        }
69        return coord;
70      }
71    }
72
73    public double[] Demands {
74      get {
75        return demand.ToArray();
76      }
77    }
78
79    public double[] Readytimes {
80      get {
81        return readyTime.ToArray();
82      }
83    }
84
85    public double[] Duetimes {
86      get {
87        return dueTime.ToArray();
88      }
89    }
90
91    public double[] Servicetimes {
92      get {
93        return serviceTime.ToArray();
94      }
95    }
96
97    public String ProblemName {
98      get {
99        return problemName;
100      }
101    }
102
103    public SolomonParser() {
104      xCoord = new List<double>();
105      yCoord = new List<double>();
106      demand = new List<double>();
107      readyTime = new List<double>();
108      dueTime = new List<double>();
109      serviceTime = new List<double>();
110    }
111
112    public SolomonParser(string file): this() {
113      this.file = file;
114    }
115
116    public SolomonParser(Stream stream): this() {
117      this.stream = stream;
118    }
119
120    public void Parse() {
121      string line;
122      Regex reg = new Regex(@"-?\d+");
123      MatchCollection m;
124
125      StreamReader reader;
126      if (stream != null)
127        reader = new StreamReader(stream);
128      else
129        reader = new StreamReader(file);
130
131      using (reader) {
132        problemName = reader.ReadLine();
133        for (int i = 0; i < 3; i++) {
134          reader.ReadLine();
135        }
136        line = reader.ReadLine();
137
138        m = reg.Matches(line);
139        if (m.Count != 2)
140          throw new InvalidDataException("File has wrong format!");
141
142        vehicles = int.Parse(m[0].Value);
143        capacity = double.Parse(m[1].Value);
144
145        for (int i = 0; i < 4; i++) {
146          reader.ReadLine();
147        }
148
149        line = reader.ReadLine();
150        while ((line != null) && (line.Length > 5)) {
151          m = reg.Matches(line);
152          if (m.Count != 7) { continue; }
153          xCoord.Add((double)int.Parse(m[1].Value));
154          yCoord.Add((double)int.Parse(m[2].Value));
155          demand.Add((double)int.Parse(m[3].Value));
156          readyTime.Add((double)int.Parse(m[4].Value));
157          double st = (double)int.Parse(m[6].Value);
158          dueTime.Add((double)int.Parse(m[5].Value));
159          serviceTime.Add(st);
160          line = reader.ReadLine();
161        }
162        cities = serviceTime.Count;
163      }
164    }
165  }
166}
Note: See TracBrowser for help on using the repository browser.