Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1966: simplified parsers

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