Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/06/19 18:03:51 (6 years ago)
Author:
mkommend
Message:

#2974: Reverted changes in TreeToAutoDiffTermConverter.

File:
1 edited

Legend:

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

    r16500 r16508  
    9898
    9999      // use a transformator object which holds the state (variable list, parameter list, ...) for recursive transformation of the tree
    100       var transformator = new TreeToAutoDiffTermConverter(makeVariableWeightsVariable);
     100      var transformator = new TreeToAutoDiffTermConverter(makeVariableWeightsVariable, addLinearScalingTerms);
    101101      AutoDiff.Term term;
    102102      try {
    103103        term = transformator.ConvertToAutoDiff(tree.Root.GetSubtree(0));
    104 
    105         if (addLinearScalingTerms) {
    106           // scaling variables α, β are given at the beginning of the parameter vector
    107           var alpha = new AutoDiff.Variable();
    108           var beta = new AutoDiff.Variable();
    109           transformator.variables.Insert(0, alpha);
    110           transformator.variables.Insert(0, beta);
    111 
    112           term = term * alpha + beta;
    113         }
    114 
    115104        var parameterEntries = transformator.parameters.ToArray(); // guarantee same order for keys and values
    116105        var compiledTerm = term.Compile(transformator.variables.ToArray(),
    117106          parameterEntries.Select(kvp => kvp.Value).ToArray());
    118 
    119107        parameters = new List<DataForVariable>(parameterEntries.Select(kvp => kvp.Key));
    120108        initialConstants = transformator.initialConstants.ToArray();
     
    123111        return true;
    124112      } catch (ConversionException) {
    125         parameters = null;
    126         initialConstants = null;
    127113        func = null;
    128114        func_grad = null;
    129       }
    130       return false;
    131     }
    132 
    133     public static bool TryConvertToAutoDiff(ISymbolicExpressionTree tree, bool addLinearScalingTerms, IEnumerable<DataForVariable> variables,
    134       out IParametricCompiledTerm autoDiffTerm, out double[] initialConstants) {
    135       // use a transformator object which holds the state (variable list, parameter list, ...) for recursive transformation of the tree
    136       //TODO change ctor
    137       var transformator = new TreeToAutoDiffTermConverter(true);
    138       var parameters = new AutoDiff.Variable[variables.Count()];
    139 
    140       int i = 0;
    141       foreach(var variable in variables) {
    142         var autoDiffVar = new AutoDiff.Variable();
    143         transformator.parameters.Add(variable, autoDiffVar);
    144         parameters[i] = autoDiffVar;
    145         i++;
    146       }
    147 
    148       AutoDiff.Term term;
    149       try {
    150         term = transformator.ConvertToAutoDiff(tree.Root.GetSubtree(0));
    151         if (addLinearScalingTerms) {
    152           // scaling variables α, β are given at the end of the parameter vector
    153           var alpha = new AutoDiff.Variable();
    154           var beta = new AutoDiff.Variable();
    155 
    156           term = term * alpha + beta;
    157 
    158           transformator.variables.Add(alpha);
    159           transformator.variables.Add(beta);
    160 
    161           transformator.initialConstants.Add(1.0);
    162           transformator.initialConstants.Add(0.0);
    163         }
    164 
    165         var compiledTerm = term.Compile(transformator.variables.ToArray(), parameters);
    166         autoDiffTerm = compiledTerm;
    167         initialConstants = transformator.initialConstants.ToArray();
    168 
    169         return true;
    170       } catch (ConversionException) {
    171         autoDiffTerm = null;
     115        parameters = null;
    172116        initialConstants = null;
    173117      }
     
    180124    private readonly List<AutoDiff.Variable> variables;
    181125    private readonly bool makeVariableWeightsVariable;
    182 
    183     private TreeToAutoDiffTermConverter(bool makeVariableWeightsVariable) {
     126    private readonly bool addLinearScalingTerms;
     127
     128    private TreeToAutoDiffTermConverter(bool makeVariableWeightsVariable, bool addLinearScalingTerms) {
    184129      this.makeVariableWeightsVariable = makeVariableWeightsVariable;
     130      this.addLinearScalingTerms = addLinearScalingTerms;
    185131      this.initialConstants = new List<double>();
    186132      this.parameters = new Dictionary<DataForVariable, AutoDiff.Variable>();
     
    325271      }
    326272      if (node.Symbol is StartSymbol) {
    327         return ConvertToAutoDiff(node.GetSubtree(0));
     273        if (addLinearScalingTerms) {
     274          // scaling variables α, β are given at the beginning of the parameter vector
     275          var alpha = new AutoDiff.Variable();
     276          var beta = new AutoDiff.Variable();
     277          variables.Add(beta);
     278          variables.Add(alpha);
     279          var t = ConvertToAutoDiff(node.GetSubtree(0));
     280          return t * alpha + beta;
     281        } else return ConvertToAutoDiff(node.GetSubtree(0));
    328282      }
    329283      throw new ConversionException();
Note: See TracChangeset for help on using the changeset viewer.