[15473] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15617] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[15473] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.IO;
|
---|
| 25 | using System.Text;
|
---|
| 26 |
|
---|
| 27 | namespace 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++) {
|
---|
[15822] | 58 | PackingItem item = new PackingItem(width, height, length, Bin, weight, 0, material) {
|
---|
[15617] | 59 | RotateEnabled = rotate == 0 ? false : true,
|
---|
[15646] | 60 | TiltEnabled = tilt == 0 ? false : true,
|
---|
[15731] | 61 | IsStackabel = stack == 0 ? false : true,
|
---|
| 62 | SupportedWeight = weight
|
---|
[15617] | 63 | };
|
---|
[15473] | 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 | }
|
---|