#region License Information /* HeuristicLab * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System.Collections.Generic; using System.IO; using System.Text.RegularExpressions; using System; namespace HeuristicLab.PDPSimulation { class TSPLibDynPDPParser : DynPDPParser { public TSPLibDynPDPParser(string file) : base(file) { } public override void Parse() { string line; Regex reg = new Regex(@"-?(\d+\.\d+|\d+)"); MatchCollection m; using (var reader = new StreamReader(file)) { line = reader.ReadLine(); problemName = line.Split(':')[1].Remove(0, 1); line = reader.ReadLine(); string vehicleLine = problemName = line.Split(':')[1].Remove(0, 1); int vehicleCount = int.Parse(vehicleLine); line = reader.ReadLine(); string capacityLine = problemName = line.Split(':')[1].Remove(0, 1); int capacity = int.Parse(capacityLine); line = reader.ReadLine(); string dueTimeLine = problemName = line.Split(':')[1].Remove(0, 1); int dueTime = int.Parse(dueTimeLine); List xCoord = new List(); List yCoord = new List(); line = reader.ReadLine(); line = reader.ReadLine(); m = reg.Matches(line); while (m.Count == 3) { xCoord.Add(int.Parse(m[1].Value, System.Globalization.CultureInfo.InvariantCulture)); yCoord.Add(int.Parse(m[2].Value, System.Globalization.CultureInfo.InvariantCulture)); line = reader.ReadLine(); m = reg.Matches(line); } for (int i = 0; i < vehicleCount; i++) { Vehicle v = new Vehicle(); v.xCoord = xCoord[0]; v.yCoord = yCoord[0]; v.capacity = capacity; v.readyTime = 0; v.dueTime = dueTime; vehicles.Add(v); } if (line.Contains("EDGE_WEIGHT_TYPE: EXPLICIT")) { line = reader.ReadLine(); int count = xCoord.Count; distanceMatrix.useDistanceMatrix = true; distanceMatrix.coordinates = new int[count, 2]; for (int i = 0; i < count; i++) { distanceMatrix.coordinates[i, 0] = xCoord[i]; distanceMatrix.coordinates[i, 1] = yCoord[i]; } distanceMatrix.matrix = new double[count, count]; line = reader.ReadLine(); m = reg.Matches(line); int j = 0; while (m.Count == count) { for (int i = 0; i < count; i++) { distanceMatrix.matrix[i, j] = double.Parse(m[i].Value, System.Globalization.CultureInfo.InvariantCulture); } line = reader.ReadLine(); m = reg.Matches(line); j++; } if (j != count) throw new Exception("Invalid distance matrix definition"); } line = reader.ReadLine(); m = reg.Matches(line); while (m.Count == 11) { Order o = new Order(); int pickupId = int.Parse(m[1].Value, System.Globalization.CultureInfo.InvariantCulture); int deliveryId = int.Parse(m[2].Value, System.Globalization.CultureInfo.InvariantCulture); double pickupTWOpen = double.Parse(m[3].Value, System.Globalization.CultureInfo.InvariantCulture); double pickupTWClose = double.Parse(m[4].Value, System.Globalization.CultureInfo.InvariantCulture); double pickupServiceTime = double.Parse(m[5].Value, System.Globalization.CultureInfo.InvariantCulture); double deliveryTWOpen = double.Parse(m[6].Value, System.Globalization.CultureInfo.InvariantCulture); double deliveryTWClose = double.Parse(m[7].Value, System.Globalization.CultureInfo.InvariantCulture); double deliveryServiceTime = double.Parse(m[8].Value, System.Globalization.CultureInfo.InvariantCulture); double demand = double.Parse(m[9].Value, System.Globalization.CultureInfo.InvariantCulture); double revealed = double.Parse(m[10].Value, System.Globalization.CultureInfo.InvariantCulture); o.revealedTime = revealed; o.pickupXCoord = xCoord[pickupId]; o.pickupYCoord = yCoord[pickupId]; o.deliveryXCoord = xCoord[deliveryId]; o.deliveryYCoord = yCoord[deliveryId]; o.demand = demand; o.pickupServiceTime = pickupServiceTime; o.pickupReadyTime = pickupTWOpen; o.pickupDueTime = pickupTWClose; o.deliveryServiceTime = deliveryServiceTime; o.deliveryReadyTime = deliveryTWOpen; o.deliveryDueTime = deliveryTWClose; orders.Add(o); line = reader.ReadLine(); m = reg.Matches(line); } } } } }