Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2864_PermutationProblems/HeuristicLab.Problems.Instances.LinearOrdering/3.3/LOPParser.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.4 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.LinearOrdering
29{
30    public class LOPParser
31    {
32        public string Name { get; set; }
33        public string Description { get; set; }
34        public int Dimension { get; set; }
35        public double[,] Matrix { get; set; }
36        public int[] BestKnownPermutation { get; set; }
37        public double? BestKnownQuality { get; set; }
38
39        public LOPParser()
40        {
41            Reset();
42        }
43
44        public void Reset()
45        {
46            Name = Description = String.Empty;
47            Dimension = 0;
48            BestKnownQuality = 0;
49            Matrix = null;
50            BestKnownPermutation = null;
51        }
52
53        public void Parse(string file)
54        {
55            using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read))
56            {
57                Parse(stream);
58            }
59        }
60
61        public void Export(string file)
62        {
63            using (Stream stream = new FileStream(file, FileMode.Create, FileAccess.Write))
64            {
65                Export(stream);
66            }
67        }
68
69        public void Parse(Stream stream)
70        {
71            using (var reader = new StreamReader(stream, Encoding.UTF8, true, 4092, true))
72            {
73                Name = reader.ReadLine().Trim();
74                Description = reader.ReadLine().Trim();
75                BestKnownQuality = double.Parse(reader.ReadLine().Trim());
76                var delim = new char[] { ' ', '\t' };
77
78                Dimension = int.Parse(reader.ReadLine().Trim());
79                Matrix = new double[Dimension, Dimension];
80
81                for (int k = 0; k < Dimension; k++)
82                {
83                    if (reader.EndOfStream) throw new InvalidDataException("Unexpected End of Stream.");
84                    var valLine = reader.ReadLine();
85                    while (String.IsNullOrWhiteSpace(valLine)) valLine = reader.ReadLine();
86                    var vals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries);
87
88
89                    for (int i = 0; i < Dimension; i++)
90                    {
91                        Matrix[k, i] = double.Parse(vals[i], CultureInfo.InvariantCulture.NumberFormat);
92                    }
93                }
94
95                while (!reader.EndOfStream)
96                {
97                    var vals = reader.ReadLine().Split(delim, StringSplitOptions.RemoveEmptyEntries);
98                    if (vals.Count() == Dimension) { BestKnownPermutation = vals.Select(val => Int32.Parse(val)).ToArray(); }
99                }
100            }
101        }
102
103        public void Export(Stream stream)
104        {
105            using (var writer = new StreamWriter(stream, Encoding.UTF8, 4092, true))
106            {
107                writer.WriteLine(Name);
108                writer.WriteLine(Description);
109                writer.WriteLine(BestKnownQuality);
110                writer.WriteLine(Dimension);
111
112                for (int k = 0; k < Dimension; k++)
113                {
114                    for (int i = 0; i < Dimension; i++)
115                    {
116                        writer.Write(Matrix[k, i] + '\t');
117                    }
118                    writer.WriteLine();
119                }
120
121                if (BestKnownPermutation != null) {
122                    writer.WriteLine(BestKnownPermutation);
123                }
124                writer.Flush();
125            }
126        }
127    }
128}
Note: See TracBrowser for help on using the repository browser.