Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2864_PermutationProblems/HeuristicLab.Problems.Instances.PFSP/3.3/FSSPTAILIBParser.cs @ 15541

Last change on this file since 15541 was 15541, checked in by fholzing, 6 years ago

#2864: CleanUp of old code, added LOP benchmark instances

File size: 4.6 KB
Line 
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
22using System;
23using System.Globalization;
24using System.IO;
25using System.Linq;
26using System.Text;
27
28namespace 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        public void Parse(Stream stream)
71        {
72            using (var reader = new StreamReader(stream, Encoding.UTF8, true, 4092, true))
73            {
74                Name = reader.ReadLine().Trim();
75                Description = reader.ReadLine().Trim();
76                var delim = new char[] { ' ', '\t' };
77
78                var info = reader.ReadLine().Split(delim, StringSplitOptions.RemoveEmptyEntries);
79                Jobs = int.Parse(info[0]);
80                Machines = int.Parse(info[1]);
81                var bestKnownQuality = reader.ReadLine().Trim();
82                BestKnownQuality = int.Parse(bestKnownQuality);
83                ProcessingTimes = new double[Machines, Jobs];
84
85                for (int k = 0; k < Machines; k++)
86                {
87                    if (reader.EndOfStream) throw new InvalidDataException("Unexpected End of Stream.");
88                    var valLine = reader.ReadLine();
89                    while (String.IsNullOrWhiteSpace(valLine)) valLine = reader.ReadLine();
90                    var vals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries);
91
92
93                    for (int i = 0; i < Jobs; i++)
94                    {
95                        ProcessingTimes[k, i] = double.Parse(vals[i], CultureInfo.InvariantCulture.NumberFormat);
96                    }
97                }
98
99                while (!reader.EndOfStream)
100                {
101                    var vals = reader.ReadLine().Split(delim, StringSplitOptions.RemoveEmptyEntries);
102                    if (vals.Count() == Jobs) { BestKnownSchedule = vals.Select(val => Int32.Parse(val)).ToArray(); }
103                }
104            }
105        }
106   
107        public void Export(Stream stream)
108        {
109            using (var writer = new StreamWriter(stream, Encoding.UTF8, 4092, true))
110            {
111                writer.WriteLine(Name);
112                writer.WriteLine(Description);
113                writer.WriteLine(Jobs + '\t' + Machines);
114                writer.WriteLine(BestKnownQuality);
115
116                for (int k = 0; k < Machines; k++)
117                {
118                    for (int i = 0; i < Jobs; i++)
119                    {
120                        writer.Write(ProcessingTimes[k, i] + "\t");
121                    }
122                    writer.WriteLine();
123                }
124
125                if (BestKnownSchedule != null) {
126                    writer.WriteLine(BestKnownSchedule);
127                }
128                writer.Flush();
129            }
130        }
131    }
132}
Note: See TracBrowser for help on using the repository browser.