Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.Instances.Orienteering/3.3/SchildeParser.cs @ 13305

Last change on this file since 13305 was 13305, checked in by ascheibe, 8 years ago

#2428 merged r12793, r13093, r13219, r13253 into stable

File size: 10.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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                   
182          pointCount = Coordinates.GetLength(1);
183          Distances = new double[pointCount, pointCount];
184          break;
185
186        case 'c': // Number of constraints
187          if (inputString.Length < 3)
188            throw new IOException("Invalid Definition C");
189
190          // Remove the 'c,'
191          inputString = inputString.Remove(0, inputString.IndexOf(',') + 1);
192          break;
193
194        case 's': // Number of scores per point
195          if (inputString.Length < 3)
196            throw new IOException("Invalid Definition S");
197
198          // Remove the 's,'
199          inputString = inputString.Remove(0, inputString.IndexOf(',') + 1);
200          int scoreCount = int.Parse(inputString, FormatProvider);
201
202          pointCount = Coordinates.GetLength(0);
203          Scores = new double[pointCount, scoreCount];
204
205          break;
206
207        case 'b': // Index of the starting point
208          if (inputString.Length < 3)
209            throw new IOException("Invalid Definition B");
210
211          // Remove the 'b,'
212          inputString = inputString.Remove(0, inputString.IndexOf(',') + 1);
213          StartingPoint = int.Parse(inputString, FormatProvider);
214          break;
215
216        case 'e': // Index of the terminus point
217          if (inputString.Length < 3)
218            throw new IOException("Invalid Definition E");
219
220          // Remove the 'e,'
221          inputString = inputString.Remove(0, inputString.IndexOf(',') + 1);
222          TerminalPoint = int.Parse(inputString, FormatProvider);
223          break;
224
225        case 'u': // Upper bound constraint
226          if (inputString.Length < 5)
227            throw new IOException("Invalid Definition U");
228
229          // Remove the 'u,'
230          inputString = inputString.Remove(0, inputString.IndexOf(',') + 1);
231          // Get the target
232          temp = inputString.Substring(0, inputString.IndexOf(','));
233          // Remove the target to get the target value
234          inputString = inputString.Remove(0, inputString.IndexOf(',') + 1);
235
236          UpperBoundConstraint = double.Parse(inputString, FormatProvider);
237          break;
238
239        case 'l': // Lower bound constraint
240          if (inputString.Length < 5)
241            throw new IOException("Invalid Definition L");
242
243          // Remove the 'l,'
244          inputString = inputString.Remove(0, inputString.IndexOf(',') + 1);
245          // Get the target
246          temp = inputString.Substring(0, inputString.IndexOf(','));
247          // Remove the target to get the target value
248          inputString = inputString.Remove(0, inputString.IndexOf(',') + 1);
249
250          LowerConstraint = double.Parse(inputString, FormatProvider);
251          break;
252
253        case 'x': // Maximization constraint
254          if (inputString.Length < 5)
255            throw new IOException("Invalid Definition X");
256
257          // Remove the 'x,'
258          inputString = inputString.Remove(0, inputString.IndexOf(',') + 1);
259          // Get the target
260          temp = inputString.Substring(0, inputString.IndexOf(','));
261          // Remove the target to get the target value
262          inputString = inputString.Remove(0, inputString.IndexOf(',') + 1);
263
264          break;
265
266        case 'i': // Minimization constraint
267          if (inputString.Length < 5)
268            throw new IOException("Invalid Definition I");
269
270          // Remove the 'i,'
271          inputString = inputString.Remove(0, inputString.IndexOf(',') + 1);
272          // Get the target
273          temp = inputString.Substring(0, inputString.IndexOf(','));
274          // Remove the target to get the target value
275          inputString = inputString.Remove(0, inputString.IndexOf(',') + 1);
276
277          break;
278      }
279    }
280  }
281}
Note: See TracBrowser for help on using the repository browser.