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 |
|
---|
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.PermutationProblems.LinearOrdering {
|
---|
29 | public class LOPParser {
|
---|
30 | public string Name { get; set; }
|
---|
31 | public string Description { get; set; }
|
---|
32 | public int Dimension { get; set; }
|
---|
33 | public double[,] Matrix { get; set; }
|
---|
34 | public int[] BestKnownPermutation { get; set; }
|
---|
35 | public double? BestKnownQuality { get; set; }
|
---|
36 |
|
---|
37 | public LOPParser() {
|
---|
38 | Reset();
|
---|
39 | }
|
---|
40 |
|
---|
41 | public void Reset() {
|
---|
42 | Name = Description = String.Empty;
|
---|
43 | Dimension = 0;
|
---|
44 | BestKnownQuality = 0;
|
---|
45 | Matrix = null;
|
---|
46 | BestKnownPermutation = 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 | public void Parse(Stream stream) {
|
---|
62 | using (var reader = new StreamReader(stream, Encoding.UTF8, true, 4092, true)) {
|
---|
63 | Name = reader.ReadLine().Trim();
|
---|
64 | Description = reader.ReadLine().Trim();
|
---|
65 | BestKnownQuality = double.Parse(reader.ReadLine().Trim());
|
---|
66 | var delim = new char[] { ' ', '\t' };
|
---|
67 |
|
---|
68 | Dimension = int.Parse(reader.ReadLine().Trim());
|
---|
69 | Matrix = new double[Dimension, Dimension];
|
---|
70 |
|
---|
71 | for (int k = 0; k < Dimension; k++) {
|
---|
72 | if (reader.EndOfStream) throw new InvalidDataException("Unexpected End of Stream.");
|
---|
73 | var valLine = reader.ReadLine();
|
---|
74 | while (String.IsNullOrWhiteSpace(valLine)) valLine = reader.ReadLine();
|
---|
75 | var vals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries);
|
---|
76 |
|
---|
77 |
|
---|
78 | for (int i = 0; i < Dimension; i++) {
|
---|
79 | Matrix[k, i] = double.Parse(vals[i], CultureInfo.InvariantCulture.NumberFormat);
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | while (!reader.EndOfStream) {
|
---|
84 | var vals = reader.ReadLine().Split(delim, StringSplitOptions.RemoveEmptyEntries);
|
---|
85 | if (vals.Count() == Dimension) { BestKnownPermutation = vals.Select(val => Int32.Parse(val)).ToArray(); }
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | public void Export(Stream stream) {
|
---|
91 | using (var writer = new StreamWriter(stream, Encoding.UTF8, 4092, true)) {
|
---|
92 | writer.WriteLine(Name);
|
---|
93 | writer.WriteLine(Description);
|
---|
94 | writer.WriteLine(BestKnownQuality);
|
---|
95 | writer.WriteLine(Dimension);
|
---|
96 |
|
---|
97 | for (int k = 0; k < Dimension; k++) {
|
---|
98 | for (int i = 0; i < Dimension; i++) {
|
---|
99 | writer.Write(Matrix[k, i] + '\t');
|
---|
100 | }
|
---|
101 | writer.WriteLine();
|
---|
102 | }
|
---|
103 |
|
---|
104 | if (BestKnownPermutation != null) {
|
---|
105 | writer.WriteLine(BestKnownPermutation);
|
---|
106 | }
|
---|
107 | writer.Flush();
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|