1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 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 |
|
---|
23 | using System;
|
---|
24 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
25 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
26 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Tests {
|
---|
27 |
|
---|
28 |
|
---|
29 | [TestClass]
|
---|
30 | public class DerivativeTest {
|
---|
31 | [TestMethod]
|
---|
32 | [TestCategory("Problems.DataAnalysis.Symbolic")]
|
---|
33 | [TestProperty("Time", "short")]
|
---|
34 | public void DeriveExpressions() {
|
---|
35 | var formatter = new InfixExpressionFormatter();
|
---|
36 | var parser = new InfixExpressionParser();
|
---|
37 | Assert.AreEqual("0", Derive("3", "x"));
|
---|
38 | Assert.AreEqual("1", Derive("x", "x"));
|
---|
39 | Assert.AreEqual("10", Derive("10*x", "x"));
|
---|
40 | Assert.AreEqual("10", Derive("x*10", "x"));
|
---|
41 | Assert.AreEqual("(2*'x')", Derive("x*x", "x"));
|
---|
42 | Assert.AreEqual("((('x' * 'x') * 2) + ('x' * 'x'))", Derive("x*x*x", "x")); // simplifier does not merge (x*x)*2 + x*x to 3*x*x
|
---|
43 | Assert.AreEqual("0", Derive("10*x", "y"));
|
---|
44 | Assert.AreEqual("20", Derive("10*x+20*y", "y"));
|
---|
45 | Assert.AreEqual("6", Derive("2*3*x", "x"));
|
---|
46 | Assert.AreEqual("(10*'y')", Derive("10*x*y+20*y", "x"));
|
---|
47 | Assert.AreEqual("(1 / (SQR('x') * (-1)))", Derive("1/x", "x"));
|
---|
48 | Assert.AreEqual("('y' / (SQR('x') * (-1)))", Derive("y/x", "x"));
|
---|
49 | Assert.AreEqual("((((-2*'x') + (-1)) * ('a' + 'b')) / SQR(('x' + ('x' * 'x'))))",
|
---|
50 | Derive("(a+b)/(x+x*x)", "x"));
|
---|
51 | Assert.AreEqual("((((-2*'x') + (-1)) * ('a' + 'b')) / SQR(('x' + SQR('x'))))", Derive("(a+b)/(x+SQR(x))", "x"));
|
---|
52 | Assert.AreEqual("EXP('x')", Derive("exp(x)", "x"));
|
---|
53 | Assert.AreEqual("(EXP((3*'x')) * 3)", Derive("exp(3*x)", "x"));
|
---|
54 | Assert.AreEqual("(1 / 'x')", Derive("log(x)", "x"));
|
---|
55 | Assert.AreEqual("(1 / 'x')", Derive("log(3*x)", "x")); // 3 * 1/(3*x)
|
---|
56 | Assert.AreEqual("(1 / ('x' + (0.333333333333333*'y')))", Derive("log(3*x+y)", "x")); // simplifier does not try to keep fractions
|
---|
57 | Assert.AreEqual("(1 / (SQRT(((3*'x') + 'y')) * 0.666666666666667))", Derive("sqrt(3*x+y)", "x")); // 3 / (2 * sqrt(3*x+y)) = 1 / ((2/3) * sqrt(3*x+y))
|
---|
58 | Assert.AreEqual("(COS((3*'x')) * 3)", Derive("sin(3*x)", "x"));
|
---|
59 | Assert.AreEqual("(SIN((3*'x')) * (-3))", Derive("cos(3*x)", "x"));
|
---|
60 | Assert.AreEqual("(1 / (SQR(COS((3*'x'))) * 0.333333333333333))", Derive("tan(3*x)", "x")); // diff(tan(f(x)), x) = 1.0 / cos²(f(x)), simplifier puts constant factor into the denominator
|
---|
61 |
|
---|
62 | Assert.AreEqual("0", Derive("(a+b)/(x+SQR(x))", "y")); // df(a,b,x) / dy = 0
|
---|
63 |
|
---|
64 | {
|
---|
65 | // special case: Inv(x) using only one argument to the division symbol
|
---|
66 | // f(x) = 1/x
|
---|
67 | var root = new ProgramRootSymbol().CreateTreeNode();
|
---|
68 | var start = new StartSymbol().CreateTreeNode();
|
---|
69 | var div = new Division().CreateTreeNode();
|
---|
70 | var varNode = (VariableTreeNode)(new Variable().CreateTreeNode());
|
---|
71 | varNode.Weight = 1.0;
|
---|
72 | varNode.VariableName = "x";
|
---|
73 | div.AddSubtree(varNode);
|
---|
74 | start.AddSubtree(div);
|
---|
75 | root.AddSubtree(start);
|
---|
76 | var t = new SymbolicExpressionTree(root);
|
---|
77 | Assert.AreEqual("(1 / (SQR('x') * (-1)))",
|
---|
78 | formatter.Format(DerivativeCalculator.Derive(t, "x")));
|
---|
79 | }
|
---|
80 |
|
---|
81 | {
|
---|
82 | // special case: multiplication with only one argument
|
---|
83 | var root = new ProgramRootSymbol().CreateTreeNode();
|
---|
84 | var start = new StartSymbol().CreateTreeNode();
|
---|
85 | var mul = new Multiplication().CreateTreeNode();
|
---|
86 | var varNode = (VariableTreeNode)(new Variable().CreateTreeNode());
|
---|
87 | varNode.Weight = 3.0;
|
---|
88 | varNode.VariableName = "x";
|
---|
89 | mul.AddSubtree(varNode);
|
---|
90 | start.AddSubtree(mul);
|
---|
91 | root.AddSubtree(start);
|
---|
92 | var t = new SymbolicExpressionTree(root);
|
---|
93 | Assert.AreEqual("3",
|
---|
94 | formatter.Format(DerivativeCalculator.Derive(t, "x")));
|
---|
95 | }
|
---|
96 |
|
---|
97 | {
|
---|
98 | // division with multiple arguments
|
---|
99 | // div(x, y, z) is interpreted as (x / y) / z
|
---|
100 | var root = new ProgramRootSymbol().CreateTreeNode();
|
---|
101 | var start = new StartSymbol().CreateTreeNode();
|
---|
102 | var div = new Division().CreateTreeNode();
|
---|
103 | var varNode1 = (VariableTreeNode)(new Variable().CreateTreeNode());
|
---|
104 | varNode1.Weight = 3.0;
|
---|
105 | varNode1.VariableName = "x";
|
---|
106 | var varNode2 = (VariableTreeNode)(new Variable().CreateTreeNode());
|
---|
107 | varNode2.Weight = 4.0;
|
---|
108 | varNode2.VariableName = "y";
|
---|
109 | var varNode3 = (VariableTreeNode)(new Variable().CreateTreeNode());
|
---|
110 | varNode3.Weight = 5.0;
|
---|
111 | varNode3.VariableName = "z";
|
---|
112 | div.AddSubtree(varNode1); div.AddSubtree(varNode2); div.AddSubtree(varNode3);
|
---|
113 | start.AddSubtree(div);
|
---|
114 | root.AddSubtree(start);
|
---|
115 | var t = new SymbolicExpressionTree(root);
|
---|
116 |
|
---|
117 | Assert.AreEqual("(('y' * 'z' * 60) / (SQR('y') * SQR('z') * 400))", // actually 3 / (4y 5z) but simplifier is not smart enough to cancel numerator and denominator
|
---|
118 | // 60 y z / y² z² 20² == 6 / y z 40 == 3 / y z 20
|
---|
119 | formatter.Format(DerivativeCalculator.Derive(t, "x")));
|
---|
120 | Assert.AreEqual("(('x' * 'z' * (-60)) / (SQR('y') * SQR('z') * 400))", // actually 3x * -(4 5 z) / (4y 5z)² = -3x / (20 y² z)
|
---|
121 | // -3 4 5 x z / 4² y² 5² z² = -60 x z / 20² z² y² == -60 x z / y² z² 20²
|
---|
122 | formatter.Format(DerivativeCalculator.Derive(t, "y")));
|
---|
123 | Assert.AreEqual("(('x' * 'y' * (-60)) / (SQR('y') * SQR('z') * 400))",
|
---|
124 | formatter.Format(DerivativeCalculator.Derive(t, "z")));
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | private string Derive(string expr, string variable) {
|
---|
129 | var parser = new InfixExpressionParser();
|
---|
130 | var formatter = new InfixExpressionFormatter();
|
---|
131 |
|
---|
132 | var t = parser.Parse(expr);
|
---|
133 | var tPrime = DerivativeCalculator.Derive(t, variable);
|
---|
134 |
|
---|
135 | return formatter.Format(tPrime);
|
---|
136 | }
|
---|
137 | }
|
---|
138 | }
|
---|