#region License Information /* HeuristicLab * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Diagnostics; using HeuristicLab.Common; using System.Linq; using System.Collections.Generic; using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; // double.IsAlmost extension namespace HeuristicLab.Problems.DataAnalysis.MultiVariate.Regression.Symbolic.Evaluators { public static class PartialSymbolicDifferential { private static readonly DifferentialVariable differential = new DifferentialVariable(); private static readonly Constant @const = new Constant(); private static readonly ConstantTreeNode const0 = new ConstantTreeNode(@const); private static readonly ConstantTreeNode const1 = new ConstantTreeNode(@const); private static readonly Symbol addition = new Addition(); private static readonly Symbol subtraction = new Subtraction(); private static readonly Symbol multiplication = new Multiplication(); private static readonly Symbol division = new Division(); //private static readonly Symbol power = new Power(); private static readonly Symbol exp = new Exponential(); private static readonly Symbol ln = new Logarithm(); private static readonly Symbol cos = new Cosine(); private static readonly Symbol sin = new Sine(); private static readonly Symbol tan = new Tangent(); static PartialSymbolicDifferential() { const0.Value = 0.0; const1.Value = 1.0; } public static SymbolicExpressionTreeNode Apply(SymbolicExpressionTreeNode expression, string variableName, params string[] interdependentVariable) { Symbol symbol = expression.Symbol; if (symbol is Variable) { var variable = (VariableTreeNode)expression; if (variable.VariableName == variableName) return MakeConstant(variable.Weight); else if (interdependentVariable.Contains(variable.VariableName)) { return MakeProduct(MakeConstant(variable.Weight), MakeDivision(MakeDifferential(1.0, variable.VariableName), MakeDifferential(1.0, variableName))); } else return const0; } else if (symbol is Constant) { return const0; } else if (symbol is Addition) { var sum = addition.CreateTreeNode(); foreach (var subExp in expression.SubTrees) { sum.AddSubTree(Apply(subExp, variableName, interdependentVariable)); } return sum; } else if (symbol is Subtraction) { var diff = subtraction.CreateTreeNode(); foreach (var subExp in expression.SubTrees) { diff.AddSubTree(Apply(subExp, variableName, interdependentVariable)); } return diff; } else if (symbol is Multiplication) { if (expression.SubTrees.Count() == 1) return Apply(expression.SubTrees[0], variableName, interdependentVariable); SymbolicExpressionTreeNode sum = const0; for (int i = 0; i < expression.SubTrees.Count; i++) { SymbolicExpressionTreeNode prod = const1; for (int j = 0; j < expression.SubTrees.Count; j++) { if (j != i) prod = MakeProduct(prod, expression.SubTrees[j]); else prod = MakeProduct(prod, Apply(expression.SubTrees[j], variableName, interdependentVariable)); } sum = MakeSum(sum, prod); } return sum; } else if (symbol is Division) { // u/v (v*du/dx-u*dv/dx)/v^2 if (expression.SubTrees.Count == 1) { SymbolicExpressionTreeNode v = expression.SubTrees[0]; SymbolicExpressionTreeNode dv = Apply(v, variableName, interdependentVariable); return MakeDifference(const1, MakeDivision(dv, MakeSqr(v))); } else if (expression.SubTrees.Count == 2) { SymbolicExpressionTreeNode u = expression.SubTrees[0]; SymbolicExpressionTreeNode v = expression.SubTrees[1]; SymbolicExpressionTreeNode du = Apply(u, variableName, interdependentVariable); SymbolicExpressionTreeNode dv = Apply(v, variableName, interdependentVariable); u = MakeDivision(MakeDifference(MakeProduct(v, du), MakeProduct(u, dv)), MakeSqr(v)); for (int i = 2; i < expression.SubTrees.Count; i++) { v = expression.SubTrees[i]; dv = Apply(v, variableName, interdependentVariable); du = Apply(u, variableName, interdependentVariable); u = MakeDivision(MakeDifference(MakeProduct(v, du), MakeProduct(u, dv)), MakeSqr(v)); } return u; } else throw new NotSupportedException("Derivative of division with arity " + expression.SubTrees.Count + " is not supported."); } else if (symbol is Exponential) { // e^u e^u*du/dx return MakeProduct(MakeExp(expression.SubTrees[0]), Apply(expression.SubTrees[0], variableName, interdependentVariable)); } else if (symbol is Logarithm) { return MakeProduct(MakeDivision(const1, expression.SubTrees[0]), Apply(expression.SubTrees[0], variableName, interdependentVariable)); } //case EvaluatorSymbolTable.POWER: { // var a = expression.SubTrees[0]; // var b = expression.SubTrees[1]; // return Apply(MakeExp(MakeProduct(b, MakeLn(a))), variableName, interdependentVariable); // } //case EvaluatorSymbolTable.SIGNUM: { // return const0; // } else if (symbol is Sine) { return MakeProduct(MakeCos(expression.SubTrees[0]), Apply(expression.SubTrees[0], variableName, interdependentVariable)); } else if (symbol is Cosine) { return MakeProduct(MakeNegSin(expression.SubTrees[0]), Apply(expression.SubTrees[0], variableName, interdependentVariable)); } else if (symbol is Tangent) { return MakeProduct(MakeSum(const1, MakeTanSqr(expression.SubTrees[0])), Apply(expression.SubTrees[0], variableName, interdependentVariable)); } //else if(symbol is Sqrt) { // var powTree = new FunctionTreeBase(power); // powTree.AddSubTree(expression.SubTrees[0]); // powTree.AddSubTree(MakeConstant(0.5)); // return Apply(powTree, variableName, interdependentVariable); // } else if (symbol is ProgramRootSymbol) { var branch = Apply(expression.SubTrees[0], variableName, interdependentVariable); expression.RemoveSubTree(0); expression.InsertSubTree(0, branch); return expression; } else if (symbol is StartSymbol) { var branch = Apply(expression.SubTrees[0], variableName, interdependentVariable); expression.RemoveSubTree(0); expression.InsertSubTree(0, branch); return expression; } else { throw new NotImplementedException(); } } private static SymbolicExpressionTreeNode MakeDifferential(double c, string v) { DifferentialVariableTreeNode diffTree = (DifferentialVariableTreeNode)differential.CreateTreeNode(); diffTree.VariableName = v; diffTree.Weight = c; return diffTree; } private static SymbolicExpressionTreeNode MakeTanSqr(SymbolicExpressionTreeNode expression) { var tanTree1 = tan.CreateTreeNode(); var tanTree2 = tan.CreateTreeNode(); tanTree2.AddSubTree(expression); tanTree1.AddSubTree(tanTree2); return tanTree1; } private static SymbolicExpressionTreeNode MakeNegSin(SymbolicExpressionTreeNode expression) { var sinTree = sin.CreateTreeNode(); sinTree.AddSubTree(expression); return MakeDifference(const0, sinTree); } private static SymbolicExpressionTreeNode MakeCos(SymbolicExpressionTreeNode expression) { var cosTree = cos.CreateTreeNode(); cosTree.AddSubTree(expression); return cosTree; } private static SymbolicExpressionTreeNode MakeLn(SymbolicExpressionTreeNode a) { var lnTree = ln.CreateTreeNode(); lnTree.AddSubTree(a); return lnTree; } private static SymbolicExpressionTreeNode MakeExp(SymbolicExpressionTreeNode expression) { if (IsConstantZero(expression)) return const1; var expTree = exp.CreateTreeNode(); expTree.AddSubTree(expression); return expTree; } private static SymbolicExpressionTreeNode MakeDivision(SymbolicExpressionTreeNode x, SymbolicExpressionTreeNode y) { if (IsConstantZero(x)) return const0; var div = division.CreateTreeNode(); div.AddSubTree(x); div.AddSubTree(y); return div; } private static SymbolicExpressionTreeNode MakeSqr(SymbolicExpressionTreeNode x) { var sqr = multiplication.CreateTreeNode(); sqr.AddSubTree(x); sqr.AddSubTree((SymbolicExpressionTreeNode)x.Clone()); return sqr; } private static SymbolicExpressionTreeNode MakeProduct(SymbolicExpressionTreeNode x, SymbolicExpressionTreeNode y) { if (IsConstantZero(x) || IsConstantZero(y)) { return const0; } else if (IsConstantOne(x)) return y; else if (IsConstantOne(y)) return x; var product = multiplication.CreateTreeNode(); product.AddSubTree(x); product.AddSubTree(y); return product; } private static bool IsConstantOne(SymbolicExpressionTreeNode x) { return x == const1; } private static bool IsConstantZero(SymbolicExpressionTreeNode x) { return x == const0; } private static SymbolicExpressionTreeNode MakeSum(SymbolicExpressionTreeNode a, SymbolicExpressionTreeNode b) { if (IsConstantZero(a) && IsConstantZero(b)) return const0; else if (IsConstantZero(a)) return b; else if (IsConstantZero(b)) return a; var product = addition.CreateTreeNode(); product.AddSubTree(a); product.AddSubTree(b); return product; } private static SymbolicExpressionTreeNode MakeDifference(SymbolicExpressionTreeNode a, SymbolicExpressionTreeNode b) { if (IsConstantZero(a) && IsConstantZero(b)) return const0; else if (IsConstantZero(b)) return a; var diff = subtraction.CreateTreeNode(); diff.AddSubTree(a); diff.AddSubTree(b); return diff; } private static SymbolicExpressionTreeNode MakeConstant(double c) { if (c.IsAlmost(1.0)) return const1; else if (c.IsAlmost(0.0)) return const0; var cExp = new ConstantTreeNode(@const); cExp.Value = c; return cExp; } } }