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
Files:
1 deleted
11 edited
1 moved

Legend:

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

    r14049 r14055  
    8585    }
    8686
    87     public override PackingPosition FindExtremePointForItem(PackingItem measures, bool rotated, bool stackingConstraints) {
    88       PackingItem item = new PackingItem(
    89         rotated ? measures.Height : measures.Width,
    90         rotated ? measures.Width : measures.Height,
    91         measures.TargetBin);
     87    public override PackingPosition FindExtremePointForItem(PackingItem item, bool rotated, bool stackingConstraints) {
     88      PackingItem rotatedItem = new PackingItem(
     89        rotated ? item.Height : item.Width,
     90        rotated ? item.Width : item.Height,
     91        item.TargetBin);
    9292
    9393      int epIndex = 0;
    94       while (epIndex < ExtremePoints.Count && (!IsPositionFeasible(item, ExtremePoints.ElementAt(epIndex)))) { epIndex++; }
     94      while (epIndex < ExtremePoints.Count && (!IsPositionFeasible(rotatedItem, ExtremePoints.ElementAt(epIndex)))) { epIndex++; }
    9595
    9696      if (epIndex < ExtremePoints.Count) {
     
    102102      return null;
    103103    }
    104     public override PackingPosition FindPositionBySliding(PackingItem measures, bool rotated) {
     104    public override PackingPosition FindPositionBySliding(PackingItem item, bool rotated) {
    105105      PackingPosition currentPosition = new PackingPosition(0,
    106         BinMeasures.Width - (rotated ? measures.Height : measures.Width),
    107         BinMeasures.Height - (rotated ? measures.Width : measures.Height), rotated);
     106        BinMeasures.Width - (rotated ? item.Height : item.Width),
     107        BinMeasures.Height - (rotated ? item.Width : item.Height), rotated);
    108108      //Slide the item as far as possible to the left
    109       while (IsPositionFeasible(measures, PackingPosition.MoveLeft(currentPosition))
    110         || IsPositionFeasible(measures, PackingPosition.MoveDown(currentPosition))) {
     109      while (IsPositionFeasible(item, PackingPosition.MoveLeft(currentPosition))
     110        || IsPositionFeasible(item, PackingPosition.MoveDown(currentPosition))) {
    111111        //Slide the item as far as possible to the bottom
    112         while (IsPositionFeasible(measures, PackingPosition.MoveDown(currentPosition))) {
     112        while (IsPositionFeasible(item, PackingPosition.MoveDown(currentPosition))) {
    113113          currentPosition = PackingPosition.MoveDown(currentPosition);
    114114        }
    115         if (IsPositionFeasible(measures, PackingPosition.MoveLeft(currentPosition)))
     115        if (IsPositionFeasible(item, PackingPosition.MoveLeft(currentPosition)))
    116116          currentPosition = PackingPosition.MoveLeft(currentPosition);
    117117      }
    118118
    119       return IsPositionFeasible(measures, currentPosition) ? currentPosition : null;
    120     }
    121 
    122     public override void SlidingBasedPacking(ref List<int> sequence, ItemList<PackingItem> itemMeasures) {
     119      return IsPositionFeasible(item, currentPosition) ? currentPosition : null;
     120    }
     121
     122    public override void SlidingBasedPacking(ref List<int> sequence, ItemList<PackingItem> items) {
    123123      var temp = new List<int>(sequence);
    124124      for (int i = 0; i < temp.Count; i++) {
    125         var item = itemMeasures[temp[i]];
     125        var item = items[temp[i]];
    126126        var position = FindPositionBySliding(item, false);
    127127        if (position != null) {
     
    131131      }
    132132    }
    133     public override void SlidingBasedPacking(ref List<int> sequence, ItemList<PackingItem> itemMeasures, Dictionary<int, bool> rotationArray) {
     133    public override void SlidingBasedPacking(ref List<int> sequence, ItemList<PackingItem> items, Dictionary<int, bool> rotationArray) {
    134134      var temp = new List<int>(sequence);
    135135      for (int i = 0; i < temp.Count; i++) {
    136         var item = itemMeasures[temp[i]];
     136        var item = items[temp[i]];
    137137        var position = FindPositionBySliding(item, rotationArray[temp[i]]);
    138138        if (position != null) {
     
    142142      }
    143143    }
    144     public override void ExtremePointBasedPacking(ref List<int> sequence, ItemList<PackingItem> itemMeasures, bool stackingConstraints) {
     144    public override void ExtremePointBasedPacking(ref List<int> sequence, ItemList<PackingItem> items, bool stackingConstraints) {
    145145      var temp = new List<int>(sequence);
    146146      foreach (int itemID in temp) {
    147         var item = itemMeasures[itemID];
     147        var item = items[itemID];
    148148        var positionFound = FindExtremePointForItem(item, false, false);
    149149        if (positionFound != null) {
     
    153153      }
    154154    }
    155     public override void ExtremePointBasedPacking(ref List<int> sequence, ItemList<PackingItem> itemMeasures, bool stackingConstraints, Dictionary<int, bool> rotationArray) {
     155    public override void ExtremePointBasedPacking(ref List<int> sequence, ItemList<PackingItem> items, bool stackingConstraints, Dictionary<int, bool> rotationArray) {
    156156      var temp = new List<int>(sequence);
    157157      foreach (int itemID in temp) {
    158         var item = itemMeasures[itemID];
     158        var item = items[itemID];
    159159        var positionFound = FindExtremePointForItem(item, rotationArray[itemID], false);
    160160        if (positionFound != null) {
  • branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.2D/3.3/HeuristicLab.Problems.BinPacking2D-3.3.csproj

    r14053 r14055  
    169169    <Compile Include="Instances\BPPORLIBDataDescriptor.cs" />
    170170    <Compile Include="Instances\BPPORLIBParser.cs" />
    171     <Compile Include="Instances\RealBPPData.cs" />
    172171    <Compile Include="Interfaces\I2DGVDecoder.cs" />
    173172    <Compile Include="Interfaces\I2DMCVDecoder.cs" />
  • 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
  • branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.2D/3.3/Problem.cs

    r14054 r14055  
    4444      Name = "2D BPP Default Instance",
    4545      Description = "The default instance for 2D Bin Packing.",
    46       BinMeasures = new int[] { 20, 16 },
    47       ItemMeasures = new int[][] {
    48         new int[] {3,8},
    49         new int[] {5,3},
    50         new int[] {9,3},
    51         new int[] {2,7},
    52         new int[] {5,3},
    53         new int[] {9,3},
    54         new int[] {2,7},
    55         new int[] {5,3},
    56         new int[] {9,3},
    57         new int[] {2,7},
    58         new int[] {5,3},
    59         new int[] {9,3},
    60         new int[] {2,7},
    61         new int[] {5,3},
    62         new int[] {9,3},
    63         new int[] {2,7},
    64         new int[] {5,3},
    65         new int[] {9,3},
    66         new int[] {2,7},
    67         new int[] {5,3},
    68         new int[] {9,3},
    69         new int[] {2,7},
    70         new int[] {5,3},
    71         new int[] {9,3},
    72         new int[] {2,7},
    73         new int[] {5,3},
    74         new int[] {9,3},
    75         new int[] {2,7},
    76         new int[] {5,3},
    77         new int[] {9,3},
    78         new int[] {2,7},
    79         new int[] {5,3},
    80         new int[] {9,3},
    81         new int[] {2,7},
    82         new int[] {5,3},
    83         new int[] {9,3},
    84         new int[] {2,7}
     46      BinShape = new PackingShape(20, 16),
     47      Items = new PackingItem[] {
     48        new PackingItem(3,  8, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
     49        new PackingItem(5,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
     50        new PackingItem(9,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
     51        new PackingItem(2,  7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
     52        new PackingItem(5,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
     53        new PackingItem(9,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
     54        new PackingItem(2,  7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
     55        new PackingItem(5,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
     56        new PackingItem(9,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
     57        new PackingItem(2,  7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
     58        new PackingItem(5,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
     59        new PackingItem(9,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
     60        new PackingItem(2,  7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
     61        new PackingItem(5,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
     62        new PackingItem(9,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
     63        new PackingItem(2,  7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
     64        new PackingItem(5,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
     65        new PackingItem(9,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
     66        new PackingItem(2,  7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
     67        new PackingItem(5,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
     68        new PackingItem(9,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
     69        new PackingItem(2,  7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
     70        new PackingItem(5,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
     71        new PackingItem(9,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
     72        new PackingItem(2,  7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
     73        new PackingItem(5,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
     74        new PackingItem(9,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
     75        new PackingItem(2,  7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
     76        new PackingItem(5,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
     77        new PackingItem(9,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
     78        new PackingItem(2,  7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
     79        new PackingItem(5,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
     80        new PackingItem(9,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
     81        new PackingItem(2,  7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
     82        new PackingItem(5,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
     83        new PackingItem(9,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
     84        new PackingItem(2,  7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}
    8585      },
    86       Items = 30
    8786    };
    8887    #endregion
     
    9190    protected Problem(bool deserializing) : base(deserializing) { }
    9291    protected Problem(Problem original, Cloner cloner)
    93       : base(original, cloner) {
     92          : base(original, cloner) {
    9493    }
    9594    public override IDeepCloneable Clone(Cloner cloner) {
     
    9796    }
    9897    public Problem() : base(
    99       new DecodingEvaluator<Permutation, PackingPosition, PackingShape, PackingItem>()) {
     98          new DecodingEvaluator<Permutation, PackingPosition, PackingShape, PackingItem>()) {
    10099    }
    101100
    102101    #region Problem instance handling
    103102    public void Load(BPPData data) {
    104       var realData = data as RealBPPData;
    105       var binData = new PackingShape(data.BinMeasures[0], data.BinMeasures[1]);
    106 
    107       var itemData = new ItemList<PackingItem>(data.Items);
    108       for (int j = 0; j < data.Items; j++) {
    109         var bin = new PackingShape(data.BinMeasures[0], data.BinMeasures[1]);
    110         var item = new PackingItem(data.ItemMeasures[j][0], data.ItemMeasures[j][1], bin);
    111         if (realData != null) {
    112           item.Weight = realData.ItemWeights[j];
    113           item.Material = realData.ItemMaterials[j];
    114         }
    115         itemData.Add(item);
    116       }
    117103
    118104      BestKnownQuality = data.BestKnownQuality.HasValue ? new DoubleValue(data.BestKnownQuality.Value) : null;
    119105
    120       PackingBinMeasures = binData;
    121       PackingItemMeasures = itemData;
     106      PackingBinMeasures = data.BinShape;
     107      PackingItemMeasures = new ItemList<PackingItem>(data.Items);
    122108
    123109      ApplyHorizontalOrientation();
     
    129115
    130116    public BPPData Export() {
    131       var result = new BPPData {
     117      return new BPPData {
    132118        Name = Name,
    133119        Description = Description,
    134         Items = PackingItemsParameter.Value.Value,
    135         BinMeasures = new int[] { PackingBinMeasures.Width, PackingBinMeasures.Height }
     120        BinShape = PackingBinMeasures,
     121        Items = PackingItemMeasures.ToArray()
    136122      };
    137 
    138       var itemMeasures = new int[result.Items][];
    139       int i = 0;
    140       foreach (var item in PackingItemMeasures) {
    141         itemMeasures[i] = new int[] { item.Width, item.Height };
    142         i++;
    143       }
    144       result.ItemMeasures = itemMeasures;
    145       return result;
    146123    }
    147124    #endregion
  • branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.3D/3.3/HeuristicLab.Problems.BinPacking3D-3.3.csproj

    r14053 r14055  
    173173    <Compile Include="Instances\BPPORLIBDataDescriptor.cs" />
    174174    <Compile Include="Instances\BPPORLIBParser.cs" />
    175     <Compile Include="Instances\RealBPPData.cs" />
    176175    <Compile Include="MoveEvaluators\PackingMoveEvaluator3DGV.cs" />
    177176    <Compile Include="MoveEvaluators\PackingMoveEvaluator3DMCV.cs" />
  • branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.3D/3.3/Instances/BPPData.cs

    r14053 r14055  
    2323
    2424namespace HeuristicLab.Problems.BinPacking3D {
    25   /// <summary>
    26   /// Describes instances of the regular three dimensional bin packing problem (3DBPP).
    27   /// </summary>
     25
    2826  public class BPPData {
    2927    /// <summary>
     
    3937    /// The number of items.
    4038    /// </summary>
    41     public int Items { get; set; }
    42     /// <summary>
    43     /// The measures of the bin.
    44     /// </summary>
    45     public int[] BinMeasures { get; set; }
    46     /// <summary>
    47     /// The measures of the items.
    48     /// </summary>
    49     public int[][] ItemMeasures { get; set; }
    50 
     39    public int NumItems { get { return Items == null ? 0 : Items.Length; } }
     40    public PackingShape BinShape { get; set; }
     41    public PackingItem[] Items { get; set; }
    5142    /// <summary>
    5243    /// Optional! The quality of the best-known solution.
  • branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.3D/3.3/Instances/BPPInstanceProvider.cs

    r14053 r14055  
    3030using HeuristicLab.Problems.Instances;
    3131
    32 namespace HeuristicLab.Problems.BinPacking3D { 
     32namespace HeuristicLab.Problems.BinPacking3D {
    3333  public class BPPORLIBInstanceProvider : ProblemInstanceProvider<BPPData>, IProblemInstanceProvider<BPPData> {
    3434
     
    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://www.diku.dk/~pisinger/new3dbpp/readme.3dbpp"); }
    4545    }
    4646
     
    5959          if (string.IsNullOrWhiteSpace(entry.Name)) continue;
    6060          yield return new BPPORLIBDataDescriptor(
    61             name: Path.GetFileNameWithoutExtension(entry.Name), 
    62             description: GetDescription(), 
    63             instanceIdentifier: entry.FullName, 
     61            name: Path.GetFileNameWithoutExtension(entry.Name),
     62            description: GetDescription(),
     63            instanceIdentifier: entry.FullName,
    6464            solutionIdentifier: null);
    6565        }
     
    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.3D/3.3/Instances/BPPORLIBParser.cs

    r14053 r14055  
    2525
    2626namespace HeuristicLab.Problems.BinPacking3D {
    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]), int.Parse(tok[2]));
     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]), int.Parse(tok[2]), 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 Martello and Vigo.";
     75          else if (problemClassNr >= 6 && problemClassNr <= 9)
     76            instance.Description = "Proposed by Berkey and Wang.";
     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} {2,-5}   WBIN,HBIN", instance.BinShape.Width, instance.BinShape.Height, instance.BinShape.Depth));
     93        for (int i = 0; i < instance.NumItems; i++) {
     94          if (i == 0)
     95            writer.WriteLine("{0,-5} {1,-5} {2,-5}   W(I),H(I)D(I),I=1,...,N", instance.Items[i].Width, instance.Items[i].Height, instance.Items[i].Depth);
     96          else
     97            writer.WriteLine("{0,-5} {1,-5} {2,-5}", instance.Items[i].Width, instance.Items[i].Height, instance.Items[i].Depth);
    13798
     99        }
     100        writer.Flush();
    138101      }
    139       writer.Flush();
    140102    }
    141103
  • branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.3D/3.3/Problem.cs

    r14054 r14055  
    3737  [StorableClass]
    3838  [Creatable(CreatableAttribute.Categories.CombinatorialProblems, Priority = 310)]
    39   // TODO don't support generic BPPData but only 3d BPPData
    4039  public class Problem : Problem<PackingPosition, PackingShape, PackingItem>, IProblemInstanceConsumer<BPPData>, IProblemInstanceExporter<BPPData> {
    41 
    42 
    4340    #region Default Instance
    4441    private static readonly BPPData DefaultInstance = new BPPData() {
    4542      Name = "3D BPP Default Instance",
    4643      Description = "The default instance for 3D Bin Packing.",
    47       BinMeasures = new int[] { 25, 25, 35 },
    48       ItemMeasures = new int[][] {
    49         new int[] {12,5,10},
    50         new int[] {10,18,20},
    51         new int[] {9,7,7},
    52         new int[] {21,12,4},
    53         new int[] {8,8,12},
    54         new int[] {3,6,14},
    55         new int[] {20,4,9},
    56         new int[] {5,9,8},
    57         new int[] {7,17,3},
    58         new int[] {13,20,15},
    59         new int[] {9,11,9},
    60         new int[] {10,18,20},
    61         new int[] {9,7,7},
    62         new int[] {21,12,4},
    63         new int[] {8,8,12},
    64         new int[] {3,6,14},
    65         new int[] {20,4,9},
    66         new int[] {5,9,8},
    67         new int[] {7,17,3},
    68         new int[] {13,20,15},
    69         new int[] {9,11,9},
    70         new int[] {10,18,20},
    71         new int[] {9,7,7},
    72         new int[] {21,12,4},
    73         new int[] {8,8,12},
    74         new int[] {3,6,14},
    75         new int[] {20,4,9},
    76         new int[] {5,9,8},
    77         new int[] {7,17,3},
    78         new int[] {13,20,15},
    79         new int[] {9,11,9},
    80         new int[] {10,18,20},
    81         new int[] {9,7,7},
    82         new int[] {21,12,4},
    83         new int[] {8,8,12},
    84         new int[] {3,6,14},
    85         new int[] {20,4,9},
    86         new int[] {5,9,8},
    87         new int[] {7,17,3},
    88         new int[] {13,20,15},
    89         new int[] {9,11,9}
     44      BinShape = new PackingShape(25, 25, 35),
     45      Items = new PackingItem[] {
     46        new PackingItem(12,5,10, new PackingShape(25,25,35)),
     47        new PackingItem(10,18,20, new PackingShape(25,25,35)),
     48        new PackingItem(9,7,7, new PackingShape(25,25,35)),
     49        new PackingItem(21,12,4, new PackingShape(25,25,35)),
     50        new PackingItem(8,8,12, new PackingShape(25,25,35)),
     51        new PackingItem(3,6,14, new PackingShape(25,25,35)),
     52        new PackingItem(20,4,9, new PackingShape(25,25,35)),
     53        new PackingItem(5,9,8, new PackingShape(25,25,35)),
     54        new PackingItem(7,17,3, new PackingShape(25,25,35)),
     55        new PackingItem(13,20,15, new PackingShape(25,25,35)),
     56        new PackingItem(9,11,9, new PackingShape(25,25,35)),
     57        new PackingItem(10,18,20, new PackingShape(25,25,35)),
     58        new PackingItem(9,7,7, new PackingShape(25,25,35)),
     59        new PackingItem(21,12,4, new PackingShape(25,25,35)),
     60        new PackingItem(8,8,12, new PackingShape(25,25,35)),
     61        new PackingItem(3,6,14, new PackingShape(25,25,35)),
     62        new PackingItem(20,4,9, new PackingShape(25,25,35)),
     63        new PackingItem(5,9,8, new PackingShape(25,25,35)),
     64        new PackingItem(7,17,3, new PackingShape(25,25,35)),
     65        new PackingItem(13,20,15, new PackingShape(25,25,35)),
     66        new PackingItem(9,11,9, new PackingShape(25,25,35)),
     67        new PackingItem(10,18,20, new PackingShape(25,25,35)),
     68        new PackingItem(9,7,7, new PackingShape(25,25,35)),
     69        new PackingItem(21,12,4, new PackingShape(25,25,35)),
     70        new PackingItem(8,8,12, new PackingShape(25,25,35)),
     71        new PackingItem(3,6,14, new PackingShape(25,25,35)),
     72        new PackingItem(20,4,9, new PackingShape(25,25,35)),
     73        new PackingItem(5,9,8, new PackingShape(25,25,35)),
     74        new PackingItem(7,17,3, new PackingShape(25,25,35)),
     75        new PackingItem(13,20,15, new PackingShape(25,25,35)),
     76        new PackingItem(9,11, 9,new PackingShape(25,25,35)),
     77        new PackingItem(10,18,20, new PackingShape(25,25,35)),
     78        new PackingItem(9,7,7, new PackingShape(25,25,35)),
     79        new PackingItem(21,12,4, new PackingShape(25,25,35)),
     80        new PackingItem(8,8,12, new PackingShape(25,25,35)),
     81        new PackingItem(3,6,14, new PackingShape(25,25,35)),
     82        new PackingItem(20,4,9, new PackingShape(25,25,35)),
     83        new PackingItem(5,9,8, new PackingShape(25,25,35)),
     84        new PackingItem(7,17,3, new PackingShape(25,25,35)),
     85        new PackingItem(13,20,15, new PackingShape(25,25,35)),
     86        new PackingItem(9,11,9, new PackingShape(25,25,35))
    9087      },
    91       Items = 30
    9288    };
    9389    #endregion
     
    105101    }
    106102
     103    public void Load(BPPData data) {
    107104
    108 
    109     public void Load(BPPData data) {
    110       var realData = data as RealBPPData;
    111       var binData = new PackingShape(data.BinMeasures[0], data.BinMeasures[1], data.BinMeasures[2]);
    112 
    113       var itemData = new ItemList<PackingItem>(data.Items);
    114       for (int j = 0; j < data.Items; j++) {
    115         var bin = new PackingShape(data.BinMeasures[0], data.BinMeasures[1], data.BinMeasures[2]);
    116         var item = new PackingItem(data.ItemMeasures[j][0], data.ItemMeasures[j][1], data.ItemMeasures[j][2], bin);
    117         if (realData != null) {
    118           item.Weight = realData.ItemWeights[j];
    119           item.Material = realData.ItemMaterials[j];
    120         }
    121         itemData.Add(item);
    122       }
    123105
    124106      BestKnownQuality = data.BestKnownQuality.HasValue ? new DoubleValue(data.BestKnownQuality.Value) : null;
    125107
    126       PackingBinMeasures = binData;
    127       PackingItemMeasures = itemData;
     108      PackingBinMeasures = data.BinShape;
     109      PackingItemMeasures = new ItemList<PackingItem>(data.Items);
    128110
    129111      ApplyHorizontalOrientation();
     
    134116
    135117    public BPPData Export() {
    136       var result = new BPPData {
     118      return new BPPData {
    137119        Name = Name,
    138120        Description = Description,
    139         Items = PackingItemsParameter.Value.Value,
    140         BinMeasures = new int[] { PackingBinMeasures.Width, PackingBinMeasures.Height, PackingBinMeasures.Depth }
     121        BinShape = PackingBinMeasures,
     122        Items = PackingItemMeasures.ToArray()
    141123      };
    142 
    143       var itemMeasures = new int[result.Items][];
    144       int i = 0;
    145       foreach (var item in PackingItemMeasures) {
    146         itemMeasures[i] = new int[] { item.Width, item.Height, item.Depth };
    147         i++;
    148       }
    149       result.ItemMeasures = itemMeasures;
    150       return result;
    151124    }
    152125
  • branches/HeuristicLab.BinPacking/old files/RealBPPData.cs

    r14054 r14055  
    3535    /// </summary>
    3636    public int[] ItemMaterials { get; set; }
    37 
    3837  }
    3938}
Note: See TracChangeset for help on using the changeset viewer.