#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.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.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Tracking.Analyzers {
[Item("SymbolicDataAnalysisFragmentLengthAnalyzer", "An analyzer for fragment length.")]
[StorableClass]
public class SymbolicDataAnalysisFragmentLengthAnalyzer : EvolutionTrackingAnalyzer {
[StorableConstructor]
protected SymbolicDataAnalysisFragmentLengthAnalyzer(bool deserializing) : base(deserializing) { }
public SymbolicDataAnalysisFragmentLengthAnalyzer() {
UpdateCounterParameter.ActualName = "FragmentLengthAnalyzerUpdateCounter";
}
protected SymbolicDataAnalysisFragmentLengthAnalyzer(SymbolicDataAnalysisFragmentLengthAnalyzer original, Cloner cloner) : base(original, cloner) { }
public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicDataAnalysisFragmentLengthAnalyzer(this, cloner); }
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 children = PopulationGraph.GetByRank(Generation.Value).Where(x => x.InDegree > 0).ToList();
double avgFragmentLength = 0;
if (children.Any())
avgFragmentLength = children.Select(x => (IFragment)x.InArcs.Last().Data).Where(f => f != null).Average(x => x.Root.GetLength());
DataTable table;
if (!Results.ContainsKey("AverageFragmentLength")) {
table = new DataTable("Average fragment length");
var row = new DataRow("Average fragment length") { VisualProperties = { StartIndexZero = true } };
row.Values.Add(avgFragmentLength);
table.Rows.Add(row);
Results.Add(new Result("AverageFragmentLength", table));
} else {
table = (DataTable)Results["AverageFragmentLength"].Value;
table.Rows["Average fragment length"].Values.Add(avgFragmentLength);
}
return base.Apply();
}
}
}