source: branches/3107_LearningALPS/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Creators/SymbolFrequencyReinitializationStrategyController.cs @ 17925

Last change on this file since 17925 was 17925, checked in by pfleck, 10 months ago

#3107 Switched to the ClonedSymbolicExpressionTreeGrammar variable in the global scope instead of using the original grammar from the problem to avoid changing the original grammar.

File size: 3.0 KB
Line 
1using System.Linq;
2
3using HEAL.Attic;
4using HeuristicLab.Common;
5using HeuristicLab.Core;
6using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
7using HeuristicLab.Operators;
8using HeuristicLab.Optimization;
9using HeuristicLab.Parameters;
10
11namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
12  [Item("SymbolFrequencyReinitializationStrategyController", "")]
13  [StorableType("8F0B7020-A02F-491D-881A-5CE550426E5B")]
14  public class SymbolFrequencyReinitializationStrategyController : InstrumentedOperator, IReinitializationStrategyController {
15
16    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
17    private const string SymbolicExpressionTreeGrammarParameterName = "ClonedSymbolicExpressionTreeGrammar";
18
19    #region Parameter Properties
20    public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
21      get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
22    }
23
24    public IValueLookupParameter<ISymbolicExpressionGrammar> SymbolicExpressionTreeGrammarParameter {
25      get { return (IValueLookupParameter<ISymbolicExpressionGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; }
26    }
27    #endregion
28
29    #region Constructors
30    public SymbolFrequencyReinitializationStrategyController() {
31      Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree whose length should be calculated."));
32      Parameters.Add(new ValueLookupParameter<ISymbolicExpressionGrammar>(SymbolicExpressionTreeGrammarParameterName, "The tree grammar that defines the correct syntax of symbolic expression trees that should be created."));
33    }
34
35    private SymbolFrequencyReinitializationStrategyController(SymbolFrequencyReinitializationStrategyController original, Cloner cloner)
36      : base(original, cloner) { }
37    public override IDeepCloneable Clone(Cloner cloner) {
38      return new SymbolFrequencyReinitializationStrategyController(this, cloner);
39    }
40
41    [StorableConstructor]
42    private SymbolFrequencyReinitializationStrategyController(StorableConstructorFlag _) : base(_) { }
43    [StorableHook(HookType.AfterDeserialization)]
44    private void AfterDeserialization() {
45    }
46    #endregion
47
48
49    public override IOperation InstrumentedApply() {
50      var trees = SymbolicExpressionTreeParameter.ActualValue;
51      var grammar = SymbolicExpressionTreeGrammarParameter.ActualValue;
52
53      var symbolFrequencies = SymbolicExpressionSymbolFrequencyAnalyzer.CalculateSymbolFrequencies(trees).ToDictionary(x => x.Key, x => x.Value);
54
55      foreach (var s in grammar.AllowedSymbols) {
56        if (s is ProgramRootSymbol || s is StartSymbol) {
57          continue;
58        }
59
60        if (symbolFrequencies.TryGetValue(s.Name, out double f)) {
61          s.InitialFrequency = f;
62        }
63      }
64
65      return base.InstrumentedApply();
66    }
67  }
68}
Note: See TracBrowser for help on using the repository browser.