Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/28/18 15:44:31 (6 years ago)
Author:
mkommend
Message:

#2974: Added unit tests and refactoring.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/TreeToAutoDiffTermConverter.cs

    r16458 r16461  
    131131    }
    132132
     133    public static bool TryConvertToAutoDiff(ISymbolicExpressionTree tree, bool makeVariableWeightsVariable, bool addLinearScalingTerms, Dictionary<DataForVariable, AutoDiff.Variable> parameters,
     134      out ParametricFunction func,
     135      out ParametricFunctionGradient func_grad
     136  ) {
     137
     138      // use a transformator object which holds the state (variable list, parameter list, ...) for recursive transformation of the tree
     139      var transformator = new TreeToAutoDiffTermConverter(makeVariableWeightsVariable, parameters);
     140      AutoDiff.Term term;
     141      try {
     142        term = transformator.ConvertToAutoDiff(tree.Root.GetSubtree(0));
     143
     144        if (addLinearScalingTerms) {
     145          // scaling variables α, β are given at the beginning of the parameter vector
     146          var alpha = new AutoDiff.Variable();
     147          var beta = new AutoDiff.Variable();
     148          transformator.variables.Insert(0, alpha);
     149          transformator.variables.Insert(0, beta);
     150
     151          term = term * alpha + beta;
     152        }
     153       
     154        var compiledTerm = term.Compile(transformator.variables.ToArray(), parameters.Select(kvp => kvp.Value).ToArray());
     155
     156
     157        func = (vars, @params) => compiledTerm.Evaluate(vars, @params);
     158        func_grad = (vars, @params) => compiledTerm.Differentiate(vars, @params);
     159        return true;
     160      } catch (ConversionException) {
     161        func = null;
     162        func_grad = null;
     163      }
     164      return false;
     165    }
     166
    133167    // state for recursive transformation of trees
    134168    private readonly List<double> initialConstants;
     
    137171    private readonly bool makeVariableWeightsVariable;
    138172
    139     private TreeToAutoDiffTermConverter(bool makeVariableWeightsVariable) {
     173    private TreeToAutoDiffTermConverter(bool makeVariableWeightsVariable, Dictionary<DataForVariable, AutoDiff.Variable> parameters = null) {
    140174      this.makeVariableWeightsVariable = makeVariableWeightsVariable;
    141175      this.initialConstants = new List<double>();
    142       this.parameters = new Dictionary<DataForVariable, AutoDiff.Variable>();
     176      if (parameters == null)
     177        this.parameters = new Dictionary<DataForVariable, AutoDiff.Variable>();
     178      else
     179        this.parameters = parameters;
    143180      this.variables = new List<AutoDiff.Variable>();
    144181    }
Note: See TracChangeset for help on using the changeset viewer.