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

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

#3136

  • added linear scaling support for structure template parameter
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; }
41
42    [Storable]
43    private bool applyLinearScaling = false;
44    public bool ApplyLinearScaling {
45      get => applyLinearScaling;
46      set {
47        applyLinearScaling = value;
48        //subFunctions = GetSubFunctions();
49        OnChanged();
50      }
51    }
52
53    protected InfixExpressionParser Parser { get; set; } = new InfixExpressionParser();
54
55    #endregion
56
57    #region Events
58    public event EventHandler Changed;
59   
60    private void OnChanged() => Changed?.Invoke(this, EventArgs.Empty);
61    #endregion
62
63    #region Constructors
64    public StructureTemplate() { }
65
66    [StorableConstructor]
67    protected StructureTemplate(StorableConstructorFlag _) : base(_) { }
68
69    protected StructureTemplate(StructureTemplate original, Cloner cloner) { }
70    #endregion
71
72    #region Cloning
73    public override IDeepCloneable Clone(Cloner cloner) =>
74      new StructureTemplate(this, cloner);
75    #endregion
76
77    private Dictionary<string, SubFunction> GetSubFunctions() {
78      var subFunctions = new Dictionary<string, SubFunction>();
79      foreach (var node in Tree.IterateNodesPrefix())
80        if (node is SubFunctionTreeNode subFunctionTreeNode) {
81          if (!subFunctionTreeNode.Arguments.Any())
82            throw new ArgumentException($"The sub-function '{subFunctionTreeNode}' requires inputs (e.g. {subFunctionTreeNode.Name}(var1, var2)).");
83
84          if (subFunctions.TryGetValue(subFunctionTreeNode.Name, out SubFunction v)) {
85            if(!v.Arguments.SequenceEqual(subFunctionTreeNode.Arguments))
86              throw new ArgumentException(
87                $"The sub-function '{v.Name}' has (at least two) different signatures " +
88                $"({v.Name}({string.Join(",", v.Arguments)}) <> {subFunctionTreeNode.Name}({string.Join(",", subFunctionTreeNode.Arguments)})).");
89          } else {
90            var subFunction = new SubFunction() {
91              Name = subFunctionTreeNode.Name,
92              Arguments = subFunctionTreeNode.Arguments
93            };
94            subFunction.Changed += OnSubFunctionChanged;
95            subFunctions.Add(subFunction.Name, subFunction);
96          }
97        }
98      return subFunctions;
99    }
100
101    private ISymbolicExpressionTree AddLinearScalingTerms(ISymbolicExpressionTree tree) {
102      var clonedTree = (ISymbolicExpressionTree)tree.Clone();
103      var startNode = clonedTree.Root.Subtrees.First();
104      var template = startNode.Subtrees.First();
105
106      var add = new Addition();
107      var addNode = add.CreateTreeNode();
108
109      var mul = new Multiplication();
110      var mulNode = mul.CreateTreeNode();
111
112      var c1 = new Constant();
113      var c1Node = (ConstantTreeNode)c1.CreateTreeNode();
114      c1Node.Value = 0.0;
115      var c2 = new Constant();
116      var c2Node = (ConstantTreeNode)c2.CreateTreeNode();
117      c2Node.Value = 1.0;
118     
119      addNode.AddSubtree(c1Node);
120      addNode.AddSubtree(mulNode);
121      mulNode.AddSubtree(c2Node);
122       
123      startNode.RemoveSubtree(0);
124      startNode.AddSubtree(addNode);
125      mulNode.AddSubtree(template);
126      return clonedTree;
127    }
128
129    private void OnSubFunctionChanged(object sender, EventArgs e) => OnChanged();
130  }
131}
Note: See TracBrowser for help on using the repository browser.