Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.Instances.VehicleRouting/3.4/TaillardFormat/TaillardParser.cs @ 12371

Last change on this file since 12371 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 3.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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 TaillardParser {
29    private string file;
30    private Stream stream;
31    private string problemName;
32    private int cities;
33    private double capacity;
34    private List<double> xCoord;
35    private List<double> yCoord;
36    private List<double> demand;
37
38    public int Cities {
39      get {
40        return cities;
41      }
42    }
43
44    public double Capacity {
45      get {
46        return capacity;
47      }
48    }
49
50    public double[,] Coordinates {
51      get {
52        double[] x = xCoord.ToArray();
53        double[] y = yCoord.ToArray();
54        double[,] coord = new double[x.Length, 2];
55        for (int i = 0; i < x.Length; i++) {
56          coord[i, 0] = x[i];
57          coord[i, 1] = y[i];
58        }
59        return coord;
60      }
61    }
62
63    public double[] Demands {
64      get {
65        return demand.ToArray();
66      }
67    }
68
69    public String ProblemName {
70      get {
71        return problemName;
72      }
73    }
74
75    public TaillardParser() {
76      xCoord = new List<double>();
77      yCoord = new List<double>();
78      demand = new List<double>();
79    }
80
81    public TaillardParser(string file)
82      : this() {
83      this.file = file;
84    }
85
86    public TaillardParser(Stream stream)
87      : this() {
88      this.stream = stream;
89    }
90
91    public void Parse() {
92      string line;
93      Regex reg = new Regex(@"-?\d+(\.\d+)?");
94      MatchCollection m;
95
96      StreamReader reader;
97      if (stream != null) {
98        reader = new StreamReader(stream);
99      } else {
100        reader = new StreamReader(file);
101        problemName = Path.GetFileNameWithoutExtension(file);
102      }
103
104      using (reader) {
105        line = reader.ReadLine();
106        m = reg.Matches(line);
107        if (m.Count != 2)
108          throw new InvalidDataException("File has wrong format!");
109
110        cities = int.Parse(m[0].Value);
111
112        line = reader.ReadLine();
113        m = reg.Matches(line);
114        capacity = int.Parse(m[0].Value);
115
116        line = reader.ReadLine();
117        m = reg.Matches(line);
118        xCoord.Add(double.Parse(m[0].Value, System.Globalization.CultureInfo.InvariantCulture));
119        yCoord.Add(double.Parse(m[1].Value, System.Globalization.CultureInfo.InvariantCulture));
120        demand.Add(0);
121
122        for (int i = 0; i < cities; i++) {
123          line = reader.ReadLine();
124          m = reg.Matches(line);
125          xCoord.Add(double.Parse(m[1].Value, System.Globalization.CultureInfo.InvariantCulture));
126          yCoord.Add(double.Parse(m[2].Value, System.Globalization.CultureInfo.InvariantCulture));
127          demand.Add(int.Parse(m[3].Value));
128        }
129      }
130    }
131  }
132}
Note: See TracBrowser for help on using the repository browser.