Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.Orienteering/HeuristicLab.Problems.Instances.Orienteering/3.3/SchildeParser.cs @ 11319

Last change on this file since 11319 was 11319, checked in by pfleck, 10 years ago

#2208 Renamed TerminusPoint to TerminalPoint

File size: 10.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.Globalization;
25using System.IO;
26using System.Linq;
27
28namespace HeuristicLab.Problems.Instances.Orienteering {
29  public class SchildeParser {
30    private static readonly IFormatProvider FormatProvider = new CultureInfo("en-US");
31
32    private int currentPoint;
33    private int currentDistance;
34
35    public double[,] Coordinates { get; private set; }
36    public double[,] Distances { get; private set; }
37    public double[,] Scores { get; private set; }
38    public int StartingPoint { get; private set; }
39    public int TerminalPoint { get; private set; }
40    public double UpperBoundConstraint { get; private set; }
41    public double LowerConstraint { get; private set; }
42
43    public void Reset() {
44      currentPoint = 0;
45      currentDistance = 0;
46
47      Coordinates = null;
48      Distances = null;
49      Scores = null;
50      StartingPoint = 0;
51      TerminalPoint = 0;
52      UpperBoundConstraint = 0.0;
53      LowerConstraint = 0.0;
54
55    }
56
57    public void Parse(string file) {
58      using (var stream = new FileStream(file, FileMode.Open, FileAccess.Read)) {
59        Parse(stream);
60      }
61    }
62    public void Parse(Stream stream) {
63      int lineNumber = 0;
64
65      using (var reader = new StreamReader(stream)) {
66        // Read the lines from the file
67        while (!reader.EndOfStream) {
68          string lineBuffer = reader.ReadLine();
69
70          // Remove all whitespace
71          lineBuffer = new string(lineBuffer.ToCharArray().Where(c => !char.IsWhiteSpace(c)).ToArray());
72
73          // If the line contains data, continue
74          if (lineBuffer.Length > 1) {
75            // If the line contains not only a comment, process it
76            int commentPosition = lineBuffer.IndexOf("//");
77            if (commentPosition != 0) {
78              // Count the lines that include data
79              lineNumber++;
80
81              // Remove a comment if there is one
82              if (commentPosition > 0)
83                lineBuffer = lineBuffer.Remove(commentPosition);
84
85              // Convert the line to lowercase characters
86              lineBuffer = lineBuffer.ToLower();
87
88              // Skip version info in file
89              if (lineNumber > 1) {
90                // Extract the needed information
91                ExtractData(lineBuffer);
92              }
93            }
94          }
95        }
96      }
97    }
98    private void ExtractData(string inputString) {
99      string temp;
100      int pointCount;
101
102      switch (inputString[0]) {
103        case 'p': // Point description
104          if (inputString.Length < 8)
105            throw new IOException("Invalid Definition P");
106
107          // Remove the 'p,'
108          inputString = inputString.Remove(0, inputString.IndexOf(',') + 1);
109          // Get the position_x
110          string positionX = inputString.Substring(0, inputString.IndexOf(','));
111          // Remove the position_x
112          inputString = inputString.Remove(0, inputString.IndexOf(',') + 1);
113          // Get the postion_y
114          string positionY = inputString.Substring(0, inputString.IndexOf(','));
115          // Remove the position_y to get the scores
116          inputString = inputString.Remove(0, inputString.IndexOf(',') + 1);
117
118          Coordinates[currentPoint, 0] = double.Parse(positionX, FormatProvider);
119          Coordinates[currentPoint, 1] = double.Parse(positionY, FormatProvider);
120
121          // Extract all score values for this point
122          int scoreIndex = 0;
123          while (true) {
124            string score;
125            if (inputString.IndexOf(',') != -1) {
126              score = inputString.Substring(0, inputString.IndexOf(','));
127              inputString = inputString.Remove(0, inputString.IndexOf(',') + 1);
128              Scores[currentPoint, scoreIndex++] = int.Parse(score, FormatProvider);
129            } else {
130              score = inputString;
131              Scores[currentPoint, scoreIndex++] = int.Parse(score, FormatProvider);
132              break;
133            }
134          }
135          currentPoint++;
136          break;
137
138        case 'd': // Distance information
139          if (inputString.Length < 11)
140            throw new IOException("Invalid Definition D");
141
142          //var tempDistances = new List<double>();
143
144          // Remove the 'd,'
145          inputString = inputString.Remove(0, inputString.IndexOf(',') + 1);
146
147          // Extract all distances for this point
148          int distanceIndex = 0;
149          while (true) {
150            string distance;
151            if (inputString.IndexOf(',') != -1) {
152              distance = inputString.Substring(0, inputString.IndexOf(','));
153              inputString = inputString.Remove(0, inputString.IndexOf(',') + 1);
154              Distances[currentDistance, distanceIndex++] = double.Parse(distance, FormatProvider);
155            } else {
156              distance = inputString;
157              Distances[currentDistance, distanceIndex++] = double.Parse(distance, FormatProvider);
158              break;
159            }
160          }
161          break;
162
163        case 'n': // Number of points
164          if (inputString.Length < 3)
165            throw new IOException("Invalid Definition N");
166
167          // Remove the 'n,'
168          inputString = inputString.Remove(0, inputString.IndexOf(',') + 1);
169          // Extract the number of points
170
171          pointCount = int.Parse(inputString, FormatProvider);
172          Coordinates = new double[pointCount, 2];
173          break;
174
175        case 'm': // 1 if Distance-Matrix is given
176          if (inputString.Length < 3)
177            throw new IOException("Invalid Definition M");
178
179          // Remove the 'm,'
180          inputString = inputString.Remove(0, inputString.IndexOf(',') + 1);
181          // Extract the number of points
182          bool matrixGiven = true;
183
184          pointCount = Coordinates.GetLength(1);
185          Distances = new double[pointCount, pointCount];
186          break;
187
188        case 'c': // Number of constraints
189          if (inputString.Length < 3)
190            throw new IOException("Invalid Definition C");
191
192          // Remove the 'c,'
193          inputString = inputString.Remove(0, inputString.IndexOf(',') + 1);
194          break;
195
196        case 's': // Number of scores per point
197          if (inputString.Length < 3)
198            throw new IOException("Invalid Definition S");
199
200          // Remove the 's,'
201          inputString = inputString.Remove(0, inputString.IndexOf(',') + 1);
202          int scoreCount = int.Parse(inputString, FormatProvider);
203
204          pointCount = Coordinates.GetLength(0);
205          Scores = new double[pointCount, scoreCount];
206
207          break;
208
209        case 'b': // Index of the starting point
210          if (inputString.Length < 3)
211            throw new IOException("Invalid Definition B");
212
213          // Remove the 'b,'
214          inputString = inputString.Remove(0, inputString.IndexOf(',') + 1);
215          StartingPoint = int.Parse(inputString, FormatProvider);
216          break;
217
218        case 'e': // Index of the terminus point
219          if (inputString.Length < 3)
220            throw new IOException("Invalid Definition E");
221
222          // Remove the 'e,'
223          inputString = inputString.Remove(0, inputString.IndexOf(',') + 1);
224          TerminalPoint = int.Parse(inputString, FormatProvider);
225          break;
226
227        case 'u': // Upper bound constraint
228          if (inputString.Length < 5)
229            throw new IOException("Invalid Definition U");
230
231          // Remove the 'u,'
232          inputString = inputString.Remove(0, inputString.IndexOf(',') + 1);
233          // Get the target
234          temp = inputString.Substring(0, inputString.IndexOf(','));
235          // Remove the target to get the target value
236          inputString = inputString.Remove(0, inputString.IndexOf(',') + 1);
237
238          UpperBoundConstraint = double.Parse(inputString, FormatProvider);
239          break;
240
241        case 'l': // Lower bound constraint
242          if (inputString.Length < 5)
243            throw new IOException("Invalid Definition L");
244
245          // Remove the 'l,'
246          inputString = inputString.Remove(0, inputString.IndexOf(',') + 1);
247          // Get the target
248          temp = inputString.Substring(0, inputString.IndexOf(','));
249          // Remove the target to get the target value
250          inputString = inputString.Remove(0, inputString.IndexOf(',') + 1);
251
252          LowerConstraint = double.Parse(inputString, FormatProvider);
253          break;
254
255        case 'x': // Maximization constraint
256          if (inputString.Length < 5)
257            throw new IOException("Invalid Definition X");
258
259          // Remove the 'x,'
260          inputString = inputString.Remove(0, inputString.IndexOf(',') + 1);
261          // Get the target
262          temp = inputString.Substring(0, inputString.IndexOf(','));
263          // Remove the target to get the target value
264          inputString = inputString.Remove(0, inputString.IndexOf(',') + 1);
265
266          break;
267
268        case 'i': // Minimization constraint
269          if (inputString.Length < 5)
270            throw new IOException("Invalid Definition I");
271
272          // Remove the 'i,'
273          inputString = inputString.Remove(0, inputString.IndexOf(',') + 1);
274          // Get the target
275          temp = inputString.Substring(0, inputString.IndexOf(','));
276          // Remove the target to get the target value
277          inputString = inputString.Remove(0, inputString.IndexOf(',') + 1);
278
279          break;
280      }
281    }
282  }
283}
Note: See TracBrowser for help on using the repository browser.