Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.Instances.ElloumiCTAP/3.3/ElloumiCTAPParser.cs @ 17548

Last change on this file since 17548 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 4.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;
25
26namespace HeuristicLab.Problems.Instances.ElloumiCTAP {
27  public class ElloumiCTAPParser {
28    public int Tasks { get; private set; }
29    public int Processors { get; private set; }
30    public double[,] ExecutionCosts { get; private set; }
31    public double[,] CommunicationCosts { get; private set; }
32    public double[] MemoryRequirements { get; private set; }
33    public double[] MemoryCapacities { get; private set; }
34
35    public ElloumiCTAPParser() {
36      Reset();
37    }
38
39    public void Reset() {
40      Tasks = 0;
41      Processors = 0;
42      ExecutionCosts = null;
43      CommunicationCosts = null;
44      MemoryRequirements = null;
45      MemoryCapacities = null;
46    }
47
48    public void Parse(string file) {
49      using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read)) {
50        Parse(stream);
51      }
52    }
53
54    /// <summary>
55    /// Reads from the given stream data which is expected to be in the Elloumi's CTAP format.
56    /// </summary>
57    /// <remarks>
58    /// The stream is not closed or disposed. The caller has to take care of that.
59    /// </remarks>
60    /// <param name="stream">The stream to read data from.</param>
61    /// <returns>True if the file was successfully read or false otherwise.</returns>
62    public void Parse(Stream stream) {
63      var separator = new char[] { ' ', '\t' };
64      var reader = new StreamReader(stream);
65      string line = Continue(reader);
66
67      Tasks = int.Parse(line);
68
69      line = Continue(reader);
70
71      Processors = int.Parse(line);
72
73      line = Continue(reader);
74
75      ExecutionCosts = new double[Processors, Tasks];
76      for (int i = 0; i < Processors; i++) {
77        string[] costsPerTask = line.Split(separator, StringSplitOptions.RemoveEmptyEntries);
78        for (int j = 0; j < Tasks; j++)
79          ExecutionCosts[i, j] = double.Parse(costsPerTask[j], CultureInfo.InvariantCulture.NumberFormat);
80        line = Continue(reader);
81      }
82
83      CommunicationCosts = new double[Tasks, Tasks];
84      for (int i = 0; i < Tasks - 1; i++) {
85        string[] costsTaskToTasks = line.Split(separator, StringSplitOptions.RemoveEmptyEntries);
86        for (int j = i + 1; j < Tasks; j++) {
87          CommunicationCosts[i, j] = double.Parse(costsTaskToTasks[j - i - 1], CultureInfo.InvariantCulture.NumberFormat);
88          CommunicationCosts[j, i] = CommunicationCosts[i, j];
89        }
90        line = Continue(reader);
91      }
92
93      MemoryRequirements = new double[Tasks];
94      string[] requirementsPerTask = line.Split(separator, StringSplitOptions.RemoveEmptyEntries);
95      for (int i = 0; i < Tasks; i++)
96        MemoryRequirements[i] = double.Parse(requirementsPerTask[i], CultureInfo.InvariantCulture.NumberFormat);
97
98      line = Continue(reader);
99
100      MemoryCapacities = new double[Processors];
101      string[] capacitiesPerProcessor = line.Split(separator, StringSplitOptions.RemoveEmptyEntries);
102      for (int i = 0; i < Processors; i++)
103        MemoryCapacities[i] = double.Parse(capacitiesPerProcessor[i], CultureInfo.InvariantCulture.NumberFormat);
104
105    }
106
107    private static string Continue(StreamReader reader) {
108      string line = String.Empty;
109      while (String.IsNullOrEmpty(line) && !reader.EndOfStream) {
110        line = reader.ReadLine();
111      }
112      return line;
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.