source: branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking/3.3/Analyzers/BestBinPackingSolutionAnalyzer.cs @ 14048

Last change on this file since 14048 was 14048, checked in by gkronber, 5 years ago

#1966: renamed *PackingDimension -> PackingPosition

File size: 4.9 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 System.Linq;
23using HeuristicLab.Collections;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.PackingEncoding;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.BinPacking {
33  [Item("BestBinPackingSolutionAnalyzer", "An operator for analyzing the best solution of BinPacking Problems given in packingPlan-representation.")]
34  [StorableClass]
35  public class BestBinPackingSolutionAnalyzer<D, B, I> : BinPackingAnalyzer<D, B, I>, IStochasticOperator
36    where D : class, IPackingPosition
37    where B : PackingShape<D>
38    where I : PackingShape<D>, IPackingItem {
39
40    public ILookupParameter<IRandom> RandomParameter {
41      get { return (LookupParameter<IRandom>)Parameters["Random"]; }
42    }
43    public IRandom Random {
44      get { return RandomParameter.ActualValue; }
45    }
46
47    [StorableConstructor]
48    protected BestBinPackingSolutionAnalyzer(bool deserializing) : base(deserializing) { }
49    protected BestBinPackingSolutionAnalyzer(BestBinPackingSolutionAnalyzer<D, B, I> original, Cloner cloner)
50      : base(original, cloner) {
51    }
52    public BestBinPackingSolutionAnalyzer()
53      : base() {
54      Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator."));
55    }
56    public override IDeepCloneable Clone(Cloner cloner) {
57      return new BestBinPackingSolutionAnalyzer<D, B, I>(this, cloner);
58    }
59
60    public override IOperation Apply() {
61      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
62      ItemArray<PackingPlan<D, B, I>> solutions = PackingPlanParameter.ActualValue;
63      ResultCollection results = ResultsParameter.ActualValue;
64      bool max = MaximizationParameter.ActualValue.Value;
65      DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
66
67      int i = -1;
68      if (!max)
69        i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
70      else i = qualities.Select((x, index) => new { index, x.Value }).OrderByDescending(x => x.Value).First().index;
71
72      if (bestKnownQuality == null ||
73          max && qualities[i].Value > bestKnownQuality.Value ||
74          !max && qualities[i].Value < bestKnownQuality.Value) {
75        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
76        BestKnownSolutionParameter.ActualValue = (PackingPlan<D, B, I>)solutions[i].Clone();
77      }
78
79      PackingPlan<D, B, I> bestSolution = BestSolutionParameter.ActualValue;
80      if (bestSolution == null) {
81        bestSolution = (PackingPlan<D, B, I>)solutions[i].Clone();
82        bestSolution.Quality = (DoubleValue)qualities[i].Clone();
83        BestSolutionParameter.ActualValue = bestSolution;
84        results.Add(new Result("Best Packing Solution", bestSolution));
85      } else {
86        if (max && bestSolution.Quality.Value < qualities[i].Value ||
87          !max && bestSolution.Quality.Value > qualities[i].Value) {
88          bestSolution.Quality.Value = qualities[i].Value;
89          //bestSolution.PackingItemPositions = new ObservableDictionary<int, D> (solutions[i].PackingItemPositions);
90          //bestSolution.PackingBinMeasures = new ObservableDictionary<int, B>(solutions[i].PackingBinMeasures);
91          bestSolution.BinPackings = new ObservableList<BinPacking<D, B, I>>(solutions[i].BinPackings);
92        }
93      }
94      string binUtilKey = "Overall Bin Utilization";
95      DoubleValue binUtil = BinUtilizationRegularIdenticalBinEvaluator<D, B, I>.CalculateBinUtilization(bestSolution);
96      if (!results.ContainsKey(binUtilKey))
97        results.Add(new Result(binUtilKey, binUtil));
98      else
99        results[binUtilKey].Value = binUtil;
100
101
102      string nocKey = "Nr Of Containers";
103      if (!results.ContainsKey(nocKey))
104        results.Add(new Result(nocKey, new IntValue(bestSolution.NrOfBins)));
105      else
106        results[nocKey].Value = new IntValue(bestSolution.NrOfBins);
107
108      return base.Apply();
109    }
110  }
111}
Note: See TracBrowser for help on using the repository browser.