source: branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/StructureTemplate/StructureTemplate.cs @ 18065

Last change on this file since 18065 was 18065, checked in by dpiringe, 9 months ago

#3136

  • modified InfixExpressionParser to fully support SubFunctionSymbol
    • created a SubFunctionTreeNode to store the function arguments
  • modified StructureTemplateView to regenerate the content state
  • first implementation for the main tree build up logic
File size: 2.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using HeuristicLab.Core;
7using HEAL.Attic;
8using HeuristicLab.Common;
9using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
10
11namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
12  [StorableType("E3C038DB-C6AA-457D-9F65-AF16C44CCE22")]
13  [Item("StructureTemplate", "Structure Template")]
14  public class StructureTemplate : Item {
15
16    #region Properties
17    [Storable]
18    private string template = "";
19    public string Template {
20      get => template;
21      set {
22        if(template != value) {
23          template = value;
24          tree = Parser.Parse(template);
25          GetSubFunctions(Tree);
26          OnChanged();
27        }
28      }
29    }
30
31    [Storable]
32    private ISymbolicExpressionTree tree;
33    public ISymbolicExpressionTree Tree => tree;
34
35    //[Storable]
36    //private IDictionary<SubFunctionTreeNode, SubFunction> subFunctions;
37    [Storable]
38    public IDictionary<SubFunctionTreeNode, SubFunction> SubFunctions { get; private set; } = new Dictionary<SubFunctionTreeNode, SubFunction>();
39      //subFunctions == null ? new Dictionary<SubFunctionTreeNode, SubFunction>() : subFunctions;
40
41    protected InfixExpressionParser Parser { get; set; } = new InfixExpressionParser();
42    #endregion
43
44    #region Events
45    public event EventHandler Changed;
46   
47    private void OnChanged() => Changed?.Invoke(this, EventArgs.Empty);
48    #endregion
49
50    #region Constructors
51    public StructureTemplate() {
52      Template = "f(x)*f(y)+5";
53    }
54
55    [StorableConstructor]
56    protected StructureTemplate(StorableConstructorFlag _) : base(_) { }
57
58    protected StructureTemplate(StructureTemplate original, Cloner cloner) { }
59    #endregion
60
61    #region Cloning
62    public override IDeepCloneable Clone(Cloner cloner) =>
63      new StructureTemplate(this, cloner);
64    #endregion
65
66    private void GetSubFunctions(ISymbolicExpressionTree tree) {
67      int count = 1;
68      foreach (var node in tree.IterateNodesPrefix())
69        if (node is SubFunctionTreeNode subFunctionTreeNode) {
70          var subFunction = new SubFunction() {
71            Name = $"f{count++}({string.Join(",", subFunctionTreeNode.FunctionArguments)})",
72            FunctionArguments = subFunctionTreeNode.FunctionArguments
73          };
74          SubFunctions.Add(subFunctionTreeNode, subFunction);
75        }
76
77    }
78  }
79}
Note: See TracBrowser for help on using the repository browser.