Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.Instances.ElloumiCTAP/3.3/ElloumiCTAPSolutionParser.cs @ 18242

Last change on this file since 18242 was 17246, checked in by gkronber, 5 years ago

#2925: merged r17037:17242 from trunk to branch

File size: 2.4 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;
25
26namespace HeuristicLab.Problems.Instances.ElloumiCTAP {
27  public class ElloumiCTAPSolutionParser {
28    public int[] Assignment { get; private set; }
29    public double Quality { get; private set; }
30    public Exception Error { get; private set; }
31
32    public ElloumiCTAPSolutionParser() {
33      Reset();
34    }
35
36    public void Reset() {
37      Assignment = null;
38      Quality = double.NaN;
39      Error = null;
40    }
41
42    public bool Parse(string file, int tasks) {
43      using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read)) {
44        return Parse(stream, tasks);
45      }
46    }
47
48    public bool Parse(Stream stream, int tasks) {
49      Error = null;
50      try {
51        StreamReader reader = new StreamReader(stream);
52        char[] delim = new char[] { ' ', '\t' };
53
54        string line = Continue(reader);
55        Assignment = new int[tasks];
56        for (int i = 0; i < tasks; i++) {
57          string[] assign = line.Split(delim, StringSplitOptions.RemoveEmptyEntries);
58          Assignment[int.Parse(assign[0]) - 1] = int.Parse(assign[1]) - 1;
59          line = Continue(reader);
60        }
61
62        Quality = double.Parse(line.Trim(), CultureInfo.InvariantCulture.NumberFormat);
63
64        return true;
65      } catch (Exception e) {
66        Error = e;
67        return false;
68      }
69    }
70
71    private static string Continue(StreamReader reader) {
72      string line = String.Empty;
73      while (String.IsNullOrEmpty(line) && !reader.EndOfStream) {
74        line = reader.ReadLine();
75      }
76      return line;
77    }
78  }
79}
Note: See TracBrowser for help on using the repository browser.