Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.Instances.ElloumiCTAP/3.3/ElloumiCTAPParser.cs @ 11642

Last change on this file since 11642 was 11171, checked in by ascheibe, 10 years ago

#2115 merged r11170 (copyright update) into trunk

File size: 4.2 KB
RevLine 
[7445]1#region License Information
2/* HeuristicLab
[11171]3 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[7445]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
[7538]48    public void Parse(string file) {
[7445]49      using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read)) {
[7538]50        Parse(stream);
[7445]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>
[7538]62    public void Parse(Stream stream) {
[7445]63      var separator = new char[] { ' ', '\t' };
[7538]64      var reader = new StreamReader(stream);
65      string line = Continue(reader);
[7445]66
[7538]67      Tasks = int.Parse(line);
[7445]68
[7538]69      line = Continue(reader);
[7445]70
[7538]71      Processors = int.Parse(line);
[7445]72
[7538]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);
[7445]80        line = Continue(reader);
[7538]81      }
[7445]82
[7538]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];
[7445]89        }
[7538]90        line = Continue(reader);
91      }
[7445]92
[7538]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);
[7445]97
[7538]98      line = Continue(reader);
[7445]99
[7538]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);
[7445]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.