1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Runtime.InteropServices.WindowsRuntime;
|
---|
5 | using System.Text;
|
---|
6 | using System.Threading.Tasks;
|
---|
7 | using HeuristicLab.Common;
|
---|
8 | using HeuristicLab.Core;
|
---|
9 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
10 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
11 |
|
---|
12 | namespace 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 | }
|
---|