Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2915-AbsoluteSymbol/HeuristicLab.Problems.Instances.Scheduling/3.3/JSSPORLIBParser.cs @ 16057

Last change on this file since 16057 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 4.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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;
25using System.Linq;
26using System.Text;
27
28namespace HeuristicLab.Problems.Instances.Scheduling {
29  public class JSSPORLIBParser {
30    public string Name { get; set; }
31    public string Description { get; set; }
32    public int Jobs { get; set; }
33    public int Resources { get; set; }
34    public double[,] ProcessingTimes { get; set; }
35    public int[,] Demands { get; set; }
36    public double[] DueDates { get; set; }
37
38    public JSSPORLIBParser() {
39      Reset();
40    }
41
42    public void Reset() {
43      Name = Description = String.Empty;
44      Jobs = Resources = 0;
45      ProcessingTimes = null;
46      Demands = null;
47    }
48
49    public void Parse(string file) {
50      using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read)) {
51        Parse(stream);
52      }
53    }
54
55    public void Export(string file) {
56      using (Stream stream = new FileStream(file, FileMode.Create, FileAccess.Write)) {
57        Export(stream);
58      }
59    }
60
61    /// <summary>
62    /// Reads from the given stream data which is expected to be in the JSSP ORLIB format.
63    /// </summary>
64    /// <remarks>
65    /// The stream is not closed or disposed. The caller has to take care of that.
66    /// </remarks>
67    /// <param name="stream">The stream to read data from.</param>
68    public void Parse(Stream stream) {
69      using (var reader = new StreamReader(stream, Encoding.UTF8, true, 4092, true)) {
70        Name = reader.ReadLine().Trim();
71        Description = reader.ReadLine().Trim();
72        var delim = new char[] {' ', '\t'};
73
74        var info = reader.ReadLine().Split(delim, StringSplitOptions.RemoveEmptyEntries);
75        Jobs = int.Parse(info[0]);
76        Resources = int.Parse(info[1]);
77        ProcessingTimes = new double[Jobs, Resources];
78        Demands = new int[Jobs, Resources];
79
80        for (int k = 0; k < Jobs; k++) {
81          if (reader.EndOfStream) throw new InvalidDataException("Unexpected End of Stream.");
82          var valLine = reader.ReadLine();
83          while (String.IsNullOrWhiteSpace(valLine)) valLine = reader.ReadLine();
84          var vals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries);
85          if (vals.Length > 2 * Resources) {
86            if (DueDates == null) DueDates = new double[Jobs];
87            DueDates[k] = double.Parse(vals.Last(), CultureInfo.InvariantCulture.NumberFormat);
88          }
89
90          for (int i = 0; i < Resources; i++) {
91            Demands[k, i] = int.Parse(vals[2 * i]);
92            ProcessingTimes[k, i] = double.Parse(vals[2 * i + 1], CultureInfo.InvariantCulture.NumberFormat);
93          }
94        }
95      }
96    }
97
98    /// <summary>
99    /// Writes to the given stream data which is expected to be in the JSSP ORLIB format.
100    /// </summary>
101    /// <remarks>
102    /// The stream is not closed or disposed. The caller has to take care of that.
103    /// </remarks>
104    /// <param name="stream">The stream to write data to.</param>
105    public void Export(Stream stream) {
106      using (var writer = new StreamWriter(stream, Encoding.UTF8, 4092, true)) {
107        writer.WriteLine(Name);
108        writer.WriteLine(Description);
109        writer.WriteLine(Jobs.ToString(CultureInfo.InvariantCulture.NumberFormat) + " " + Resources.ToString(CultureInfo.InvariantCulture.NumberFormat));
110        for (int i = 0; i < Jobs; i++) {
111          for (int j = 0; j < Resources; j++) {
112            writer.Write(Demands[i, j] + " " + ProcessingTimes[i, j] + " ");
113          }
114          if (DueDates != null) writer.Write(DueDates[i].ToString("r", CultureInfo.InvariantCulture.NumberFormat));
115          writer.WriteLine();
116        }
117        writer.Flush();
118      }
119    }
120
121  }
122}
Note: See TracBrowser for help on using the repository browser.