Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/04/19 17:37:13 (6 years ago)
Author:
mkommend
Message:

#2974: Added intermediate version of new constants optimization for profiling.

Location:
branches/2974_Constants_Optimization
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/2974_Constants_Optimization

    • Property svn:ignore
      •  

        old new  
        11packages
         2TestResults
  • branches/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/TreeToAutoDiffTermConverter.cs

    r16463 r16500  
    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       out double[] initialConstants
    137   ) {
     133    public static bool TryConvertToAutoDiff(ISymbolicExpressionTree tree, bool addLinearScalingTerms, IEnumerable<DataForVariable> variables,
     134      out IParametricCompiledTerm autoDiffTerm, out double[] initialConstants) {
    138135      // 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);
     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
    140148      AutoDiff.Term term;
    141149      try {
    142 
    143 
     150        term = transformator.ConvertToAutoDiff(tree.Root.GetSubtree(0));
    144151        if (addLinearScalingTerms) {
    145           // scaling variables α, β are given at the beginning of the parameter vector
     152          // scaling variables α, β are given at the end of the parameter vector
    146153          var alpha = new AutoDiff.Variable();
    147154          var beta = new AutoDiff.Variable();
     155
     156          term = term * alpha + beta;
     157
     158          transformator.variables.Add(alpha);
    148159          transformator.variables.Add(beta);
    149           transformator.variables.Add(alpha);
    150           term = transformator.ConvertToAutoDiff(tree.Root.GetSubtree(0));
    151           term = term * alpha + beta;
    152         } else {
    153           term = transformator.ConvertToAutoDiff(tree.Root.GetSubtree(0));
    154         }
    155 
    156         var compiledTerm = term.Compile(transformator.variables.ToArray(), parameters.Values.ToArray());
    157         func = (vars, @params) => compiledTerm.Evaluate(vars, @params);
    158         func_grad = (vars, @params) => compiledTerm.Differentiate(vars, @params);
     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;
    159167        initialConstants = transformator.initialConstants.ToArray();
    160168
    161169        return true;
    162170      } catch (ConversionException) {
    163         func = null;
    164         func_grad = null;
     171        autoDiffTerm = null;
    165172        initialConstants = null;
    166173      }
     
    174181    private readonly bool makeVariableWeightsVariable;
    175182
    176     private TreeToAutoDiffTermConverter(bool makeVariableWeightsVariable, Dictionary<DataForVariable, AutoDiff.Variable> parameters = null) {
     183    private TreeToAutoDiffTermConverter(bool makeVariableWeightsVariable) {
    177184      this.makeVariableWeightsVariable = makeVariableWeightsVariable;
    178185      this.initialConstants = new List<double>();
    179       if (parameters == null)
    180         this.parameters = new Dictionary<DataForVariable, AutoDiff.Variable>();
    181       else
    182         this.parameters = parameters;
     186      this.parameters = new Dictionary<DataForVariable, AutoDiff.Variable>();
    183187      this.variables = new List<AutoDiff.Variable>();
    184188    }
Note: See TracChangeset for help on using the changeset viewer.