1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HEAL.Attic;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
31 | [StorableType("E3C038DB-C6AA-457D-9F65-AF16C44CCE22")]
|
---|
32 | [Item("StructureTemplate", "Structure Template")]
|
---|
33 | public class StructureTemplate : Item {
|
---|
34 |
|
---|
35 | #region Properties
|
---|
36 | [Storable]
|
---|
37 | private string template;
|
---|
38 | public string Template {
|
---|
39 | get => template;
|
---|
40 | set {
|
---|
41 | if (value == template) return;
|
---|
42 |
|
---|
43 | var parsedTree = Parser.Parse(value);
|
---|
44 | //assignment must be done after successfully parsing the tree
|
---|
45 | template = value;
|
---|
46 |
|
---|
47 | if (applyLinearScaling)
|
---|
48 | parsedTree = LinearScaling.AddLinearScalingTerms(parsedTree);
|
---|
49 | Tree = parsedTree;
|
---|
50 | OnChanged();
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | private ISymbolicExpressionTree _oldTree;
|
---|
55 | [Storable(OldName = "treeWithoutLinearScaling")]
|
---|
56 | private ISymbolicExpressionTree _OldTreeW {
|
---|
57 | set => _oldTree = value;
|
---|
58 | }
|
---|
59 |
|
---|
60 | [Storable]
|
---|
61 | private ISymbolicExpressionTree tree;
|
---|
62 | public ISymbolicExpressionTree Tree {
|
---|
63 | get => tree;
|
---|
64 | private set {
|
---|
65 | containsNumericParameters = null;
|
---|
66 |
|
---|
67 | var newFunctions = CreateSubFunctions(value);
|
---|
68 | var oldFunctions = subFunctions?.Intersect(newFunctions)
|
---|
69 | ?? Enumerable.Empty<SubFunction>();
|
---|
70 | // adds new functions and keeps the old ones (if they match)
|
---|
71 | var functionsToAdd = newFunctions.Except(oldFunctions);
|
---|
72 | subFunctions = functionsToAdd.Concat(oldFunctions).ToList();
|
---|
73 | RegisterSubFunctionEventHandlers(functionsToAdd);
|
---|
74 |
|
---|
75 | tree = value;
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | private bool? containsNumericParameters;
|
---|
80 | public bool ContainsNumericParameters {
|
---|
81 | get {
|
---|
82 | if (!containsNumericParameters.HasValue)
|
---|
83 | containsNumericParameters = Tree.IterateNodesPrefix().OfType<NumberTreeNode>().Any();
|
---|
84 |
|
---|
85 | return containsNumericParameters.Value;
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | [Storable]
|
---|
90 | private IList<SubFunction> subFunctions = new List<SubFunction>();
|
---|
91 | public IEnumerable<SubFunction> SubFunctions => subFunctions;
|
---|
92 |
|
---|
93 | [Storable]
|
---|
94 | private bool applyLinearScaling;
|
---|
95 | public bool ApplyLinearScaling {
|
---|
96 | get => applyLinearScaling;
|
---|
97 | set {
|
---|
98 | if (value == applyLinearScaling) return;
|
---|
99 |
|
---|
100 | applyLinearScaling = value;
|
---|
101 | if (applyLinearScaling) LinearScaling.AddLinearScalingTerms(Tree);
|
---|
102 | else LinearScaling.RemoveLinearScalingTerms(Tree);
|
---|
103 |
|
---|
104 | OnChanged();
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | protected InfixExpressionParser Parser { get; set; } = new InfixExpressionParser();
|
---|
109 | #endregion
|
---|
110 |
|
---|
111 | #region Events
|
---|
112 | public event EventHandler Changed;
|
---|
113 |
|
---|
114 | private void OnChanged() => Changed?.Invoke(this, EventArgs.Empty);
|
---|
115 | #endregion
|
---|
116 |
|
---|
117 | #region Constructors
|
---|
118 | public StructureTemplate() {
|
---|
119 | Reset();
|
---|
120 | }
|
---|
121 |
|
---|
122 | [StorableConstructor]
|
---|
123 | protected StructureTemplate(StorableConstructorFlag _) : base(_) { }
|
---|
124 |
|
---|
125 | protected StructureTemplate(StructureTemplate original, Cloner cloner) : base(original, cloner) {
|
---|
126 | this.tree = cloner.Clone(original.tree);
|
---|
127 | this.template = original.Template;
|
---|
128 | this.applyLinearScaling = original.ApplyLinearScaling;
|
---|
129 | this.subFunctions = original.subFunctions.Select(cloner.Clone).ToList();
|
---|
130 | RegisterSubFunctionEventHandlers(SubFunctions);
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | [StorableHook(HookType.AfterDeserialization)]
|
---|
135 | private void AfterDeserialization() {
|
---|
136 | if (Tree == null && _oldTree != null) {
|
---|
137 | if (ApplyLinearScaling) _oldTree = LinearScaling.AddLinearScalingTerms(_oldTree);
|
---|
138 | Tree = _oldTree;
|
---|
139 | _oldTree = null;
|
---|
140 | }
|
---|
141 |
|
---|
142 | RegisterSubFunctionEventHandlers(SubFunctions);
|
---|
143 | }
|
---|
144 | #endregion
|
---|
145 |
|
---|
146 | #region Cloning
|
---|
147 | public override IDeepCloneable Clone(Cloner cloner) =>
|
---|
148 | new StructureTemplate(this, cloner);
|
---|
149 | #endregion
|
---|
150 |
|
---|
151 | public void Reset() {
|
---|
152 | subFunctions = new List<SubFunction>();
|
---|
153 | tree = null;
|
---|
154 | containsNumericParameters = null;
|
---|
155 | Template = "f(_)";
|
---|
156 | }
|
---|
157 |
|
---|
158 | private static IList<SubFunction> CreateSubFunctions(ISymbolicExpressionTree tree) {
|
---|
159 | var subFunctions = new List<SubFunction>();
|
---|
160 | foreach (var node in tree.IterateNodesPrefix())
|
---|
161 | if (node is SubFunctionTreeNode subFunctionTreeNode) {
|
---|
162 | if (!subFunctionTreeNode.Arguments.Any())
|
---|
163 | throw new ArgumentException($"The sub-function '{subFunctionTreeNode}' requires inputs (e.g. {subFunctionTreeNode.Name}(var1, var2)).");
|
---|
164 |
|
---|
165 | var existingSubFunction = subFunctions.Where(x => x.Name == subFunctionTreeNode.Name).FirstOrDefault();
|
---|
166 | if (existingSubFunction != null) {
|
---|
167 | // an existing subFunction must have the same signature
|
---|
168 | if (!existingSubFunction.Arguments.SequenceEqual(subFunctionTreeNode.Arguments))
|
---|
169 | throw new ArgumentException(
|
---|
170 | $"The sub-function '{existingSubFunction.Name}' has (at least two) different signatures " +
|
---|
171 | $"({existingSubFunction.Name}({string.Join(",", existingSubFunction.Arguments)}) <> " +
|
---|
172 | $"{subFunctionTreeNode.Name}({string.Join(",", subFunctionTreeNode.Arguments)})).");
|
---|
173 | } else {
|
---|
174 | var subFunction = new SubFunction() {
|
---|
175 | Name = subFunctionTreeNode.Name,
|
---|
176 | Arguments = subFunctionTreeNode.Arguments
|
---|
177 | };
|
---|
178 | subFunctions.Add(subFunction);
|
---|
179 | }
|
---|
180 | }
|
---|
181 | return subFunctions;
|
---|
182 | }
|
---|
183 |
|
---|
184 | private void RegisterSubFunctionEventHandlers(IEnumerable<SubFunction> subFunctions) {
|
---|
185 | foreach (var sf in subFunctions) {
|
---|
186 | sf.Changed += (o, e) => OnChanged();
|
---|
187 | }
|
---|
188 | }
|
---|
189 | }
|
---|
190 | }
|
---|