Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Problems.VehicleRouting/3.3/SolomonParser.cs @ 5436

Last change on this file since 5436 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

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