[12265] | 1 | #region License information
|
---|
[12225] | 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System.Linq;
|
---|
| 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 27 | using HeuristicLab.EvolutionTracking;
|
---|
[12265] | 28 | using HeuristicLab.Parameters;
|
---|
[12225] | 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Tracking.Analyzers {
|
---|
| 32 | [Item("SymbolicDataAnalysisSubtreeSampleCountAnalyzer", "An analyzer for trace graph overlap.")]
|
---|
| 33 | [StorableClass]
|
---|
| 34 | public class SymbolicDataAnalysisSubtreeSampleCountAnalyzer : EvolutionTrackingAnalyzer<ISymbolicExpressionTree> {
|
---|
[12265] | 35 | private const string CacheTraceNodesParameterName = "CacheTraceNodes";
|
---|
[12225] | 36 | private readonly TraceCalculator traceCalculator = new TraceCalculator { UpdateVertexWeights = true, UpdateSubtreeWeights = true, CacheTraceNodes = true };
|
---|
| 37 |
|
---|
[12265] | 38 | #region parameters
|
---|
| 39 | public IFixedValueParameter<BoolValue> CacheTraceNodesParameter {
|
---|
| 40 | get { return (IFixedValueParameter<BoolValue>)Parameters[CacheTraceNodesParameterName]; }
|
---|
| 41 | }
|
---|
| 42 | #endregion
|
---|
| 43 |
|
---|
| 44 | #region parameter properties
|
---|
| 45 | public bool CacheTraceNodes {
|
---|
| 46 | get { return CacheTraceNodesParameter.Value.Value; }
|
---|
| 47 | set { CacheTraceNodesParameter.Value.Value = value; }
|
---|
| 48 | }
|
---|
| 49 | #endregion
|
---|
| 50 |
|
---|
[12225] | 51 | public SymbolicDataAnalysisSubtreeSampleCountAnalyzer() {
|
---|
[12265] | 52 | UpdateCounterParameter.ActualName = "SubtreeSampleCountAnalyzerUpdateCounter";
|
---|
| 53 |
|
---|
| 54 | Parameters.Add(new FixedValueParameter<BoolValue>(CacheTraceNodesParameterName, new BoolValue(true)));
|
---|
[12225] | 55 | }
|
---|
| 56 |
|
---|
[12966] | 57 | protected SymbolicDataAnalysisSubtreeSampleCountAnalyzer(SymbolicDataAnalysisSubtreeSampleCountAnalyzer original, Cloner cloner)
|
---|
[12225] | 58 | : base(original, cloner) {
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 62 | return new SymbolicDataAnalysisSubtreeSampleCountAnalyzer(this, cloner);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | public override void ClearState() {
|
---|
| 66 | base.ClearState();
|
---|
| 67 | traceCalculator.ResetState();
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | public override void InitializeState() {
|
---|
| 71 | traceCalculator.ResetState();
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | [StorableConstructor]
|
---|
[12966] | 75 | protected SymbolicDataAnalysisSubtreeSampleCountAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
[12225] | 76 |
|
---|
| 77 | public override IOperation Apply() {
|
---|
| 78 | int updateInterval = UpdateIntervalParameter.Value.Value;
|
---|
| 79 | IntValue updateCounter = UpdateCounterParameter.ActualValue;
|
---|
| 80 | // if counter does not yet exist then initialize it with update interval
|
---|
| 81 | // to make sure the solutions are analyzed on the first application of this operator
|
---|
| 82 | if (updateCounter == null) {
|
---|
| 83 | updateCounter = new IntValue(updateInterval);
|
---|
| 84 | UpdateCounterParameter.ActualValue = updateCounter;
|
---|
| 85 | }
|
---|
| 86 | //analyze solutions only every 'updateInterval' times
|
---|
| 87 | if (updateCounter.Value != updateInterval) {
|
---|
| 88 | updateCounter.Value++;
|
---|
| 89 | return base.Apply();
|
---|
| 90 | }
|
---|
| 91 | updateCounter.Value = 1;
|
---|
| 92 |
|
---|
| 93 | if (PopulationGraph == null)
|
---|
| 94 | return base.Apply();
|
---|
| 95 |
|
---|
[12265] | 96 | traceCalculator.CacheTraceNodes = CacheTraceNodes;
|
---|
[12225] | 97 | if (Generation.Value > 0) {
|
---|
| 98 | var rank = PopulationGraph.GetByRank(Generation.Value).Cast<IGenealogyGraphNode<ISymbolicExpressionTree>>().ToList();
|
---|
[12265] | 99 | foreach (var n in rank)
|
---|
| 100 | traceCalculator.Trace(n, 2, false); // during this call weights will be updated
|
---|
| 101 |
|
---|
| 102 | foreach (var v in traceCalculator.TraceGraph.Vertices) {
|
---|
| 103 | var g = PopulationGraph.GetByContent(v.Data);
|
---|
| 104 | g.Weight = v.Weight;
|
---|
[12225] | 105 | }
|
---|
| 106 | }
|
---|
[12283] | 107 |
|
---|
[12225] | 108 | return base.Apply();
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 | }
|
---|