1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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.Diagnostics;
|
---|
25 | using System.Linq;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
30 | /// <summary>
|
---|
31 | /// Simplifier for symbolic expressions
|
---|
32 | /// </summary>
|
---|
33 | public class SymbolicDataAnalysisExpressionTreeSimplifier {
|
---|
34 | private Addition addSymbol = new Addition();
|
---|
35 | private Subtraction subSymbol = new Subtraction();
|
---|
36 | private Multiplication mulSymbol = new Multiplication();
|
---|
37 | private Division divSymbol = new Division();
|
---|
38 | private Constant constSymbol = new Constant();
|
---|
39 | private Variable varSymbol = new Variable();
|
---|
40 | private Logarithm logSymbol = new Logarithm();
|
---|
41 | private Exponential expSymbol = new Exponential();
|
---|
42 | private Root rootSymbol = new Root();
|
---|
43 | private Power powSymbol = new Power();
|
---|
44 | private Sine sineSymbol = new Sine();
|
---|
45 | private Cosine cosineSymbol = new Cosine();
|
---|
46 | private Tangent tanSymbol = new Tangent();
|
---|
47 | private IfThenElse ifThenElseSymbol = new IfThenElse();
|
---|
48 | private And andSymbol = new And();
|
---|
49 | private Or orSymbol = new Or();
|
---|
50 | private Not notSymbol = new Not();
|
---|
51 | private GreaterThan gtSymbol = new GreaterThan();
|
---|
52 | private LessThan ltSymbol = new LessThan();
|
---|
53 |
|
---|
54 | public ISymbolicExpressionTree Simplify(ISymbolicExpressionTree originalTree) {
|
---|
55 | var clone = (ISymbolicExpressionTreeNode)originalTree.Root.Clone();
|
---|
56 | // macro expand (initially no argument trees)
|
---|
57 | var macroExpandedTree = MacroExpand(clone, clone.GetSubtree(0), new List<ISymbolicExpressionTreeNode>());
|
---|
58 | ISymbolicExpressionTreeNode rootNode = (new ProgramRootSymbol()).CreateTreeNode();
|
---|
59 | rootNode.AddSubtree(GetSimplifiedTree(macroExpandedTree));
|
---|
60 | return new SymbolicExpressionTree(rootNode);
|
---|
61 | }
|
---|
62 |
|
---|
63 | // the argumentTrees list contains already expanded trees used as arguments for invocations
|
---|
64 | private ISymbolicExpressionTreeNode MacroExpand(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode node, IList<ISymbolicExpressionTreeNode> argumentTrees) {
|
---|
65 | List<ISymbolicExpressionTreeNode> subtrees = new List<ISymbolicExpressionTreeNode>(node.Subtrees);
|
---|
66 | while (node.SubtreesCount > 0) node.RemoveSubtree(0);
|
---|
67 | if (node.Symbol is InvokeFunction) {
|
---|
68 | var invokeSym = node.Symbol as InvokeFunction;
|
---|
69 | var defunNode = FindFunctionDefinition(root, invokeSym.FunctionName);
|
---|
70 | var macroExpandedArguments = new List<ISymbolicExpressionTreeNode>();
|
---|
71 | foreach (var subtree in subtrees) {
|
---|
72 | macroExpandedArguments.Add(MacroExpand(root, subtree, argumentTrees));
|
---|
73 | }
|
---|
74 | return MacroExpand(root, defunNode, macroExpandedArguments);
|
---|
75 | } else if (node.Symbol is Argument) {
|
---|
76 | var argSym = node.Symbol as Argument;
|
---|
77 | // return the correct argument sub-tree (already macro-expanded)
|
---|
78 | return (SymbolicExpressionTreeNode)argumentTrees[argSym.ArgumentIndex].Clone();
|
---|
79 | } else {
|
---|
80 | // recursive application
|
---|
81 | foreach (var subtree in subtrees) {
|
---|
82 | node.AddSubtree(MacroExpand(root, subtree, argumentTrees));
|
---|
83 | }
|
---|
84 | return node;
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | private ISymbolicExpressionTreeNode FindFunctionDefinition(ISymbolicExpressionTreeNode root, string functionName) {
|
---|
89 | foreach (var subtree in root.Subtrees.OfType<DefunTreeNode>()) {
|
---|
90 | if (subtree.FunctionName == functionName) return subtree.GetSubtree(0);
|
---|
91 | }
|
---|
92 |
|
---|
93 | throw new ArgumentException("Definition of function " + functionName + " not found.");
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | #region symbol predicates
|
---|
98 | // arithmetic
|
---|
99 | private bool IsDivision(ISymbolicExpressionTreeNode node) {
|
---|
100 | return node.Symbol is Division;
|
---|
101 | }
|
---|
102 |
|
---|
103 | private bool IsMultiplication(ISymbolicExpressionTreeNode node) {
|
---|
104 | return node.Symbol is Multiplication;
|
---|
105 | }
|
---|
106 |
|
---|
107 | private bool IsSubtraction(ISymbolicExpressionTreeNode node) {
|
---|
108 | return node.Symbol is Subtraction;
|
---|
109 | }
|
---|
110 |
|
---|
111 | private bool IsAddition(ISymbolicExpressionTreeNode node) {
|
---|
112 | return node.Symbol is Addition;
|
---|
113 | }
|
---|
114 |
|
---|
115 | private bool IsAverage(ISymbolicExpressionTreeNode node) {
|
---|
116 | return node.Symbol is Average;
|
---|
117 | }
|
---|
118 | // exponential
|
---|
119 | private bool IsLog(ISymbolicExpressionTreeNode node) {
|
---|
120 | return node.Symbol is Logarithm;
|
---|
121 | }
|
---|
122 | private bool IsExp(ISymbolicExpressionTreeNode node) {
|
---|
123 | return node.Symbol is Exponential;
|
---|
124 | }
|
---|
125 | private bool IsRoot(ISymbolicExpressionTreeNode node) {
|
---|
126 | return node.Symbol is Root;
|
---|
127 | }
|
---|
128 | private bool IsPower(ISymbolicExpressionTreeNode node) {
|
---|
129 | return node.Symbol is Power;
|
---|
130 | }
|
---|
131 | // trigonometric
|
---|
132 | private bool IsSine(ISymbolicExpressionTreeNode node) {
|
---|
133 | return node.Symbol is Sine;
|
---|
134 | }
|
---|
135 | private bool IsCosine(ISymbolicExpressionTreeNode node) {
|
---|
136 | return node.Symbol is Cosine;
|
---|
137 | }
|
---|
138 | private bool IsTangent(ISymbolicExpressionTreeNode node) {
|
---|
139 | return node.Symbol is Tangent;
|
---|
140 | }
|
---|
141 | // boolean
|
---|
142 | private bool IsIfThenElse(ISymbolicExpressionTreeNode node) {
|
---|
143 | return node.Symbol is IfThenElse;
|
---|
144 | }
|
---|
145 | private bool IsAnd(ISymbolicExpressionTreeNode node) {
|
---|
146 | return node.Symbol is And;
|
---|
147 | }
|
---|
148 | private bool IsOr(ISymbolicExpressionTreeNode node) {
|
---|
149 | return node.Symbol is Or;
|
---|
150 | }
|
---|
151 | private bool IsNot(ISymbolicExpressionTreeNode node) {
|
---|
152 | return node.Symbol is Not;
|
---|
153 | }
|
---|
154 | // comparison
|
---|
155 | private bool IsGreaterThan(ISymbolicExpressionTreeNode node) {
|
---|
156 | return node.Symbol is GreaterThan;
|
---|
157 | }
|
---|
158 | private bool IsLessThan(ISymbolicExpressionTreeNode node) {
|
---|
159 | return node.Symbol is LessThan;
|
---|
160 | }
|
---|
161 |
|
---|
162 | private bool IsBoolean(ISymbolicExpressionTreeNode node) {
|
---|
163 | return
|
---|
164 | node.Symbol is GreaterThan ||
|
---|
165 | node.Symbol is LessThan ||
|
---|
166 | node.Symbol is And ||
|
---|
167 | node.Symbol is Or;
|
---|
168 | }
|
---|
169 |
|
---|
170 | // terminals
|
---|
171 | private bool IsVariable(ISymbolicExpressionTreeNode node) {
|
---|
172 | return node.Symbol is Variable;
|
---|
173 | }
|
---|
174 |
|
---|
175 | private bool IsConstant(ISymbolicExpressionTreeNode node) {
|
---|
176 | return node.Symbol is Constant;
|
---|
177 | }
|
---|
178 |
|
---|
179 | #endregion
|
---|
180 |
|
---|
181 | /// <summary>
|
---|
182 | /// Creates a new simplified tree
|
---|
183 | /// </summary>
|
---|
184 | /// <param name="original"></param>
|
---|
185 | /// <returns></returns>
|
---|
186 | public ISymbolicExpressionTreeNode GetSimplifiedTree(ISymbolicExpressionTreeNode original) {
|
---|
187 | if (IsConstant(original) || IsVariable(original)) {
|
---|
188 | return (ISymbolicExpressionTreeNode)original.Clone();
|
---|
189 | } else if (IsAddition(original)) {
|
---|
190 | return SimplifyAddition(original);
|
---|
191 | } else if (IsSubtraction(original)) {
|
---|
192 | return SimplifySubtraction(original);
|
---|
193 | } else if (IsMultiplication(original)) {
|
---|
194 | return SimplifyMultiplication(original);
|
---|
195 | } else if (IsDivision(original)) {
|
---|
196 | return SimplifyDivision(original);
|
---|
197 | } else if (IsAverage(original)) {
|
---|
198 | return SimplifyAverage(original);
|
---|
199 | } else if (IsLog(original)) {
|
---|
200 | return SimplifyLog(original);
|
---|
201 | } else if (IsExp(original)) {
|
---|
202 | return SimplifyExp(original);
|
---|
203 | } else if (IsRoot(original)) {
|
---|
204 | return SimplifyRoot(original);
|
---|
205 | } else if (IsPower(original)) {
|
---|
206 | return SimplifyPower(original);
|
---|
207 | } else if (IsSine(original)) {
|
---|
208 | return SimplifySine(original);
|
---|
209 | } else if (IsCosine(original)) {
|
---|
210 | return SimplifyCosine(original);
|
---|
211 | } else if (IsTangent(original)) {
|
---|
212 | return SimplifyTangent(original);
|
---|
213 | } else if (IsIfThenElse(original)) {
|
---|
214 | return SimplifyIfThenElse(original);
|
---|
215 | } else if (IsGreaterThan(original)) {
|
---|
216 | return SimplifyGreaterThan(original);
|
---|
217 | } else if (IsLessThan(original)) {
|
---|
218 | return SimplifyLessThan(original);
|
---|
219 | } else if (IsAnd(original)) {
|
---|
220 | return SimplifyAnd(original);
|
---|
221 | } else if (IsOr(original)) {
|
---|
222 | return SimplifyOr(original);
|
---|
223 | } else if (IsNot(original)) {
|
---|
224 | return SimplifyNot(original);
|
---|
225 | } else {
|
---|
226 | return SimplifyAny(original);
|
---|
227 | }
|
---|
228 | }
|
---|
229 |
|
---|
230 |
|
---|
231 | #region specific simplification routines
|
---|
232 | private ISymbolicExpressionTreeNode SimplifyAny(ISymbolicExpressionTreeNode original) {
|
---|
233 | // can't simplify this function but simplify all subtrees
|
---|
234 | List<ISymbolicExpressionTreeNode> subtrees = new List<ISymbolicExpressionTreeNode>(original.Subtrees);
|
---|
235 | while (original.Subtrees.Count() > 0) original.RemoveSubtree(0);
|
---|
236 | var clone = (SymbolicExpressionTreeNode)original.Clone();
|
---|
237 | List<ISymbolicExpressionTreeNode> simplifiedSubtrees = new List<ISymbolicExpressionTreeNode>();
|
---|
238 | foreach (var subtree in subtrees) {
|
---|
239 | simplifiedSubtrees.Add(GetSimplifiedTree(subtree));
|
---|
240 | original.AddSubtree(subtree);
|
---|
241 | }
|
---|
242 | foreach (var simplifiedSubtree in simplifiedSubtrees) {
|
---|
243 | clone.AddSubtree(simplifiedSubtree);
|
---|
244 | }
|
---|
245 | if (simplifiedSubtrees.TrueForAll(t => IsConstant(t))) {
|
---|
246 | SimplifyConstantExpression(clone);
|
---|
247 | }
|
---|
248 | return clone;
|
---|
249 | }
|
---|
250 |
|
---|
251 | private ISymbolicExpressionTreeNode SimplifyConstantExpression(ISymbolicExpressionTreeNode original) {
|
---|
252 | // not yet implemented
|
---|
253 | return original;
|
---|
254 | }
|
---|
255 |
|
---|
256 | private ISymbolicExpressionTreeNode SimplifyAverage(ISymbolicExpressionTreeNode original) {
|
---|
257 | if (original.Subtrees.Count() == 1) {
|
---|
258 | return GetSimplifiedTree(original.GetSubtree(0));
|
---|
259 | } else {
|
---|
260 | // simplify expressions x0..xn
|
---|
261 | // make sum(x0..xn) / n
|
---|
262 | Trace.Assert(original.Subtrees.Count() > 1);
|
---|
263 | var sum = original.Subtrees
|
---|
264 | .Select(x => GetSimplifiedTree(x))
|
---|
265 | .Aggregate((a, b) => MakeSum(a, b));
|
---|
266 | return MakeFraction(sum, MakeConstant(original.Subtrees.Count()));
|
---|
267 | }
|
---|
268 | }
|
---|
269 |
|
---|
270 | private ISymbolicExpressionTreeNode SimplifyDivision(ISymbolicExpressionTreeNode original) {
|
---|
271 | if (original.Subtrees.Count() == 1) {
|
---|
272 | return Invert(GetSimplifiedTree(original.GetSubtree(0)));
|
---|
273 | } else {
|
---|
274 | // simplify expressions x0..xn
|
---|
275 | // make multiplication (x0 * 1/(x1 * x1 * .. * xn))
|
---|
276 | Trace.Assert(original.Subtrees.Count() > 1);
|
---|
277 | var simplifiedTrees = original.Subtrees.Select(x => GetSimplifiedTree(x));
|
---|
278 | return
|
---|
279 | MakeProduct(simplifiedTrees.First(), Invert(simplifiedTrees.Skip(1).Aggregate((a, b) => MakeProduct(a, b))));
|
---|
280 | }
|
---|
281 | }
|
---|
282 |
|
---|
283 | private ISymbolicExpressionTreeNode SimplifyMultiplication(ISymbolicExpressionTreeNode original) {
|
---|
284 | if (original.Subtrees.Count() == 1) {
|
---|
285 | return GetSimplifiedTree(original.GetSubtree(0));
|
---|
286 | } else {
|
---|
287 | Trace.Assert(original.Subtrees.Count() > 1);
|
---|
288 | return original.Subtrees
|
---|
289 | .Select(x => GetSimplifiedTree(x))
|
---|
290 | .Aggregate((a, b) => MakeProduct(a, b));
|
---|
291 | }
|
---|
292 | }
|
---|
293 |
|
---|
294 | private ISymbolicExpressionTreeNode SimplifySubtraction(ISymbolicExpressionTreeNode original) {
|
---|
295 | if (original.Subtrees.Count() == 1) {
|
---|
296 | return Negate(GetSimplifiedTree(original.GetSubtree(0)));
|
---|
297 | } else {
|
---|
298 | // simplify expressions x0..xn
|
---|
299 | // make addition (x0,-x1..-xn)
|
---|
300 | Trace.Assert(original.Subtrees.Count() > 1);
|
---|
301 | var simplifiedTrees = original.Subtrees.Select(x => GetSimplifiedTree(x));
|
---|
302 | return simplifiedTrees.Take(1)
|
---|
303 | .Concat(simplifiedTrees.Skip(1).Select(x => Negate(x)))
|
---|
304 | .Aggregate((a, b) => MakeSum(a, b));
|
---|
305 | }
|
---|
306 | }
|
---|
307 |
|
---|
308 | private ISymbolicExpressionTreeNode SimplifyAddition(ISymbolicExpressionTreeNode original) {
|
---|
309 | if (original.Subtrees.Count() == 1) {
|
---|
310 | return GetSimplifiedTree(original.GetSubtree(0));
|
---|
311 | } else {
|
---|
312 | // simplify expression x0..xn
|
---|
313 | // make addition (x0..xn)
|
---|
314 | Trace.Assert(original.Subtrees.Count() > 1);
|
---|
315 | return original.Subtrees
|
---|
316 | .Select(x => GetSimplifiedTree(x))
|
---|
317 | .Aggregate((a, b) => MakeSum(a, b));
|
---|
318 | }
|
---|
319 | }
|
---|
320 |
|
---|
321 | private ISymbolicExpressionTreeNode SimplifyNot(ISymbolicExpressionTreeNode original) {
|
---|
322 | return MakeNot(GetSimplifiedTree(original.GetSubtree(0)));
|
---|
323 | }
|
---|
324 | private ISymbolicExpressionTreeNode SimplifyOr(ISymbolicExpressionTreeNode original) {
|
---|
325 | return original.Subtrees
|
---|
326 | .Select(x => GetSimplifiedTree(x))
|
---|
327 | .Aggregate((a, b) => MakeOr(a, b));
|
---|
328 | }
|
---|
329 | private ISymbolicExpressionTreeNode SimplifyAnd(ISymbolicExpressionTreeNode original) {
|
---|
330 | return original.Subtrees
|
---|
331 | .Select(x => GetSimplifiedTree(x))
|
---|
332 | .Aggregate((a, b) => MakeAnd(a, b));
|
---|
333 | }
|
---|
334 | private ISymbolicExpressionTreeNode SimplifyLessThan(ISymbolicExpressionTreeNode original) {
|
---|
335 | return MakeLessThan(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1)));
|
---|
336 | }
|
---|
337 | private ISymbolicExpressionTreeNode SimplifyGreaterThan(ISymbolicExpressionTreeNode original) {
|
---|
338 | return MakeGreaterThan(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1)));
|
---|
339 | }
|
---|
340 | private ISymbolicExpressionTreeNode SimplifyIfThenElse(ISymbolicExpressionTreeNode original) {
|
---|
341 | return MakeIfThenElse(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1)), GetSimplifiedTree(original.GetSubtree(2)));
|
---|
342 | }
|
---|
343 | private ISymbolicExpressionTreeNode SimplifyTangent(ISymbolicExpressionTreeNode original) {
|
---|
344 | return MakeTangent(GetSimplifiedTree(original.GetSubtree(0)));
|
---|
345 | }
|
---|
346 | private ISymbolicExpressionTreeNode SimplifyCosine(ISymbolicExpressionTreeNode original) {
|
---|
347 | return MakeCosine(GetSimplifiedTree(original.GetSubtree(0)));
|
---|
348 | }
|
---|
349 | private ISymbolicExpressionTreeNode SimplifySine(ISymbolicExpressionTreeNode original) {
|
---|
350 | return MakeSine(GetSimplifiedTree(original.GetSubtree(0)));
|
---|
351 | }
|
---|
352 | private ISymbolicExpressionTreeNode SimplifyExp(ISymbolicExpressionTreeNode original) {
|
---|
353 | return MakeExp(GetSimplifiedTree(original.GetSubtree(0)));
|
---|
354 | }
|
---|
355 |
|
---|
356 | private ISymbolicExpressionTreeNode SimplifyLog(ISymbolicExpressionTreeNode original) {
|
---|
357 | return MakeLog(GetSimplifiedTree(original.GetSubtree(0)));
|
---|
358 | }
|
---|
359 | private ISymbolicExpressionTreeNode SimplifyRoot(ISymbolicExpressionTreeNode original) {
|
---|
360 | return MakeRoot(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1)));
|
---|
361 | }
|
---|
362 |
|
---|
363 | private ISymbolicExpressionTreeNode SimplifyPower(ISymbolicExpressionTreeNode original) {
|
---|
364 | return MakePower(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1)));
|
---|
365 | }
|
---|
366 | #endregion
|
---|
367 |
|
---|
368 | #region low level tree restructuring
|
---|
369 | private ISymbolicExpressionTreeNode MakeNot(ISymbolicExpressionTreeNode t) {
|
---|
370 | if (IsConstant(t)) {
|
---|
371 | var constNode = t as ConstantTreeNode;
|
---|
372 | if (constNode.Value > 0) return MakeConstant(-1.0);
|
---|
373 | else return MakeConstant(1.0);
|
---|
374 | } else if (IsNot(t)) {
|
---|
375 | return t.GetSubtree(0);
|
---|
376 | } else if (!IsBoolean(t)) {
|
---|
377 | var gtNode = gtSymbol.CreateTreeNode();
|
---|
378 | gtNode.AddSubtree(t); gtNode.AddSubtree(MakeConstant(0.0));
|
---|
379 | var notNode = notSymbol.CreateTreeNode();
|
---|
380 | notNode.AddSubtree(gtNode);
|
---|
381 | return notNode;
|
---|
382 | } else {
|
---|
383 | var notNode = notSymbol.CreateTreeNode();
|
---|
384 | notNode.AddSubtree(t);
|
---|
385 | return notNode;
|
---|
386 | }
|
---|
387 | }
|
---|
388 |
|
---|
389 | private ISymbolicExpressionTreeNode MakeOr(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
|
---|
390 | if (IsConstant(a) && IsConstant(b)) {
|
---|
391 | var constA = a as ConstantTreeNode;
|
---|
392 | var constB = b as ConstantTreeNode;
|
---|
393 | if (constA.Value > 0.0 || constB.Value > 0.0) {
|
---|
394 | return MakeConstant(1.0);
|
---|
395 | } else {
|
---|
396 | return MakeConstant(-1.0);
|
---|
397 | }
|
---|
398 | } else if (IsConstant(a)) {
|
---|
399 | return MakeOr(b, a);
|
---|
400 | } else if (IsConstant(b)) {
|
---|
401 | var constT = b as ConstantTreeNode;
|
---|
402 | if (constT.Value > 0.0) {
|
---|
403 | // boolean expression is necessarily true
|
---|
404 | return MakeConstant(1.0);
|
---|
405 | } else {
|
---|
406 | // the constant value has no effect on the result of the boolean condition so we can drop the constant term
|
---|
407 | var orNode = orSymbol.CreateTreeNode();
|
---|
408 | orNode.AddSubtree(a);
|
---|
409 | return orNode;
|
---|
410 | }
|
---|
411 | } else {
|
---|
412 | var orNode = orSymbol.CreateTreeNode();
|
---|
413 | orNode.AddSubtree(a);
|
---|
414 | orNode.AddSubtree(b);
|
---|
415 | return orNode;
|
---|
416 | }
|
---|
417 | }
|
---|
418 | private ISymbolicExpressionTreeNode MakeAnd(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
|
---|
419 | if (IsConstant(a) && IsConstant(b)) {
|
---|
420 | var constA = a as ConstantTreeNode;
|
---|
421 | var constB = b as ConstantTreeNode;
|
---|
422 | if (constA.Value > 0.0 && constB.Value > 0.0) {
|
---|
423 | return MakeConstant(1.0);
|
---|
424 | } else {
|
---|
425 | return MakeConstant(-1.0);
|
---|
426 | }
|
---|
427 | } else if (IsConstant(a)) {
|
---|
428 | return MakeAnd(b, a);
|
---|
429 | } else if (IsConstant(b)) {
|
---|
430 | var constB = b as ConstantTreeNode;
|
---|
431 | if (constB.Value > 0.0) {
|
---|
432 | // the constant value has no effect on the result of the boolean condition so we can drop the constant term
|
---|
433 | var andNode = andSymbol.CreateTreeNode();
|
---|
434 | andNode.AddSubtree(a);
|
---|
435 | return andNode;
|
---|
436 | } else {
|
---|
437 | // boolean expression is necessarily false
|
---|
438 | return MakeConstant(-1.0);
|
---|
439 | }
|
---|
440 | } else {
|
---|
441 | var andNode = andSymbol.CreateTreeNode();
|
---|
442 | andNode.AddSubtree(a);
|
---|
443 | andNode.AddSubtree(b);
|
---|
444 | return andNode;
|
---|
445 | }
|
---|
446 | }
|
---|
447 | private ISymbolicExpressionTreeNode MakeLessThan(ISymbolicExpressionTreeNode leftSide, ISymbolicExpressionTreeNode rightSide) {
|
---|
448 | if (IsConstant(leftSide) && IsConstant(rightSide)) {
|
---|
449 | var lsConst = leftSide as ConstantTreeNode;
|
---|
450 | var rsConst = rightSide as ConstantTreeNode;
|
---|
451 | if (lsConst.Value < rsConst.Value) return MakeConstant(1.0);
|
---|
452 | else return MakeConstant(-1.0);
|
---|
453 | } else {
|
---|
454 | var ltNode = ltSymbol.CreateTreeNode();
|
---|
455 | ltNode.AddSubtree(leftSide);
|
---|
456 | ltNode.AddSubtree(rightSide);
|
---|
457 | return ltNode;
|
---|
458 | }
|
---|
459 | }
|
---|
460 | private ISymbolicExpressionTreeNode MakeGreaterThan(ISymbolicExpressionTreeNode leftSide, ISymbolicExpressionTreeNode rightSide) {
|
---|
461 | if (IsConstant(leftSide) && IsConstant(rightSide)) {
|
---|
462 | var lsConst = leftSide as ConstantTreeNode;
|
---|
463 | var rsConst = rightSide as ConstantTreeNode;
|
---|
464 | if (lsConst.Value > rsConst.Value) return MakeConstant(1.0);
|
---|
465 | else return MakeConstant(-1.0);
|
---|
466 | } else {
|
---|
467 | var gtNode = gtSymbol.CreateTreeNode();
|
---|
468 | gtNode.AddSubtree(leftSide);
|
---|
469 | gtNode.AddSubtree(rightSide);
|
---|
470 | return gtNode;
|
---|
471 | }
|
---|
472 | }
|
---|
473 | private ISymbolicExpressionTreeNode MakeIfThenElse(ISymbolicExpressionTreeNode condition, ISymbolicExpressionTreeNode trueBranch, ISymbolicExpressionTreeNode falseBranch) {
|
---|
474 | if (IsConstant(condition)) {
|
---|
475 | var constT = condition as ConstantTreeNode;
|
---|
476 | if (constT.Value > 0.0) return trueBranch;
|
---|
477 | else return falseBranch;
|
---|
478 | } else {
|
---|
479 | var ifNode = ifThenElseSymbol.CreateTreeNode();
|
---|
480 | if (IsBoolean(condition)) {
|
---|
481 | ifNode.AddSubtree(condition);
|
---|
482 | } else {
|
---|
483 | var gtNode = gtSymbol.CreateTreeNode();
|
---|
484 | gtNode.AddSubtree(condition); gtNode.AddSubtree(MakeConstant(0.0));
|
---|
485 | ifNode.AddSubtree(gtNode);
|
---|
486 | }
|
---|
487 | ifNode.AddSubtree(trueBranch);
|
---|
488 | ifNode.AddSubtree(falseBranch);
|
---|
489 | return ifNode;
|
---|
490 | }
|
---|
491 | }
|
---|
492 |
|
---|
493 | private ISymbolicExpressionTreeNode MakeSine(ISymbolicExpressionTreeNode node) {
|
---|
494 | if (IsConstant(node)) {
|
---|
495 | var constT = node as ConstantTreeNode;
|
---|
496 | return MakeConstant(Math.Sin(constT.Value));
|
---|
497 | } else {
|
---|
498 | var sineNode = sineSymbol.CreateTreeNode();
|
---|
499 | sineNode.AddSubtree(node);
|
---|
500 | return sineNode;
|
---|
501 | }
|
---|
502 | }
|
---|
503 | private ISymbolicExpressionTreeNode MakeTangent(ISymbolicExpressionTreeNode node) {
|
---|
504 | if (IsConstant(node)) {
|
---|
505 | var constT = node as ConstantTreeNode;
|
---|
506 | return MakeConstant(Math.Tan(constT.Value));
|
---|
507 | } else {
|
---|
508 | var tanNode = tanSymbol.CreateTreeNode();
|
---|
509 | tanNode.AddSubtree(node);
|
---|
510 | return tanNode;
|
---|
511 | }
|
---|
512 | }
|
---|
513 | private ISymbolicExpressionTreeNode MakeCosine(ISymbolicExpressionTreeNode node) {
|
---|
514 | if (IsConstant(node)) {
|
---|
515 | var constT = node as ConstantTreeNode;
|
---|
516 | return MakeConstant(Math.Cos(constT.Value));
|
---|
517 | } else {
|
---|
518 | var cosNode = cosineSymbol.CreateTreeNode();
|
---|
519 | cosNode.AddSubtree(node);
|
---|
520 | return cosNode;
|
---|
521 | }
|
---|
522 | }
|
---|
523 | private ISymbolicExpressionTreeNode MakeExp(ISymbolicExpressionTreeNode node) {
|
---|
524 | if (IsConstant(node)) {
|
---|
525 | var constT = node as ConstantTreeNode;
|
---|
526 | return MakeConstant(Math.Exp(constT.Value));
|
---|
527 | } else if (IsLog(node)) {
|
---|
528 | return node.GetSubtree(0);
|
---|
529 | } else if (IsAddition(node)) {
|
---|
530 | return node.Subtrees.Select(s => MakeExp(s)).Aggregate((s, t) => MakeProduct(s, t));
|
---|
531 | } else if (IsSubtraction(node)) {
|
---|
532 | return node.Subtrees.Select(s => MakeExp(s)).Aggregate((s, t) => MakeProduct(s, Negate(t)));
|
---|
533 | } else {
|
---|
534 | var expNode = expSymbol.CreateTreeNode();
|
---|
535 | expNode.AddSubtree(node);
|
---|
536 | return expNode;
|
---|
537 | }
|
---|
538 | }
|
---|
539 | private ISymbolicExpressionTreeNode MakeLog(ISymbolicExpressionTreeNode node) {
|
---|
540 | if (IsConstant(node)) {
|
---|
541 | var constT = node as ConstantTreeNode;
|
---|
542 | return MakeConstant(Math.Log(constT.Value));
|
---|
543 | } else if (IsExp(node)) {
|
---|
544 | return node.GetSubtree(0);
|
---|
545 | } else if (IsMultiplication(node)) {
|
---|
546 | return node.Subtrees.Select(s => MakeLog(s)).Aggregate((x, y) => MakeSum(x, y));
|
---|
547 | } else if (IsDivision(node)) {
|
---|
548 | var subtractionNode = subSymbol.CreateTreeNode();
|
---|
549 | foreach (var subtree in node.Subtrees) {
|
---|
550 | subtractionNode.AddSubtree(MakeLog(subtree));
|
---|
551 | }
|
---|
552 | return subtractionNode;
|
---|
553 | } else {
|
---|
554 | var logNode = logSymbol.CreateTreeNode();
|
---|
555 | logNode.AddSubtree(node);
|
---|
556 | return logNode;
|
---|
557 | }
|
---|
558 | }
|
---|
559 | private ISymbolicExpressionTreeNode MakeRoot(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
|
---|
560 | if (IsConstant(a) && IsConstant(b)) {
|
---|
561 | var constA = a as ConstantTreeNode;
|
---|
562 | var constB = b as ConstantTreeNode;
|
---|
563 | return MakeConstant(Math.Pow(constA.Value, 1.0 / Math.Round(constB.Value)));
|
---|
564 | } else if (IsConstant(b)) {
|
---|
565 | var constB = b as ConstantTreeNode;
|
---|
566 | var constBValue = Math.Round(constB.Value);
|
---|
567 | if (constBValue.IsAlmost(1.0)) {
|
---|
568 | return a;
|
---|
569 | } else if (constBValue.IsAlmost(0.0)) {
|
---|
570 | return MakeConstant(1.0);
|
---|
571 | } else if (constBValue.IsAlmost(-1.0)) {
|
---|
572 | return MakeFraction(MakeConstant(1.0), a);
|
---|
573 | } else if (constBValue < 0) {
|
---|
574 | var rootNode = rootSymbol.CreateTreeNode();
|
---|
575 | rootNode.AddSubtree(a);
|
---|
576 | rootNode.AddSubtree(MakeConstant(-1.0 * constBValue));
|
---|
577 | return MakeFraction(MakeConstant(1.0), rootNode);
|
---|
578 | } else {
|
---|
579 | var rootNode = rootSymbol.CreateTreeNode();
|
---|
580 | rootNode.AddSubtree(a);
|
---|
581 | rootNode.AddSubtree(MakeConstant(constBValue));
|
---|
582 | return rootNode;
|
---|
583 | }
|
---|
584 | } else {
|
---|
585 | var rootNode = rootSymbol.CreateTreeNode();
|
---|
586 | rootNode.AddSubtree(a);
|
---|
587 | rootNode.AddSubtree(b);
|
---|
588 | return rootNode;
|
---|
589 | }
|
---|
590 | }
|
---|
591 | private ISymbolicExpressionTreeNode MakePower(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
|
---|
592 | if (IsConstant(a) && IsConstant(b)) {
|
---|
593 | var constA = a as ConstantTreeNode;
|
---|
594 | var constB = b as ConstantTreeNode;
|
---|
595 | return MakeConstant(Math.Pow(constA.Value, Math.Round(constB.Value)));
|
---|
596 | } else if (IsConstant(b)) {
|
---|
597 | var constB = b as ConstantTreeNode;
|
---|
598 | double exponent = Math.Round(constB.Value);
|
---|
599 | if (exponent.IsAlmost(0.0)) {
|
---|
600 | return MakeConstant(1.0);
|
---|
601 | } else if (exponent.IsAlmost(1.0)) {
|
---|
602 | return a;
|
---|
603 | } else if (exponent.IsAlmost(-1.0)) {
|
---|
604 | return MakeFraction(MakeConstant(1.0), a);
|
---|
605 | } else if (exponent < 0) {
|
---|
606 | var powNode = powSymbol.CreateTreeNode();
|
---|
607 | powNode.AddSubtree(a);
|
---|
608 | powNode.AddSubtree(MakeConstant(-1.0 * exponent));
|
---|
609 | return MakeFraction(MakeConstant(1.0), powNode);
|
---|
610 | } else {
|
---|
611 | var powNode = powSymbol.CreateTreeNode();
|
---|
612 | powNode.AddSubtree(a);
|
---|
613 | powNode.AddSubtree(MakeConstant(exponent));
|
---|
614 | return powNode;
|
---|
615 | }
|
---|
616 | } else {
|
---|
617 | var powNode = powSymbol.CreateTreeNode();
|
---|
618 | powNode.AddSubtree(a);
|
---|
619 | powNode.AddSubtree(b);
|
---|
620 | return powNode;
|
---|
621 | }
|
---|
622 | }
|
---|
623 |
|
---|
624 |
|
---|
625 | // MakeFraction, MakeProduct and MakeSum take two already simplified trees and create a new simplified tree
|
---|
626 | private ISymbolicExpressionTreeNode MakeFraction(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
|
---|
627 | if (IsConstant(a) && IsConstant(b)) {
|
---|
628 | // fold constants
|
---|
629 | return MakeConstant(((ConstantTreeNode)a).Value / ((ConstantTreeNode)b).Value);
|
---|
630 | } if (IsConstant(a) && !((ConstantTreeNode)a).Value.IsAlmost(1.0)) {
|
---|
631 | return MakeFraction(MakeConstant(1.0), MakeProduct(b, Invert(a)));
|
---|
632 | } else if (IsVariable(a) && IsConstant(b)) {
|
---|
633 | // merge constant values into variable weights
|
---|
634 | var constB = ((ConstantTreeNode)b).Value;
|
---|
635 | ((VariableTreeNode)a).Weight /= constB;
|
---|
636 | return a;
|
---|
637 | } else if (IsVariable(a) && IsVariable(b) && AreSameVariable(a, b)) {
|
---|
638 | // cancel variables
|
---|
639 | var aVar = a as VariableTreeNode;
|
---|
640 | var bVar = b as VariableTreeNode;
|
---|
641 | return MakeConstant(aVar.Weight / bVar.Weight);
|
---|
642 | } else if (IsAddition(a) && IsConstant(b)) {
|
---|
643 | return a.Subtrees
|
---|
644 | .Select(x => GetSimplifiedTree(x))
|
---|
645 | .Select(x => MakeFraction(x, b))
|
---|
646 | .Aggregate((c, d) => MakeSum(c, d));
|
---|
647 | } else if (IsMultiplication(a) && IsConstant(b)) {
|
---|
648 | return MakeProduct(a, Invert(b));
|
---|
649 | } else if (IsDivision(a) && IsConstant(b)) {
|
---|
650 | // (a1 / a2) / c => (a1 / (a2 * c))
|
---|
651 | Trace.Assert(a.Subtrees.Count() == 2);
|
---|
652 | return MakeFraction(a.GetSubtree(0), MakeProduct(a.GetSubtree(1), b));
|
---|
653 | } else if (IsDivision(a) && IsDivision(b)) {
|
---|
654 | // (a1 / a2) / (b1 / b2) =>
|
---|
655 | Trace.Assert(a.Subtrees.Count() == 2);
|
---|
656 | Trace.Assert(b.Subtrees.Count() == 2);
|
---|
657 | return MakeFraction(MakeProduct(a.GetSubtree(0), b.GetSubtree(1)), MakeProduct(a.GetSubtree(1), b.GetSubtree(0)));
|
---|
658 | } else if (IsDivision(a)) {
|
---|
659 | // (a1 / a2) / b => (a1 / (a2 * b))
|
---|
660 | Trace.Assert(a.Subtrees.Count() == 2);
|
---|
661 | return MakeFraction(a.GetSubtree(0), MakeProduct(a.GetSubtree(1), b));
|
---|
662 | } else if (IsDivision(b)) {
|
---|
663 | // a / (b1 / b2) => (a * b2) / b1
|
---|
664 | Trace.Assert(b.Subtrees.Count() == 2);
|
---|
665 | return MakeFraction(MakeProduct(a, b.GetSubtree(1)), b.GetSubtree(0));
|
---|
666 | } else {
|
---|
667 | var div = divSymbol.CreateTreeNode();
|
---|
668 | div.AddSubtree(a);
|
---|
669 | div.AddSubtree(b);
|
---|
670 | return div;
|
---|
671 | }
|
---|
672 | }
|
---|
673 |
|
---|
674 | private ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
|
---|
675 | if (IsConstant(a) && IsConstant(b)) {
|
---|
676 | // fold constants
|
---|
677 | ((ConstantTreeNode)a).Value += ((ConstantTreeNode)b).Value;
|
---|
678 | return a;
|
---|
679 | } else if (IsConstant(a)) {
|
---|
680 | // c + x => x + c
|
---|
681 | // b is not constant => make sure constant is on the right
|
---|
682 | return MakeSum(b, a);
|
---|
683 | } else if (IsConstant(b) && ((ConstantTreeNode)b).Value.IsAlmost(0.0)) {
|
---|
684 | // x + 0 => x
|
---|
685 | return a;
|
---|
686 | } else if (IsAddition(a) && IsAddition(b)) {
|
---|
687 | // merge additions
|
---|
688 | var add = addSymbol.CreateTreeNode();
|
---|
689 | // add all sub trees except for the last
|
---|
690 | for (int i = 0; i < a.Subtrees.Count() - 1; i++) add.AddSubtree(a.GetSubtree(i));
|
---|
691 | for (int i = 0; i < b.Subtrees.Count() - 1; i++) add.AddSubtree(b.GetSubtree(i));
|
---|
692 | if (IsConstant(a.Subtrees.Last()) && IsConstant(b.Subtrees.Last())) {
|
---|
693 | add.AddSubtree(MakeSum(a.Subtrees.Last(), b.Subtrees.Last()));
|
---|
694 | } else if (IsConstant(a.Subtrees.Last())) {
|
---|
695 | add.AddSubtree(b.Subtrees.Last());
|
---|
696 | add.AddSubtree(a.Subtrees.Last());
|
---|
697 | } else {
|
---|
698 | add.AddSubtree(a.Subtrees.Last());
|
---|
699 | add.AddSubtree(b.Subtrees.Last());
|
---|
700 | }
|
---|
701 | MergeVariablesInSum(add);
|
---|
702 | if (add.Subtrees.Count() == 1) {
|
---|
703 | return add.GetSubtree(0);
|
---|
704 | } else {
|
---|
705 | return add;
|
---|
706 | }
|
---|
707 | } else if (IsAddition(b)) {
|
---|
708 | return MakeSum(b, a);
|
---|
709 | } else if (IsAddition(a) && IsConstant(b)) {
|
---|
710 | // a is an addition and b is a constant => append b to a and make sure the constants are merged
|
---|
711 | var add = addSymbol.CreateTreeNode();
|
---|
712 | // add all sub trees except for the last
|
---|
713 | for (int i = 0; i < a.Subtrees.Count() - 1; i++) add.AddSubtree(a.GetSubtree(i));
|
---|
714 | if (IsConstant(a.Subtrees.Last()))
|
---|
715 | add.AddSubtree(MakeSum(a.Subtrees.Last(), b));
|
---|
716 | else {
|
---|
717 | add.AddSubtree(a.Subtrees.Last());
|
---|
718 | add.AddSubtree(b);
|
---|
719 | }
|
---|
720 | return add;
|
---|
721 | } else if (IsAddition(a)) {
|
---|
722 | // a is already an addition => append b
|
---|
723 | var add = addSymbol.CreateTreeNode();
|
---|
724 | add.AddSubtree(b);
|
---|
725 | foreach (var subtree in a.Subtrees) {
|
---|
726 | add.AddSubtree(subtree);
|
---|
727 | }
|
---|
728 | MergeVariablesInSum(add);
|
---|
729 | if (add.Subtrees.Count() == 1) {
|
---|
730 | return add.GetSubtree(0);
|
---|
731 | } else {
|
---|
732 | return add;
|
---|
733 | }
|
---|
734 | } else {
|
---|
735 | var add = addSymbol.CreateTreeNode();
|
---|
736 | add.AddSubtree(a);
|
---|
737 | add.AddSubtree(b);
|
---|
738 | MergeVariablesInSum(add);
|
---|
739 | if (add.Subtrees.Count() == 1) {
|
---|
740 | return add.GetSubtree(0);
|
---|
741 | } else {
|
---|
742 | return add;
|
---|
743 | }
|
---|
744 | }
|
---|
745 | }
|
---|
746 |
|
---|
747 | // makes sure variable symbols in sums are combined
|
---|
748 | // possible improvement: combine sums of products where the products only reference the same variable
|
---|
749 | private void MergeVariablesInSum(ISymbolicExpressionTreeNode sum) {
|
---|
750 | var subtrees = new List<ISymbolicExpressionTreeNode>(sum.Subtrees);
|
---|
751 | while (sum.Subtrees.Count() > 0) sum.RemoveSubtree(0);
|
---|
752 | var groupedVarNodes = from node in subtrees.OfType<VariableTreeNode>()
|
---|
753 | let lag = (node is LaggedVariableTreeNode) ? ((LaggedVariableTreeNode)node).Lag : 0
|
---|
754 | group node by node.VariableName + lag into g
|
---|
755 | select g;
|
---|
756 | var unchangedSubtrees = subtrees.Where(t => !(t is VariableTreeNode));
|
---|
757 |
|
---|
758 | foreach (var variableNodeGroup in groupedVarNodes) {
|
---|
759 | var weightSum = variableNodeGroup.Select(t => t.Weight).Sum();
|
---|
760 | var representative = variableNodeGroup.First();
|
---|
761 | representative.Weight = weightSum;
|
---|
762 | sum.AddSubtree(representative);
|
---|
763 | }
|
---|
764 | foreach (var unchangedSubtree in unchangedSubtrees)
|
---|
765 | sum.AddSubtree(unchangedSubtree);
|
---|
766 | }
|
---|
767 |
|
---|
768 |
|
---|
769 | private ISymbolicExpressionTreeNode MakeProduct(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
|
---|
770 | if (IsConstant(a) && IsConstant(b)) {
|
---|
771 | // fold constants
|
---|
772 | ((ConstantTreeNode)a).Value *= ((ConstantTreeNode)b).Value;
|
---|
773 | return a;
|
---|
774 | } else if (IsConstant(a)) {
|
---|
775 | // a * $ => $ * a
|
---|
776 | return MakeProduct(b, a);
|
---|
777 | } else if (IsConstant(b) && ((ConstantTreeNode)b).Value.IsAlmost(1.0)) {
|
---|
778 | // $ * 1.0 => $
|
---|
779 | return a;
|
---|
780 | } else if (IsConstant(b) && IsVariable(a)) {
|
---|
781 | // multiply constants into variables weights
|
---|
782 | ((VariableTreeNode)a).Weight *= ((ConstantTreeNode)b).Value;
|
---|
783 | return a;
|
---|
784 | } else if (IsConstant(b) && IsAddition(a)) {
|
---|
785 | // multiply constants into additions
|
---|
786 | return a.Subtrees.Select(x => MakeProduct(x, b)).Aggregate((c, d) => MakeSum(c, d));
|
---|
787 | } else if (IsDivision(a) && IsDivision(b)) {
|
---|
788 | // (a1 / a2) * (b1 / b2) => (a1 * b1) / (a2 * b2)
|
---|
789 | Trace.Assert(a.Subtrees.Count() == 2);
|
---|
790 | Trace.Assert(b.Subtrees.Count() == 2);
|
---|
791 | return MakeFraction(MakeProduct(a.GetSubtree(0), b.GetSubtree(0)), MakeProduct(a.GetSubtree(1), b.GetSubtree(1)));
|
---|
792 | } else if (IsDivision(a)) {
|
---|
793 | // (a1 / a2) * b => (a1 * b) / a2
|
---|
794 | Trace.Assert(a.Subtrees.Count() == 2);
|
---|
795 | return MakeFraction(MakeProduct(a.GetSubtree(0), b), a.GetSubtree(1));
|
---|
796 | } else if (IsDivision(b)) {
|
---|
797 | // a * (b1 / b2) => (b1 * a) / b2
|
---|
798 | Trace.Assert(b.Subtrees.Count() == 2);
|
---|
799 | return MakeFraction(MakeProduct(b.GetSubtree(0), a), b.GetSubtree(1));
|
---|
800 | } else if (IsMultiplication(a) && IsMultiplication(b)) {
|
---|
801 | // merge multiplications (make sure constants are merged)
|
---|
802 | var mul = mulSymbol.CreateTreeNode();
|
---|
803 | for (int i = 0; i < a.Subtrees.Count(); i++) mul.AddSubtree(a.GetSubtree(i));
|
---|
804 | for (int i = 0; i < b.Subtrees.Count(); i++) mul.AddSubtree(b.GetSubtree(i));
|
---|
805 | MergeVariablesAndConstantsInProduct(mul);
|
---|
806 | return mul;
|
---|
807 | } else if (IsMultiplication(b)) {
|
---|
808 | return MakeProduct(b, a);
|
---|
809 | } else if (IsMultiplication(a)) {
|
---|
810 | // a is already an multiplication => append b
|
---|
811 | a.AddSubtree(b);
|
---|
812 | MergeVariablesAndConstantsInProduct(a);
|
---|
813 | return a;
|
---|
814 | } else {
|
---|
815 | var mul = mulSymbol.CreateTreeNode();
|
---|
816 | mul.AddSubtree(a);
|
---|
817 | mul.AddSubtree(b);
|
---|
818 | MergeVariablesAndConstantsInProduct(mul);
|
---|
819 | return mul;
|
---|
820 | }
|
---|
821 | }
|
---|
822 | #endregion
|
---|
823 |
|
---|
824 |
|
---|
825 | #region helper functions
|
---|
826 |
|
---|
827 | private bool AreSameVariable(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
|
---|
828 | var aLaggedVar = a as LaggedVariableTreeNode;
|
---|
829 | var bLaggedVar = b as LaggedVariableTreeNode;
|
---|
830 | if (aLaggedVar != null && bLaggedVar != null) {
|
---|
831 | return aLaggedVar.VariableName == bLaggedVar.VariableName &&
|
---|
832 | aLaggedVar.Lag == bLaggedVar.Lag;
|
---|
833 | }
|
---|
834 | var aVar = a as VariableTreeNode;
|
---|
835 | var bVar = b as VariableTreeNode;
|
---|
836 | if (aVar != null && bVar != null) {
|
---|
837 | return aVar.VariableName == bVar.VariableName;
|
---|
838 | }
|
---|
839 | return false;
|
---|
840 | }
|
---|
841 |
|
---|
842 | // helper to combine the constant factors in products and to combine variables (powers of 2, 3...)
|
---|
843 | private void MergeVariablesAndConstantsInProduct(ISymbolicExpressionTreeNode prod) {
|
---|
844 | var subtrees = new List<ISymbolicExpressionTreeNode>(prod.Subtrees);
|
---|
845 | while (prod.Subtrees.Count() > 0) prod.RemoveSubtree(0);
|
---|
846 | var groupedVarNodes = from node in subtrees.OfType<VariableTreeNode>()
|
---|
847 | let lag = (node is LaggedVariableTreeNode) ? ((LaggedVariableTreeNode)node).Lag : 0
|
---|
848 | group node by node.VariableName + lag into g
|
---|
849 | orderby g.Count()
|
---|
850 | select g;
|
---|
851 | var constantProduct = (from node in subtrees.OfType<VariableTreeNode>()
|
---|
852 | select node.Weight)
|
---|
853 | .Concat(from node in subtrees.OfType<ConstantTreeNode>()
|
---|
854 | select node.Value)
|
---|
855 | .DefaultIfEmpty(1.0)
|
---|
856 | .Aggregate((c1, c2) => c1 * c2);
|
---|
857 |
|
---|
858 | var unchangedSubtrees = from tree in subtrees
|
---|
859 | where !(tree is VariableTreeNode)
|
---|
860 | where !(tree is ConstantTreeNode)
|
---|
861 | select tree;
|
---|
862 |
|
---|
863 | foreach (var variableNodeGroup in groupedVarNodes) {
|
---|
864 | var representative = variableNodeGroup.First();
|
---|
865 | representative.Weight = 1.0;
|
---|
866 | if (variableNodeGroup.Count() > 1) {
|
---|
867 | var poly = mulSymbol.CreateTreeNode();
|
---|
868 | for (int p = 0; p < variableNodeGroup.Count(); p++) {
|
---|
869 | poly.AddSubtree((ISymbolicExpressionTreeNode)representative.Clone());
|
---|
870 | }
|
---|
871 | prod.AddSubtree(poly);
|
---|
872 | } else {
|
---|
873 | prod.AddSubtree(representative);
|
---|
874 | }
|
---|
875 | }
|
---|
876 |
|
---|
877 | foreach (var unchangedSubtree in unchangedSubtrees)
|
---|
878 | prod.AddSubtree(unchangedSubtree);
|
---|
879 |
|
---|
880 | if (!constantProduct.IsAlmost(1.0)) {
|
---|
881 | prod.AddSubtree(MakeConstant(constantProduct));
|
---|
882 | }
|
---|
883 | }
|
---|
884 |
|
---|
885 |
|
---|
886 | /// <summary>
|
---|
887 | /// x => x * -1
|
---|
888 | /// Doesn't create new trees and manipulates x
|
---|
889 | /// </summary>
|
---|
890 | /// <param name="x"></param>
|
---|
891 | /// <returns>-x</returns>
|
---|
892 | private ISymbolicExpressionTreeNode Negate(ISymbolicExpressionTreeNode x) {
|
---|
893 | if (IsConstant(x)) {
|
---|
894 | ((ConstantTreeNode)x).Value *= -1;
|
---|
895 | } else if (IsVariable(x)) {
|
---|
896 | var variableTree = (VariableTreeNode)x;
|
---|
897 | variableTree.Weight *= -1.0;
|
---|
898 | } else if (IsAddition(x)) {
|
---|
899 | // (x0 + x1 + .. + xn) * -1 => (-x0 + -x1 + .. + -xn)
|
---|
900 | List<ISymbolicExpressionTreeNode> subtrees = new List<ISymbolicExpressionTreeNode>(x.Subtrees);
|
---|
901 | while (x.Subtrees.Count() > 0) x.RemoveSubtree(0);
|
---|
902 | foreach (var subtree in subtrees) {
|
---|
903 | x.AddSubtree(Negate(subtree));
|
---|
904 | }
|
---|
905 | } else if (IsMultiplication(x) || IsDivision(x)) {
|
---|
906 | // x0 * x1 * .. * xn * -1 => x0 * x1 * .. * -xn
|
---|
907 | x.RemoveSubtree(x.Subtrees.Count() - 1);
|
---|
908 | x.AddSubtree(Negate(x.Subtrees.Last())); // last is maybe a constant, prefer to negate the constant
|
---|
909 | } else {
|
---|
910 | // any other function
|
---|
911 | return MakeProduct(x, MakeConstant(-1));
|
---|
912 | }
|
---|
913 | return x;
|
---|
914 | }
|
---|
915 |
|
---|
916 | /// <summary>
|
---|
917 | /// x => 1/x
|
---|
918 | /// Doesn't create new trees and manipulates x
|
---|
919 | /// </summary>
|
---|
920 | /// <param name="x"></param>
|
---|
921 | /// <returns></returns>
|
---|
922 | private ISymbolicExpressionTreeNode Invert(ISymbolicExpressionTreeNode x) {
|
---|
923 | if (IsConstant(x)) {
|
---|
924 | return MakeConstant(1.0 / ((ConstantTreeNode)x).Value);
|
---|
925 | } else if (IsDivision(x)) {
|
---|
926 | Trace.Assert(x.Subtrees.Count() == 2);
|
---|
927 | return MakeFraction(x.GetSubtree(1), x.GetSubtree(0));
|
---|
928 | } else {
|
---|
929 | // any other function
|
---|
930 | return MakeFraction(MakeConstant(1), x);
|
---|
931 | }
|
---|
932 | }
|
---|
933 |
|
---|
934 | private ISymbolicExpressionTreeNode MakeConstant(double value) {
|
---|
935 | ConstantTreeNode constantTreeNode = (ConstantTreeNode)(constSymbol.CreateTreeNode());
|
---|
936 | constantTreeNode.Value = value;
|
---|
937 | return constantTreeNode;
|
---|
938 | }
|
---|
939 |
|
---|
940 | private ISymbolicExpressionTreeNode MakeVariable(double weight, string name) {
|
---|
941 | var tree = (VariableTreeNode)varSymbol.CreateTreeNode();
|
---|
942 | tree.Weight = weight;
|
---|
943 | tree.VariableName = name;
|
---|
944 | return tree;
|
---|
945 | }
|
---|
946 | #endregion
|
---|
947 | }
|
---|
948 | }
|
---|