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

Last change on this file since 13605 was 13605, checked in by gkronber, 6 years ago

#1966 refactoring

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