#region License Information
/* HeuristicLab
* Copyright (C) 2002-2013 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;
using System.Linq;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
[Item("SolutionToPopulationAnalyzer", "An operator that analyzes the diversity of solutions compared to the population.")]
[StorableClass]
public class SolutionToPopulationAnalyzer : InitializableOperator, IStatefulItem {
private const string ResultsParameterName = "Results";
private const string GenerationsParameterName = "Generations";
#region Parameter properties
public IValueLookupParameter MaximizationParameter {
get { return (IValueLookupParameter)Parameters["Maximization"]; }
}
public ILookupParameter ResultsParameter {
get { return (ILookupParameter)Parameters[ResultsParameterName]; }
}
public ILookupParameter GenerationsParameter {
get { return (ILookupParameter)Parameters[GenerationsParameterName]; }
}
public ILookupParameter QualityParameter {
get { return (ILookupParameter)Parameters["Quality"]; }
}
public ILookupParameter SolutionParameter {
get { return (ILookupParameter)Parameters["Solution"]; }
}
public ILookupParameter> OperatorsParameter {
get { return (ILookupParameter>)Parameters["Operators"]; }
}
public IValueParameter SimilarityCalculatorParameter {
get { return (IValueParameter)Parameters["SimilarityCalculator"]; }
}
public IValueParameter ChartPostfixParameter {
get { return (IValueParameter)Parameters["ChartPostfix"]; }
}
public ILookupParameter BestKnownQualityParameter {
get { return (ILookupParameter)Parameters["BestKnownQuality"]; }
}
public ILookupParameter WorstKnownQualityParameter {
get { return (ILookupParameter)Parameters["WorstKnownQuality"]; }
}
#endregion
#region Properties
public ResultCollection Results {
get { return ResultsParameter.ActualValue; }
}
[Storable]
private ScatterPlotHelper populationDiversityPlot, populationQualityPlot, qualityPlot;
[Storable]
private int cnt = 0;
[Storable]
private int lastGeneration = 0;
[Storable]
private bool scalingFinished = false;
#endregion
[StorableConstructor]
private SolutionToPopulationAnalyzer(bool deserializing) : base(deserializing) { }
private SolutionToPopulationAnalyzer(SolutionToPopulationAnalyzer original, Cloner cloner)
: base(original, cloner) {
cnt = original.cnt;
lastGeneration = original.lastGeneration;
populationDiversityPlot = (ScatterPlotHelper)original.populationDiversityPlot.Clone(cloner);
populationQualityPlot = (ScatterPlotHelper)original.populationQualityPlot.Clone(cloner);
qualityPlot = (ScatterPlotHelper)original.qualityPlot.Clone(cloner);
scalingFinished = original.scalingFinished;
}
public SolutionToPopulationAnalyzer()
: base() {
Parameters.Add(new LookupParameter(ResultsParameterName, "The results collection where the analysis values should be stored."));
Parameters.Add(new LookupParameter(GenerationsParameterName, "Nr of generations."));
Parameters.Add(new LookupParameter("Quality", "The evaluated quality of the child solution."));
QualityParameter.ActualName = "TSPTourLength";
Parameters.Add(new LookupParameter("Solution"));
SolutionParameter.ActualName = "TSPTour";
Parameters.Add(new ValueParameter("SimilarityCalculator"));
Parameters.Add(new ValueParameter("ChartPostfix", new StringValue(string.Empty)));
Parameters.Add(new LookupParameter>("Operators", "The operators and items that the problem provides to the algorithms."));
Parameters.Add(new ValueLookupParameter("Maximization", "True if the problem is a maximization problem, false otherwise"));
Parameters.Add(new LookupParameter("BestKnownQuality", "The quality of the best known solution of this problem."));
Parameters.Add(new LookupParameter("WorstKnownQuality", "The quality of the worst known solution of this problem."));
populationDiversityPlot = new ScatterPlotHelper(false, true, false, true);
populationQualityPlot = new ScatterPlotHelper(false, true, true, true);
qualityPlot = new ScatterPlotHelper(false, true, true, true);
}
public override IDeepCloneable Clone(Cloner cloner) {
return new SolutionToPopulationAnalyzer(this, cloner);
}
protected override void InitializeAction() {
if (SimilarityCalculatorParameter.Value == null) {
SimilarityCalculatorParameter.Value = OperatorsParameter.ActualValue.OfType().FirstOrDefault();
}
populationDiversityPlot.InitializePlot(Results, "Solution to Population Diversity " + ChartPostfixParameter.Value.Value, "Solution Index", "Diversity");
populationQualityPlot.InitializePlot(Results, "Solution Quality Difference to Population " + ChartPostfixParameter.Value.Value, "Solution Index", "Quality Difference");
qualityPlot.InitializePlot(Results, "Solution Quality " + ChartPostfixParameter.Value.Value, "Solution Index", "Quality");
Reset();
}
public override IOperation Apply() {
Initialize();
string curGenStr = GenerationsParameter.ActualValue.Value.ToString();
double quality = QualityParameter.ActualValue.Value;
IItem solution = SolutionParameter.ActualValue;
string qualityVariableName = QualityParameter.ActualName;
string solutionVariableName = SolutionParameter.ActualName;
ISingleObjectiveSolutionSimilarityCalculator simCalc = SimilarityCalculatorParameter.Value;
Scope artificialSolutionScope = new Scope();
artificialSolutionScope.Variables.Add(new Variable(solutionVariableName, solution));
IScope oldPop = ReverseScopeTreeLookup("Remaining");
if (oldPop == null)
throw new Exception("Couldn't find the remaining scope");
if (GenerationsParameter.ActualValue.Value != 0) {
if (GenerationsParameter.ActualValue.Value > lastGeneration) {
Reset();
}
double oldPopQuality = 0.0;
double solToPopDiversity = 0.0;
int popSize = 0;
foreach (IScope oldSolScope in oldPop.SubScopes) {
double curQuality = ((DoubleValue)oldSolScope.Variables[qualityVariableName].Value).Value;
IItem curSol = oldSolScope.Variables[solutionVariableName].Value;
oldPopQuality += curQuality;
solToPopDiversity += simCalc.CalculateSolutionSimilarity(artificialSolutionScope, oldSolScope);
popSize++;
}
if (WorstKnownQualityParameter.ActualValue != null && !scalingFinished) {
scalingFinished = true;
double bkQuality = BestKnownQualityParameter.ActualValue.Value;
double wkQuality = WorstKnownQualityParameter.ActualValue.Value;
if (MaximizationParameter.ActualValue.Value) {
if (populationQualityPlot.Max == double.MinValue) {
populationQualityPlot.Max = bkQuality - wkQuality;
qualityPlot.Max = bkQuality;
populationQualityPlot.Min = 0;
qualityPlot.Min = wkQuality;
}
} else {
if (populationQualityPlot.Min == double.MaxValue) {
populationQualityPlot.Max = wkQuality - bkQuality;
qualityPlot.Max = wkQuality;
populationQualityPlot.Min = 0;
qualityPlot.Min = bkQuality;
}
}
}
Point2D popQualityPoint;
if (MaximizationParameter.ActualValue.Value) {
popQualityPoint = new Point2D(cnt, quality - (oldPopQuality / popSize));
} else {
popQualityPoint = new Point2D(cnt, (oldPopQuality / popSize) - quality);
}
Point2D solQuality = new Point2D(cnt, quality);
Point2D diversityPoint = new Point2D(cnt++, solToPopDiversity / popSize);
populationDiversityPlot.AddPoint(curGenStr, diversityPoint);
populationQualityPlot.AddPoint(curGenStr, popQualityPoint);
qualityPlot.AddPoint(curGenStr, solQuality);
}
return base.Apply();
}
private void Reset() {
cnt = 0;
lastGeneration = GenerationsParameter.ActualValue.Value;
}
public override void ClearState() {
populationQualityPlot.CleanUp();
populationDiversityPlot.CleanUp();
qualityPlot.CleanUp();
scalingFinished = false;
}
private IScope ReverseScopeTreeLookup(string scopeName) {
var currentScope = ExecutionContext.Scope;
while (currentScope != null) {
var scopes = currentScope.SubScopes.Where(x => x.Name == scopeName);
if (scopes.Count() > 0)
return scopes.First();
currentScope = currentScope.Parent;
}
return null;
}
}
}