source: branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.2D/3.3/Problem.cs @ 14128

Last change on this file since 14128 was 14128, checked in by gkronber, 3 years ago

#1966: refactoring of bin packing implementation

File size: 7.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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 HeuristicLab.Encodings.PermutationEncoding;
26using HeuristicLab.PluginInfrastructure;
27using HeuristicLab.Encodings.PackingEncoding.PackingSequence;
28using HeuristicLab.Problems.Instances;
29using HeuristicLab.Data;
30using HeuristicLab.Problems.BinPacking;
31
32namespace HeuristicLab.Problems.BinPacking2D {
33  [Item("Bin Packing Problem (2D, identical rectangles) (BPP)", "Represents a two-dimensional bin-packing problem using only bins with identical measures and bins/items with rectangular shapes.")]
34  [StorableClass]
35  [Creatable(CreatableAttribute.Categories.CombinatorialProblems, Priority = 300)]
36  public class Problem : Problem<PackingPosition, PackingShape, PackingItem>, IProblemInstanceConsumer<BPPData>, IProblemInstanceExporter<BPPData> {
37
38    #region Default Instance
39    private static readonly BPPData DefaultInstance = new BPPData() {
40      Name = "2D BPP Default Instance",
41      Description = "The default instance for 2D Bin Packing.",
42      BinShape = new PackingShape(20, 16),
43      Items = new PackingItem[] {
44        new PackingItem(3,  8, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
45        new PackingItem(5,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
46        new PackingItem(9,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
47        new PackingItem(2,  7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
48        new PackingItem(5,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
49        new PackingItem(9,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
50        new PackingItem(2,  7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
51        new PackingItem(5,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
52        new PackingItem(9,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
53        new PackingItem(2,  7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
54        new PackingItem(5,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
55        new PackingItem(9,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
56        new PackingItem(2,  7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
57        new PackingItem(5,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
58        new PackingItem(9,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
59        new PackingItem(2,  7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
60        new PackingItem(5,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
61        new PackingItem(9,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
62        new PackingItem(2,  7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
63        new PackingItem(5,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
64        new PackingItem(9,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
65        new PackingItem(2,  7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
66        new PackingItem(5,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
67        new PackingItem(9,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
68        new PackingItem(2,  7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
69        new PackingItem(5,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
70        new PackingItem(9,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
71        new PackingItem(2,  7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
72        new PackingItem(5,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
73        new PackingItem(9,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
74        new PackingItem(2,  7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
75        new PackingItem(5,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
76        new PackingItem(9,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
77        new PackingItem(2,  7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
78        new PackingItem(5,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
79        new PackingItem(9,  3, new PackingShape(20, 16)) { Material = 1, Weight = 1.0},
80        new PackingItem(2,  7, new PackingShape(20, 16)) { Material = 1, Weight = 1.0}
81      },
82    };
83    #endregion
84
85    [StorableConstructor]
86    protected Problem(bool deserializing) : base(deserializing) { }
87    protected Problem(Problem original, Cloner cloner)
88          : base(original, cloner) {
89    }
90    public override IDeepCloneable Clone(Cloner cloner) {
91      return new Problem(this, cloner);
92    }
93    public Problem() : base(
94          new DecodingEvaluator<Permutation, PackingPosition, PackingShape, PackingItem>()) {
95    }
96
97    #region Problem instance handling
98    public void Load(BPPData data) {
99
100      BestKnownQuality = data.BestKnownQuality.HasValue ? new DoubleValue(data.BestKnownQuality.Value) : null;
101
102      PackingBinMeasures = data.BinShape;
103      PackingItemMeasures = new ItemList<PackingItem>(data.Items);
104
105      ApplyHorizontalOrientation();
106      SortItems();
107      PackingItemsParameter.Value.Value = PackingItemMeasures.Count;
108      LowerBoundParameter.Value.Value = CalculateLowerBound();
109    }
110
111
112    public BPPData Export() {
113      return new BPPData {
114        Name = Name,
115        Description = Description,
116        BinShape = PackingBinMeasures,
117        Items = PackingItemMeasures.ToArray()
118      };
119    }
120    #endregion
121
122    #region Helpers
123    protected override void InitializeDecoder() {
124      // Operators.RemoveAll(op => op is I3DOperator); TODO
125
126      PackingSolutionDecoderParameter.ValidValues.Clear();
127      if (SolutionCreator is PackingSequenceRandomCreator) {
128        PackingSolutionDecoderParameter.ValidValues.UnionWith(ApplicationManager.Manager.GetInstances<I2DPSDecoder>());
129      } /*else if (SolutionCreator is GroupingVectorRandomCreator) {
130        PackingSolutionDecoderParameter.ValidValues.UnionWith(ApplicationManager.Manager.GetInstances<I2DGVDecoder>());
131      } else if (SolutionCreator is MultiComponentVectorRandomCreator) {
132        PackingSolutionDecoderParameter.ValidValues.UnionWith(ApplicationManager.Manager.GetInstances<I2DMCVDecoder>());
133      } */ else {
134        string error = "The given problem does not support the selected solution-creator.";
135        ErrorHandling.ShowErrorDialog(error, null);
136      }
137    }
138
139    protected override IEvaluator CreateDefaultEvaluator() {
140      return new PackingRatioEvaluator();
141    }
142
143    protected override void InitializeProblemData() {
144      Load(DefaultInstance);
145    }
146
147    protected override void RemoveTooBigItems() {
148      PackingItemMeasures.RemoveAll(pi =>
149        !PackingBinMeasures.Encloses(new PackingPosition(0, 0, 0, false), pi) &&
150        !PackingBinMeasures.Encloses(new PackingPosition(0, 0, 0, true), pi));
151    }
152    #endregion
153  }
154}
Note: See TracBrowser for help on using the repository browser.