1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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 | using System.Linq;
|
---|
26 | using System.Text;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Problems.Instances.PFSP
|
---|
29 | {
|
---|
30 | public class FSSPTAILIBParser
|
---|
31 | {
|
---|
32 | public string Name { get; set; }
|
---|
33 | public string Description { get; set; }
|
---|
34 | public int Jobs { get; set; }
|
---|
35 | public int Machines { get; set; }
|
---|
36 | public double[,] ProcessingTimes { get; set; }
|
---|
37 |
|
---|
38 | public int[] BestKnownSchedule { get; set; }
|
---|
39 | public int BestKnownQuality { get; set; }
|
---|
40 |
|
---|
41 | public FSSPTAILIBParser()
|
---|
42 | {
|
---|
43 | Reset();
|
---|
44 | }
|
---|
45 |
|
---|
46 | public void Reset()
|
---|
47 | {
|
---|
48 | Name = Description = String.Empty;
|
---|
49 | Jobs = Machines = BestKnownQuality = 0;
|
---|
50 | ProcessingTimes = null;
|
---|
51 | BestKnownSchedule = null;
|
---|
52 | }
|
---|
53 |
|
---|
54 | public void Parse(string file)
|
---|
55 | {
|
---|
56 | using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read))
|
---|
57 | {
|
---|
58 | Parse(stream);
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | public void Export(string file)
|
---|
63 | {
|
---|
64 | using (Stream stream = new FileStream(file, FileMode.Create, FileAccess.Write))
|
---|
65 | {
|
---|
66 | Export(stream);
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | /// <summary>
|
---|
71 | /// Reads from the given stream data which is expected to be in the JSSP ORLIB format.
|
---|
72 | /// </summary>
|
---|
73 | /// <remarks>
|
---|
74 | /// The stream is not closed or disposed. The caller has to take care of that.
|
---|
75 | /// </remarks>
|
---|
76 | /// <param name="stream">The stream to read data from.</param>
|
---|
77 | public void Parse(Stream stream)
|
---|
78 | {
|
---|
79 | using (var reader = new StreamReader(stream, Encoding.UTF8, true, 4092, true))
|
---|
80 | {
|
---|
81 | Name = reader.ReadLine().Trim();
|
---|
82 | Description = reader.ReadLine().Trim();
|
---|
83 | var delim = new char[] { ' ', '\t' };
|
---|
84 |
|
---|
85 | var info = reader.ReadLine().Split(delim, StringSplitOptions.RemoveEmptyEntries);
|
---|
86 | Jobs = int.Parse(info[0]);
|
---|
87 | Machines = int.Parse(info[1]);
|
---|
88 | var bestKnownQuality = reader.ReadLine().Trim();
|
---|
89 | BestKnownQuality = int.Parse(bestKnownQuality);
|
---|
90 | ProcessingTimes = new double[Machines, Jobs];
|
---|
91 |
|
---|
92 | for (int k = 0; k < Machines; k++)
|
---|
93 | {
|
---|
94 | if (reader.EndOfStream) throw new InvalidDataException("Unexpected End of Stream.");
|
---|
95 | var valLine = reader.ReadLine();
|
---|
96 | while (String.IsNullOrWhiteSpace(valLine)) valLine = reader.ReadLine();
|
---|
97 | var vals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries);
|
---|
98 |
|
---|
99 |
|
---|
100 | for (int i = 0; i < Jobs; i++)
|
---|
101 | {
|
---|
102 | ProcessingTimes[k, i] = double.Parse(vals[i], CultureInfo.InvariantCulture.NumberFormat);
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | while (!reader.EndOfStream)
|
---|
107 | {
|
---|
108 | var vals = reader.ReadLine().Split(delim, StringSplitOptions.RemoveEmptyEntries);
|
---|
109 | if (vals.Count() == Jobs) { BestKnownSchedule = vals.Select(val => Int32.Parse(val)).ToArray(); }
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | /// <summary>
|
---|
115 | /// Writes to the given stream data which is expected to be in the JSSP ORLIB format.
|
---|
116 | /// </summary>
|
---|
117 | /// <remarks>
|
---|
118 | /// The stream is not closed or disposed. The caller has to take care of that.
|
---|
119 | /// </remarks>
|
---|
120 | /// <param name="stream">The stream to write data to.</param>
|
---|
121 | public void Export(Stream stream)
|
---|
122 | {
|
---|
123 | //TODO: complete Export, seems as if it is just a dummy (copy-paste)-implementation
|
---|
124 | using (var writer = new StreamWriter(stream, Encoding.UTF8, 4092, true))
|
---|
125 | {
|
---|
126 | writer.WriteLine(Name);
|
---|
127 | writer.WriteLine(Description);
|
---|
128 | writer.WriteLine(Jobs.ToString(CultureInfo.InvariantCulture.NumberFormat) + " " + Machines.ToString(CultureInfo.InvariantCulture.NumberFormat));
|
---|
129 | for (int i = 0; i < Machines; i++)
|
---|
130 | {
|
---|
131 | for (int j = 0; j < Jobs; j++)
|
---|
132 | {
|
---|
133 | writer.Write(ProcessingTimes[i, j] + " ");
|
---|
134 | }
|
---|
135 | writer.WriteLine();
|
---|
136 | }
|
---|
137 | writer.Flush();
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | }
|
---|
142 | }
|
---|