Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.BinPacking/old files/Problem2d.cs @ 14710

Last change on this file since 14710 was 14146, checked in by gkronber, 8 years ago

#1966: new implementation for 2d bin packing problem with permutation encoding

File size: 7.8 KB
RevLine 
[9348]1#region License Information
2/* HeuristicLab
[13032]3 * Copyright (C) 2002-2015 Joseph Helm and Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[9348]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;
[14039]29using HeuristicLab.Data;
[14046]30using HeuristicLab.Problems.BinPacking;
[9348]31
[14046]32namespace HeuristicLab.Problems.BinPacking2D {
[13460]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.")]
[9348]34  [StorableClass]
[13460]35  [Creatable(CreatableAttribute.Categories.CombinatorialProblems, Priority = 300)]
[14049]36  public class Problem : Problem<PackingPosition, PackingShape, PackingItem>, IProblemInstanceConsumer<BPPData>, IProblemInstanceExporter<BPPData> {
[9348]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.",
[14055]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}
[9348]81      },
82    };
83    #endregion
84
85    [StorableConstructor]
[14049]86    protected Problem(bool deserializing) : base(deserializing) { }
87    protected Problem(Problem original, Cloner cloner)
[14055]88          : base(original, cloner) {
[9348]89    }
90    public override IDeepCloneable Clone(Cloner cloner) {
[14049]91      return new Problem(this, cloner);
[9348]92    }
[14049]93    public Problem() : base(
[14055]94          new DecodingEvaluator<Permutation, PackingPosition, PackingShape, PackingItem>()) {
[9348]95    }
96
[14039]97    #region Problem instance handling
[14040]98    public void Load(BPPData data) {
[9348]99
[14039]100      BestKnownQuality = data.BestKnownQuality.HasValue ? new DoubleValue(data.BestKnownQuality.Value) : null;
101
[14055]102      PackingBinMeasures = data.BinShape;
103      PackingItemMeasures = new ItemList<PackingItem>(data.Items);
[14039]104
105      ApplyHorizontalOrientation();
106      SortItems();
107      PackingItemsParameter.Value.Value = PackingItemMeasures.Count;
108      LowerBoundParameter.Value.Value = CalculateLowerBound();
109    }
110
111
[14040]112    public BPPData Export() {
[14055]113      return new BPPData {
[14039]114        Name = Name,
115        Description = Description,
[14055]116        BinShape = PackingBinMeasures,
117        Items = PackingItemMeasures.ToArray()
[14039]118      };
119    }
120    #endregion
121
[9348]122    #region Helpers
123    protected override void InitializeDecoder() {
[13611]124      // Operators.RemoveAll(op => op is I3DOperator); TODO
[9495]125
[9563]126      PackingSolutionDecoderParameter.ValidValues.Clear();
[14039]127      if (SolutionCreator is PackingSequenceRandomCreator) {
[9563]128        PackingSolutionDecoderParameter.ValidValues.UnionWith(ApplicationManager.Manager.GetInstances<I2DPSDecoder>());
[14128]129      } /*else if (SolutionCreator is GroupingVectorRandomCreator) {
[9563]130        PackingSolutionDecoderParameter.ValidValues.UnionWith(ApplicationManager.Manager.GetInstances<I2DGVDecoder>());
[14039]131      } else if (SolutionCreator is MultiComponentVectorRandomCreator) {
[9563]132        PackingSolutionDecoderParameter.ValidValues.UnionWith(ApplicationManager.Manager.GetInstances<I2DMCVDecoder>());
[14128]133      } */ else {
[9348]134        string error = "The given problem does not support the selected solution-creator.";
[13460]135        ErrorHandling.ShowErrorDialog(error, null);
[9348]136      }
137    }
138
[14050]139    protected override IEvaluator CreateDefaultEvaluator() {
140      return new PackingRatioEvaluator();
[9348]141    }
142
143    protected override void InitializeProblemData() {
144      Load(DefaultInstance);
145    }
[9598]146
147    protected override void RemoveTooBigItems() {
148      PackingItemMeasures.RemoveAll(pi =>
[14048]149        !PackingBinMeasures.Encloses(new PackingPosition(0, 0, 0, false), pi) &&
150        !PackingBinMeasures.Encloses(new PackingPosition(0, 0, 0, true), pi));
[9598]151    }
[9348]152    #endregion
153  }
154}
Note: See TracBrowser for help on using the repository browser.