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

Last change on this file since 18072 was 18072, checked in by chaider, 8 months ago

#3136

  • Added info text in StructureTemplateView
  • Fixed cloning constructors
  • Added check if linear scaling nodes are set
File size: 4.4 KB
Line 
1using System;
2using System.Linq;
3using System.Collections.Generic;
4using HeuristicLab.Core;
5using HEAL.Attic;
6using HeuristicLab.Common;
7using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
8
9namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
10  [StorableType("E3C038DB-C6AA-457D-9F65-AF16C44CCE22")]
11  [Item("StructureTemplate", "Structure Template")]
12  public class StructureTemplate : Item {
13
14    #region Properties
15    [Storable]
16    private string template = "";
17    public string Template {
18      get => template;
19      set {
20        template = value;
21        Tree = Parser.Parse(template);
22        OnChanged();
23      }
24    }
25
26    [Storable]
27    private ISymbolicExpressionTree treeWithoutLinearScaling;
28    [Storable]
29    private ISymbolicExpressionTree treeWithLinearScaling;
30    public ISymbolicExpressionTree Tree {
31      get => ApplyLinearScaling ? treeWithLinearScaling : treeWithoutLinearScaling;
32      private set {
33        treeWithLinearScaling = AddLinearScalingTerms(value);
34        treeWithoutLinearScaling = value;
35        SubFunctions = GetSubFunctions();
36      }
37    }
38
39    [Storable]
40    public IReadOnlyDictionary<string, SubFunction> SubFunctions { get; private set; } = new Dictionary<string, SubFunction>();
41
42    [Storable]
43    private bool applyLinearScaling = false;
44    public bool ApplyLinearScaling {
45      get => applyLinearScaling;
46      set {
47        applyLinearScaling = value;
48        OnChanged();
49      }
50    }
51
52    protected InfixExpressionParser Parser { get; set; } = new InfixExpressionParser();
53
54    #endregion
55
56    #region Events
57    public event EventHandler Changed;
58   
59    private void OnChanged() => Changed?.Invoke(this, EventArgs.Empty);
60    #endregion
61
62    #region Constructors
63    public StructureTemplate() { }
64
65    [StorableConstructor]
66    protected StructureTemplate(StorableConstructorFlag _) : base(_) { }
67
68    protected StructureTemplate(StructureTemplate original, Cloner cloner) : base(original, cloner) { }
69    #endregion
70
71    #region Cloning
72    public override IDeepCloneable Clone(Cloner cloner) =>
73      new StructureTemplate(this, cloner);
74    #endregion
75
76   
77
78    private Dictionary<string, SubFunction> GetSubFunctions() {
79      var subFunctions = new Dictionary<string, SubFunction>();
80      foreach (var node in Tree.IterateNodesPrefix())
81        if (node is SubFunctionTreeNode subFunctionTreeNode) {
82          if (!subFunctionTreeNode.Arguments.Any())
83            throw new ArgumentException($"The sub-function '{subFunctionTreeNode}' requires inputs (e.g. {subFunctionTreeNode.Name}(var1, var2)).");
84
85          if (subFunctions.TryGetValue(subFunctionTreeNode.Name, out SubFunction v)) {
86            if(!v.Arguments.SequenceEqual(subFunctionTreeNode.Arguments))
87              throw new ArgumentException(
88                $"The sub-function '{v.Name}' has (at least two) different signatures " +
89                $"({v.Name}({string.Join(",", v.Arguments)}) <> {subFunctionTreeNode.Name}({string.Join(",", subFunctionTreeNode.Arguments)})).");
90          } else {
91            var subFunction = new SubFunction() {
92              Name = subFunctionTreeNode.Name,
93              Arguments = subFunctionTreeNode.Arguments
94            };
95            subFunction.Changed += OnSubFunctionChanged;
96            subFunctions.Add(subFunction.Name, subFunction);
97          }
98        }
99      return subFunctions;
100    }
101
102    private ISymbolicExpressionTree AddLinearScalingTerms(ISymbolicExpressionTree tree) {
103      var clonedTree = (ISymbolicExpressionTree)tree.Clone();
104      var startNode = clonedTree.Root.Subtrees.First();
105      var template = startNode.Subtrees.First();
106
107      var add = new Addition();
108      var addNode = add.CreateTreeNode();
109
110      var mul = new Multiplication();
111      var mulNode = mul.CreateTreeNode();
112
113      var c1 = new Constant();
114      var c1Node = (ConstantTreeNode)c1.CreateTreeNode();
115      c1Node.Value = 0.0;
116      var c2 = new Constant();
117      var c2Node = (ConstantTreeNode)c2.CreateTreeNode();
118      c2Node.Value = 1.0;
119     
120      addNode.AddSubtree(c1Node);
121      addNode.AddSubtree(mulNode);
122      mulNode.AddSubtree(c2Node);
123       
124      startNode.RemoveSubtree(0);
125      startNode.AddSubtree(addNode);
126      mulNode.AddSubtree(template);
127      return clonedTree;
128    }
129
130    private void OnSubFunctionChanged(object sender, EventArgs e) => OnChanged();
131  }
132}
Note: See TracBrowser for help on using the repository browser.