Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Parsers/ORLIBParser.cs @ 7694

Last change on this file since 7694 was 4362, checked in by svonolfe, 14 years ago

Added initial version - WIP (#1177)

File size: 3.3 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;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using System.IO;
27using System.Globalization;
28
29namespace HeuristicLab.Problems.VehicleRouting.Parsers {
30  class ORLIBParser {
31    private StreamReader source;
32    CultureInfo culture = new CultureInfo("en-US");
33   
34    private string name;
35    public string Name {
36      get {
37        return name;
38      }
39    }
40
41    private double[,] vertices;
42    public double[,] Vertices {
43      get {
44        return vertices;
45      }
46    }
47
48    private double capacity;
49    public double Capacity {
50      get {
51        return capacity;
52      }
53    }
54
55    private double maxRouteTime;
56    public double MaxRouteTime {
57      get {
58        return maxRouteTime;
59      }
60    }
61
62    private double serviceTime;
63    public double ServiceTime {
64      get {
65        return serviceTime;
66      }
67    }
68
69    private double[] demands;
70    public double[] Demands {
71      get {
72        return demands;
73      }
74    }
75
76    public ORLIBParser(String path) {
77      source = null;
78     
79      name = path;
80      vertices = null;
81      capacity = -1;
82      maxRouteTime = -1;
83      serviceTime = -1;
84      demands = null;
85    }
86
87    public void Parse() {
88      using (source = new StreamReader(name)) {
89        //Read header
90        string line = source.ReadLine();
91        string[] tokens = line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
92
93        int customers = int.Parse(tokens[0]);
94        demands = new double[customers + 1];
95        demands[0] = 0;
96        vertices = new double[customers + 1, 2];
97
98        capacity = int.Parse(tokens[1]);
99        maxRouteTime = int.Parse(tokens[2]);
100        serviceTime = int.Parse(tokens[3]);
101
102        //Read depot
103        line = source.ReadLine();
104        tokens = line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
105        vertices[0, 0] = double.Parse(tokens[0], culture.NumberFormat);
106        vertices[0, 1] = double.Parse(tokens[1], culture.NumberFormat);
107
108        for (int i = 0; i < customers; i++) {
109          line = source.ReadLine();
110          tokens = line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
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);
114        }
115      }
116
117      source = null;
118    }
119  }
120}
Note: See TracBrowser for help on using the repository browser.