Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/13/16 09:30:01 (8 years ago)
Author:
gkronber
Message:

#1966: simplified parsers

Location:
branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.2D/3.3/Instances
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.2D/3.3/Instances/BPPData.cs

    r14053 r14055  
    2323
    2424namespace HeuristicLab.Problems.BinPacking2D {
    25   /// <summary>
    26   /// Describes instances of the regular two dimensional bin packing problem (2DBPP).
    2725
    28   /// </summary>
    29 
    30   // TODO implement specifically for 2d and 3d instances
    3126  public class BPPData {
    3227    /// <summary>
     
    4237    /// The number of items.
    4338    /// </summary>
    44     public int Items { get; set; }
    45     /// <summary>
    46     /// The measures of the bin.
    47     /// </summary>
    48     public int[] BinMeasures { get; set; }
    49     /// <summary>
    50     /// The measures of the items.
    51     /// </summary>
    52     public int[][] ItemMeasures { get; set; }
    53 
     39    public int NumItems { get { return Items == null ? 0 : Items.Length; } }
     40    public PackingShape BinShape { get; set; }
     41    public PackingItem[] Items { get; set; }
    5442    /// <summary>
    5543    /// Optional! The quality of the best-known solution.
  • branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.2D/3.3/Instances/BPPInstanceProvider.cs

    r14053 r14055  
    4242
    4343    public override Uri WebLink {
    44       get { return new Uri("http://people.brunel.ac.uk/~mastjjb/jeb/orlib/binpacktwoinfo.html , http://www.diku.dk/~pisinger/new3dbpp/readme.3dbpp"); }
     44      get { return new Uri("http://people.brunel.ac.uk/~mastjjb/jeb/orlib/binpacktwoinfo.html"); }
    4545    }
    4646
     
    7474
    7575        using (var stream = entry.Open()) {
    76           var parser = new BPPORLIBParser();
    77           parser.Parse(stream);
    78           var instance = Load(parser);
     76          var instance = BPPORLIBParser.Parse(stream);
    7977          instance.Name = id.Name;
    8078          instance.Description = id.Description;
     
    8987    }
    9088    public override BPPData ImportData(string path) {
    91       var parser = new BPPORLIBParser();
    92       parser.Parse(path);
    93       var instance = Load(parser);
     89      var instance = BPPORLIBParser.Parse(path);
    9490      instance.Name = Path.GetFileName(path);
    9591      instance.Description = "Loaded from file \"" + path + "\" on " + DateTime.Now;
    96       return instance;
    97     }
    98 
    99     private BPPData Load(BPPORLIBParser parser) {
    100       var instance = new BPPData {
    101         Items = parser.Items,
    102         BinMeasures = parser.BinMeasures,
    103         ItemMeasures = parser.ItemMeasures
    104       };
    10592      return instance;
    10693    }
     
    11198
    11299    public override void ExportData(BPPData instance, string path) {
    113       var parser = new BPPORLIBParser {
    114         Name = instance.Name,
    115         Description = instance.Description,
    116         Items = instance.Items,
    117         BinMeasures = instance.BinMeasures,
    118         ItemMeasures = instance.ItemMeasures
    119       };
    120       parser.Export(path);
     100      BPPORLIBParser.Export(instance, path);
    121101    }
    122102
  • branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.2D/3.3/Instances/BPPORLIBParser.cs

    r14053 r14055  
    2525
    2626namespace HeuristicLab.Problems.BinPacking2D {
    27   public class BPPORLIBParser {
    28     public string Name { get; set; }
    29     public string Description { get; set; }
    30     public int Items { get; set; }
    31     public int[] BinMeasures { get; set; }
    32     public int[][] ItemMeasures { get; set; }
    33 
    34     public BPPORLIBParser() {
    35       Reset();
    36     }
    37 
    38     public void Reset() {
    39       Name = Description = String.Empty;
    40       Items = 0;
    41       BinMeasures = null;
    42       ItemMeasures = null;
    43     }
    44 
    45     public void Parse(string file) {
     27  public static class BPPORLIBParser {
     28    public static BPPData Parse(string file) {
    4629      using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read)) {
    47         Parse(stream);
     30        return Parse(stream);
    4831      }
    4932    }
    5033
    51     public void Export(string file) {
     34    public static void Export(BPPData instance, string file) {
    5235      using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read)) {
    53         Export(stream);
     36        Export(instance, stream);
    5437      }
    5538    }
     
    6245    /// </remarks>
    6346    /// <param name="stream">The stream to read data from.</param>
    64     /// <returns>True if the file was successfully read or false otherwise.</returns>
    65     public void Parse(Stream stream) {
    66       var reader = new StreamReader(stream);
    67       var delim = new char[] { ' ', '\t' };
    68       var problemClass = reader.ReadLine().Split(delim, StringSplitOptions.RemoveEmptyEntries);
     47    /// <returns>The parsed problem instance</returns>
     48    public static BPPData Parse(Stream stream) {
     49      using (var reader = new StreamReader(stream)) {
     50        var instance = new BPPData();
     51        var delim = new char[] { ' ', '\t' };
     52        var problemClass = reader.ReadLine().Split(delim, StringSplitOptions.RemoveEmptyEntries);
    6953
    70       var nrOfItems = reader.ReadLine().Split(delim, StringSplitOptions.RemoveEmptyEntries);
    71       Items = int.Parse(nrOfItems[0]);
    72       //Numbering of instances..
    73       reader.ReadLine();
    74       var binMeasures = reader.ReadLine().Split(delim, StringSplitOptions.RemoveEmptyEntries);
    75       int depth;
    76       bool isThreeDimensional = false;
    77       if (int.TryParse(binMeasures[2], out depth)) {
    78         isThreeDimensional = true;
    79         BinMeasures = new int[3];
    80         BinMeasures[0] = int.Parse(binMeasures[0]);
    81         BinMeasures[1] = int.Parse(binMeasures[1]);
    82         BinMeasures[2] = depth;
    83       } else {
    84         BinMeasures = new int[2];
    85         BinMeasures[0] = int.Parse(binMeasures[0]);
    86         BinMeasures[1] = int.Parse(binMeasures[1]);
     54        var tok = reader.ReadLine().Split(delim, StringSplitOptions.RemoveEmptyEntries);
     55        var numItems = int.Parse(tok[0]);
     56        //Numbering of instances..
     57        reader.ReadLine();
     58        tok = reader.ReadLine().Split(delim, StringSplitOptions.RemoveEmptyEntries);
     59        instance.BinShape = new PackingShape(int.Parse(tok[0]), int.Parse(tok[1]));
     60        instance.Items = new PackingItem[numItems];
     61
     62        for (int k = 0; k < numItems; k++) {
     63          if (reader.EndOfStream) throw new InvalidDataException("Unexpected End of Stream (at line " + k + ").");
     64          var valLine = reader.ReadLine();
     65          while (String.IsNullOrWhiteSpace(valLine)) valLine = reader.ReadLine();
     66          tok = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries);
     67          instance.Items[k] = new PackingItem(int.Parse(tok[0]), int.Parse(tok[1]), instance.BinShape) { Material = 1, Weight = 1.0 }; // these problem instances do not specify weights and materials
     68        }
     69
     70        int problemClassNr = -1;
     71        if (int.TryParse(problemClass[0], out problemClassNr)) {
     72          instance.Name = "dbpp_class_0" + problemClassNr;
     73          if (problemClassNr >= 1 && problemClassNr <= 5)
     74            instance.Description = "Proposed by Berkey and Wang.";
     75          else if (problemClassNr >= 6 && problemClassNr <= 10)
     76            instance.Description = "Proposed by Martello and Vigo.";
     77        } else {
     78          instance.Name = "Unknown";
     79          instance.Description = "<Missing>";
     80        }
     81
     82        return instance;
    8783      }
    88       ItemMeasures = new int[Items][];
    89 
    90       for (int k = 0; k < Items; k++) {
    91         if (reader.EndOfStream) throw new InvalidDataException("Unexpected End of Stream (at line " + k + ").");
    92         var valLine = reader.ReadLine();
    93         while (String.IsNullOrWhiteSpace(valLine)) valLine = reader.ReadLine();
    94         var vals = valLine.Split(delim, StringSplitOptions.RemoveEmptyEntries);
    95         ItemMeasures[k] = new int[isThreeDimensional ? 3 : 2];
    96         ItemMeasures[k][0] = int.Parse(vals[0]);
    97         ItemMeasures[k][1] = int.Parse(vals[1]);
    98         if (isThreeDimensional)
    99           ItemMeasures[k][2] = int.Parse(vals[2]);
    100       }
    101 
    102       Int32 problemClassNr = -1;
    103       if (int.TryParse(problemClass[0], out problemClassNr)) {
    104         Name = (isThreeDimensional ? "3" : "2") + "dbpp_class_0" + problemClassNr;
    105         if (!isThreeDimensional) {
    106           if (problemClassNr >= 1 && problemClassNr <= 5)
    107             Description = "Proposed by Berkey and Wang.";
    108           else if (problemClassNr >= 6 && problemClassNr <= 10)
    109             Description = "Proposed by Martello and Vigo.";
    110         } else {
    111           if (problemClassNr >= 1 && problemClassNr <= 5)
    112             Description = "Proposed by Martello and Vigo.";
    113           else if (problemClassNr >= 6 && problemClassNr <= 9)
    114             Description = "Proposed by Berkey and Wang.";
    115         }
    116       } else {
    117         Name = "Unknown";
    118         Description = "<Missing>";
    119       }
    120 
    121 
    12284    }
    12385
    124     public void Export(Stream stream) {
    125       bool isThreeDimensional = BinMeasures.Length == 3;
    126       var nameParts = Name.Split('_');
     86    public static void Export(BPPData instance, Stream stream) {
     87      var nameParts = instance.Name.Split('_');
    12788      int problemClassNr = int.Parse(nameParts[2]);
    12889
    129       var writer = new StreamWriter(stream);
    130       writer.WriteLine(String.Format("{0,-5}               PROBLEM CLASS ({1})", problemClassNr, Description));
    131       writer.WriteLine(String.Format("{0,-5} {1,-5} {2,-5}   WBIN,HBIN,DBIN", BinMeasures[0], BinMeasures[1], BinMeasures[2]));
    132       for (int i = 0; i < Items; i++) {
    133         if (i == 0)
    134           writer.WriteLine("{0,-5} {1,-5} {2,-5}   W(I),H(I),D(I),I=1,...,N", ItemMeasures[i][0], ItemMeasures[i][1], ItemMeasures[i][2]);
    135         else
    136           writer.WriteLine("{0,-5} {1,-5} {2,-5}", ItemMeasures[i][0], ItemMeasures[i][1], ItemMeasures[i][2]);
     90      using (var writer = new StreamWriter(stream)) {
     91        writer.WriteLine(String.Format("{0,-5}               PROBLEM CLASS ({1})", problemClassNr, instance.Description));
     92        writer.WriteLine(String.Format("{0,-5} {1,-5}   WBIN,HBIN", instance.BinShape.Width, instance.BinShape.Height));
     93        for (int i = 0; i < instance.NumItems; i++) {
     94          if (i == 0)
     95            writer.WriteLine("{0,-5} {1,-5}   W(I),H(I),I=1,...,N", instance.Items[i].Width, instance.Items[i].Height);
     96          else
     97            writer.WriteLine("{0,-5} {1,-5}", instance.Items[i].Width, instance.Items[i].Height);
    13798
     99        }
     100        writer.Flush();
    138101      }
    139       writer.Flush();
    140102    }
    141103
Note: See TracChangeset for help on using the changeset viewer.