[14162] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2016 Joseph Helm and 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 |
|
---|
| 22 | using HeuristicLab.Core;
|
---|
| 23 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using System.Collections.Generic;
|
---|
| 26 | using System.Linq;
|
---|
| 27 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.Problems.BinPacking3D {
|
---|
| 30 | [StorableClass]
|
---|
| 31 | public abstract class IntegerVectorDecoderBase : Item, IDecoder<IntegerVector> {
|
---|
| 32 |
|
---|
| 33 | [StorableConstructor]
|
---|
| 34 | protected IntegerVectorDecoderBase(bool deserializing) : base(deserializing) { }
|
---|
| 35 | protected IntegerVectorDecoderBase(IntegerVectorDecoderBase original, Cloner cloner)
|
---|
| 36 | : base(original, cloner) {
|
---|
| 37 | }
|
---|
| 38 | protected IntegerVectorDecoderBase() : base() { }
|
---|
| 39 |
|
---|
| 40 |
|
---|
| 41 | public virtual Solution Decode(IntegerVector intVec, PackingShape binShape, IList<PackingItem> items, bool useStackingConstraints) {
|
---|
| 42 | var sequenceMatrix = IntegerVectorProblem.GenerateSequenceMatrix(intVec);
|
---|
| 43 | Solution result = CreateSolution(binShape, useStackingConstraints);
|
---|
| 44 |
|
---|
| 45 | //Fill bins according to grouping vector
|
---|
| 46 | IList<int> remainingIDs = new List<int>();
|
---|
| 47 | foreach (var sequence in sequenceMatrix) {
|
---|
| 48 | remainingIDs = remainingIDs.Concat(sequence).ToList();
|
---|
[14170] | 49 | result.Bins.Add(CreatePacking(result, ref remainingIDs, items, useStackingConstraints));
|
---|
[14162] | 50 | }
|
---|
| 51 | result.UpdateBinPackings();
|
---|
| 52 |
|
---|
| 53 | //Try to put remaining items in existing bins
|
---|
| 54 | var temp = new List<int>(remainingIDs);
|
---|
| 55 | foreach (int id in temp) {
|
---|
[14170] | 56 | foreach (BinPacking3D bp in result.Bins) {
|
---|
[14162] | 57 | var position = FindPositionForItem(bp, items[id], useStackingConstraints);
|
---|
| 58 | if (position != null) {
|
---|
| 59 | bp.PackItem(id, items[id], position);
|
---|
| 60 | remainingIDs.Remove(id);
|
---|
| 61 | break;
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | //Put still remaining items in new bins
|
---|
| 67 | while (remainingIDs.Count > 0) {
|
---|
[14170] | 68 | result.Bins.Add(CreatePacking(result, ref remainingIDs, items, useStackingConstraints));
|
---|
[14162] | 69 | }
|
---|
| 70 | result.UpdateBinPackings();
|
---|
| 71 |
|
---|
| 72 | // gkronber: original implementation by Helm also updates the encoded solution (TODO)
|
---|
| 73 | // var newSolution = new int[intVec.Length];
|
---|
| 74 | // int binIndex = 0;
|
---|
| 75 | // foreach (var bp in result.BinPackings) {
|
---|
| 76 | // foreach (var entry in bp.ItemPositions)
|
---|
| 77 | // newSolution[entry.Key] = binIndex;
|
---|
| 78 | // binIndex++;
|
---|
| 79 | // }
|
---|
| 80 | // solution.GroupingVector = new IntegerVector(newSolution);
|
---|
| 81 |
|
---|
| 82 | return result;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | protected abstract Solution CreateSolution(PackingShape binShape, bool useStackingConstraints);
|
---|
| 86 | protected abstract PackingPosition FindPositionForItem(BinPacking3D bp, PackingItem item, bool useStackingConstraints);
|
---|
| 87 | protected abstract BinPacking3D CreatePacking(Solution partialSolution, ref IList<int> remainingIDs, IList<PackingItem> items, bool useStackingConstraints);
|
---|
| 88 | }
|
---|
| 89 | }
|
---|