[16206] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16654] | 3 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[16206] | 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;
|
---|
[16294] | 23 | using System.Collections.Generic;
|
---|
[16206] | 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 26 |
|
---|
| 27 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
| 28 | public static class DerivativeCalculator {
|
---|
| 29 | public static ISymbolicExpressionTree Derive(ISymbolicExpressionTree tree, string variableName) {
|
---|
[16294] | 30 | if (tree.Root.SubtreeCount != 1)
|
---|
| 31 | throw new NotImplementedException("Derive is not implemented for symbolic expressions with automatically defined functions (ADF)");
|
---|
| 32 | if (tree.Root.GetSubtree(0).SubtreeCount != 1)
|
---|
| 33 | throw new NotImplementedException("Derive is not implemented for multi-variate symbolic expressions");
|
---|
[16206] | 34 | var mainBranch = tree.Root.GetSubtree(0).GetSubtree(0);
|
---|
| 35 | var root = new ProgramRootSymbol().CreateTreeNode();
|
---|
| 36 | root.AddSubtree(new StartSymbol().CreateTreeNode());
|
---|
| 37 | var dTree = TreeSimplifier.GetSimplifiedTree(Derive(mainBranch, variableName));
|
---|
[16294] | 38 | //var dTree = Derive(mainBranch, variableName);
|
---|
[16206] | 39 | root.GetSubtree(0).AddSubtree(dTree);
|
---|
| 40 | return new SymbolicExpressionTree(root);
|
---|
| 41 | }
|
---|
| 42 |
|
---|
[16294] | 43 | private static readonly Constant constantSy = new Constant();
|
---|
| 44 | private static readonly Addition addSy = new Addition();
|
---|
| 45 | private static readonly Subtraction subSy = new Subtraction();
|
---|
| 46 | private static readonly Multiplication mulSy = new Multiplication();
|
---|
| 47 | private static readonly Division divSy = new Division();
|
---|
| 48 | private static readonly Cosine cosSy = new Cosine();
|
---|
| 49 | private static readonly Square sqrSy = new Square();
|
---|
[16654] | 50 | private static readonly Absolute absSy = new Absolute();
|
---|
| 51 | private static readonly SquareRoot sqrtSy = new SquareRoot();
|
---|
[16206] | 52 |
|
---|
| 53 | public static ISymbolicExpressionTreeNode Derive(ISymbolicExpressionTreeNode branch, string variableName) {
|
---|
| 54 | if (branch.Symbol is Constant) {
|
---|
| 55 | return CreateConstant(0.0);
|
---|
| 56 | }
|
---|
| 57 | if (branch.Symbol is Variable) {
|
---|
| 58 | var varNode = branch as VariableTreeNode;
|
---|
| 59 | if (varNode.VariableName == variableName) {
|
---|
| 60 | return CreateConstant(varNode.Weight);
|
---|
| 61 | } else {
|
---|
| 62 | return CreateConstant(0.0);
|
---|
| 63 | }
|
---|
| 64 | }
|
---|
| 65 | if (branch.Symbol is Addition) {
|
---|
| 66 | var sum = addSy.CreateTreeNode();
|
---|
| 67 | foreach (var subTree in branch.Subtrees) {
|
---|
| 68 | sum.AddSubtree(Derive(subTree, variableName));
|
---|
| 69 | }
|
---|
| 70 | return sum;
|
---|
| 71 | }
|
---|
| 72 | if (branch.Symbol is Subtraction) {
|
---|
| 73 | var sum = subSy.CreateTreeNode();
|
---|
| 74 | foreach (var subTree in branch.Subtrees) {
|
---|
| 75 | sum.AddSubtree(Derive(subTree, variableName));
|
---|
| 76 | }
|
---|
| 77 | return sum;
|
---|
| 78 | }
|
---|
| 79 | if (branch.Symbol is Multiplication) {
|
---|
| 80 | // (f * g)' = f'*g + f*g'
|
---|
| 81 | // for multiple factors: (f * g * h)' = ((f*g) * h)' = (f*g)' * h + (f*g) * h'
|
---|
| 82 |
|
---|
| 83 | if (branch.SubtreeCount >= 2) {
|
---|
| 84 | var f = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
|
---|
| 85 | var g = (ISymbolicExpressionTreeNode)branch.GetSubtree(1).Clone();
|
---|
| 86 | var fprime = Derive(f, variableName);
|
---|
| 87 | var gprime = Derive(g, variableName);
|
---|
| 88 | var fgPrime = Sum(Product(f, gprime), Product(fprime, g));
|
---|
| 89 | for (int i = 2; i < branch.SubtreeCount; i++) {
|
---|
| 90 | var fg = Product((ISymbolicExpressionTreeNode)f.Clone(), (ISymbolicExpressionTreeNode)g.Clone());
|
---|
| 91 | var h = (ISymbolicExpressionTreeNode)branch.GetSubtree(i).Clone();
|
---|
| 92 | var hPrime = Derive(h, variableName);
|
---|
| 93 | fgPrime = Sum(Product(fgPrime, h), Product(fg, hPrime));
|
---|
| 94 | }
|
---|
| 95 | return fgPrime;
|
---|
[16294] | 96 | } else
|
---|
| 97 | // multiplication with only one argument has no effect -> derive the argument
|
---|
| 98 | return Derive(branch.GetSubtree(0), variableName);
|
---|
[16206] | 99 | }
|
---|
| 100 | if (branch.Symbol is Division) {
|
---|
| 101 | // (f/g)' = (f'g - g'f) / g²
|
---|
| 102 | if (branch.SubtreeCount == 1) {
|
---|
| 103 | var g = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
|
---|
| 104 | var gPrime = Product(CreateConstant(-1.0), Derive(g, variableName));
|
---|
| 105 | var sqrNode = new Square().CreateTreeNode();
|
---|
| 106 | sqrNode.AddSubtree(g);
|
---|
| 107 | return Div(gPrime, sqrNode);
|
---|
[16294] | 108 | } else {
|
---|
| 109 | // for two subtrees:
|
---|
| 110 | // (f/g)' = (f'g - fg')/g²
|
---|
| 111 |
|
---|
| 112 | // if there are more than 2 subtrees
|
---|
| 113 | // div(x,y,z) is interpretered as (x/y)/z
|
---|
| 114 | // which is the same as x / (y*z)
|
---|
| 115 |
|
---|
| 116 | // --> make a product of all but the first subtree and differentiate as for the 2-argument case above
|
---|
[16206] | 117 | var f = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
|
---|
[16294] | 118 | var g = Product(branch.Subtrees.Skip(1).Select(n => (ISymbolicExpressionTreeNode)n.Clone()));
|
---|
[16206] | 119 | var fprime = Derive(f, variableName);
|
---|
| 120 | var gprime = Derive(g, variableName);
|
---|
[16294] | 121 | var gSqr = Square(g);
|
---|
| 122 | return Div(Subtract(Product(fprime, g), Product(f, gprime)), gSqr);
|
---|
| 123 | }
|
---|
[16206] | 124 | }
|
---|
| 125 | if (branch.Symbol is Logarithm) {
|
---|
| 126 | var f = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
|
---|
| 127 | return Product(Div(CreateConstant(1.0), f), Derive(f, variableName));
|
---|
| 128 | }
|
---|
| 129 | if (branch.Symbol is Exponential) {
|
---|
| 130 | var f = (ISymbolicExpressionTreeNode)branch.Clone();
|
---|
| 131 | return Product(f, Derive(branch.GetSubtree(0), variableName));
|
---|
| 132 | }
|
---|
[16294] | 133 | if (branch.Symbol is Square) {
|
---|
[16206] | 134 | var f = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
|
---|
| 135 | return Product(Product(CreateConstant(2.0), f), Derive(f, variableName));
|
---|
[16294] | 136 | }
|
---|
| 137 | if (branch.Symbol is SquareRoot) {
|
---|
[16206] | 138 | var f = (ISymbolicExpressionTreeNode)branch.Clone();
|
---|
| 139 | var u = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
|
---|
[16213] | 140 | return Product(Div(CreateConstant(1.0), Product(CreateConstant(2.0), f)), Derive(u, variableName));
|
---|
[16206] | 141 | }
|
---|
[16654] | 142 | if (branch.Symbol is CubeRoot) {
|
---|
| 143 | var f = (ISymbolicExpressionTreeNode)branch.Clone();
|
---|
| 144 | var u = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
|
---|
| 145 | return Product(Div(CreateConstant(1.0), Product(CreateConstant(3.0), Square(f))), Derive(u, variableName));
|
---|
| 146 | }
|
---|
| 147 | if (branch.Symbol is Cube) {
|
---|
| 148 | var f = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
|
---|
| 149 | return Product(Product(CreateConstant(3.0), Square(f)), Derive(f, variableName));
|
---|
| 150 | }
|
---|
| 151 | if (branch.Symbol is Absolute) {
|
---|
| 152 | var f = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
|
---|
| 153 | var absf = Abs((ISymbolicExpressionTreeNode)f.Clone());
|
---|
| 154 | return Product(Div(f, absf), Derive(f, variableName));
|
---|
| 155 | }
|
---|
| 156 | if (branch.Symbol is AnalyticQuotient) {
|
---|
| 157 | // aq(a(x), b(x)) = a(x) / sqrt(b(x)²+1)
|
---|
| 158 | var a = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
|
---|
| 159 | var b = (ISymbolicExpressionTreeNode)branch.GetSubtree(1).Clone();
|
---|
| 160 |
|
---|
| 161 | var definition = Div(a, SquareRoot(Sum(Square(b), CreateConstant(1.0))));
|
---|
| 162 | return Derive(definition, variableName);
|
---|
| 163 | }
|
---|
[16206] | 164 | if (branch.Symbol is Sine) {
|
---|
| 165 | var u = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
|
---|
| 166 | var cos = (new Cosine()).CreateTreeNode();
|
---|
| 167 | cos.AddSubtree(u);
|
---|
| 168 | return Product(cos, Derive(u, variableName));
|
---|
| 169 | }
|
---|
| 170 | if (branch.Symbol is Cosine) {
|
---|
| 171 | var u = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
|
---|
| 172 | var sin = (new Sine()).CreateTreeNode();
|
---|
| 173 | sin.AddSubtree(u);
|
---|
| 174 | return Product(CreateConstant(-1.0), Product(sin, Derive(u, variableName)));
|
---|
| 175 | }
|
---|
[16294] | 176 | if (branch.Symbol is Tangent) {
|
---|
| 177 | // tan(x)' = 1 / cos²(x)
|
---|
| 178 | var fxp = Derive(branch.GetSubtree(0), variableName);
|
---|
| 179 | var u = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
|
---|
| 180 | return Div(fxp, Square(Cosine(u)));
|
---|
| 181 | }
|
---|
[16216] | 182 | throw new NotSupportedException(string.Format("Symbol {0} is not supported.", branch.Symbol));
|
---|
[16206] | 183 | }
|
---|
| 184 |
|
---|
| 185 |
|
---|
| 186 | private static ISymbolicExpressionTreeNode Product(ISymbolicExpressionTreeNode f, ISymbolicExpressionTreeNode g) {
|
---|
| 187 | var product = mulSy.CreateTreeNode();
|
---|
| 188 | product.AddSubtree(f);
|
---|
| 189 | product.AddSubtree(g);
|
---|
| 190 | return product;
|
---|
| 191 | }
|
---|
[16294] | 192 | private static ISymbolicExpressionTreeNode Product(IEnumerable<ISymbolicExpressionTreeNode> fs) {
|
---|
| 193 | var product = mulSy.CreateTreeNode();
|
---|
| 194 | foreach (var f in fs) product.AddSubtree(f);
|
---|
| 195 | return product;
|
---|
| 196 | }
|
---|
[16206] | 197 | private static ISymbolicExpressionTreeNode Div(ISymbolicExpressionTreeNode f, ISymbolicExpressionTreeNode g) {
|
---|
| 198 | var div = divSy.CreateTreeNode();
|
---|
| 199 | div.AddSubtree(f);
|
---|
| 200 | div.AddSubtree(g);
|
---|
| 201 | return div;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | private static ISymbolicExpressionTreeNode Sum(ISymbolicExpressionTreeNode f, ISymbolicExpressionTreeNode g) {
|
---|
| 205 | var sum = addSy.CreateTreeNode();
|
---|
| 206 | sum.AddSubtree(f);
|
---|
| 207 | sum.AddSubtree(g);
|
---|
| 208 | return sum;
|
---|
| 209 | }
|
---|
| 210 | private static ISymbolicExpressionTreeNode Subtract(ISymbolicExpressionTreeNode f, ISymbolicExpressionTreeNode g) {
|
---|
| 211 | var sum = subSy.CreateTreeNode();
|
---|
| 212 | sum.AddSubtree(f);
|
---|
| 213 | sum.AddSubtree(g);
|
---|
| 214 | return sum;
|
---|
| 215 | }
|
---|
[16294] | 216 | private static ISymbolicExpressionTreeNode Cosine(ISymbolicExpressionTreeNode f) {
|
---|
| 217 | var cos = cosSy.CreateTreeNode();
|
---|
| 218 | cos.AddSubtree(f);
|
---|
| 219 | return cos;
|
---|
| 220 | }
|
---|
[16654] | 221 | private static ISymbolicExpressionTreeNode Abs(ISymbolicExpressionTreeNode f) {
|
---|
| 222 | var abs = absSy.CreateTreeNode();
|
---|
| 223 | abs.AddSubtree(f);
|
---|
| 224 | return abs;
|
---|
| 225 | }
|
---|
[16294] | 226 | private static ISymbolicExpressionTreeNode Square(ISymbolicExpressionTreeNode f) {
|
---|
| 227 | var sqr = sqrSy.CreateTreeNode();
|
---|
| 228 | sqr.AddSubtree(f);
|
---|
| 229 | return sqr;
|
---|
| 230 | }
|
---|
[16654] | 231 | private static ISymbolicExpressionTreeNode SquareRoot(ISymbolicExpressionTreeNode f) {
|
---|
| 232 | var sqrt = sqrtSy.CreateTreeNode();
|
---|
| 233 | sqrt.AddSubtree(f);
|
---|
| 234 | return sqrt;
|
---|
| 235 | }
|
---|
[16294] | 236 |
|
---|
[16206] | 237 | private static ISymbolicExpressionTreeNode CreateConstant(double v) {
|
---|
| 238 | var constNode = (ConstantTreeNode)constantSy.CreateTreeNode();
|
---|
| 239 | constNode.Value = v;
|
---|
| 240 | return constNode;
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | public static bool IsCompatible(ISymbolicExpressionTree tree) {
|
---|
| 244 | var containsUnknownSymbol = (
|
---|
| 245 | from n in tree.Root.GetSubtree(0).IterateNodesPrefix()
|
---|
| 246 | where
|
---|
| 247 | !(n.Symbol is Variable) &&
|
---|
| 248 | !(n.Symbol is Constant) &&
|
---|
| 249 | !(n.Symbol is Addition) &&
|
---|
| 250 | !(n.Symbol is Subtraction) &&
|
---|
| 251 | !(n.Symbol is Multiplication) &&
|
---|
| 252 | !(n.Symbol is Division) &&
|
---|
| 253 | !(n.Symbol is Logarithm) &&
|
---|
| 254 | !(n.Symbol is Exponential) &&
|
---|
| 255 | !(n.Symbol is Square) &&
|
---|
| 256 | !(n.Symbol is SquareRoot) &&
|
---|
[16654] | 257 | !(n.Symbol is Cube) &&
|
---|
| 258 | !(n.Symbol is CubeRoot) &&
|
---|
| 259 | !(n.Symbol is Absolute) &&
|
---|
| 260 | !(n.Symbol is AnalyticQuotient) &&
|
---|
[16206] | 261 | !(n.Symbol is Sine) &&
|
---|
| 262 | !(n.Symbol is Cosine) &&
|
---|
[16294] | 263 | !(n.Symbol is Tangent) &&
|
---|
[16206] | 264 | !(n.Symbol is StartSymbol)
|
---|
| 265 | select n).Any();
|
---|
| 266 | return !containsUnknownSymbol;
|
---|
| 267 | }
|
---|
| 268 | }
|
---|
| 269 | }
|
---|