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 |
|
---|
36 | var newFunctions = GetSubFunctions();
|
---|
37 | var oldFunctions = subFunctions?.Intersect(newFunctions)
|
---|
38 | ?? Enumerable.Empty<SubFunction>();
|
---|
39 | // adds new functions and keeps the old ones (if they match)
|
---|
40 | subFunctions = newFunctions.Except(oldFunctions).Concat(oldFunctions).ToList();
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | [Storable]
|
---|
45 | private IList<SubFunction> subFunctions = new List<SubFunction>();
|
---|
46 | public IEnumerable<SubFunction> SubFunctions => subFunctions;
|
---|
47 |
|
---|
48 | [Storable]
|
---|
49 | private bool applyLinearScaling;
|
---|
50 | public bool ApplyLinearScaling {
|
---|
51 | get => applyLinearScaling;
|
---|
52 | set {
|
---|
53 | applyLinearScaling = value;
|
---|
54 | OnChanged();
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | protected InfixExpressionParser Parser { get; set; } = new InfixExpressionParser();
|
---|
59 |
|
---|
60 | #endregion
|
---|
61 |
|
---|
62 | #region Events
|
---|
63 | public event EventHandler Changed;
|
---|
64 |
|
---|
65 | private void OnChanged() => Changed?.Invoke(this, EventArgs.Empty);
|
---|
66 | #endregion
|
---|
67 |
|
---|
68 | #region Constructors
|
---|
69 | public StructureTemplate() {
|
---|
70 | Reset();
|
---|
71 | }
|
---|
72 |
|
---|
73 | [StorableConstructor]
|
---|
74 | protected StructureTemplate(StorableConstructorFlag _) : base(_) { }
|
---|
75 |
|
---|
76 | protected StructureTemplate(StructureTemplate original, Cloner cloner) : base(original, cloner) {
|
---|
77 | this.Tree = cloner.Clone(original.Tree);
|
---|
78 | this.Template = original.Template;
|
---|
79 | this.ApplyLinearScaling = original.ApplyLinearScaling;
|
---|
80 | this.subFunctions = original.subFunctions.Select(cloner.Clone).ToList();
|
---|
81 | RegisterEventHandlers();
|
---|
82 | }
|
---|
83 |
|
---|
84 |
|
---|
85 | [StorableHook(HookType.AfterDeserialization)]
|
---|
86 | private void AfterDeserialization() {
|
---|
87 | RegisterEventHandlers();
|
---|
88 | }
|
---|
89 | #endregion
|
---|
90 |
|
---|
91 | #region Cloning
|
---|
92 | public override IDeepCloneable Clone(Cloner cloner) =>
|
---|
93 | new StructureTemplate(this, cloner);
|
---|
94 | #endregion
|
---|
95 |
|
---|
96 | public void Reset() {
|
---|
97 | subFunctions = new List<SubFunction>();
|
---|
98 | treeWithoutLinearScaling = null;
|
---|
99 | treeWithLinearScaling = null;
|
---|
100 | applyLinearScaling = false;
|
---|
101 | Template = "f(_)";
|
---|
102 | OnChanged();
|
---|
103 | }
|
---|
104 |
|
---|
105 | private IList<SubFunction> GetSubFunctions() {
|
---|
106 | var subFunctions = new List<SubFunction>();
|
---|
107 | foreach (var node in Tree.IterateNodesPrefix())
|
---|
108 | if (node is SubFunctionTreeNode subFunctionTreeNode) {
|
---|
109 | if (!subFunctionTreeNode.Arguments.Any())
|
---|
110 | throw new ArgumentException($"The sub-function '{subFunctionTreeNode}' requires inputs (e.g. {subFunctionTreeNode.Name}(var1, var2)).");
|
---|
111 |
|
---|
112 | var existingSubFunction = subFunctions.Where(x => x.Name == subFunctionTreeNode.Name).FirstOrDefault();
|
---|
113 | if (existingSubFunction != null) {
|
---|
114 | // an existing subFunction needs the same signature
|
---|
115 | if(!existingSubFunction.Arguments.SequenceEqual(subFunctionTreeNode.Arguments))
|
---|
116 | throw new ArgumentException(
|
---|
117 | $"The sub-function '{existingSubFunction.Name}' has (at least two) different signatures " +
|
---|
118 | $"({existingSubFunction.Name}({string.Join(",", existingSubFunction.Arguments)}) <> " +
|
---|
119 | $"{subFunctionTreeNode.Name}({string.Join(",", subFunctionTreeNode.Arguments)})).");
|
---|
120 | } else {
|
---|
121 | var subFunction = new SubFunction() {
|
---|
122 | Name = subFunctionTreeNode.Name,
|
---|
123 | Arguments = subFunctionTreeNode.Arguments
|
---|
124 | };
|
---|
125 | subFunction.Changed += OnSubFunctionChanged;
|
---|
126 | subFunctions.Add(subFunction);
|
---|
127 | }
|
---|
128 | }
|
---|
129 | return subFunctions;
|
---|
130 | }
|
---|
131 |
|
---|
132 | private void RegisterEventHandlers() {
|
---|
133 | foreach(var sf in SubFunctions) {
|
---|
134 | sf.Changed += OnSubFunctionChanged;
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | private ISymbolicExpressionTree AddLinearScalingTerms(ISymbolicExpressionTree tree) {
|
---|
139 | var clonedTree = (ISymbolicExpressionTree)tree.Clone();
|
---|
140 | var startNode = clonedTree.Root.Subtrees.First();
|
---|
141 | var template = startNode.Subtrees.First();
|
---|
142 |
|
---|
143 | var add = new Addition();
|
---|
144 | var addNode = add.CreateTreeNode();
|
---|
145 |
|
---|
146 | var mul = new Multiplication();
|
---|
147 | var mulNode = mul.CreateTreeNode();
|
---|
148 |
|
---|
149 | var offset = new Number();
|
---|
150 | var offsetNode = (NumberTreeNode)offset.CreateTreeNode();
|
---|
151 | offsetNode.Value = 0.0;
|
---|
152 | var scale = new Number();
|
---|
153 | var scaleNode = (NumberTreeNode)scale.CreateTreeNode();
|
---|
154 | scaleNode.Value = 1.0;
|
---|
155 |
|
---|
156 | addNode.AddSubtree(offsetNode);
|
---|
157 | addNode.AddSubtree(mulNode);
|
---|
158 | mulNode.AddSubtree(scaleNode);
|
---|
159 |
|
---|
160 | startNode.RemoveSubtree(0);
|
---|
161 | startNode.AddSubtree(addNode);
|
---|
162 | mulNode.AddSubtree(template);
|
---|
163 | return clonedTree;
|
---|
164 | }
|
---|
165 |
|
---|
166 | private void OnSubFunctionChanged(object sender, EventArgs e) => OnChanged();
|
---|
167 | }
|
---|
168 | }
|
---|