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 added
4 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/Constants Optimization/Util.cs

    r16461 r16500  
    2323using System.Collections.Generic;
    2424using System.Linq;
     25using HeuristicLab.Common;
    2526using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2627using static HeuristicLab.Problems.DataAnalysis.Symbolic.TreeToAutoDiffTermConverter;
     
    4849    }
    4950
    50     public static Dictionary<DataForVariable, AutoDiff.Variable> ExtractParameters(IDataset dataset) {
    51       var parameters = new Dictionary<DataForVariable, AutoDiff.Variable>();
     51    public static List<DataForVariable> GenerateVariables(IDataset dataset) {
     52      var variables = new List<DataForVariable>();
    5253      foreach (var doubleVariable in dataset.DoubleVariables) {
    5354        var data = new DataForVariable(doubleVariable, string.Empty, 0);
    54         var param = new AutoDiff.Variable();
    55         parameters.Add(data, param);
     55        variables.Add(data);
    5656      }
    5757
     
    5959        foreach (var stringValue in dataset.GetStringValues(stringVariable).Distinct()) {
    6060          var data = new DataForVariable(stringVariable, stringValue, 0);
    61           var param = new AutoDiff.Variable();
    62           parameters.Add(data, param);
     61          variables.Add(data);
    6362        }
    6463      }
    65       return parameters;
     64      return variables;
     65    }
     66
     67    public static List<DataForVariable> ExtractLaggedVariables(ISymbolicExpressionTree tree) {
     68      var variables = new HashSet<DataForVariable>();
     69      foreach (var laggedNode in tree.IterateNodesPrefix().OfType<ILaggedTreeNode>()) {
     70        var laggedVariableTreeNode = laggedNode as LaggedVariableTreeNode;
     71        if (laggedVariableTreeNode != null) {
     72          var data = new DataForVariable(laggedVariableTreeNode.VariableName, string.Empty, laggedVariableTreeNode.Lag);
     73          if (!variables.Contains(data)) variables.Add(data);
     74        }
     75      }
     76      return variables.ToList();
    6677    }
    6778
     
    7990          for (int j = 0; j < factorVarTreeNode.Weights.Length; j++)
    8091            constants.Add(factorVarTreeNode.Weights[j]);
    81         }        
     92        } else throw new NotSupportedException(string.Format("Terminal nodes of type {0} are not supported.", node.GetType().GetPrettyName()));
    8293      }
    8394      return constants.ToArray();
     
    97108          for (int j = 0; j < factorVarTreeNode.Weights.Length; j++)
    98109            factorVarTreeNode.Weights[j] = constants[i++];
    99         }
     110        } else throw new NotSupportedException(string.Format("Terminal nodes of type {0} are not supported.", node.GetType().GetPrettyName()));
    100111      }
    101112    }
  • 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    }
  • branches/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj

    r16460 r16500  
    213213      <SubType>Code</SubType>
    214214    </Compile>
     215    <Compile Include="Constants Optimization\IConstantsOptimizer.cs" />
     216    <Compile Include="Constants Optimization\LMConstantsOptimizer.cs" />
    215217    <Compile Include="Constants Optimization\Util.cs" />
    216218    <Compile Include="Converters\LinearModelToTreeConverter.cs" />
Note: See TracChangeset for help on using the changeset viewer.