Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11193 was 9593, checked in by jhelm, 11 years ago

#1966: Applied some heavy refactoring on the decoder-classes and cleaned up the code a bit;

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