Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/22/18 17:15:29 (6 years ago)
Author:
fholzing
Message:

#2864: Adapted PFSP and LOP to the new BasicProblem

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2864_PermutationProblems/HeuristicLab.Problems.Instances.LinearOrdering/3.3/LOPParser.cs

    r15541 r15639  
    2626using System.Text;
    2727
    28 namespace 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; }
     28namespace HeuristicLab.Problems.Instances.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; }
    3836
    39         public LOPParser()
    40         {
    41             Reset();
     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          }
    4281        }
    4382
    44         public void Reset()
    45         {
    46             Name = Description = String.Empty;
    47             Dimension = 0;
    48             BestKnownQuality = 0;
    49             Matrix = null;
    50             BestKnownPermutation = null;
     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();
    51102        }
    52103
    53         public void Parse(string file)
    54         {
    55             using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read))
    56             {
    57                 Parse(stream);
    58             }
     104        if (BestKnownPermutation != null) {
     105          writer.WriteLine(BestKnownPermutation);
    59106        }
    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         }
     107        writer.Flush();
     108      }
    127109    }
     110  }
    128111}
Note: See TracChangeset for help on using the changeset viewer.