Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking/3.3/Problem/RegularIdenticalBinPackingProblem.cs @ 12966

Last change on this file since 12966 was 9598, checked in by jhelm, 12 years ago

#1966: Fixed a bug which caused the stacking-contraint to be wrongly enforced; Did some cleanup;

File size: 13.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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 System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Problems.BinPacking.Interfaces;
27using HeuristicLab.Problems.BinPacking.Shapes;
28using HeuristicLab.Optimization;
29using HeuristicLab.Core;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Common;
32using HeuristicLab.Parameters;
33using HeuristicLab.Encodings.PermutationEncoding;
34using HeuristicLab.Problems.BinPacking.Evaluators;
35using HeuristicLab.Problems.BinPacking.Decoders;
36using HeuristicLab.PluginInfrastructure;
37using HeuristicLab.Data;
38using HeuristicLab.Encodings.PackingEncoding.PackingPlan;
39using HeuristicLab.Problems.BinPacking.Analyzers;
40using HeuristicLab.Encodings.PackingEncoding.PackingSequence;
41using HeuristicLab.Encodings.PackingEncoding.Interfaces;
42using HeuristicLab.Encodings.PackingEncoding.GroupingVector;
43using HeuristicLab.Problems.Instances;
44using HeuristicLab.Encodings.PackingEncoding.MultiComponentVector;
45using HeuristicLab.Problems.BinPacking.Dimensions;
46
47namespace HeuristicLab.Problems.BinPacking.Problem {
48  [Item("RegularIdenticalBinPackingProblem", "Represents a bin-packing problem-instance using only bins with identical measures and bins/items with regular shapes (e.g. rectangle, cuboid).")]
49  [StorableClass]
50  public abstract class RegularIdenticalBinPackingProblem<D, B, I> : BinPackingProblem<D, B, I>, IProblemInstanceConsumer<BPPData>,  IProblemInstanceExporter<BPPData>
51    where D : class, IPackingDimensions
52    where B : PackingShape<D>, IPackingBin, IRegularPackingShape, new()
53    where I : PackingShape<D>, IPackingItem, IRegularPackingShape, new() {       
54
55    #region Parameter Properties
56    public ValueParameter<B> PackingBinMeasuresParameter {
57      get { return (ValueParameter<B>)Parameters["PackingBinMeasures"]; }
58    }
59    public ConstrainedValueParameter<IPackingSolutionDecoder> PackingSolutionDecoderParameter {
60      get { return (ConstrainedValueParameter<IPackingSolutionDecoder>)Parameters["PackingSolutionDecoder"]; }
61    }
62    #endregion
63
64    #region Properties
65    public B PackingBinMeasures {
66      get { return PackingBinMeasuresParameter.Value; }
67      set { PackingBinMeasuresParameter.Value = value; }
68    }
69    public PackingSolutionDecoder<D,B,I> PackingSolutionDecoder {
70      get { return PackingSolutionDecoderParameter.Value as PackingSolutionDecoder<D, B, I>; }
71      set { PackingSolutionDecoderParameter.Value = value; }
72    }
73    #endregion
74
75
76    [StorableConstructor]
77    protected RegularIdenticalBinPackingProblem(bool deserializing) : base(deserializing) { }
78    protected RegularIdenticalBinPackingProblem(RegularIdenticalBinPackingProblem<D,B,I> original, Cloner cloner)
79      : base(original, cloner) {
80      InitializeEventHandlers();
81    }
82
83    protected RegularIdenticalBinPackingProblem(IPackingPlanEvaluationAlgorithm e) : this(e, new MultiComponentVectorRandomCreator()) { }
84    protected RegularIdenticalBinPackingProblem(IPackingPlanEvaluationAlgorithm e, IPackingSolutionCreator c)
85      : base(e, c) {
86      Parameters.Add(new ValueParameter<B>("PackingBinMeasures", "Packing-bin data defining the measures of the used bins.", new B()));
87      Parameters.Add(new ValueParameter<IPackingPlanEvaluator>("PackingPlanEvaluator", "The evaluator is used to determine the quality of a solution.", CreateDefaultEvaluator()));
88      Parameters.Add(new ConstrainedValueParameter<IPackingSolutionDecoder>("PackingSolutionDecoder", "The operator that decodes the representation and creates a packing plan."));
89      this.Maximization.Value = true;
90      InitializeProblemInstance();
91      InitializeOperators();
92      InitializeEventHandlers();
93    }
94
95   
96
97
98    #region Helpers
99    protected override void InitializeProblemInstance() {
100      InitializeProblemData();
101      ApplyHorizontalOrientation();
102      SortItems();
103      RemoveTooBigItems();
104      PackingItemsParameter.Value.Value = PackingItemMeasures.Count;
105      LowerBoundParameter.Value.Value = CalculateLowerBound();
106    }
107    protected abstract void RemoveTooBigItems();
108    protected abstract void InitializeProblemData();
109    protected abstract IPackingPlanEvaluator CreateDefaultEvaluator();
110    private void ApplyHorizontalOrientation() {
111      PackingBinMeasures.ApplyHorizontalOrientation();
112      foreach (IRegularPackingShape shape in PackingItemMeasures) {
113        shape.ApplyHorizontalOrientation();
114      }
115    }
116    private void SortItems() {
117      PackingItemMeasures.Sort((x, y) => y.CompareTo(x));
118    }
119    private int CalculateLowerBound() {
120      //This is the obvious continuous lower bound calculation; Martello and Vigo proposed a better way but it has not been implemented yet;
121      int items = PackingItemMeasures.Select(x => x.MultipliedMeasures).Sum();
122      int bin = PackingBinMeasures.MultipliedMeasures;
123      return (items + bin - 1)/(bin);
124    }
125
126    protected override void InitializeOperators() {
127      Operators.Clear(); 
128      Operators.Add(new BestBinPackingSolutionAnalyzer<D, B, I>());
129
130      ApplyEncoding();
131      ParameterizeOperators();
132    }
133    private void ApplyEncoding() {
134      if (SolutionCreator.GetType() == typeof(PackingSequenceRandomCreator)) {
135        Operators.AddRange(ApplicationManager.Manager.GetInstances<IPackingSequenceOperator>());
136        InitializeDecoder();
137      } else if (SolutionCreator.GetType() == typeof(GroupingVectorRandomCreator)) {
138        Operators.AddRange(ApplicationManager.Manager.GetInstances<IGroupingVectorOperator>());
139        InitializeDecoder();
140      } else if (SolutionCreator.GetType() == typeof(MultiComponentVectorRandomCreator)) {
141        Operators.AddRange(ApplicationManager.Manager.GetInstances<IMultiComponentVectorOperator>());
142        InitializeDecoder();
143      }
144    }
145    private void ParameterizeOperators() {   
146      foreach (var op in Operators.OfType<BestBinPackingSolutionAnalyzer<D, B, I>>()) {
147        op.QualityParameter.ActualName = PackingPlanEvaluator.QualityParameter.ActualName;
148      }
149
150      Evaluator.PackingSolutionDecoderParameter.ActualName = PackingSolutionDecoderParameter.Name;
151      Evaluator.PackingSolutionDecoderParameter.Hidden = true;
152      Evaluator.PackingPlanEvaluatorParameter.ActualName = PackingPlanEvaluatorParameter.Name;
153      Evaluator.PackingPlanEvaluatorParameter.Hidden = true;
154      Evaluator.QualityParameter.ActualName = PackingPlanEvaluator.QualityParameter.ActualName;
155      Evaluator.QualityParameter.Hidden = true;
156
157      if (PackingSolutionDecoder != null)
158        PackingSolutionDecoder.EncodedSolutionParameter.ActualName = SolutionCreator.EncodedSolutionParameter.ActualName;
159
160      if (PackingSolutionDecoder != null) {
161        PackingPlanEvaluator.PackingPlanParameter.ActualName = PackingSolutionDecoder.PackingPlanParameter.ActualName;
162        PackingPlanEvaluator.PackingPlanParameter.Hidden = true;
163      } else {
164        PackingPlanEvaluator.PackingPlanParameter.ActualName = PackingPlanEvaluator.PackingPlanParameter.Name;
165        PackingPlanEvaluator.PackingPlanParameter.Hidden = false;
166      }
167
168      foreach (var op in Operators.OfType<IPackingSolutionManipulator>()) {
169        op.EncodedSolutionParameter.ActualName = SolutionCreator.EncodedSolutionParameter.ActualName;
170        op.EncodedSolutionParameter.Hidden = true;
171      }
172
173      foreach (var op in Operators.OfType<IPackingSolutionCrossover>()) {
174        op.ChildParameter.ActualName = SolutionCreator.EncodedSolutionParameter.ActualName;
175        op.ChildParameter.Hidden = true;
176        op.ParentsParameter.ActualName = SolutionCreator.EncodedSolutionParameter.ActualName;
177        op.ParentsParameter.Hidden = true;
178      }
179
180      foreach (var op in Operators.OfType<BestBinPackingSolutionAnalyzer<D, B, I>>()) {
181        op.QualityParameter.ActualName = PackingPlanEvaluator.QualityParameter.ActualName;
182        if (PackingSolutionDecoder != null) {
183          op.PackingPlanParameter.ActualName = PackingSolutionDecoder.PackingPlanParameter.ActualName;
184          op.PackingPlanParameter.Hidden = true;
185        } else {
186          op.PackingPlanParameter.ActualName = op.PackingPlanParameter.Name;
187          op.PackingPlanParameter.Hidden = false;
188        }
189      }
190
191    }
192
193    protected override void InitializeEventHandlers() {
194      PackingPlanEvaluatorParameter.ValueChanged += PackingPlanEvaluatorParameter_ValueChanged;
195      PackingPlanEvaluator.QualityParameter.ActualNameChanged += PackingPlanEvaluator_QualityParameter_ActualNameChanged;
196      SolutionCreatorParameter.ValueChanged += SolutionCreatorParameter_ValueChanged;
197      SolutionCreator.EncodedSolutionParameter.ActualNameChanged += SolutionCreator_EncodedSolutionParameter_ActualNameChanged;
198      PackingSolutionDecoderParameter.ValueChanged += PackingSolutionDecoderParameter_ValueChanged;
199      if (PackingSolutionDecoder != null) PackingSolutionDecoder.PackingPlanParameter.ActualNameChanged += PackingSolutionDecoder_PackingPlanParameter_ActualNameChanged;
200    }
201
202    #endregion
203
204    #region Events
205    protected override void OnSolutionCreatorChanged() {
206      InitializeOperators();
207    }
208    protected override void OnEvaluatorChanged() {
209      base.OnEvaluatorChanged();
210      ParameterizeOperators();
211    }
212    private void PackingPlanEvaluatorParameter_ValueChanged(object sender, EventArgs eventArgs) {
213      PackingPlanEvaluator.QualityParameter.ActualNameChanged += PackingPlanEvaluator_QualityParameter_ActualNameChanged;
214      ParameterizeOperators();
215    }
216    private void PackingPlanEvaluator_QualityParameter_ActualNameChanged(object sender, EventArgs eventArgs) {
217      ParameterizeOperators();
218    }
219    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs eventArgs) {
220      SolutionCreator.EncodedSolutionParameter.ActualNameChanged += SolutionCreator_EncodedSolutionParameter_ActualNameChanged;
221      ParameterizeOperators();
222    }
223    private void SolutionCreator_EncodedSolutionParameter_ActualNameChanged(object sender, EventArgs eventArgs) {
224      ParameterizeOperators();
225    }
226    private void PackingSolutionDecoderParameter_ValueChanged(object sender, EventArgs eventArgs) {
227      if (PackingSolutionDecoder != null) PackingSolutionDecoder.PackingPlanParameter.ActualNameChanged += PackingSolutionDecoder_PackingPlanParameter_ActualNameChanged;
228      ParameterizeOperators();
229    }
230    private void PackingSolutionDecoder_PackingPlanParameter_ActualNameChanged(object sender, EventArgs eventArgs) {
231      ParameterizeOperators();
232    }
233    #endregion
234
235
236    #region Problem instance handling
237    public virtual void Load(BPPData data) {
238      var realData = data as RealBPPData;
239      var binData = new B();
240      binData.InitializeFromMeasures (data.BinMeasures);
241
242      var itemData = new ItemList<I>(data.Items);
243      for (int j = 0; j < data.Items; j++) {
244        var item = new I();
245        item.InitializeFromMeasures(data.ItemMeasures[j]);
246        item.AddTargetBinMeasures(data.BinMeasures);
247        if (realData != null) {
248          item.Weight = realData.ItemWeights[j];
249          item.Material = realData.ItemMaterials[j];
250        }
251        itemData.Add(item);
252      }
253
254      BestKnownQuality = data.BestKnownQuality.HasValue ? new DoubleValue(data.BestKnownQuality.Value) : null;
255                                                                   
256      PackingBinMeasures = binData;   
257      //PackingItemsParameter.Value.Value = data.Items;
258      PackingItemMeasures = itemData;
259
260      ApplyHorizontalOrientation();
261      SortItems();
262      PackingItemsParameter.Value.Value = PackingItemMeasures.Count;
263      LowerBoundParameter.Value.Value = CalculateLowerBound();
264    }           
265
266    public BPPData Export() {
267      var result = new BPPData{
268        Name = Name,
269        Description = Description,
270        Items = PackingItemsParameter.Value.Value,
271        BinMeasures = PackingBinMeasures.ToArray()
272      };
273
274      var itemMeasures = new int[result.Items][];
275      int i = 0;
276      foreach (var item in PackingItemMeasures) {
277        itemMeasures[i] = item.ToArray();
278        i++;
279      }
280      result.ItemMeasures = itemMeasures;
281      return result;
282    }
283    #endregion
284  }
285}
Note: See TracBrowser for help on using the repository browser.