Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.Instances.Scheduling/3.3/JSSPORLIBParser.cs @ 13782

Last change on this file since 13782 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 4.1 KB
RevLine 
[8882]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[8882]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;
26
27namespace HeuristicLab.Problems.Instances.Scheduling {
28  public class JSSPORLIBParser {
29    public string Name { get; set; }
30    public string Description { get; set; }
31    public int Jobs { get; set; }
32    public int Resources { get; set; }
33    public double[,] ProcessingTimes { get; set; }
34    public int[,] Demands { get; set; }
35    public double[] DueDates { get; set; }
36
37    public JSSPORLIBParser() {
38      Reset();
39    }
40
41    public void Reset() {
42      Name = Description = String.Empty;
43      Jobs = Resources = 0;
44      ProcessingTimes = null;
45      Demands = 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    public void Export(string file) {
55      using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read)) {
56        Export(stream);
57      }
58    }
59
60    /// <summary>
61    /// Reads from the given stream data which is expected to be in the JSSP ORLIB format.
62    /// </summary>
63    /// <remarks>
64    /// The stream is not closed or disposed. The caller has to take care of that.
65    /// </remarks>
66    /// <param name="stream">The stream to read data from.</param>
67    /// <returns>True if the file was successfully read or false otherwise.</returns>
68    public void Parse(Stream stream) {
69      var reader = new StreamReader(stream);
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    public void Export(Stream stream) {
99      var writer = new StreamWriter(stream);
100      writer.WriteLine(Name);
101      writer.WriteLine(Description);
102      writer.WriteLine(Jobs.ToString(CultureInfo.InvariantCulture.NumberFormat) + " " + Resources.ToString(CultureInfo.InvariantCulture.NumberFormat));
103      for (int i = 0; i < Jobs; i++) {
104        for (int j = 0; j < Resources; j++) {
105          writer.Write(Demands[i, j] + " " + ProcessingTimes[i, j] + " ");
106        }
107        if (DueDates != null) writer.Write(DueDates[i].ToString(CultureInfo.InvariantCulture.NumberFormat));
108        writer.WriteLine();
109      }
110      writer.Flush();
111    }
112
113  }
114}
Note: See TracBrowser for help on using the repository browser.