Free cookie consent management tool by TermsFeed Policy Generator

source: branches/MemPRAlgorithm/HeuristicLab.Problems.BinPacking/3.3/2D/IntegerVectorEncoding/IntegerVectorDecoderBase.cs @ 14429

Last change on this file since 14429 was 14167, checked in by gkronber, 8 years ago

#2641:

  • fixed persistence bug
  • added unit test to create BPP sample
  • added BPP sample to start page
  • renaming of properties
File size: 3.5 KB
Line 
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
22using HeuristicLab.Core;
23using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
24using HeuristicLab.Common;
25using System.Collections.Generic;
26using System.Linq;
27using HeuristicLab.Encodings.IntegerVectorEncoding;
28
29namespace HeuristicLab.Problems.BinPacking2D {
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) {
42      var sequenceMatrix = IntegerVectorProblem.GenerateSequenceMatrix(intVec);
43      Solution result = CreateSolution(binShape);
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();
49        result.Bins.Add(CreatePacking(result, ref remainingIDs, items));
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) {
56        foreach (BinPacking2D bp in result.Bins) {
57          var position = FindPositionForItem(bp, items[id]);
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) {
68        result.Bins.Add(CreatePacking(result, ref remainingIDs, items));
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);
86    protected abstract PackingPosition FindPositionForItem(BinPacking2D bp, PackingItem item);
87    protected abstract BinPacking2D CreatePacking(Solution partialSolution, ref IList<int> remainingIDs, IList<PackingItem> items);
88  }
89}
Note: See TracBrowser for help on using the repository browser.