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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.IO;
|
---|
25 | using System.Text.RegularExpressions;
|
---|
26 |
|
---|
27 | namespace 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): this() {
|
---|
82 | this.file = file;
|
---|
83 | }
|
---|
84 |
|
---|
85 | public TaillardParser(Stream stream)
|
---|
86 | : this() {
|
---|
87 | this.stream = stream;
|
---|
88 | }
|
---|
89 |
|
---|
90 | public void Parse() {
|
---|
91 | string line;
|
---|
92 | Regex reg = new Regex(@"-?\d+(\.\d+)?");
|
---|
93 | MatchCollection m;
|
---|
94 |
|
---|
95 | StreamReader reader;
|
---|
96 | if (stream != null) {
|
---|
97 | reader = new StreamReader(stream);
|
---|
98 | } else {
|
---|
99 | reader = new StreamReader(file);
|
---|
100 | problemName = Path.GetFileNameWithoutExtension(file);
|
---|
101 | }
|
---|
102 |
|
---|
103 | using (reader) {
|
---|
104 | line = reader.ReadLine();
|
---|
105 | m = reg.Matches(line);
|
---|
106 | if (m.Count != 2)
|
---|
107 | throw new InvalidDataException("File has wrong format!");
|
---|
108 |
|
---|
109 | cities = int.Parse(m[0].Value);
|
---|
110 |
|
---|
111 | line = reader.ReadLine();
|
---|
112 | m = reg.Matches(line);
|
---|
113 | capacity = int.Parse(m[0].Value);
|
---|
114 |
|
---|
115 | line = reader.ReadLine();
|
---|
116 | m = reg.Matches(line);
|
---|
117 | xCoord.Add(double.Parse(m[0].Value, System.Globalization.CultureInfo.InvariantCulture));
|
---|
118 | yCoord.Add(double.Parse(m[1].Value, System.Globalization.CultureInfo.InvariantCulture));
|
---|
119 | demand.Add(0);
|
---|
120 |
|
---|
121 | for (int i = 0; i < cities; i++) {
|
---|
122 | line = reader.ReadLine();
|
---|
123 | m = reg.Matches(line);
|
---|
124 | xCoord.Add(double.Parse(m[1].Value, System.Globalization.CultureInfo.InvariantCulture));
|
---|
125 | yCoord.Add(double.Parse(m[2].Value, System.Globalization.CultureInfo.InvariantCulture));
|
---|
126 | demand.Add(int.Parse(m[3].Value));
|
---|
127 | }
|
---|
128 | }
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|