1 | using System.Linq;
|
---|
2 |
|
---|
3 | using HEAL.Attic;
|
---|
4 | using HeuristicLab.Common;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
7 | using HeuristicLab.Operators;
|
---|
8 | using HeuristicLab.Optimization;
|
---|
9 | using HeuristicLab.Parameters;
|
---|
10 |
|
---|
11 | namespace 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 = "SymbolicExpressionTreeGrammar";
|
---|
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 | }
|
---|