source: branches/HeuristicLab.Algorithms.IteratedSentenceConstruction/HeuristicLab.Algorithms.IteratedSymbolicExpressionConstruction/3.3/StateFunctions/SymbolicExpressionTermsStateFunction.cs @ 12967

Last change on this file since 12967 was 12967, checked in by gkronber, 7 years ago

#2471: implemented linear state value approximation using sparse binary features and implemented a state function for generating features for symbolic expression trees

File size: 2.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Runtime.InteropServices.WindowsRuntime;
5using System.Text;
6using System.Threading.Tasks;
7using HeuristicLab.Common;
8using HeuristicLab.Core;
9using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
10using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
11
12namespace HeuristicLab.Algorithms.IteratedSymbolicExpressionConstruction {
13  [StorableClass]
14  [Item("SymbolicExpressionTermsStateFunction", "")]
15  public class SymbolicExpressionTermsStateFunction : Item, IStateFunction {
16    public SymbolicExpressionTermsStateFunction()
17      : base() {
18    }
19
20    public object CreateState(ISymbolicExpressionTreeNode root, List<ISymbol> actions, ISymbolicExpressionTreeNode parentNode, int childIdx) {
21      var terms = new List<Tuple<string, double>>();
22      var formatter = new HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.SymbolicExpressionTreeStringFormatter();
23      var t = new SymbolicExpressionTree();
24      terms.Add(Tuple.Create("const", 1.0));
25      foreach (var n in root.IterateNodesPrefix()) {
26        if (n.Symbol is StartSymbol && n.SubtreeCount > 0) {
27          t.Root = n;
28          terms.Add(Tuple.Create("start-" + n.GetSubtree(0).Symbol.Name, 1.0));
29        } else if (n.Symbol.Name == "+" || n.Symbol.Name == "-") {
30          foreach (var child in n.Subtrees.OrderBy(c => c.Symbol.Name)) {
31            if (child.Symbol.Name == "+" || n.Symbol.Name == "-") continue; // skip (+ (+ ...) ...), only non-decomposable terms are relevant for us
32            t.Root = child;
33            terms.Add(Tuple.Create(formatter.Format(t), 1.0));
34          }
35        }
36      }
37      return terms;
38    }
39
40    #region item
41    [StorableConstructor]
42    protected SymbolicExpressionTermsStateFunction(bool deserializing) : base(deserializing) { }
43    protected SymbolicExpressionTermsStateFunction(SymbolicExpressionTermsStateFunction original, Cloner cloner)
44      : base(original, cloner) {
45    }
46    public override IDeepCloneable Clone(Cloner cloner) {
47      return new SymbolicExpressionTermsStateFunction(this, cloner);
48    }
49    #endregion
50  }
51}
Note: See TracBrowser for help on using the repository browser.