Changeset 4317 for branches/VRP/HeuristicLab.Problems.VehicleRouting
- Timestamp:
- 08/25/10 16:29:06 (14 years ago)
- Location:
- branches/VRP/HeuristicLab.Problems.VehicleRouting/3.3
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.3/ORLIBParser.cs
r4315 r4317 25 25 using System.Text; 26 26 using System.IO; 27 using System.Globalization; 27 28 28 29 namespace HeuristicLab.Problems.VehicleRouting { 29 30 class ORLIBParser { 30 31 private StreamReader source; 32 CultureInfo culture = new CultureInfo("en-US"); 31 33 32 34 private string name; … … 101 103 line = source.ReadLine(); 102 104 tokens = line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); 103 vertices[0, 0] = double.Parse(tokens[0] );104 vertices[0, 1] = double.Parse(tokens[1] );105 vertices[0, 0] = double.Parse(tokens[0], culture.NumberFormat); 106 vertices[0, 1] = double.Parse(tokens[1], culture.NumberFormat); 105 107 106 108 for (int i = 0; i < customers; i++) { 107 109 line = source.ReadLine(); 108 110 tokens = line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); 109 vertices[i + 1, 0] = double.Parse(tokens[0] );110 vertices[i + 1, 1] = double.Parse(tokens[1] );111 demands[i + 1] = double.Parse(tokens[2] );111 vertices[i + 1, 0] = double.Parse(tokens[0], culture.NumberFormat); 112 vertices[i + 1, 1] = double.Parse(tokens[1], culture.NumberFormat); 113 demands[i + 1] = double.Parse(tokens[2], culture.NumberFormat); 112 114 } 113 115 } -
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.3/TSPLIBParser.cs
r4232 r4317 26 26 namespace HeuristicLab.Problems.VehicleRouting { 27 27 /// <summary> 28 /// Parses a *. tsp file in TSPLIB format and extracts its information about a TSP.28 /// Parses a *.vrp file in TSPLIB format and extracts its information about a VRP. 29 29 /// </summary> 30 30 public class TSPLIBParser { … … 43 43 private const int DIM = 4; 44 44 private const int WEIGHTTYPE = 5; 45 private const int WEIGHTFORMAT = 11; 45 46 private const int NODETYPE = 6; 46 47 private const int NODESECTION = 7; 47 48 private const int CAPACITY = 8; 49 private const int DISTANCE = 12; 50 private const int VEHICLES = 13; 48 51 private const int DEPOTSECTION = 9; 49 52 private const int DEMANDSECTION = 10; 50 53 51 54 private StreamReader source; 55 private bool alternateFormat; 56 CultureInfo culture = new CultureInfo("en-US"); 52 57 53 58 private string name; 54 59 /// <summary> 55 /// Gets the name of the parsed TSP.60 /// Gets the name of the parsed VRP. 56 61 /// </summary> 57 62 public string Name { … … 60 65 private string comment; 61 66 /// <summary> 62 /// Gets the comment of the parsed TSP.67 /// Gets the comment of the parsed VRP. 63 68 /// </summary> 64 69 public string Comment { … … 67 72 private double[,] vertices; 68 73 /// <summary> 69 /// Gets the vertices of the parsed TSP.74 /// Gets the vertices of the parsed VRP. 70 75 /// </summary> 71 76 public double[,] Vertices { … … 74 79 private TSPLIBEdgeWeightType weightType; 75 80 /// <summary> 76 /// Gets the weight type of the parsed TSP.81 /// Gets the weight format of the parsed VRP. 77 82 /// </summary> 78 83 public TSPLIBEdgeWeightType WeightType { … … 87 92 } 88 93 94 private double distance; 95 public double Distance { 96 get { 97 return distance; 98 } 99 } 100 101 private int vehicles; 102 public int Vehicles { 103 get { 104 return vehicles; 105 } 106 } 107 89 108 private int depot; 90 109 public int Depot { … … 104 123 /// Initializes a new instance of <see cref="TSPLIBParser"/> with the given <paramref name="path"/>. 105 124 /// </summary> 106 /// <exception cref="ArgumentException">Thrown if the input file is not a TSPLIB TSP file (*. tsp)125 /// <exception cref="ArgumentException">Thrown if the input file is not a TSPLIB TSP file (*.vrp) 107 126 /// </exception> 108 /// <param name="path">The path where the TSP is stored.</param>127 /// <param name="path">The path where the VRP is stored.</param> 109 128 public TSPLIBParser(String path) { 110 129 if (!path.EndsWith(".vrp")) 111 throw new ArgumentException("Input file has to be a TSPLIB CVRP file (*. tsp).");112 113 source = n ew StreamReader(path);130 throw new ArgumentException("Input file has to be a TSPLIB CVRP file (*.vrp, *.txt)."); 131 132 source = null; 114 133 name = path; 115 134 comment = string.Empty; … … 117 136 weightType = TSPLIBEdgeWeightType.UNDEFINED; 118 137 capacity = -1; 138 distance = -1; 139 vehicles = -1; 119 140 depot = -1; 120 141 demands = null; 121 } 122 123 /// <summary> 124 /// Reads the TSPLIB TSP file and parses the elements. 142 alternateFormat = false; 143 } 144 145 /// <summary> 146 /// Reads the TSPLIB VRP file and parses the elements. 125 147 /// </summary> 126 148 /// <exception cref="InvalidDataException">Thrown if the file has an invalid format or contains invalid data.</exception> 127 149 public void Parse() { 128 int section = -1; 129 string str = null; 130 bool typeIsChecked = false; 131 bool weightTypeIsChecked = false; 132 133 do { 134 str = source.ReadLine(); 135 section = GetSection(str); 136 137 if (section != -1) { 138 switch (section) { 139 case NAME: 140 ReadName(str); 141 break; 142 case TYPE: 143 CheckType(str); 144 typeIsChecked = true; 145 break; 146 case COMMENT: 147 ReadComment(str); 148 break; 149 case DIM: 150 InitArrays(str); 151 break; 152 case WEIGHTTYPE: 153 ReadWeightType(str); 154 weightTypeIsChecked = true; 155 break; 156 case NODETYPE: 157 CheckNodeType(str); 158 break; 159 case NODESECTION: 160 ReadVertices(); 161 break; 162 case CAPACITY: 163 ReadCapacity(str); 164 break; 165 case DEPOTSECTION: 166 ReadDepot(); 167 break; 168 case DEMANDSECTION: 169 ReadDemands(); 170 break; 150 using (source = new StreamReader(name)) { 151 int section = -1; 152 string str = null; 153 bool typeIsChecked = false; 154 bool weightTypeIsChecked = false; 155 bool weightFormatIsChecked = false; 156 157 do { 158 str = source.ReadLine(); 159 section = GetSection(str); 160 161 if (section != -1) { 162 switch (section) { 163 case NAME: 164 ReadName(str); 165 break; 166 case TYPE: 167 CheckType(str); 168 typeIsChecked = true; 169 break; 170 case COMMENT: 171 ReadComment(str); 172 break; 173 case DIM: 174 InitArrays(str); 175 break; 176 case WEIGHTTYPE: 177 ReadWeightType(str); 178 weightTypeIsChecked = true; 179 break; 180 case WEIGHTFORMAT: 181 ReadWeightFormat(str); 182 weightFormatIsChecked = true; 183 break; 184 case NODETYPE: 185 CheckNodeType(str); 186 break; 187 case NODESECTION: 188 ReadVertices(); 189 break; 190 case CAPACITY: 191 ReadCapacity(str); 192 break; 193 case DISTANCE: 194 ReadDistance(str); 195 break; 196 case VEHICLES: 197 ReadVehicles(str); 198 break; 199 case DEPOTSECTION: 200 ReadDepot(); 201 break; 202 case DEMANDSECTION: 203 ReadDemands(); 204 break; 205 } 171 206 } 172 } 173 } while (!((section == EOF) || (str == null))); 174 175 if (!(typeIsChecked && weightTypeIsChecked)) 176 throw new InvalidDataException("Input file does not contain type or edge weight type information."); 207 } while (!((section == EOF) || (str == null))); 208 209 if (!typeIsChecked || !weightTypeIsChecked || 210 (alternateFormat && !weightFormatIsChecked) || 211 weightType == TSPLIBEdgeWeightType.UNDEFINED) 212 throw new InvalidDataException("Input file does not contain format or edge weight format information."); 213 } 214 source = null; 177 215 } 178 216 … … 198 236 if (token.Equals("edge_weight_type", StringComparison.OrdinalIgnoreCase)) 199 237 return WEIGHTTYPE; 238 if (token.Equals("edge_weight_format", StringComparison.OrdinalIgnoreCase)) 239 return WEIGHTFORMAT; 200 240 if (token.Equals("node_coord_type", StringComparison.OrdinalIgnoreCase)) 201 241 return NODETYPE; … … 204 244 if (token.Equals("capacity", StringComparison.OrdinalIgnoreCase)) 205 245 return CAPACITY; 246 if (token.Equals("distance", StringComparison.OrdinalIgnoreCase)) 247 return DISTANCE; 248 if (token.Equals("vehicles", StringComparison.OrdinalIgnoreCase)) 249 return VEHICLES; 206 250 if (token.Equals("depot_section", StringComparison.OrdinalIgnoreCase)) 207 251 return DEPOTSECTION; … … 222 266 string type = tokens[tokens.Length - 1].Trim(); 223 267 if (!type.Equals("cvrp", StringComparison.OrdinalIgnoreCase)) 224 throw new InvalidDataException("Input file typeis not \"CVRP\"");268 throw new InvalidDataException("Input file format is not \"CVRP\""); 225 269 } 226 270 … … 247 291 else if (type.Equals("geo", StringComparison.OrdinalIgnoreCase)) 248 292 weightType = TSPLIBEdgeWeightType.GEO; 293 else if (type.Equals("function", StringComparison.OrdinalIgnoreCase)) { 294 weightType = TSPLIBEdgeWeightType.UNDEFINED; 295 alternateFormat = true; 296 } else 297 throw new InvalidDataException("Input file contains an unsupported edge weight format (only \"EUC_2D\" and \"GEO\" are supported)."); 298 } 299 300 private void ReadWeightFormat(string str) { 301 string[] tokens = str.Split(new string[] { ":" }, StringSplitOptions.None); 302 string format = tokens[tokens.Length - 1].Trim(); 303 304 if (alternateFormat && format.Equals("euc_2d", StringComparison.OrdinalIgnoreCase)) 305 weightType = TSPLIBEdgeWeightType.EUC_2D; 306 else if (format.Equals("geo", StringComparison.OrdinalIgnoreCase)) 307 weightType = TSPLIBEdgeWeightType.GEO; 249 308 else 250 throw new InvalidDataException("Input file contains an unsupported edge weight type (only \"EUC_2D\" and \"GEO\" are supported).");309 throw new InvalidDataException("Input file contains an unsupported edge weight format."); 251 310 } 252 311 … … 256 315 257 316 if (!type.Equals("twod_coords", StringComparison.OrdinalIgnoreCase)) 258 throw new InvalidDataException("Input file contains an unsupported node coordinates type(only \"TWOD_COORDS\" is supported).");317 throw new InvalidDataException("Input file contains an unsupported node coordinates format (only \"TWOD_COORDS\" is supported)."); 259 318 } 260 319 … … 264 323 265 324 for (int i = 0; i < (vertices.Length / 2); i++) { 266 string str = source.ReadLine(); 267 string[] tokens = str.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); 268 269 if (tokens.Length != 3) 270 throw new InvalidDataException("Input file contains invalid node coordinates."); 271 272 CultureInfo culture = new CultureInfo("en-US"); 273 vertices[i, 0] = double.Parse(tokens[1], culture.NumberFormat); 274 vertices[i, 1] = double.Parse(tokens[2], culture.NumberFormat); 325 if (!(alternateFormat && i == 0)) { 326 string str = source.ReadLine(); 327 string[] tokens = str.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); 328 329 if (tokens.Length != 3) 330 throw new InvalidDataException("Input file contains invalid node coordinates."); 331 332 vertices[i, 0] = double.Parse(tokens[1], culture.NumberFormat); 333 vertices[i, 1] = double.Parse(tokens[2], culture.NumberFormat); 334 } 275 335 } 276 336 } … … 278 338 private void ReadCapacity(string str) { 279 339 string[] tokens = str.Split(new string[] { ":" }, StringSplitOptions.None); 280 capacity = double.Parse(tokens[tokens.Length - 1].Trim()); 340 capacity = double.Parse(tokens[tokens.Length - 1].Trim(), culture.NumberFormat); 341 } 342 343 private void ReadDistance(string str) { 344 string[] tokens = str.Split(new string[] { ":" }, StringSplitOptions.None); 345 distance = double.Parse(tokens[tokens.Length - 1].Trim(), culture.NumberFormat); 346 } 347 348 private void ReadVehicles(string str) { 349 string[] tokens = str.Split(new string[] { ":" }, StringSplitOptions.None); 350 vehicles = int.Parse(tokens[tokens.Length - 1].Trim()); 281 351 } 282 352 283 353 private void ReadDepot() { 284 int read; 285 286 do { 287 string str = source.ReadLine(); 288 read = int.Parse(str); 289 if (read != -1) 290 depot = read; 291 } while (read != -1); 354 if (alternateFormat) { 355 string[] tokens; 356 do { 357 string str = source.ReadLine(); 358 tokens = str.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); 359 360 if (tokens.Length == 2) { 361 vertices[0, 0] = double.Parse(tokens[0], culture.NumberFormat); 362 vertices[0, 1] = double.Parse(tokens[1], culture.NumberFormat); 363 depot = 1; 364 } 365 } while(tokens.Length == 2); 366 } else { 367 int read; 368 do { 369 string str = source.ReadLine(); 370 read = int.Parse(str); 371 if (read != -1) 372 depot = read; 373 } while (read != -1); 374 } 292 375 } 293 376 … … 297 380 298 381 for (int i = 0; i < demands.Length; i++) { 299 string str = source.ReadLine(); 300 string[] tokens = str.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); 301 302 if (tokens.Length != 2) 303 throw new InvalidDataException("Input file contains invalid demands."); 304 305 int index = int.Parse(tokens[0]); 306 double value = double.Parse(tokens[1]); 307 308 demands[index - 1] = value; 382 if (!(alternateFormat && i == 0)) { 383 string str = source.ReadLine(); 384 string[] tokens = str.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); 385 386 if (tokens.Length != 2) 387 throw new InvalidDataException("Input file contains invalid demands."); 388 389 int index = int.Parse(tokens[0]); 390 double value = double.Parse(tokens[1]); 391 392 if (alternateFormat) 393 demands[index] = value; 394 else 395 demands[index - 1] = value; 396 } else { 397 demands[0] = 0; 398 } 309 399 } 310 400 } -
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.3/VehicleRoutingProblem.cs
r4315 r4317 461 461 462 462 Coordinates = new DoubleMatrix(parser.Vertices); 463 Vehicles.Value = problemSize - 1; 463 if (parser.Vehicles != -1) 464 Vehicles.Value = parser.Vehicles; 465 else 466 Vehicles.Value = problemSize - 1; 464 467 Capacity.Value = parser.Capacity; 465 468 Demand = new DoubleArray(parser.Demands); … … 472 475 DueTime[i] = int.MaxValue; 473 476 ServiceTime[i] = 0; 477 } 478 479 if (parser.Distance != -1) { 480 DueTime[0] = parser.Distance; 474 481 } 475 482
Note: See TracChangeset
for help on using the changeset viewer.