Free cookie consent management tool by TermsFeed Policy Generator

source: branches/BinPackingExtension/HeuristicLab.Problems.BinPacking/3.3/3D/Instances/ThreeDInstanceParser.cs @ 14838

Last change on this file since 14838 was 14838, checked in by abeham, 7 years ago

Added some benchmark instances

File size: 2.7 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2017 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#endregion
23
24using System;
25using System.Collections.Generic;
26using System.IO;
27using System.Text;
28
29namespace HeuristicLab.Problems.BinPacking3D {
30  public class ThreeDInstanceParser {
31    public PackingShape Bin { get; private set; }
32    public List<PackingItem> Items { get; private set; }
33
34    public void Parse(Stream stream) {
35      var reader = new StreamReader(stream);
36      var length = GetNextInteger(reader).Value;
37      var width = GetNextInteger(reader).Value;
38      var height = GetNextInteger(reader).Value;
39      var maxWeight = GetNextInteger(reader).Value;
40      Bin = new PackingShape(width, height, length);
41      Items = new List<PackingItem>();
42      while (true) {
43        try {
44          var id = GetNextInteger(reader).Value;
45          var pieces = GetNextInteger(reader).Value;
46          length = GetNextInteger(reader).Value;
47          width = GetNextInteger(reader).Value;
48          height = GetNextInteger(reader).Value;
49          var weight = GetNextInteger(reader).Value;
50          var stack = GetNextInteger(reader).Value;
51          var rotate = GetNextInteger(reader).Value;
52          var tilt = GetNextInteger(reader).Value;
53          for (var i = 0; i < pieces; i++) {
54            Items.Add(new PackingItem(width, height, length, Bin, weight, 1));
55          }
56        } catch (InvalidOperationException) {
57          break;
58        }
59      }
60    }
61
62    private int? GetNextInteger(StreamReader reader) {
63      var next = reader.Read();
64      var builder = new StringBuilder();
65      while (next >= 0 && !char.IsDigit((char)next)) next = reader.Read();
66      if (next == -1) return null;
67      while (char.IsDigit((char)next)) {
68        builder.Append((char)next);
69        next = reader.Read();
70        if (next == -1) break;
71      }
72      return int.Parse(builder.ToString());
73    }
74  }
75}
Note: See TracBrowser for help on using the repository browser.