1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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 |
|
---|
22 | using System;
|
---|
23 | using System.Globalization;
|
---|
24 | using System.IO;
|
---|
25 |
|
---|
26 | namespace 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 | public Exception Error { get; private set; }
|
---|
35 |
|
---|
36 | public ElloumiCTAPParser() {
|
---|
37 | Reset();
|
---|
38 | }
|
---|
39 |
|
---|
40 | public void Reset() {
|
---|
41 | Tasks = 0;
|
---|
42 | Processors = 0;
|
---|
43 | ExecutionCosts = null;
|
---|
44 | CommunicationCosts = null;
|
---|
45 | MemoryRequirements = null;
|
---|
46 | MemoryCapacities = null;
|
---|
47 | Error = null;
|
---|
48 | }
|
---|
49 |
|
---|
50 | public bool Parse(string file) {
|
---|
51 | using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read)) {
|
---|
52 | return Parse(stream);
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | /// <summary>
|
---|
57 | /// Reads from the given stream data which is expected to be in the Elloumi's CTAP format.
|
---|
58 | /// </summary>
|
---|
59 | /// <remarks>
|
---|
60 | /// The stream is not closed or disposed. The caller has to take care of that.
|
---|
61 | /// </remarks>
|
---|
62 | /// <param name="stream">The stream to read data from.</param>
|
---|
63 | /// <returns>True if the file was successfully read or false otherwise.</returns>
|
---|
64 | public bool Parse(Stream stream) {
|
---|
65 | Error = null;
|
---|
66 | var separator = new char[] { ' ', '\t' };
|
---|
67 | try {
|
---|
68 | StreamReader reader = new StreamReader(stream);
|
---|
69 | string line = Continue(reader);
|
---|
70 |
|
---|
71 | Tasks = int.Parse(line);
|
---|
72 |
|
---|
73 | line = Continue(reader);
|
---|
74 |
|
---|
75 | Processors = int.Parse(line);
|
---|
76 |
|
---|
77 | line = Continue(reader);
|
---|
78 |
|
---|
79 | ExecutionCosts = new double[Processors, Tasks];
|
---|
80 | for (int i = 0; i < Processors; i++) {
|
---|
81 | string[] costsPerTask = line.Split(separator, StringSplitOptions.RemoveEmptyEntries);
|
---|
82 | for (int j = 0; j < Tasks; j++)
|
---|
83 | ExecutionCosts[i, j] = double.Parse(costsPerTask[j], CultureInfo.InvariantCulture.NumberFormat);
|
---|
84 | line = Continue(reader);
|
---|
85 | }
|
---|
86 |
|
---|
87 | CommunicationCosts = new double[Tasks, Tasks];
|
---|
88 | for (int i = 0; i < Tasks - 1; i++) {
|
---|
89 | string[] costsTaskToTasks = line.Split(separator, StringSplitOptions.RemoveEmptyEntries);
|
---|
90 | for (int j = i + 1; j < Tasks; j++) {
|
---|
91 | CommunicationCosts[i, j] = double.Parse(costsTaskToTasks[j - i - 1], CultureInfo.InvariantCulture.NumberFormat);
|
---|
92 | CommunicationCosts[j, i] = CommunicationCosts[i, j];
|
---|
93 | }
|
---|
94 | line = Continue(reader);
|
---|
95 | }
|
---|
96 |
|
---|
97 | MemoryRequirements = new double[Tasks];
|
---|
98 | string[] requirementsPerTask = line.Split(separator, StringSplitOptions.RemoveEmptyEntries);
|
---|
99 | for (int i = 0; i < Tasks; i++)
|
---|
100 | MemoryRequirements[i] = double.Parse(requirementsPerTask[i], CultureInfo.InvariantCulture.NumberFormat);
|
---|
101 |
|
---|
102 | line = Continue(reader);
|
---|
103 |
|
---|
104 | MemoryCapacities = new double[Processors];
|
---|
105 | string[] capacitiesPerProcessor = line.Split(separator, StringSplitOptions.RemoveEmptyEntries);
|
---|
106 | for (int i = 0; i < Processors; i++)
|
---|
107 | MemoryCapacities[i] = double.Parse(capacitiesPerProcessor[i], CultureInfo.InvariantCulture.NumberFormat);
|
---|
108 |
|
---|
109 | return true;
|
---|
110 | } catch (Exception e) {
|
---|
111 | Error = e;
|
---|
112 | return false;
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | private static string Continue(StreamReader reader) {
|
---|
117 | string line = String.Empty;
|
---|
118 | while (String.IsNullOrEmpty(line) && !reader.EndOfStream) {
|
---|
119 | line = reader.ReadLine();
|
---|
120 | }
|
---|
121 | return line;
|
---|
122 | }
|
---|
123 | }
|
---|
124 | }
|
---|