Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionaryTracking/3.4/Analyzers/BuildingBlocks/RelevantBuildingBlocksAnalyzer.cs @ 10264

Last change on this file since 10264 was 10264, checked in by bburlacu, 11 years ago

#1772: Created new branch for the redesigned version of the tracking plugin

File size: 10.8 KB
Line 
1using System.Collections.Generic;
2using System.Linq;
3using HeuristicLab.Analysis;
4using HeuristicLab.Common;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
8using HeuristicLab.Operators;
9using HeuristicLab.Optimization;
10using HeuristicLab.Parameters;
11using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
12using HeuristicLab.Problems.DataAnalysis.Symbolic;
13using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
14
15namespace HeuristicLab.EvolutionaryTracking.Analyzers.BuildingBlocks {
16  [StorableClass]
17  public class RelevantBuildingBlocksAnalyzer : SingleSuccessorOperator, IAnalyzer {
18    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
19    private const string UpdateIntervalParameterName = "UpdateInterval";
20    private const string UpdateCounterParameterName = "UpdateCounter";
21    private const string ResultsParameterName = "Results";
22    private const string MatchVariablesParameterName = "MatchVariableNames";
23    private const string MatchVariableWeightsParameterName = "MatchVariableWeights";
24    private const string MatchConstantValuesParameterName = "MatchConstantValues";
25    private const string SymbolicExpressionTreeGrammarParameterName = "SymbolicExpressionTreeGrammar";
26    private const string ClonedSymbolicExpressionTreeGrammarParameterName = "ClonedSymbolicExpressionTreeGrammar";
27    private const string SymbolicRegressionSolutionParameterName = "SymbolicRegressionSolution";
28    private const string PopulationGraphParameterName = "PopulationGraph";
29    private const string FrequentFragmentsTableParameterName = "FrequentFragmentsTable";
30    private const string OccurrenceFrequencyTresholdParameterName = "OccurrenceThreshold";
31
32    private readonly Dictionary<string, IFragment> foundFragments;
33
34    #region Parameters
35    public ValueParameter<IntValue> UpdateIntervalParameter {
36      get { return (ValueParameter<IntValue>)Parameters[UpdateIntervalParameterName]; }
37    }
38
39    public ValueParameter<IntValue> UpdateCounterParameter {
40      get { return (ValueParameter<IntValue>)Parameters[UpdateCounterParameterName]; }
41    }
42    public ValueParameter<DoubleValue> OccurrenceThresholdParameter {
43      get { return (ValueParameter<DoubleValue>)Parameters[OccurrenceFrequencyTresholdParameterName]; }
44    }
45    public LookupParameter<ResultCollection> ResultsParameter {
46      get { return (LookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
47    }
48    public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
49      get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
50    }
51    public ValueParameter<BoolValue> MatchVariableNamesParameter {
52      get { return (ValueParameter<BoolValue>)Parameters[MatchVariablesParameterName]; }
53    }
54    public ValueParameter<BoolValue> MatchVariableWeightsParameter {
55      get { return (ValueParameter<BoolValue>)Parameters[MatchVariableWeightsParameterName]; }
56    }
57    public ValueParameter<BoolValue> MatchConstantValuesParameter {
58      get { return (ValueParameter<BoolValue>)Parameters[MatchConstantValuesParameterName]; }
59    }
60    public IValueLookupParameter<ISymbolicExpressionGrammar> SymbolicExpressionTreeGrammarParameter {
61      get { return (IValueLookupParameter<ISymbolicExpressionGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; }
62    }
63    public ILookupParameter<ISymbolicExpressionGrammar> ClonedSymbolicExpressionTreeGrammarParameter {
64      get { return (ILookupParameter<ISymbolicExpressionGrammar>)Parameters[ClonedSymbolicExpressionTreeGrammarParameterName]; }
65    }
66    public ILookupParameter<ISymbolicRegressionSolution> SymbolicRegressionSolutionParameter {
67      get { return (ILookupParameter<ISymbolicRegressionSolution>)Parameters[SymbolicRegressionSolutionParameterName]; }
68    }
69    #endregion
70
71    #region Parameter properties
72    public IntValue UpdateInterval {
73      get { return UpdateIntervalParameter.Value; }
74    }
75    public IntValue UpdateCounter {
76      get { return UpdateCounterParameter.Value; }
77    }
78    public DoubleValue OccurrenceThreshold {
79      get { return OccurrenceThresholdParameter.Value; }
80    }
81    public ResultCollection Results {
82      get { return ResultsParameter.ActualValue; }
83    }
84    public ItemArray<ISymbolicExpressionTree> SymbolicExpressionTrees {
85      get { return SymbolicExpressionTreeParameter.ActualValue; }
86    }
87    public ISymbolicExpressionGrammar SymbolicExpressionTreeGrammar {
88      get { return ClonedSymbolicExpressionTreeGrammarParameter.ActualValue; }
89    }
90    public ISymbolicRegressionSolution SymbolicRegressionSolution {
91      get { return SymbolicRegressionSolutionParameter.ActualValue; }
92    }
93    #endregion
94
95    [StorableConstructor]
96    protected RelevantBuildingBlocksAnalyzer(bool deserializing) : base(deserializing) { }
97
98    public override IDeepCloneable Clone(Cloner cloner) { return new RelevantBuildingBlocksAnalyzer(this, cloner); }
99
100    protected RelevantBuildingBlocksAnalyzer(RelevantBuildingBlocksAnalyzer original, Cloner cloner) : base(original, cloner) { }
101
102    public RelevantBuildingBlocksAnalyzer() {
103      #region Parameters add
104      Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to analyze."));
105      Parameters.Add(new ValueParameter<IntValue>(UpdateIntervalParameterName, "The interval in which the tree length analysis should be applied.", new IntValue(1)));
106      Parameters.Add(new ValueParameter<IntValue>(UpdateCounterParameterName, "The value which counts how many times the operator was called since the last update", new IntValue(0)));
107      Parameters.Add(new ValueParameter<DoubleValue>(OccurrenceFrequencyTresholdParameterName, "The occurrence threshold for a fragment to be considered a frequenct fragment.", new DoubleValue(0.5)));
108      Parameters.Add(new ValueLookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
109      // matching predicates     
110      Parameters.Add(new ValueParameter<BoolValue>(MatchVariablesParameterName, "Specify if the symbolic expression tree comparer should match variable names.", new BoolValue(true)));
111      Parameters.Add(new ValueParameter<BoolValue>(MatchVariableWeightsParameterName, "Specify if the symbolic expression tree comparer should match variable weights.", new BoolValue(true)));
112      Parameters.Add(new ValueParameter<BoolValue>(MatchConstantValuesParameterName, "Specify if the symbolic expression tree comparer should match constant values.", new BoolValue(true)));
113      Parameters.Add(new ValueLookupParameter<ISymbolicExpressionGrammar>(SymbolicExpressionTreeGrammarParameterName, "The tree grammar that defines the correct syntax of symbolic expression trees that should be created."));
114      Parameters.Add(new LookupParameter<ISymbolicExpressionGrammar>(ClonedSymbolicExpressionTreeGrammarParameterName, "An immutable clone of the concrete grammar that is actually used to create and manipulate trees."));
115      Parameters.Add(new LookupParameter<ISymbolicRegressionSolution>(SymbolicRegressionSolutionParameterName, "The symbolic regression solution."));
116      #endregion
117      foundFragments = new Dictionary<string, IFragment>();
118    }
119
120    [StorableHook(HookType.AfterDeserialization)]
121    private void AfterDeserialization() {
122    }
123
124    public override void ClearState() {
125      foundFragments.Clear();
126      base.InitializeState();
127    }
128
129    public bool EnabledByDefault { get; private set; }
130
131    public override IOperation Apply() {
132      UpdateCounter.Value++;
133      if (UpdateCounter.Value != UpdateInterval.Value) return base.Apply();
134      UpdateCounter.Value = 0;
135
136      //      if (SymbolicRegressionSolution == null) return base.Apply();
137
138      var comparer = new SymbolicExpressionTreeNodeSimilarityComparer {
139        MatchConstantValues = MatchConstantValuesParameter.Value.Value,
140        MatchVariableNames = MatchVariableNamesParameter.Value.Value,
141        MatchVariableWeights = MatchVariableWeightsParameter.Value.Value
142      };
143
144      var fragmentComparer = new SymbolicExpressionTreeFragmentSimilarityComparer { SimilarityComparer = comparer };
145
146      //      var bestIndividual = SymbolicRegressionSolution.Model.SymbolicExpressionTree;
147      var bestTrainingSolution = (ISymbolicRegressionSolution)Results["Best training solution"].Value;
148      var bestIndividual = bestTrainingSolution.Model.SymbolicExpressionTree;
149      var subtrees = bestIndividual.IterateNodesPrefix().Where(node => node.SubtreeCount > 0).Select((node, index) => new { Node = node, Index = index });
150      var frequentFragments = (from s in subtrees
151                               let fragment = new Fragment { Root = s.Node, Index = s.Index }
152                               let count = SymbolicExpressionTrees.Count(tree => tree.Root.ContainsFragment(fragment, comparer))
153                               let frequency = (double)count / SymbolicExpressionTrees.Length
154                               where frequency >= OccurrenceThreshold.Value
155                               select fragment).Distinct(fragmentComparer).ToList();
156
157      var graph = (SymbolicExpressionTreeGenealogyGraph)Results[PopulationGraphParameterName].Value;
158      DataTable table;
159
160      if (!Results.ContainsKey(FrequentFragmentsTableParameterName)) {
161        table = new DataTable(FrequentFragmentsTableParameterName);
162        Results.Add(new Result(FrequentFragmentsTableParameterName, table));
163      } else {
164        table = (DataTable)Results[FrequentFragmentsTableParameterName].Value;
165      }
166
167      foreach (var fragment in foundFragments.Values) {
168        var prefixString = fragment.Root.ToPrefixString();
169        double count = SymbolicExpressionTrees.Count(t => t.Root.ContainsFragment(fragment, comparer));
170        double frequency = count / SymbolicExpressionTrees.Length;
171        table.Rows[prefixString].Values.Add(frequency);
172      }
173
174      var newFragments = frequentFragments.Where(f => !foundFragments.ContainsKey(f.Root.ToPrefixString())).ToList();
175      foreach (var fragment in newFragments) {
176        var prefixString = fragment.Root.ToPrefixString();
177        var row = new DataRow(prefixString);
178        row.Values.AddRange(from g in graph.Nodes.Where(n => n.Rank % 1 == 0).GroupBy(n => n.Rank)
179                            let count = (double)g.Count(n => n.SymbolicExpressionTree.Root.ContainsFragment(fragment, comparer))
180                            let frequency = count / SymbolicExpressionTrees.Length
181                            select frequency);
182        foundFragments.Add(prefixString, fragment);
183        table.Rows.Add(row);
184      }
185
186      return base.Apply();
187    }
188  }
189}
Note: See TracBrowser for help on using the repository browser.