Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.Instances.VehicleRouting/3.4/SolomonFormat/SolomonParser.cs @ 14778

Last change on this file since 14778 was 14504, checked in by pfleck, 8 years ago

#2705 Fixed localization issues in some format providers.

File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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)
113      : this() {
114      this.file = file;
115    }
116
117    public SolomonParser(Stream stream)
118      : this() {
119      this.stream = stream;
120    }
121
122    public void Parse() {
123      string line;
124      Regex reg = new Regex(@"-?\d+");
125      MatchCollection m;
126
127      StreamReader reader;
128      if (stream != null)
129        reader = new StreamReader(stream);
130      else
131        reader = new StreamReader(file);
132
133      using (reader) {
134        problemName = reader.ReadLine();
135        for (int i = 0; i < 3; i++) {
136          reader.ReadLine();
137        }
138        line = reader.ReadLine();
139
140        m = reg.Matches(line);
141        if (m.Count != 2)
142          throw new InvalidDataException("File has wrong format!");
143
144        vehicles = int.Parse(m[0].Value);
145        capacity = double.Parse(m[1].Value, System.Globalization.CultureInfo.InvariantCulture);
146
147        for (int i = 0; i < 4; i++) {
148          reader.ReadLine();
149        }
150
151        line = reader.ReadLine();
152        while ((line != null) && (line.Length > 5)) {
153          m = reg.Matches(line);
154          if (m.Count != 7) { continue; }
155          xCoord.Add((double)int.Parse(m[1].Value));
156          yCoord.Add((double)int.Parse(m[2].Value));
157          demand.Add((double)int.Parse(m[3].Value));
158          readyTime.Add((double)int.Parse(m[4].Value));
159          double st = (double)int.Parse(m[6].Value);
160          dueTime.Add((double)int.Parse(m[5].Value));
161          serviceTime.Add(st);
162          line = reader.ReadLine();
163        }
164        cities = serviceTime.Count;
165      }
166    }
167  }
168}
Note: See TracBrowser for help on using the repository browser.