Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15731 was 15731, checked in by rhanghof, 6 years ago

#2817:

  • Added a new packer.
  • Enhanced the material types.
  • Added extreme point pruning for layer support in the extrem point creators.
  • BinPacking3D: Added a graph for calculating weigth distribution of the items.
File size: 3.8 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                IsStackabel = stack == 0 ? false : true,
62                SupportedWeight = weight
63              };
64              Items.Add(item);
65            }
66          }
67        }
68      } catch (InvalidOperationException) { }
69    }
70   
71    /// <summary>
72    /// Parses the bin and puts it into the Bin property
73    /// </summary>
74    /// <param name="reader"></param>
75    private void ParseBin(StreamReader reader) {
76      var depth = GetNextInteger(reader);
77      var width = GetNextInteger(reader);
78      var height = GetNextInteger(reader);
79      var maxWeight = GetNextInteger(reader);
80
81      Bin.Depth = depth;
82      Bin.Width = width;
83      Bin.Height = height;
84    }
85
86    /// <summary>
87    /// Reads the next integer form the given stream.
88    /// </summary>
89    /// <exception cref="InvalidOperationException"></exception>
90    /// <param name="reader"></param>
91    /// <returns>Returns the next integer which has been read from the stream.</returns>
92    private int GetNextInteger(StreamReader reader) {
93      var next = reader.Read();
94      var builder = new StringBuilder();
95      while (next >= 0 && !char.IsDigit((char)next))
96        next = reader.Read();
97      if (next == -1)
98        throw new InvalidOperationException("No further integer available");
99      while (char.IsDigit((char)next)) {
100        builder.Append((char)next);
101        next = reader.Read();
102        if (next == -1)
103          break;
104      }
105      return int.Parse(builder.ToString());
106    }
107  }
108}
Note: See TracBrowser for help on using the repository browser.