#region License Information
/* HeuristicLab
* Copyright (C) 2002-2015 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.Analysis;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
using HeuristicLab.EvolutionTracking;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Tracking.Analyzers {
[Item("SymbolicDataAnalysisTraceOverlapAnalyzer", "An analyzer for trace graph overlap.")]
[StorableClass]
public class SymbolicDataAnalysisTraceOverlapAnalyzer : EvolutionTrackingAnalyzer {
private const string PercentageOfBestSolutionsParameterName = "PercentageOfBestSolutions";
#region parameters
public IFixedValueParameter PercentageOfBestSolutionsParameter {
get { return (IFixedValueParameter)Parameters[PercentageOfBestSolutionsParameterName]; }
}
#endregion
public SymbolicDataAnalysisTraceOverlapAnalyzer() {
UpdateCounterParameter.ActualName = "TraceOverlapAnalyzerUpdateCounter";
Parameters.Add(new FixedValueParameter(PercentageOfBestSolutionsParameterName, new DoubleValue(1.0)));
}
protected SymbolicDataAnalysisTraceOverlapAnalyzer(SymbolicDataAnalysisTraceOverlapAnalyzer original, Cloner cloner)
: base(original, cloner) {
}
public override IDeepCloneable Clone(Cloner cloner) {
return new SymbolicDataAnalysisTraceOverlapAnalyzer(this, cloner);
}
[StorableConstructor]
protected SymbolicDataAnalysisTraceOverlapAnalyzer(bool deserializing) : base(deserializing) { }
public override IOperation Apply() {
int updateInterval = UpdateIntervalParameter.Value.Value;
IntValue updateCounter = UpdateCounterParameter.ActualValue;
// if counter does not yet exist then initialize it with update interval
// to make sure the solutions are analyzed on the first application of this operator
if (updateCounter == null) {
updateCounter = new IntValue(updateInterval);
UpdateCounterParameter.ActualValue = updateCounter;
}
//analyze solutions only every 'updateInterval' times
if (updateCounter.Value != updateInterval) {
updateCounter.Value++;
return base.Apply();
}
updateCounter.Value = 1;
if (PopulationGraph == null)
return base.Apply();
var bestSolutionsPercentage = PercentageOfBestSolutionsParameter.Value.Value;
int n = (int)Math.Round(bestSolutionsPercentage * PopulationSize.Value);
double avgTraceOverlap = 0;
if (Generation.Value > 0) {
var nodes = PopulationGraph.GetByRank(Generation.Value).Cast>().OrderByDescending(x => x.Quality).Take(n);
avgTraceOverlap = nodes.Average(x => TraceCalculator.TraceSubtree(x, 2).Vertices.Count());
}
DataTable table;
if (!Results.ContainsKey("AverageTraceOverlap")) {
table = new DataTable("Average trace overlap");
var row = new DataRow("Average trace overlap") { VisualProperties = { StartIndexZero = true } };
row.Values.Add(avgTraceOverlap);
table.Rows.Add(row);
Results.Add(new Result("AverageTraceOverlap", table));
} else {
table = (DataTable)Results["AverageTraceOverlap"].Value;
table.Rows["Average trace overlap"].Values.Add(avgTraceOverlap);
}
return base.Apply();
}
}
}