Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.Instances.VehicleRouting/3.4/LiLimFormat/LiLimParser.cs @ 17432

Last change on this file since 17432 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

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