Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/TreeToAutoDiffTermConverter.cs @ 16796

Last change on this file since 16796 was 16676, checked in by gkronber, 6 years ago

#2974: merged r16478:16672 from trunk:HeuristicLab.Problems.DataAnalysis.Symbolic to branch:HeuristicLab.Problems.DataAnalysis.Symbolic

File size: 13.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Runtime.Serialization;
26using AutoDiff;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28
29namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
30  public class TreeToAutoDiffTermConverter {
31    public delegate double ParametricFunction(double[] vars, double[] @params);
32
33    public delegate Tuple<double[], double> ParametricFunctionGradient(double[] vars, double[] @params);
34
35    #region helper class
36    public class DataForVariable {
37      public readonly string variableName;
38      public readonly string variableValue; // for factor vars
39      public readonly int lag;
40
41      public DataForVariable(string varName, string varValue, int lag) {
42        this.variableName = varName;
43        this.variableValue = varValue;
44        this.lag = lag;
45      }
46
47      public override bool Equals(object obj) {
48        var other = obj as DataForVariable;
49        if (other == null) return false;
50        return other.variableName.Equals(this.variableName) &&
51               other.variableValue.Equals(this.variableValue) &&
52               other.lag == this.lag;
53      }
54
55      public override int GetHashCode() {
56        return variableName.GetHashCode() ^ variableValue.GetHashCode() ^ lag;
57      }
58    }
59    #endregion
60
61    #region derivations of functions
62    // create function factory for arctangent
63    private static readonly Func<Term, UnaryFunc> arctan = UnaryFunc.Factory(
64      eval: Math.Atan,
65      diff: x => 1 / (1 + x * x));
66
67    private static readonly Func<Term, UnaryFunc> sin = UnaryFunc.Factory(
68      eval: Math.Sin,
69      diff: Math.Cos);
70
71    private static readonly Func<Term, UnaryFunc> cos = UnaryFunc.Factory(
72      eval: Math.Cos,
73      diff: x => -Math.Sin(x));
74
75    private static readonly Func<Term, UnaryFunc> tan = UnaryFunc.Factory(
76      eval: Math.Tan,
77      diff: x => 1 + Math.Tan(x) * Math.Tan(x));
78    private static readonly Func<Term, UnaryFunc> tanh = UnaryFunc.Factory(
79      eval: Math.Tanh,
80      diff: x => 1 - Math.Tanh(x) * Math.Tanh(x));
81    private static readonly Func<Term, UnaryFunc> erf = UnaryFunc.Factory(
82      eval: alglib.errorfunction,
83      diff: x => 2.0 * Math.Exp(-(x * x)) / Math.Sqrt(Math.PI));
84
85    private static readonly Func<Term, UnaryFunc> norm = UnaryFunc.Factory(
86      eval: alglib.normaldistribution,
87      diff: x => -(Math.Exp(-(x * x)) * Math.Sqrt(Math.Exp(x * x)) * x) / Math.Sqrt(2 * Math.PI));
88
89    private static readonly Func<Term, UnaryFunc> abs = UnaryFunc.Factory(
90      eval: Math.Abs,
91      diff: x => Math.Sign(x)
92      );
93
94    #endregion
95
96    public static bool TryConvertToAutoDiff(ISymbolicExpressionTree tree, bool makeVariableWeightsVariable, bool addLinearScalingTerms,
97      out List<DataForVariable> parameters, out double[] initialConstants,
98      out ParametricFunction func,
99      out ParametricFunctionGradient func_grad) {
100
101      // use a transformator object which holds the state (variable list, parameter list, ...) for recursive transformation of the tree
102      var transformator = new TreeToAutoDiffTermConverter(makeVariableWeightsVariable, addLinearScalingTerms);
103      AutoDiff.Term term;
104      try {
105        term = transformator.ConvertToAutoDiff(tree.Root.GetSubtree(0));
106        var parameterEntries = transformator.parameters.ToArray(); // guarantee same order for keys and values
107        var compiledTerm = term.Compile(transformator.variables.ToArray(),
108          parameterEntries.Select(kvp => kvp.Value).ToArray());
109        parameters = new List<DataForVariable>(parameterEntries.Select(kvp => kvp.Key));
110        initialConstants = transformator.initialConstants.ToArray();
111        func = (vars, @params) => compiledTerm.Evaluate(vars, @params);
112        func_grad = (vars, @params) => compiledTerm.Differentiate(vars, @params);
113        return true;
114      } catch (ConversionException) {
115        func = null;
116        func_grad = null;
117        parameters = null;
118        initialConstants = null;
119      }
120      return false;
121    }
122
123    // state for recursive transformation of trees
124    private readonly List<double> initialConstants;
125    private readonly Dictionary<DataForVariable, AutoDiff.Variable> parameters;
126    private readonly List<AutoDiff.Variable> variables;
127    private readonly bool makeVariableWeightsVariable;
128    private readonly bool addLinearScalingTerms;
129
130    private TreeToAutoDiffTermConverter(bool makeVariableWeightsVariable, bool addLinearScalingTerms) {
131      this.makeVariableWeightsVariable = makeVariableWeightsVariable;
132      this.addLinearScalingTerms = addLinearScalingTerms;
133      this.initialConstants = new List<double>();
134      this.parameters = new Dictionary<DataForVariable, AutoDiff.Variable>();
135      this.variables = new List<AutoDiff.Variable>();
136    }
137
138    private AutoDiff.Term ConvertToAutoDiff(ISymbolicExpressionTreeNode node) {
139      if (node.Symbol is Constant) {
140        initialConstants.Add(((ConstantTreeNode)node).Value);
141        var var = new AutoDiff.Variable();
142        variables.Add(var);
143        return var;
144      }
145      if (node.Symbol is Variable || node.Symbol is BinaryFactorVariable) {
146        var varNode = node as VariableTreeNodeBase;
147        var factorVarNode = node as BinaryFactorVariableTreeNode;
148        // factor variable values are only 0 or 1 and set in x accordingly
149        var varValue = factorVarNode != null ? factorVarNode.VariableValue : string.Empty;
150        var par = FindOrCreateParameter(parameters, varNode.VariableName, varValue);
151
152        if (makeVariableWeightsVariable) {
153          initialConstants.Add(varNode.Weight);
154          var w = new AutoDiff.Variable();
155          variables.Add(w);
156          return AutoDiff.TermBuilder.Product(w, par);
157        } else {
158          return varNode.Weight * par;
159        }
160      }
161      if (node.Symbol is FactorVariable) {
162        var factorVarNode = node as FactorVariableTreeNode;
163        var products = new List<Term>();
164        foreach (var variableValue in factorVarNode.Symbol.GetVariableValues(factorVarNode.VariableName)) {
165          var par = FindOrCreateParameter(parameters, factorVarNode.VariableName, variableValue);
166
167          initialConstants.Add(factorVarNode.GetValue(variableValue));
168          var wVar = new AutoDiff.Variable();
169          variables.Add(wVar);
170
171          products.Add(AutoDiff.TermBuilder.Product(wVar, par));
172        }
173        return AutoDiff.TermBuilder.Sum(products);
174      }
175      if (node.Symbol is LaggedVariable) {
176        var varNode = node as LaggedVariableTreeNode;
177        var par = FindOrCreateParameter(parameters, varNode.VariableName, string.Empty, varNode.Lag);
178
179        if (makeVariableWeightsVariable) {
180          initialConstants.Add(varNode.Weight);
181          var w = new AutoDiff.Variable();
182          variables.Add(w);
183          return AutoDiff.TermBuilder.Product(w, par);
184        } else {
185          return varNode.Weight * par;
186        }
187      }
188      if (node.Symbol is Addition) {
189        List<AutoDiff.Term> terms = new List<Term>();
190        foreach (var subTree in node.Subtrees) {
191          terms.Add(ConvertToAutoDiff(subTree));
192        }
193        return AutoDiff.TermBuilder.Sum(terms);
194      }
195      if (node.Symbol is Subtraction) {
196        List<AutoDiff.Term> terms = new List<Term>();
197        for (int i = 0; i < node.SubtreeCount; i++) {
198          AutoDiff.Term t = ConvertToAutoDiff(node.GetSubtree(i));
199          if (i > 0) t = -t;
200          terms.Add(t);
201        }
202        if (terms.Count == 1) return -terms[0];
203        else return AutoDiff.TermBuilder.Sum(terms);
204      }
205      if (node.Symbol is Multiplication) {
206        List<AutoDiff.Term> terms = new List<Term>();
207        foreach (var subTree in node.Subtrees) {
208          terms.Add(ConvertToAutoDiff(subTree));
209        }
210        if (terms.Count == 1) return terms[0];
211        else return terms.Aggregate((a, b) => new AutoDiff.Product(a, b));
212      }
213      if (node.Symbol is Division) {
214        List<AutoDiff.Term> terms = new List<Term>();
215        foreach (var subTree in node.Subtrees) {
216          terms.Add(ConvertToAutoDiff(subTree));
217        }
218        if (terms.Count == 1) return 1.0 / terms[0];
219        else return terms.Aggregate((a, b) => new AutoDiff.Product(a, 1.0 / b));
220      }
221      if (node.Symbol is Absolute) {
222        var x1 = ConvertToAutoDiff(node.GetSubtree(0));
223        return abs(x1);
224      }
225      if (node.Symbol is AnalyticQuotient) {
226        var x1 = ConvertToAutoDiff(node.GetSubtree(0));
227        var x2 = ConvertToAutoDiff(node.GetSubtree(1));
228        return x1 / (TermBuilder.Power(1 + x2 * x2, 0.5));
229      }
230      if (node.Symbol is Logarithm) {
231        return AutoDiff.TermBuilder.Log(
232          ConvertToAutoDiff(node.GetSubtree(0)));
233      }
234      if (node.Symbol is Exponential) {
235        return AutoDiff.TermBuilder.Exp(
236          ConvertToAutoDiff(node.GetSubtree(0)));
237      }
238      if (node.Symbol is Square) {
239        return AutoDiff.TermBuilder.Power(
240          ConvertToAutoDiff(node.GetSubtree(0)), 2.0);
241      }
242      if (node.Symbol is SquareRoot) {
243        return AutoDiff.TermBuilder.Power(
244          ConvertToAutoDiff(node.GetSubtree(0)), 0.5);
245      }
246      if (node.Symbol is Cube) {
247        return AutoDiff.TermBuilder.Power(
248          ConvertToAutoDiff(node.GetSubtree(0)), 3.0);
249      }
250      if (node.Symbol is CubeRoot) {
251        return AutoDiff.TermBuilder.Power(
252          ConvertToAutoDiff(node.GetSubtree(0)), 1.0 / 3.0);
253      }
254      if (node.Symbol is Sine) {
255        return sin(
256          ConvertToAutoDiff(node.GetSubtree(0)));
257      }
258      if (node.Symbol is Cosine) {
259        return cos(
260          ConvertToAutoDiff(node.GetSubtree(0)));
261      }
262      if (node.Symbol is Tangent) {
263        return tan(
264          ConvertToAutoDiff(node.GetSubtree(0)));
265      }
266      if (node.Symbol is HyperbolicTangent) {
267        return tanh(
268          ConvertToAutoDiff(node.GetSubtree(0)));
269      }
270      if (node.Symbol is Erf) {
271        return erf(
272          ConvertToAutoDiff(node.GetSubtree(0)));
273      }
274      if (node.Symbol is Norm) {
275        return norm(
276          ConvertToAutoDiff(node.GetSubtree(0)));
277      }
278      if (node.Symbol is StartSymbol) {
279        if (addLinearScalingTerms) {
280          // scaling variables α, β are given at the beginning of the parameter vector
281          var alpha = new AutoDiff.Variable();
282          var beta = new AutoDiff.Variable();
283          variables.Add(beta);
284          variables.Add(alpha);
285          var t = ConvertToAutoDiff(node.GetSubtree(0));
286          return t * alpha + beta;
287        } else return ConvertToAutoDiff(node.GetSubtree(0));
288      }
289      throw new ConversionException();
290    }
291
292
293    // for each factor variable value we need a parameter which represents a binary indicator for that variable & value combination
294    // each binary indicator is only necessary once. So we only create a parameter if this combination is not yet available
295    private static Term FindOrCreateParameter(Dictionary<DataForVariable, AutoDiff.Variable> parameters,
296      string varName, string varValue = "", int lag = 0) {
297      var data = new DataForVariable(varName, varValue, lag);
298
299      AutoDiff.Variable par = null;
300      if (!parameters.TryGetValue(data, out par)) {
301        // not found -> create new parameter and entries in names and values lists
302        par = new AutoDiff.Variable();
303        parameters.Add(data, par);
304      }
305      return par;
306    }
307
308    public static bool IsCompatible(ISymbolicExpressionTree tree) {
309      var containsUnknownSymbol = (
310        from n in tree.Root.GetSubtree(0).IterateNodesPrefix()
311        where
312          !(n.Symbol is Variable) &&
313          !(n.Symbol is BinaryFactorVariable) &&
314          !(n.Symbol is FactorVariable) &&
315          !(n.Symbol is LaggedVariable) &&
316          !(n.Symbol is Constant) &&
317          !(n.Symbol is Addition) &&
318          !(n.Symbol is Subtraction) &&
319          !(n.Symbol is Multiplication) &&
320          !(n.Symbol is Division) &&
321          !(n.Symbol is Logarithm) &&
322          !(n.Symbol is Exponential) &&
323          !(n.Symbol is SquareRoot) &&
324          !(n.Symbol is Square) &&
325          !(n.Symbol is Sine) &&
326          !(n.Symbol is Cosine) &&
327          !(n.Symbol is Tangent) &&
328          !(n.Symbol is HyperbolicTangent) &&
329          !(n.Symbol is Erf) &&
330          !(n.Symbol is Norm) &&
331          !(n.Symbol is StartSymbol) &&
332          !(n.Symbol is Absolute) &&
333          !(n.Symbol is AnalyticQuotient) &&
334          !(n.Symbol is Cube) &&
335          !(n.Symbol is CubeRoot)
336        select n).Any();
337      return !containsUnknownSymbol;
338    }
339    #region exception class
340    [Serializable]
341    public class ConversionException : Exception {
342
343      public ConversionException() {
344      }
345
346      public ConversionException(string message) : base(message) {
347      }
348
349      public ConversionException(string message, Exception inner) : base(message, inner) {
350      }
351
352      protected ConversionException(
353        SerializationInfo info,
354        StreamingContext context) : base(info, context) {
355      }
356    }
357    #endregion
358  }
359}
Note: See TracBrowser for help on using the repository browser.