Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionConstantOptimizationEvaluator.cs @ 15136

Last change on this file since 15136 was 15136, checked in by gkronber, 7 years ago

#2686: merged r14946 and r14951 from trunk to stable

File size: 28.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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 AutoDiff;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
34  [Item("Constant Optimization Evaluator", "Calculates Pearson R² of a symbolic regression solution and optimizes the constant used.")]
35  [StorableClass]
36  public class SymbolicRegressionConstantOptimizationEvaluator : SymbolicRegressionSingleObjectiveEvaluator {
37    private const string ConstantOptimizationIterationsParameterName = "ConstantOptimizationIterations";
38    private const string ConstantOptimizationImprovementParameterName = "ConstantOptimizationImprovement";
39    private const string ConstantOptimizationProbabilityParameterName = "ConstantOptimizationProbability";
40    private const string ConstantOptimizationRowsPercentageParameterName = "ConstantOptimizationRowsPercentage";
41    private const string UpdateConstantsInTreeParameterName = "UpdateConstantsInSymbolicExpressionTree";
42    private const string UpdateVariableWeightsParameterName = "Update Variable Weights";
43
44    public IFixedValueParameter<IntValue> ConstantOptimizationIterationsParameter {
45      get { return (IFixedValueParameter<IntValue>)Parameters[ConstantOptimizationIterationsParameterName]; }
46    }
47    public IFixedValueParameter<DoubleValue> ConstantOptimizationImprovementParameter {
48      get { return (IFixedValueParameter<DoubleValue>)Parameters[ConstantOptimizationImprovementParameterName]; }
49    }
50    public IFixedValueParameter<PercentValue> ConstantOptimizationProbabilityParameter {
51      get { return (IFixedValueParameter<PercentValue>)Parameters[ConstantOptimizationProbabilityParameterName]; }
52    }
53    public IFixedValueParameter<PercentValue> ConstantOptimizationRowsPercentageParameter {
54      get { return (IFixedValueParameter<PercentValue>)Parameters[ConstantOptimizationRowsPercentageParameterName]; }
55    }
56    public IFixedValueParameter<BoolValue> UpdateConstantsInTreeParameter {
57      get { return (IFixedValueParameter<BoolValue>)Parameters[UpdateConstantsInTreeParameterName]; }
58    }
59    public IFixedValueParameter<BoolValue> UpdateVariableWeightsParameter {
60      get { return (IFixedValueParameter<BoolValue>)Parameters[UpdateVariableWeightsParameterName]; }
61    }
62
63
64    public IntValue ConstantOptimizationIterations {
65      get { return ConstantOptimizationIterationsParameter.Value; }
66    }
67    public DoubleValue ConstantOptimizationImprovement {
68      get { return ConstantOptimizationImprovementParameter.Value; }
69    }
70    public PercentValue ConstantOptimizationProbability {
71      get { return ConstantOptimizationProbabilityParameter.Value; }
72    }
73    public PercentValue ConstantOptimizationRowsPercentage {
74      get { return ConstantOptimizationRowsPercentageParameter.Value; }
75    }
76    public bool UpdateConstantsInTree {
77      get { return UpdateConstantsInTreeParameter.Value.Value; }
78      set { UpdateConstantsInTreeParameter.Value.Value = value; }
79    }
80
81    public bool UpdateVariableWeights {
82      get { return UpdateVariableWeightsParameter.Value.Value; }
83      set { UpdateVariableWeightsParameter.Value.Value = value; }
84    }
85
86    public override bool Maximization {
87      get { return true; }
88    }
89
90    [StorableConstructor]
91    protected SymbolicRegressionConstantOptimizationEvaluator(bool deserializing) : base(deserializing) { }
92    protected SymbolicRegressionConstantOptimizationEvaluator(SymbolicRegressionConstantOptimizationEvaluator original, Cloner cloner)
93      : base(original, cloner) {
94    }
95    public SymbolicRegressionConstantOptimizationEvaluator()
96      : base() {
97      Parameters.Add(new FixedValueParameter<IntValue>(ConstantOptimizationIterationsParameterName, "Determines how many iterations should be calculated while optimizing the constant of a symbolic expression tree (0 indicates other or default stopping criterion).", new IntValue(10), true));
98      Parameters.Add(new FixedValueParameter<DoubleValue>(ConstantOptimizationImprovementParameterName, "Determines the relative improvement which must be achieved in the constant optimization to continue with it (0 indicates other or default stopping criterion).", new DoubleValue(0), true) { Hidden = true });
99      Parameters.Add(new FixedValueParameter<PercentValue>(ConstantOptimizationProbabilityParameterName, "Determines the probability that the constants are optimized", new PercentValue(1), true));
100      Parameters.Add(new FixedValueParameter<PercentValue>(ConstantOptimizationRowsPercentageParameterName, "Determines the percentage of the rows which should be used for constant optimization", new PercentValue(1), true));
101      Parameters.Add(new FixedValueParameter<BoolValue>(UpdateConstantsInTreeParameterName, "Determines if the constants in the tree should be overwritten by the optimized constants.", new BoolValue(true)) { Hidden = true });
102      Parameters.Add(new FixedValueParameter<BoolValue>(UpdateVariableWeightsParameterName, "Determines if the variable weights in the tree should be  optimized.", new BoolValue(true)) { Hidden = true });
103    }
104
105    public override IDeepCloneable Clone(Cloner cloner) {
106      return new SymbolicRegressionConstantOptimizationEvaluator(this, cloner);
107    }
108
109    [StorableHook(HookType.AfterDeserialization)]
110    private void AfterDeserialization() {
111      if (!Parameters.ContainsKey(UpdateConstantsInTreeParameterName))
112        Parameters.Add(new FixedValueParameter<BoolValue>(UpdateConstantsInTreeParameterName, "Determines if the constants in the tree should be overwritten by the optimized constants.", new BoolValue(true)));
113      if (!Parameters.ContainsKey(UpdateVariableWeightsParameterName))
114        Parameters.Add(new FixedValueParameter<BoolValue>(UpdateVariableWeightsParameterName, "Determines if the variable weights in the tree should be  optimized.", new BoolValue(true)));
115    }
116
117    public override IOperation InstrumentedApply() {
118      var solution = SymbolicExpressionTreeParameter.ActualValue;
119      double quality;
120      if (RandomParameter.ActualValue.NextDouble() < ConstantOptimizationProbability.Value) {
121        IEnumerable<int> constantOptimizationRows = GenerateRowsToEvaluate(ConstantOptimizationRowsPercentage.Value);
122        quality = OptimizeConstants(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, ProblemDataParameter.ActualValue,
123           constantOptimizationRows, ApplyLinearScalingParameter.ActualValue.Value, ConstantOptimizationIterations.Value, updateVariableWeights: UpdateVariableWeights, lowerEstimationLimit: EstimationLimitsParameter.ActualValue.Lower, upperEstimationLimit: EstimationLimitsParameter.ActualValue.Upper, updateConstantsInTree: UpdateConstantsInTree);
124
125        if (ConstantOptimizationRowsPercentage.Value != RelativeNumberOfEvaluatedSamplesParameter.ActualValue.Value) {
126          var evaluationRows = GenerateRowsToEvaluate();
127          quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, evaluationRows, ApplyLinearScalingParameter.ActualValue.Value);
128        }
129      } else {
130        var evaluationRows = GenerateRowsToEvaluate();
131        quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, evaluationRows, ApplyLinearScalingParameter.ActualValue.Value);
132      }
133      QualityParameter.ActualValue = new DoubleValue(quality);
134
135      return base.InstrumentedApply();
136    }
137
138    public override double Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable<int> rows) {
139      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
140      EstimationLimitsParameter.ExecutionContext = context;
141      ApplyLinearScalingParameter.ExecutionContext = context;
142
143      // Pearson R² evaluator is used on purpose instead of the const-opt evaluator,
144      // because Evaluate() is used to get the quality of evolved models on
145      // different partitions of the dataset (e.g., best validation model)
146      double r2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScalingParameter.ActualValue.Value);
147
148      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
149      EstimationLimitsParameter.ExecutionContext = null;
150      ApplyLinearScalingParameter.ExecutionContext = null;
151
152      return r2;
153    }
154
155    #region derivations of functions
156    // create function factory for arctangent
157    private readonly Func<Term, UnaryFunc> arctan = UnaryFunc.Factory(
158      eval: Math.Atan,
159      diff: x => 1 / (1 + x * x));
160    private static readonly Func<Term, UnaryFunc> sin = UnaryFunc.Factory(
161      eval: Math.Sin,
162      diff: Math.Cos);
163    private static readonly Func<Term, UnaryFunc> cos = UnaryFunc.Factory(
164       eval: Math.Cos,
165       diff: x => -Math.Sin(x));
166    private static readonly Func<Term, UnaryFunc> tan = UnaryFunc.Factory(
167      eval: Math.Tan,
168      diff: x => 1 + Math.Tan(x) * Math.Tan(x));
169    private static readonly Func<Term, UnaryFunc> erf = UnaryFunc.Factory(
170      eval: alglib.errorfunction,
171      diff: x => 2.0 * Math.Exp(-(x * x)) / Math.Sqrt(Math.PI));
172    private static readonly Func<Term, UnaryFunc> norm = UnaryFunc.Factory(
173      eval: alglib.normaldistribution,
174      diff: x => -(Math.Exp(-(x * x)) * Math.Sqrt(Math.Exp(x * x)) * x) / Math.Sqrt(2 * Math.PI));
175    #endregion
176
177
178
179    public static double OptimizeConstants(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
180      ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable<int> rows, bool applyLinearScaling,
181      int maxIterations, bool updateVariableWeights = true,
182      double lowerEstimationLimit = double.MinValue, double upperEstimationLimit = double.MaxValue,
183      bool updateConstantsInTree = true) {
184
185      // numeric constants in the tree become variables for constant opt
186      // variables in the tree become parameters (fixed values) for constant opt
187      // for each parameter (variable in the original tree) we store the
188      // variable name, variable value (for factor vars) and lag as a DataForVariable object.
189      // A dictionary is used to find parameters
190      var variables = new List<AutoDiff.Variable>();
191      var parameters = new Dictionary<DataForVariable, AutoDiff.Variable>();
192
193      AutoDiff.Term func;
194      if (!TryTransformToAutoDiff(tree.Root.GetSubtree(0), variables, parameters, updateVariableWeights, out func))
195        throw new NotSupportedException("Could not optimize constants of symbolic expression tree due to not supported symbols used in the tree.");
196      if (parameters.Count == 0) return 0.0; // gkronber: constant expressions always have a R² of 0.0
197
198      var parameterEntries = parameters.ToArray(); // order of entries must be the same for x
199      AutoDiff.IParametricCompiledTerm compiledFunc = func.Compile(variables.ToArray(), parameterEntries.Select(kvp => kvp.Value).ToArray());
200
201      List<SymbolicExpressionTreeTerminalNode> terminalNodes = null; // gkronber only used for extraction of initial constants
202      if (updateVariableWeights)
203        terminalNodes = tree.Root.IterateNodesPrefix().OfType<SymbolicExpressionTreeTerminalNode>().ToList();
204      else
205        terminalNodes = new List<SymbolicExpressionTreeTerminalNode>
206          (tree.Root.IterateNodesPrefix()
207          .OfType<SymbolicExpressionTreeTerminalNode>()
208          .Where(node => node is ConstantTreeNode || node is FactorVariableTreeNode));
209
210      //extract inital constants
211      double[] c = new double[variables.Count];
212      {
213        c[0] = 0.0;
214        c[1] = 1.0;
215        int i = 2;
216        foreach (var node in terminalNodes) {
217          ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
218          VariableTreeNode variableTreeNode = node as VariableTreeNode;
219          BinaryFactorVariableTreeNode binFactorVarTreeNode = node as BinaryFactorVariableTreeNode;
220          FactorVariableTreeNode factorVarTreeNode = node as FactorVariableTreeNode;
221          if (constantTreeNode != null)
222            c[i++] = constantTreeNode.Value;
223          else if (updateVariableWeights && variableTreeNode != null)
224            c[i++] = variableTreeNode.Weight;
225          else if (updateVariableWeights && binFactorVarTreeNode != null)
226            c[i++] = binFactorVarTreeNode.Weight;
227          else if (factorVarTreeNode != null) {
228            // gkronber: a factorVariableTreeNode holds a category-specific constant therefore we can consider factors to be the same as constants
229            foreach (var w in factorVarTreeNode.Weights) c[i++] = w;
230          }
231        }
232      }
233      double[] originalConstants = (double[])c.Clone();
234      double originalQuality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, tree, lowerEstimationLimit, upperEstimationLimit, problemData, rows, applyLinearScaling);
235
236      alglib.lsfitstate state;
237      alglib.lsfitreport rep;
238      int retVal;
239
240      IDataset ds = problemData.Dataset;
241      double[,] x = new double[rows.Count(), parameters.Count];
242      int row = 0;
243      foreach (var r in rows) {
244        int col = 0;
245        foreach (var kvp in parameterEntries) {
246          var info = kvp.Key;
247          if (ds.VariableHasType<double>(info.variableName)) {
248            x[row, col] = ds.GetDoubleValue(info.variableName, r + info.lag);
249          } else if (ds.VariableHasType<string>(info.variableName)) {
250            x[row, col] = ds.GetStringValue(info.variableName, r) == info.variableValue ? 1 : 0;
251          } else throw new InvalidProgramException("found a variable of unknown type");
252          col++;
253        }
254        row++;
255      }
256      double[] y = ds.GetDoubleValues(problemData.TargetVariable, rows).ToArray();
257      int n = x.GetLength(0);
258      int m = x.GetLength(1);
259      int k = c.Length;
260
261      alglib.ndimensional_pfunc function_cx_1_func = CreatePFunc(compiledFunc);
262      alglib.ndimensional_pgrad function_cx_1_grad = CreatePGrad(compiledFunc);
263
264      try {
265        alglib.lsfitcreatefg(x, y, c, n, m, k, false, out state);
266        alglib.lsfitsetcond(state, 0.0, 0.0, maxIterations);
267        //alglib.lsfitsetgradientcheck(state, 0.001);
268        alglib.lsfitfit(state, function_cx_1_func, function_cx_1_grad, null, null);
269        alglib.lsfitresults(state, out retVal, out c, out rep);
270      }
271      catch (ArithmeticException) {
272        return originalQuality;
273      }
274      catch (alglib.alglibexception) {
275        return originalQuality;
276      }
277
278      //retVal == -7  => constant optimization failed due to wrong gradient
279      if (retVal != -7) UpdateConstants(tree, c.Skip(2).ToArray(), updateVariableWeights);
280      var quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, tree, lowerEstimationLimit, upperEstimationLimit, problemData, rows, applyLinearScaling);
281
282      if (!updateConstantsInTree) UpdateConstants(tree, originalConstants.Skip(2).ToArray(), updateVariableWeights);
283      if (originalQuality - quality > 0.001 || double.IsNaN(quality)) {
284        UpdateConstants(tree, originalConstants.Skip(2).ToArray(), updateVariableWeights);
285        return originalQuality;
286      }
287      return quality;
288    }
289
290    private static void UpdateConstants(ISymbolicExpressionTree tree, double[] constants, bool updateVariableWeights) {
291      int i = 0;
292      foreach (var node in tree.Root.IterateNodesPrefix().OfType<SymbolicExpressionTreeTerminalNode>()) {
293        ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
294        VariableTreeNodeBase variableTreeNodeBase = node as VariableTreeNodeBase;
295        FactorVariableTreeNode factorVarTreeNode = node as FactorVariableTreeNode;
296        if (constantTreeNode != null)
297          constantTreeNode.Value = constants[i++];
298        else if (updateVariableWeights && variableTreeNodeBase != null)
299          variableTreeNodeBase.Weight = constants[i++];
300        else if (factorVarTreeNode != null) {
301          for (int j = 0; j < factorVarTreeNode.Weights.Length; j++)
302            factorVarTreeNode.Weights[j] = constants[i++];
303        }
304      }
305    }
306
307    private static alglib.ndimensional_pfunc CreatePFunc(AutoDiff.IParametricCompiledTerm compiledFunc) {
308      return (double[] c, double[] x, ref double func, object o) => {
309        func = compiledFunc.Evaluate(c, x);
310      };
311    }
312
313    private static alglib.ndimensional_pgrad CreatePGrad(AutoDiff.IParametricCompiledTerm compiledFunc) {
314      return (double[] c, double[] x, ref double func, double[] grad, object o) => {
315        var tupel = compiledFunc.Differentiate(c, x);
316        func = tupel.Item2;
317        Array.Copy(tupel.Item1, grad, grad.Length);
318      };
319    }
320
321    private static bool TryTransformToAutoDiff(ISymbolicExpressionTreeNode node,
322      List<AutoDiff.Variable> variables, Dictionary<DataForVariable, AutoDiff.Variable> parameters,
323      bool updateVariableWeights, out AutoDiff.Term term) {
324      if (node.Symbol is Constant) {
325        var var = new AutoDiff.Variable();
326        variables.Add(var);
327        term = var;
328        return true;
329      }
330      if (node.Symbol is Variable || node.Symbol is BinaryFactorVariable) {
331        var varNode = node as VariableTreeNodeBase;
332        var factorVarNode = node as BinaryFactorVariableTreeNode;
333        // factor variable values are only 0 or 1 and set in x accordingly
334        var varValue = factorVarNode != null ? factorVarNode.VariableValue : string.Empty;
335        var par = FindOrCreateParameter(parameters, varNode.VariableName, varValue);
336
337        if (updateVariableWeights) {
338          var w = new AutoDiff.Variable();
339          variables.Add(w);
340          term = AutoDiff.TermBuilder.Product(w, par);
341        } else {
342          term = varNode.Weight * par;
343        }
344        return true;
345      }
346      if (node.Symbol is FactorVariable) {
347        var factorVarNode = node as FactorVariableTreeNode;
348        var products = new List<Term>();
349        foreach (var variableValue in factorVarNode.Symbol.GetVariableValues(factorVarNode.VariableName)) {
350          var par = FindOrCreateParameter(parameters, factorVarNode.VariableName, variableValue);
351
352          var wVar = new AutoDiff.Variable();
353          variables.Add(wVar);
354
355          products.Add(AutoDiff.TermBuilder.Product(wVar, par));
356        }
357        term = AutoDiff.TermBuilder.Sum(products);
358        return true;
359      }
360      if (node.Symbol is LaggedVariable) {
361        var varNode = node as LaggedVariableTreeNode;
362        var par = FindOrCreateParameter(parameters, varNode.VariableName, string.Empty, varNode.Lag);
363
364        if (updateVariableWeights) {
365          var w = new AutoDiff.Variable();
366          variables.Add(w);
367          term = AutoDiff.TermBuilder.Product(w, par);
368        } else {
369          term = varNode.Weight * par;
370        }
371        return true;
372      }
373      if (node.Symbol is Addition) {
374        List<AutoDiff.Term> terms = new List<Term>();
375        foreach (var subTree in node.Subtrees) {
376          AutoDiff.Term t;
377          if (!TryTransformToAutoDiff(subTree, variables, parameters, updateVariableWeights, out t)) {
378            term = null;
379            return false;
380          }
381          terms.Add(t);
382        }
383        term = AutoDiff.TermBuilder.Sum(terms);
384        return true;
385      }
386      if (node.Symbol is Subtraction) {
387        List<AutoDiff.Term> terms = new List<Term>();
388        for (int i = 0; i < node.SubtreeCount; i++) {
389          AutoDiff.Term t;
390          if (!TryTransformToAutoDiff(node.GetSubtree(i), variables, parameters, updateVariableWeights, out t)) {
391            term = null;
392            return false;
393          }
394          if (i > 0) t = -t;
395          terms.Add(t);
396        }
397        if (terms.Count == 1) term = -terms[0];
398        else term = AutoDiff.TermBuilder.Sum(terms);
399        return true;
400      }
401      if (node.Symbol is Multiplication) {
402        List<AutoDiff.Term> terms = new List<Term>();
403        foreach (var subTree in node.Subtrees) {
404          AutoDiff.Term t;
405          if (!TryTransformToAutoDiff(subTree, variables, parameters, updateVariableWeights, out t)) {
406            term = null;
407            return false;
408          }
409          terms.Add(t);
410        }
411        if (terms.Count == 1) term = terms[0];
412        else term = terms.Aggregate((a, b) => new AutoDiff.Product(a, b));
413        return true;
414
415      }
416      if (node.Symbol is Division) {
417        List<AutoDiff.Term> terms = new List<Term>();
418        foreach (var subTree in node.Subtrees) {
419          AutoDiff.Term t;
420          if (!TryTransformToAutoDiff(subTree, variables, parameters, updateVariableWeights, out t)) {
421            term = null;
422            return false;
423          }
424          terms.Add(t);
425        }
426        if (terms.Count == 1) term = 1.0 / terms[0];
427        else term = terms.Aggregate((a, b) => new AutoDiff.Product(a, 1.0 / b));
428        return true;
429      }
430      if (node.Symbol is Logarithm) {
431        AutoDiff.Term t;
432        if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, updateVariableWeights, out t)) {
433          term = null;
434          return false;
435        } else {
436          term = AutoDiff.TermBuilder.Log(t);
437          return true;
438        }
439      }
440      if (node.Symbol is Exponential) {
441        AutoDiff.Term t;
442        if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, updateVariableWeights, out t)) {
443          term = null;
444          return false;
445        } else {
446          term = AutoDiff.TermBuilder.Exp(t);
447          return true;
448        }
449      }
450      if (node.Symbol is Square) {
451        AutoDiff.Term t;
452        if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, updateVariableWeights, out t)) {
453          term = null;
454          return false;
455        } else {
456          term = AutoDiff.TermBuilder.Power(t, 2.0);
457          return true;
458        }
459      }
460      if (node.Symbol is SquareRoot) {
461        AutoDiff.Term t;
462        if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, updateVariableWeights, out t)) {
463          term = null;
464          return false;
465        } else {
466          term = AutoDiff.TermBuilder.Power(t, 0.5);
467          return true;
468        }
469      }
470      if (node.Symbol is Sine) {
471        AutoDiff.Term t;
472        if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, updateVariableWeights, out t)) {
473          term = null;
474          return false;
475        } else {
476          term = sin(t);
477          return true;
478        }
479      }
480      if (node.Symbol is Cosine) {
481        AutoDiff.Term t;
482        if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, updateVariableWeights, out t)) {
483          term = null;
484          return false;
485        } else {
486          term = cos(t);
487          return true;
488        }
489      }
490      if (node.Symbol is Tangent) {
491        AutoDiff.Term t;
492        if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, updateVariableWeights, out t)) {
493          term = null;
494          return false;
495        } else {
496          term = tan(t);
497          return true;
498        }
499      }
500      if (node.Symbol is Erf) {
501        AutoDiff.Term t;
502        if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, updateVariableWeights, out t)) {
503          term = null;
504          return false;
505        } else {
506          term = erf(t);
507          return true;
508        }
509      }
510      if (node.Symbol is Norm) {
511        AutoDiff.Term t;
512        if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, updateVariableWeights, out t)) {
513          term = null;
514          return false;
515        } else {
516          term = norm(t);
517          return true;
518        }
519      }
520      if (node.Symbol is StartSymbol) {
521        var alpha = new AutoDiff.Variable();
522        var beta = new AutoDiff.Variable();
523        variables.Add(beta);
524        variables.Add(alpha);
525        AutoDiff.Term branchTerm;
526        if (TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, updateVariableWeights, out branchTerm)) {
527          term = branchTerm * alpha + beta;
528          return true;
529        } else {
530          term = null;
531          return false;
532        }
533      }
534      term = null;
535      return false;
536    }
537
538    // for each factor variable value we need a parameter which represents a binary indicator for that variable & value combination
539    // each binary indicator is only necessary once. So we only create a parameter if this combination is not yet available
540    private static Term FindOrCreateParameter(Dictionary<DataForVariable, AutoDiff.Variable> parameters,
541      string varName, string varValue = "", int lag = 0) {
542      var data = new DataForVariable(varName, varValue, lag);
543
544      AutoDiff.Variable par = null;
545      if (!parameters.TryGetValue(data, out par)) {
546        // not found -> create new parameter and entries in names and values lists
547        par = new AutoDiff.Variable();
548        parameters.Add(data, par);
549      }
550      return par;
551    }
552
553    public static bool CanOptimizeConstants(ISymbolicExpressionTree tree) {
554      var containsUnknownSymbol = (
555        from n in tree.Root.GetSubtree(0).IterateNodesPrefix()
556        where
557         !(n.Symbol is Variable) &&
558         !(n.Symbol is BinaryFactorVariable) &&
559         !(n.Symbol is FactorVariable) &&
560         !(n.Symbol is LaggedVariable) &&
561         !(n.Symbol is Constant) &&
562         !(n.Symbol is Addition) &&
563         !(n.Symbol is Subtraction) &&
564         !(n.Symbol is Multiplication) &&
565         !(n.Symbol is Division) &&
566         !(n.Symbol is Logarithm) &&
567         !(n.Symbol is Exponential) &&
568         !(n.Symbol is SquareRoot) &&
569         !(n.Symbol is Square) &&
570         !(n.Symbol is Sine) &&
571         !(n.Symbol is Cosine) &&
572         !(n.Symbol is Tangent) &&
573         !(n.Symbol is Erf) &&
574         !(n.Symbol is Norm) &&
575         !(n.Symbol is StartSymbol)
576        select n).
577      Any();
578      return !containsUnknownSymbol;
579    }
580
581
582    #region helper class
583    private class DataForVariable {
584      public readonly string variableName;
585      public readonly string variableValue; // for factor vars
586      public readonly int lag;
587
588      public DataForVariable(string varName, string varValue, int lag) {
589        this.variableName = varName;
590        this.variableValue = varValue;
591        this.lag = lag;
592      }
593
594      public override bool Equals(object obj) {
595        var other = obj as DataForVariable;
596        if (other == null) return false;
597        return other.variableName.Equals(this.variableName) &&
598               other.variableValue.Equals(this.variableValue) &&
599               other.lag == this.lag;
600      }
601
602      public override int GetHashCode() {
603        return variableName.GetHashCode() ^ variableValue.GetHashCode() ^ lag;
604      }
605    }
606    #endregion
607  }
608}
Note: See TracBrowser for help on using the repository browser.