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