Free cookie consent management tool by TermsFeed Policy Generator

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, 3 years 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
RevLine 
[18063]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
[18065]16    #region Properties
[18063]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);
[18065]25          GetSubFunctions(Tree);
26          OnChanged();
[18063]27        }
28      }
29    }
30
31    [Storable]
32    private ISymbolicExpressionTree tree;
33    public ISymbolicExpressionTree Tree => tree;
34
[18065]35    //[Storable]
36    //private IDictionary<SubFunctionTreeNode, SubFunction> subFunctions;
[18063]37    [Storable]
[18065]38    public IDictionary<SubFunctionTreeNode, SubFunction> SubFunctions { get; private set; } = new Dictionary<SubFunctionTreeNode, SubFunction>();
39      //subFunctions == null ? new Dictionary<SubFunctionTreeNode, SubFunction>() : subFunctions;
[18063]40
41    protected InfixExpressionParser Parser { get; set; } = new InfixExpressionParser();
[18065]42    #endregion
[18063]43
[18065]44    #region Events
45    public event EventHandler Changed;
46   
47    private void OnChanged() => Changed?.Invoke(this, EventArgs.Empty);
48    #endregion
[18063]49
50    #region Constructors
[18065]51    public StructureTemplate() {
52      Template = "f(x)*f(y)+5";
53    }
[18063]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
[18065]66    private void GetSubFunctions(ISymbolicExpressionTree tree) {
[18063]67      int count = 1;
68      foreach (var node in tree.IterateNodesPrefix())
[18065]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
[18063]77    }
78  }
79}
Note: See TracBrowser for help on using the repository browser.