source: branches/HeuristicLab.Problems.Orienteering/HeuristicLab.Problems.Instances.Orienteering/3.3/SchildeOPParser.cs @ 11260

Last change on this file since 11260 was 11260, checked in by pfleck, 8 years ago

#2208

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