#region License Information
/* HeuristicLab
* Copyright (C) 2002-2015 Joseph Helm and Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System.Linq;
using HeuristicLab.Collections;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Problems.BinPacking.Interfaces;
using HeuristicLab.Encodings.PackingEncoding.PackingPlan;
using HeuristicLab.Problems.BinPacking.Shapes;
using HeuristicLab.Problems.BinPacking.Evaluators;
namespace HeuristicLab.Problems.BinPacking.Analyzers {
[Item("BestBinPackingSolutionAnalyzer", "An operator for analyzing the best solution of BinPacking Problems given in packingPlan-representation.")]
[StorableClass]
public class BestBinPackingSolutionAnalyzer : BinPackingAnalyzer, IStochasticOperator
where D : class, IPackingDimensions
where B : PackingShape, IPackingBin, IRegularPackingShape
where I : PackingShape, IPackingItem, IRegularPackingShape {
protected BestBinPackingSolutionAnalyzer(BestBinPackingSolutionAnalyzer original, Cloner cloner)
: base(original, cloner) {
}
public override IDeepCloneable Clone(Cloner cloner) {
return new BestBinPackingSolutionAnalyzer(this, cloner);
}
public ILookupParameter RandomParameter {
get { return (LookupParameter)Parameters["Random"]; }
}
public IRandom Random {
get { return RandomParameter.ActualValue; }
}
public BestBinPackingSolutionAnalyzer()
: base() {
Parameters.Add(new LookupParameter("Random", "The pseudo random number generator."));
}
public override IOperation Apply() {
ItemArray qualities = QualityParameter.ActualValue;
ItemArray> solutions = PackingPlanParameter.ActualValue;
ResultCollection results = ResultsParameter.ActualValue;
bool max = MaximizationParameter.ActualValue.Value;
DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
int i = -1;
if (!max)
i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
else i = qualities.Select((x, index) => new { index, x.Value }).OrderByDescending(x => x.Value).First().index;
if (bestKnownQuality == null ||
max && qualities[i].Value > bestKnownQuality.Value ||
!max && qualities[i].Value < bestKnownQuality.Value) {
BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
BestKnownSolutionParameter.ActualValue = (PackingPlan)solutions[i].Clone();
}
PackingPlan bestSolution = BestSolutionParameter.ActualValue;
if (bestSolution == null) {
bestSolution = (PackingPlan)solutions[i].Clone();
bestSolution.Quality = (DoubleValue)qualities[i].Clone();
BestSolutionParameter.ActualValue = bestSolution;
results.Add(new Result("Best Packing Solution", bestSolution));
} else {
if (max && bestSolution.Quality.Value < qualities[i].Value ||
!max && bestSolution.Quality.Value > qualities[i].Value) {
bestSolution.Quality.Value = qualities[i].Value;
//bestSolution.PackingItemPositions = new ObservableDictionary (solutions[i].PackingItemPositions);
//bestSolution.PackingBinMeasures = new ObservableDictionary(solutions[i].PackingBinMeasures);
bestSolution.BinPackings = new ObservableList>(solutions[i].BinPackings);
}
}
string binUtilKey = "Overall Bin Utilization";
DoubleValue binUtil = BinUtilizationRegularIdenticalBinEvaluator.CalculateBinUtilization(bestSolution);
if (!results.ContainsKey(binUtilKey))
results.Add(new Result(binUtilKey, binUtil));
else
results[binUtilKey].Value = binUtil;
string nocKey = "Nr Of Containers";
if (!results.ContainsKey(nocKey))
results.Add(new Result(nocKey, new IntValue (bestSolution.NrOfBins)));
else
results[nocKey].Value = new IntValue (bestSolution.NrOfBins);
return base.Apply();
}
}
}