#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.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; using HeuristicLab.EvolutionTracking; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Tracking.Analyzers { [Item("SymbolicDataAnalysisSubtreeSampleCountAnalyzer", "An analyzer for trace graph overlap.")] [StorableClass] public class SymbolicDataAnalysisSubtreeSampleCountAnalyzer : EvolutionTrackingAnalyzer { private const string CacheTraceNodesParameterName = "CacheTraceNodes"; private readonly TraceCalculator traceCalculator = new TraceCalculator { UpdateVertexWeights = true, UpdateSubtreeWeights = true, CacheTraceNodes = true }; #region parameters public IFixedValueParameter CacheTraceNodesParameter { get { return (IFixedValueParameter)Parameters[CacheTraceNodesParameterName]; } } #endregion #region parameter properties public bool CacheTraceNodes { get { return CacheTraceNodesParameter.Value.Value; } set { CacheTraceNodesParameter.Value.Value = value; } } #endregion public SymbolicDataAnalysisSubtreeSampleCountAnalyzer() { UpdateCounterParameter.ActualName = "SubtreeSampleCountAnalyzerUpdateCounter"; Parameters.Add(new FixedValueParameter(CacheTraceNodesParameterName, new BoolValue(true))); } protected SymbolicDataAnalysisSubtreeSampleCountAnalyzer(SymbolicDataAnalysisSubtreeSampleCountAnalyzer original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicDataAnalysisSubtreeSampleCountAnalyzer(this, cloner); } public override void ClearState() { base.ClearState(); traceCalculator.ResetState(); } public override void InitializeState() { traceCalculator.ResetState(); } [StorableConstructor] protected SymbolicDataAnalysisSubtreeSampleCountAnalyzer(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(); traceCalculator.CacheTraceNodes = CacheTraceNodes; if (Generation.Value > 0) { var rank = PopulationGraph.GetByRank(Generation.Value).Cast>().ToList(); foreach (var n in rank) traceCalculator.Trace(n, 2, false); // during this call weights will be updated foreach (var v in traceCalculator.TraceGraph.Vertices) { var g = PopulationGraph.GetByContent(v.Data); g.Weight = v.Weight; } } return base.Apply(); } } }