Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1966_HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking/3.3/PackingMoveEvaluator.cs @ 16724

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

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

File size: 5.2 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.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Encodings.PackingEncoding;
26using HeuristicLab.Encodings.PermutationEncoding;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.BinPacking {
33  [Item("PackingMoveEvaluator", "A base class for operators which evaluate Packing-Solution moves.")]
34  [StorableClass]
35  public abstract class PackingMoveEvaluator<D, B, I, M> : SingleSuccessorOperator, IPackingMoveEvaluator
36    where D : class, IPackingPosition
37    where B : PackingShape<D>
38    where I : PackingShape<D>, IPackingItem
39    where M : IItem {     
40    public override bool CanChangeName {
41      get { return false; }
42    }
43
44    public ILookupParameter<DoubleValue> QualityParameter {
45      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
46    }
47    public ILookupParameter<DoubleValue> MoveQualityParameter {
48      get { return (ILookupParameter<DoubleValue>)Parameters["MoveQuality"]; }
49    }
50    public ILookupParameter<ItemList<I>> PackingItemMeasuresParameter {
51      get { return (LookupParameter<ItemList<I>>)Parameters["PackingItemMeasures"]; }
52    }
53    public ILookupParameter<B> PackingBinMeasuresParameter {
54      get { return (LookupParameter<B>)Parameters["PackingBinMeasures"]; }
55    }
56    public IValueLookupParameter<IPackingSolutionDecoder> PackingSolutionDecoderParameter {
57      get { return (IValueLookupParameter<IPackingSolutionDecoder>)Parameters["PackingSolutionDecoder"]; }
58    }
59    public IValueLookupParameter<IEvaluator> PackingPlanEvaluatorParameter {
60      get { return (IValueLookupParameter<IEvaluator>)Parameters["PackingPlanEvaluator"]; }
61    }
62    protected ScopeParameter CurrentScopeParameter {
63      get { return (ScopeParameter)Parameters["CurrentScope"]; }
64    }
65    public ILookupParameter<IPackingMove<M>> PackingMoveParameter {
66      get { return (ILookupParameter<IPackingMove<M>>)Parameters["PackingMove"]; }
67    } 
68   
69    [StorableConstructor]
70    protected PackingMoveEvaluator(bool deserializing) : base(deserializing) { }
71    protected PackingMoveEvaluator(PackingMoveEvaluator<D, B, I, M> original, Cloner cloner) : base(original, cloner) { }
72    protected PackingMoveEvaluator()
73      : base() {
74      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of a packing solution."));
75      Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The evaluated quality of a move on a packing solution."));
76      Parameters.Add(new LookupParameter<ItemList<I>>("PackingItemMeasures", "Packing-item data taken from the bin-packing problem-instance."));
77      Parameters.Add(new LookupParameter<B>("PackingBinMeasures", "Packing-bin data taken from the bin-packing problem-instance."));
78      Parameters.Add(new ValueLookupParameter<IPackingSolutionDecoder>("PackingSolutionDecoder", "The decoding operator that is used to calculate a packing plan from the used representation."));
79      Parameters.Add(new ValueLookupParameter<IEvaluator>("PackingPlanEvaluator", "The actual packing plan evaluation operator."));
80      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope where the decoded solution should be added to."));
81      Parameters.Add(new LookupParameter<IPackingMove<M>>("PackingMove", "The move to evaluate."));
82    }
83
84
85
86    public override IOperation Apply() {
87      IPackingMove<M> move = PackingMoveParameter.ActualValue;
88      var newSolution = move.GetSolutionAfterMove();
89      CurrentScopeParameter.ActualValue.Variables.Add(new Variable("MovedSolution", newSolution));
90
91      PackingPlan<D, B, I> packingPlan = (PackingSolutionDecoderParameter.ActualValue as PackingSolutionDecoder<D, B, I>).
92        CreatePackingPlanFromEncoding(newSolution, PackingBinMeasuresParameter.ActualValue, PackingItemMeasuresParameter.ActualValue);
93
94
95      CurrentScopeParameter.ActualValue.Variables.Add(new Variable("PackingPlanAfterMove", packingPlan));
96
97      var quality = PackingRatioEvaluator<D, B, I>.CalculatePackingRatio(packingPlan);
98
99      double moveQuality = quality;
100
101      if (MoveQualityParameter.ActualValue == null) MoveQualityParameter.ActualValue = new DoubleValue(moveQuality);
102      else MoveQualityParameter.ActualValue.Value = moveQuality;
103      return base.Apply();
104    }
105
106
107  }
108}
Note: See TracBrowser for help on using the repository browser.