Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13401 was 13032, checked in by gkronber, 9 years ago

#1966:

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