Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 18075 was 18075, checked in by dpiringe, 2 years ago

#3136

  • added a hidden interpreter parameter for StructuredSymbolicRegressionSingleObjectiveProblem
  • fixed a bug which crashed the application by changing ProblemData with different variables
  • fixed a bug which crashed the application by running the problem with an empty StructureTemplate
  • added a better output of exceptions of type AggregateException
  • added and resize event handler to repaint nodes of type SubFunctionTreeNode
  • code cleanup
File size: 4.8 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; }
41
42    [Storable]
43    private bool applyLinearScaling;
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      Reset();
65    }
66
67    [StorableConstructor]
68    protected StructureTemplate(StorableConstructorFlag _) : base(_) { }
69
70    protected StructureTemplate(StructureTemplate original, Cloner cloner) : base(original, cloner) {
71      this.Tree = cloner.Clone(original.Tree);
72      this.Template = original.Template;
73      this.ApplyLinearScaling = original.ApplyLinearScaling;
74      this.SubFunctions = original.SubFunctions;
75    }
76    #endregion
77
78    #region Cloning
79    public override IDeepCloneable Clone(Cloner cloner) =>
80      new StructureTemplate(this, cloner);
81    #endregion
82
83    public void Reset() {
84      SubFunctions = new Dictionary<string, SubFunction>();
85      treeWithoutLinearScaling = null;
86      treeWithLinearScaling = null;
87      template = "";
88      applyLinearScaling = false;
89      OnChanged();
90    }
91
92    private Dictionary<string, SubFunction> GetSubFunctions() {
93      var subFunctions = new Dictionary<string, SubFunction>();
94      foreach (var node in Tree.IterateNodesPrefix())
95        if (node is SubFunctionTreeNode subFunctionTreeNode) {
96          if (!subFunctionTreeNode.Arguments.Any())
97            throw new ArgumentException($"The sub-function '{subFunctionTreeNode}' requires inputs (e.g. {subFunctionTreeNode.Name}(var1, var2)).");
98
99          if (subFunctions.TryGetValue(subFunctionTreeNode.Name, out SubFunction v)) {
100            if(!v.Arguments.SequenceEqual(subFunctionTreeNode.Arguments))
101              throw new ArgumentException(
102                $"The sub-function '{v.Name}' has (at least two) different signatures " +
103                $"({v.Name}({string.Join(",", v.Arguments)}) <> {subFunctionTreeNode.Name}({string.Join(",", subFunctionTreeNode.Arguments)})).");
104          } else {
105            var subFunction = new SubFunction() {
106              Name = subFunctionTreeNode.Name,
107              Arguments = subFunctionTreeNode.Arguments
108            };
109            subFunction.Changed += OnSubFunctionChanged;
110            subFunctions.Add(subFunction.Name, subFunction);
111          }
112        }
113      return subFunctions;
114    }
115
116    private ISymbolicExpressionTree AddLinearScalingTerms(ISymbolicExpressionTree tree) {
117      var clonedTree = (ISymbolicExpressionTree)tree.Clone();
118      var startNode = clonedTree.Root.Subtrees.First();
119      var template = startNode.Subtrees.First();
120
121      var add = new Addition();
122      var addNode = add.CreateTreeNode();
123
124      var mul = new Multiplication();
125      var mulNode = mul.CreateTreeNode();
126
127      var c1 = new Constant();
128      var c1Node = (ConstantTreeNode)c1.CreateTreeNode();
129      c1Node.Value = 0.0;
130      var c2 = new Constant();
131      var c2Node = (ConstantTreeNode)c2.CreateTreeNode();
132      c2Node.Value = 1.0;
133     
134      addNode.AddSubtree(c1Node);
135      addNode.AddSubtree(mulNode);
136      mulNode.AddSubtree(c2Node);
137       
138      startNode.RemoveSubtree(0);
139      startNode.AddSubtree(addNode);
140      mulNode.AddSubtree(template);
141      return clonedTree;
142    }
143
144    private void OnSubFunctionChanged(object sender, EventArgs e) => OnChanged();
145  }
146}
Note: See TracBrowser for help on using the repository browser.