Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DynamicVehicleRouting/HeuristicLab.PDPSimulation/3.3/Parsers/TSPLibDynPDPParser.cs @ 10194

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

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

File size: 5.5 KB
RevLine 
[8670]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;
[8839]25using System;
[8670]26
27namespace HeuristicLab.PDPSimulation {
[8675]28  class TSPLibDynPDPParser : DynPDPParser {
29    public TSPLibDynPDPParser(string file)
30      : base(file) {
[8670]31    }
32
33    public override void Parse() {
34      string line;
[8675]35      Regex reg = new Regex(@"-?(\d+\.\d+|\d+)");
[8670]36      MatchCollection m;
37
[8675]38      using (var reader = new StreamReader(file)) {
[8670]39
[8675]40        line = reader.ReadLine();
41        problemName = line.Split(':')[1].Remove(0, 1);
[8670]42
[8675]43        line = reader.ReadLine();
44        string vehicleLine = problemName = line.Split(':')[1].Remove(0, 1);
45        int vehicleCount = int.Parse(vehicleLine);
[8670]46
[8675]47        line = reader.ReadLine();
48        string capacityLine = problemName = line.Split(':')[1].Remove(0, 1);
49        int capacity = int.Parse(capacityLine);
[8670]50
[8675]51        line = reader.ReadLine();
52        string dueTimeLine = problemName = line.Split(':')[1].Remove(0, 1);
53        int dueTime = int.Parse(dueTimeLine);
[8670]54
[8839]55        List<int> xCoord = new List<int>();
56        List<int> yCoord = new List<int>();
[8670]57
58        line = reader.ReadLine();
[8675]59        line = reader.ReadLine();
[8670]60        m = reg.Matches(line);
[8675]61        while (m.Count == 3) {
[8839]62          xCoord.Add(int.Parse(m[1].Value, System.Globalization.CultureInfo.InvariantCulture));
63          yCoord.Add(int.Parse(m[2].Value, System.Globalization.CultureInfo.InvariantCulture));
[8670]64
[8675]65          line = reader.ReadLine();
66          m = reg.Matches(line);
67        }
[8670]68
[8675]69        for (int i = 0; i < vehicleCount; i++) {
70          Vehicle v = new Vehicle();
[8670]71
[8675]72          v.xCoord = xCoord[0];
73          v.yCoord = yCoord[0];
74          v.capacity = capacity;
75          v.readyTime = 0;
76          v.dueTime = dueTime;
[8670]77
[8675]78          vehicles.Add(v);
79        }
[8670]80
[8839]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
[8675]113        line = reader.ReadLine();
114        m = reg.Matches(line);
[8670]115
[8675]116        while (m.Count == 11) {
117          Order o = new Order();
[8670]118
[8795]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);
[8670]129
[8675]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;
[8670]142
[8675]143          orders.Add(o);
144
145          line = reader.ReadLine();
146          m = reg.Matches(line);
147        }
[8670]148      }
149    }
150  }
151}
Note: See TracBrowser for help on using the repository browser.