Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2817:

  • Dealing with stackable items
  • Enhanced the Evaluator
  • Added parameters some paramters to the packing 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              };
63              Items.Add(item);
64            }
65          }
66        }
67      } catch (InvalidOperationException) { }
68    }
69   
70    /// <summary>
71    /// Parses the bin and puts it into the Bin property
72    /// </summary>
73    /// <param name="reader"></param>
74    private void ParseBin(StreamReader reader) {
75      var depth = GetNextInteger(reader);
76      var width = GetNextInteger(reader);
77      var height = GetNextInteger(reader);
78      var maxWeight = GetNextInteger(reader);
79
80      Bin.Depth = depth;
81      Bin.Width = width;
82      Bin.Height = height;
83    }
84
85    /// <summary>
86    /// Reads the next integer form the given stream.
87    /// </summary>
88    /// <exception cref="InvalidOperationException"></exception>
89    /// <param name="reader"></param>
90    /// <returns>Returns the next integer which has been read from the stream.</returns>
91    private int GetNextInteger(StreamReader reader) {
92      var next = reader.Read();
93      var builder = new StringBuilder();
94      while (next >= 0 && !char.IsDigit((char)next))
95        next = reader.Read();
96      if (next == -1)
97        throw new InvalidOperationException("No further integer available");
98      while (char.IsDigit((char)next)) {
99        builder.Append((char)next);
100        next = reader.Read();
101        if (next == -1)
102          break;
103      }
104      return int.Parse(builder.ToString());
105    }
106  }
107}
Note: See TracBrowser for help on using the repository browser.