Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 18074 was 18074, checked in by chaider, 2 years ago

#3136

-Fixed cloning of StructureTemplate

File size: 4.6 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      this.Tree = cloner.Clone(original.Tree);
70      this.Template = original.Template;
71      this.ApplyLinearScaling = original.ApplyLinearScaling;
72      this.SubFunctions = original.SubFunctions;
73    }
74    #endregion
75
76    #region Cloning
77    public override IDeepCloneable Clone(Cloner cloner) =>
78      new StructureTemplate(this, cloner);
79    #endregion
80
81   
82
83    private Dictionary<string, SubFunction> GetSubFunctions() {
84      var subFunctions = new Dictionary<string, SubFunction>();
85      foreach (var node in Tree.IterateNodesPrefix())
86        if (node is SubFunctionTreeNode subFunctionTreeNode) {
87          if (!subFunctionTreeNode.Arguments.Any())
88            throw new ArgumentException($"The sub-function '{subFunctionTreeNode}' requires inputs (e.g. {subFunctionTreeNode.Name}(var1, var2)).");
89
90          if (subFunctions.TryGetValue(subFunctionTreeNode.Name, out SubFunction v)) {
91            if(!v.Arguments.SequenceEqual(subFunctionTreeNode.Arguments))
92              throw new ArgumentException(
93                $"The sub-function '{v.Name}' has (at least two) different signatures " +
94                $"({v.Name}({string.Join(",", v.Arguments)}) <> {subFunctionTreeNode.Name}({string.Join(",", subFunctionTreeNode.Arguments)})).");
95          } else {
96            var subFunction = new SubFunction() {
97              Name = subFunctionTreeNode.Name,
98              Arguments = subFunctionTreeNode.Arguments
99            };
100            subFunction.Changed += OnSubFunctionChanged;
101            subFunctions.Add(subFunction.Name, subFunction);
102          }
103        }
104      return subFunctions;
105    }
106
107    private ISymbolicExpressionTree AddLinearScalingTerms(ISymbolicExpressionTree tree) {
108      var clonedTree = (ISymbolicExpressionTree)tree.Clone();
109      var startNode = clonedTree.Root.Subtrees.First();
110      var template = startNode.Subtrees.First();
111
112      var add = new Addition();
113      var addNode = add.CreateTreeNode();
114
115      var mul = new Multiplication();
116      var mulNode = mul.CreateTreeNode();
117
118      var c1 = new Constant();
119      var c1Node = (ConstantTreeNode)c1.CreateTreeNode();
120      c1Node.Value = 0.0;
121      var c2 = new Constant();
122      var c2Node = (ConstantTreeNode)c2.CreateTreeNode();
123      c2Node.Value = 1.0;
124     
125      addNode.AddSubtree(c1Node);
126      addNode.AddSubtree(mulNode);
127      mulNode.AddSubtree(c2Node);
128       
129      startNode.RemoveSubtree(0);
130      startNode.AddSubtree(addNode);
131      mulNode.AddSubtree(template);
132      return clonedTree;
133    }
134
135    private void OnSubFunctionChanged(object sender, EventArgs e) => OnChanged();
136  }
137}
Note: See TracBrowser for help on using the repository browser.