Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1955_DynamicVehicleRouting/HeuristicLab.PDPSimulation/3.3/Parsers/TSPLibDynPDPParser.cs @ 17607

Last change on this file since 17607 was 8839, checked in by svonolfe, 12 years ago

Adapted the parser to read the distance matrix (#1955)

File size: 5.5 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.Collections.Generic;
23using System.IO;
24using System.Text.RegularExpressions;
25using System;
26
27namespace HeuristicLab.PDPSimulation {
28  class TSPLibDynPDPParser : DynPDPParser {
29    public TSPLibDynPDPParser(string file)
30      : base(file) {
31    }
32
33    public override void Parse() {
34      string line;
35      Regex reg = new Regex(@"-?(\d+\.\d+|\d+)");
36      MatchCollection m;
37
38      using (var reader = new StreamReader(file)) {
39
40        line = reader.ReadLine();
41        problemName = line.Split(':')[1].Remove(0, 1);
42
43        line = reader.ReadLine();
44        string vehicleLine = problemName = line.Split(':')[1].Remove(0, 1);
45        int vehicleCount = int.Parse(vehicleLine);
46
47        line = reader.ReadLine();
48        string capacityLine = problemName = line.Split(':')[1].Remove(0, 1);
49        int capacity = int.Parse(capacityLine);
50
51        line = reader.ReadLine();
52        string dueTimeLine = problemName = line.Split(':')[1].Remove(0, 1);
53        int dueTime = int.Parse(dueTimeLine);
54
55        List<int> xCoord = new List<int>();
56        List<int> yCoord = new List<int>();
57
58        line = reader.ReadLine();
59        line = reader.ReadLine();
60        m = reg.Matches(line);
61        while (m.Count == 3) {
62          xCoord.Add(int.Parse(m[1].Value, System.Globalization.CultureInfo.InvariantCulture));
63          yCoord.Add(int.Parse(m[2].Value, System.Globalization.CultureInfo.InvariantCulture));
64
65          line = reader.ReadLine();
66          m = reg.Matches(line);
67        }
68
69        for (int i = 0; i < vehicleCount; i++) {
70          Vehicle v = new Vehicle();
71
72          v.xCoord = xCoord[0];
73          v.yCoord = yCoord[0];
74          v.capacity = capacity;
75          v.readyTime = 0;
76          v.dueTime = dueTime;
77
78          vehicles.Add(v);
79        }
80
81        if (line.Contains("EDGE_WEIGHT_TYPE: EXPLICIT")) {
82          line = reader.ReadLine();
83
84          int count = xCoord.Count;
85
86          distanceMatrix.useDistanceMatrix = true;
87          distanceMatrix.coordinates = new int[count, 2];
88          for (int i = 0; i < count; i++) {
89            distanceMatrix.coordinates[i, 0] = xCoord[i];
90            distanceMatrix.coordinates[i, 1] = yCoord[i];
91          }
92
93          distanceMatrix.matrix = new double[count, count];
94
95          line = reader.ReadLine();
96          m = reg.Matches(line);
97
98          int j = 0;
99          while (m.Count == count) {
100            for (int i = 0; i < count; i++) {
101              distanceMatrix.matrix[i, j] = double.Parse(m[i].Value, System.Globalization.CultureInfo.InvariantCulture);             
102            }
103
104            line = reader.ReadLine();
105            m = reg.Matches(line);
106            j++;
107          }
108
109          if (j != count)
110            throw new Exception("Invalid distance matrix definition");
111        }
112
113        line = reader.ReadLine();
114        m = reg.Matches(line);
115
116        while (m.Count == 11) {
117          Order o = new Order();
118
119          int pickupId = int.Parse(m[1].Value, System.Globalization.CultureInfo.InvariantCulture);
120          int deliveryId = int.Parse(m[2].Value, System.Globalization.CultureInfo.InvariantCulture);
121          double pickupTWOpen = double.Parse(m[3].Value, System.Globalization.CultureInfo.InvariantCulture);
122          double pickupTWClose = double.Parse(m[4].Value, System.Globalization.CultureInfo.InvariantCulture);
123          double pickupServiceTime = double.Parse(m[5].Value, System.Globalization.CultureInfo.InvariantCulture);
124          double deliveryTWOpen = double.Parse(m[6].Value, System.Globalization.CultureInfo.InvariantCulture);
125          double deliveryTWClose = double.Parse(m[7].Value, System.Globalization.CultureInfo.InvariantCulture);
126          double deliveryServiceTime = double.Parse(m[8].Value, System.Globalization.CultureInfo.InvariantCulture);
127          double demand = double.Parse(m[9].Value, System.Globalization.CultureInfo.InvariantCulture);
128          double revealed = double.Parse(m[10].Value, System.Globalization.CultureInfo.InvariantCulture);
129
130          o.revealedTime = revealed;
131          o.pickupXCoord = xCoord[pickupId];
132          o.pickupYCoord = yCoord[pickupId];
133          o.deliveryXCoord = xCoord[deliveryId];
134          o.deliveryYCoord = yCoord[deliveryId];
135          o.demand = demand;
136          o.pickupServiceTime = pickupServiceTime;
137          o.pickupReadyTime = pickupTWOpen;
138          o.pickupDueTime = pickupTWClose;
139          o.deliveryServiceTime = deliveryServiceTime;
140          o.deliveryReadyTime = deliveryTWOpen;
141          o.deliveryDueTime = deliveryTWClose;
142
143          orders.Add(o);
144
145          line = reader.ReadLine();
146          m = reg.Matches(line);
147        }
148      }
149    }
150  }
151}
Note: See TracBrowser for help on using the repository browser.