using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices.WindowsRuntime; using System.Text; using System.Threading.Tasks; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Algorithms.IteratedSymbolicExpressionConstruction { [StorableClass] [Item("SymbolicExpressionTermsStateFunction", "")] public class SymbolicExpressionTermsStateFunction : Item, IStateFunction { public SymbolicExpressionTermsStateFunction() : base() { } public object CreateState(ISymbolicExpressionTreeNode root, List actions, ISymbolicExpressionTreeNode parentNode, int childIdx) { var terms = new List>(); var formatter = new HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.SymbolicExpressionTreeStringFormatter(); var t = new SymbolicExpressionTree(); terms.Add(Tuple.Create("const", 1.0)); foreach (var n in root.IterateNodesPrefix()) { if (n.Symbol is StartSymbol && n.SubtreeCount > 0) { t.Root = n; terms.Add(Tuple.Create("start-" + n.GetSubtree(0).Symbol.Name, 1.0)); } else if (n.Symbol.Name == "+" || n.Symbol.Name == "-") { foreach (var child in n.Subtrees.OrderBy(c => c.Symbol.Name)) { if (child.Symbol.Name == "+" || n.Symbol.Name == "-") continue; // skip (+ (+ ...) ...), only non-decomposable terms are relevant for us t.Root = child; terms.Add(Tuple.Create(formatter.Format(t), 1.0)); } } } return terms; } #region item [StorableConstructor] protected SymbolicExpressionTermsStateFunction(bool deserializing) : base(deserializing) { } protected SymbolicExpressionTermsStateFunction(SymbolicExpressionTermsStateFunction original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicExpressionTermsStateFunction(this, cloner); } #endregion } }