Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.Instances.VehicleRouting/3.4/LiLimFormat/LiLimParser.cs @ 7898

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

Incorporated review comments of abeham (#1177)

File size: 4.6 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 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): this() {
121      this.file = file;
122    }
123
124     public LiLimParser(Stream stream): this() {
125      this.stream = stream;
126    }
127
128     public void Parse() {
129       string line;
130       Regex reg = new Regex(@"-?\d+");
131       MatchCollection m;
132
133       StreamReader reader;
134       if (stream != null) {
135         reader = new StreamReader(stream);   
136       }
137       else {
138         reader = new StreamReader(file);
139         problemName = Path.GetFileNameWithoutExtension(file);
140       }
141
142       using (reader) {
143         line = reader.ReadLine();
144
145         m = reg.Matches(line);
146         if (m.Count != 3)
147           throw new InvalidDataException("File has wrong format!");
148
149         vehicles = int.Parse(m[0].Value);
150         capacity = double.Parse(m[1].Value);
151
152         line = reader.ReadLine();
153         while ((line != null) && (line.Length > 5)) {
154           m = reg.Matches(line);
155           if (m.Count != 9) { continue; }
156           xCoord.Add((double)int.Parse(m[1].Value));
157           yCoord.Add((double)int.Parse(m[2].Value));
158           demand.Add((double)int.Parse(m[3].Value));
159           readyTime.Add((double)int.Parse(m[4].Value));
160           double st = (double)int.Parse(m[6].Value);
161           dueTime.Add((double)int.Parse(m[5].Value));
162           serviceTime.Add(st);
163
164           int location = int.Parse(m[7].Value);
165           if (location == 0)
166             location = int.Parse(m[8].Value);
167           pickipDeliveryLocation.Add(location);
168
169           line = reader.ReadLine();
170         }
171         cities = serviceTime.Count;
172       }
173     }
174  }
175}
Note: See TracBrowser for help on using the repository browser.