source: branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/3D/Instances/ThreeDInstanceParser.cs @ 15617

Last change on this file since 15617 was 15617, checked in by rhanghof, 20 months ago

#2817:

  • The items can be rotated and tilted now.
  • Added pruning of extreme points in packed bins.
  • Added new packer which packs items by positioning them on the point with the minimum of wasted space. He uses rotating and tilting of items.
  • Added classes for sorting given items.
File size: 3.7 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.IO;
25using System.Text;
26
27namespace HeuristicLab.Problems.BinPacking3D.Instances {
28  public class ThreeDInstanceParser {
29    public PackingShape Bin { get; private set; }
30    public List<PackingItem> Items { get; private set; }
31
32    public ThreeDInstanceParser() {
33      Bin = new PackingShape();
34      Items = new List<PackingItem>();
35    }
36
37    /// <summary>
38    /// Parses a given stream and puts the parsed data into the properties
39    /// </summary>
40    /// <param name="stream"></param>
41    public void Parse(Stream stream) {
42      Items.Clear();
43      try {
44        using (var reader = new StreamReader(stream)) {
45          ParseBin(reader);
46          while (stream.CanRead) {
47            var id = GetNextInteger(reader);
48            var pieces = GetNextInteger(reader);
49            var length = GetNextInteger(reader);
50            var width = GetNextInteger(reader);
51            var height = GetNextInteger(reader);
52            var weight = GetNextInteger(reader);
53            var stack = GetNextInteger(reader);
54            var material = GetNextInteger(reader);
55            var rotate = GetNextInteger(reader);
56            var tilt = GetNextInteger(reader);
57            for (var i = 0; i < pieces; i++) {
58              PackingItem item = new PackingItem(width, height, length, Bin, weight, material) {
59                RotateEnabled = rotate == 0 ? false : true,
60                TiltEnabled = tilt == 0 ? false : true
61              };
62              Items.Add(item);
63            }
64          }
65        }
66      } catch (InvalidOperationException) { }
67    }
68   
69    /// <summary>
70    /// Parses the bin and puts it into the Bin property
71    /// </summary>
72    /// <param name="reader"></param>
73    private void ParseBin(StreamReader reader) {
74      var depth = GetNextInteger(reader);
75      var width = GetNextInteger(reader);
76      var height = GetNextInteger(reader);
77      var maxWeight = GetNextInteger(reader);
78
79      Bin.Depth = depth;
80      Bin.Width = width;
81      Bin.Height = height;
82    }
83
84    /// <summary>
85    /// Reads the next integer form the given stream.
86    /// </summary>
87    /// <exception cref="InvalidOperationException"></exception>
88    /// <param name="reader"></param>
89    /// <returns>Returns the next integer which has been read from the stream.</returns>
90    private int GetNextInteger(StreamReader reader) {
91      var next = reader.Read();
92      var builder = new StringBuilder();
93      while (next >= 0 && !char.IsDigit((char)next))
94        next = reader.Read();
95      if (next == -1)
96        throw new InvalidOperationException("No further integer available");
97      while (char.IsDigit((char)next)) {
98        builder.Append((char)next);
99        next = reader.Read();
100        if (next == -1)
101          break;
102      }
103      return int.Parse(builder.ToString());
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.