1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using HEAL.Attic;
|
---|
5 | using HeuristicLab.Common;
|
---|
6 | using HeuristicLab.Core;
|
---|
7 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
8 |
|
---|
9 | namespace 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 offset = new Number();
|
---|
128 | var offsetNode = (NumberTreeNode)offset.CreateTreeNode();
|
---|
129 | offsetNode.Value = 0.0;
|
---|
130 | var scale = new Number();
|
---|
131 | var scaleNode = (NumberTreeNode)scale.CreateTreeNode();
|
---|
132 | scaleNode.Value = 1.0;
|
---|
133 |
|
---|
134 | addNode.AddSubtree(offsetNode);
|
---|
135 | addNode.AddSubtree(mulNode);
|
---|
136 | mulNode.AddSubtree(scaleNode);
|
---|
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 | }
|
---|