Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/SolomonParser.cs @ 4047

Last change on this file since 4047 was 3947, checked in by svonolfe, 14 years ago

Fixed some problems in the VRP implementation (#1039)

File size: 4.0 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.Text.RegularExpressions;
28
29namespace HeuristicLab.Problems.VehicleRouting {
30  class SolomonParser {
31    private string file;
32    private string problemName;
33    private int cities;
34    private int vehicles;
35    private double capacity;
36    private List<double> xCoord;
37    private List<double> yCoord;
38    private List<double> demand;
39    private List<double> readyTime;
40    private List<double> dueTime;
41    private List<double> serviceTime;
42
43    public int Cities {
44      get {
45        return cities;
46      }
47    }
48
49    public int Vehicles {
50      get {
51        return vehicles;
52      }
53    }
54
55    public double Capacity {
56      get {
57        return capacity;
58      }
59    }
60
61    public double[,] Coordinates {
62      get {
63        double[] x = xCoord.ToArray();
64        double[] y = yCoord.ToArray();
65        double[,] coord = new double[x.Length, 2];
66        for (int i = 0; i < x.Length; i++) {
67          coord[i, 0] = x[i];
68          coord[i, 1] = y[i];
69        }
70        return coord;
71      }
72    }
73
74    public double[] Demands {
75      get {
76        return demand.ToArray();
77      }
78    }
79
80    public double[] Readytimes {
81      get {
82        return readyTime.ToArray();
83      }
84    }
85
86    public double[] Duetimes {
87      get {
88        return dueTime.ToArray();
89      }
90    }
91
92    public double[] Servicetimes {
93      get {
94        return serviceTime.ToArray();
95      }
96    }
97
98    public String ProblemName {
99      get {
100        return problemName;
101      }
102    }
103
104    public SolomonParser(string file) {
105      this.file = file;
106      xCoord = new List<double>();
107      yCoord = new List<double>();
108      demand = new List<double>();
109      readyTime = new List<double>();
110      dueTime = new List<double>();
111      serviceTime = new List<double>();
112    }
113
114    public void Parse() {
115      string line;
116      Regex reg = new Regex(@"-?\d+");
117      MatchCollection m;
118
119      StreamReader reader = new StreamReader(file);
120
121      problemName = reader.ReadLine();
122      for (int i = 0; i < 3; i++) {
123        reader.ReadLine();
124      }
125      line = reader.ReadLine();
126
127      m = reg.Matches(line);
128      if (m.Count != 2)
129        throw new InvalidDataException("File has wrong format!");
130
131      vehicles = int.Parse(m[0].Value);
132      capacity = double.Parse(m[1].Value);
133
134      for (int i = 0; i < 4; i++) {
135        reader.ReadLine();
136      }
137
138      line = reader.ReadLine();
139      while ((line != null) && (line.Length > 5)) {
140        m = reg.Matches(line);
141        if (m.Count != 7) { continue; }
142        xCoord.Add((double)int.Parse(m[1].Value));
143        yCoord.Add((double)int.Parse(m[2].Value));
144        demand.Add((double)int.Parse(m[3].Value));
145        readyTime.Add((double)int.Parse(m[4].Value));
146        double st = (double)int.Parse(m[6].Value);
147        dueTime.Add((double)int.Parse(m[5].Value));
148        serviceTime.Add(st);
149        line = reader.ReadLine();
150      }
151      cities = serviceTime.Count;
152    }
153
154    internal string OnReset() {
155      throw new NotImplementedException();
156    }
157  }
158}
Note: See TracBrowser for help on using the repository browser.