Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3040_VectorBasedGP/HeuristicLab.Problems.Instances.Orienteering/3.3/SchildeParser.cs @ 18242

Last change on this file since 18242 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

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