Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/26/20 16:43:25 (5 years ago)
Author:
pfleck
Message:

#3040 Added a constant opt evaluator for vectors that uses the existing AutoDiff library by unrolling all vector operations.

Location:
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4
Files:
1 edited
1 copied

Legend:

Unmodified
Added
Removed
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression-3.4.csproj

    r17502 r17726  
    141141    <Compile Include="Plugin.cs" />
    142142    <Compile Include="SingleObjective\ConstantOptimizationAnalyzer.cs" />
     143    <Compile Include="SingleObjective\Evaluators\VectorUnrollingNonlinearLeastSquaresConstantOptimizationEvaluator.cs" />
    143144    <Compile Include="SingleObjective\Evaluators\NonlinearLeastSquaresConstantOptimizationEvaluator.cs" />
    144145    <Compile Include="SingleObjective\Evaluators\SymbolicRegressionMeanRelativeErrorEvaluator.cs" />
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/VectorUnrollingNonlinearLeastSquaresConstantOptimizationEvaluator.cs

    r17725 r17726  
    3030using HeuristicLab.Parameters;
    3131using HEAL.Attic;
     32using DoubleVector = MathNet.Numerics.LinearAlgebra.Vector<double>;
    3233
    3334namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
    34   [StorableType("24B68851-036D-4446-BD6F-3823E9028FF4")]
    35   [Item("NonlinearLeastSquaresOptimizer", "")]
    36   public class NonlinearLeastSquaresConstantOptimizationEvaluator : SymbolicRegressionConstantOptimizationEvaluator {
     35  [StorableType("5F8DB251-C6F7-40AC-BC30-3C55AF655A2F")]
     36  [Item("VectorUnrollingNonlinearLeastSquaresConstantOptimizationEvaluator", "")]
     37  public class VectorUnrollingNonlinearLeastSquaresConstantOptimizationEvaluator : SymbolicRegressionConstantOptimizationEvaluator {
    3738
    3839    private const string ConstantOptimizationIterationsName = "ConstantOptimizationIterations";
     
    5152    #endregion
    5253
    53     public NonlinearLeastSquaresConstantOptimizationEvaluator()
     54    public VectorUnrollingNonlinearLeastSquaresConstantOptimizationEvaluator()
    5455      : base() {
    5556      Parameters.Add(new FixedValueParameter<IntValue>(ConstantOptimizationIterationsName, "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)));
    5657    }
    5758
    58     protected NonlinearLeastSquaresConstantOptimizationEvaluator(NonlinearLeastSquaresConstantOptimizationEvaluator original, Cloner cloner)
     59    protected VectorUnrollingNonlinearLeastSquaresConstantOptimizationEvaluator(VectorUnrollingNonlinearLeastSquaresConstantOptimizationEvaluator original, Cloner cloner)
    5960      : base(original, cloner) {
    6061    }
    6162    public override IDeepCloneable Clone(Cloner cloner) {
    62       return new NonlinearLeastSquaresConstantOptimizationEvaluator(this, cloner);
     63      return new VectorUnrollingNonlinearLeastSquaresConstantOptimizationEvaluator(this, cloner);
    6364    }
    6465    [StorableConstructor]
    65     protected NonlinearLeastSquaresConstantOptimizationEvaluator(StorableConstructorFlag _) : base(_) { }
     66    protected VectorUnrollingNonlinearLeastSquaresConstantOptimizationEvaluator(StorableConstructorFlag _) : base(_) { }
    6667
    6768    protected override ISymbolicExpressionTree OptimizeConstants(
    6869      ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable<int> rows,
    6970      CancellationToken cancellationToken = default(CancellationToken), EvaluationsCounter counter = null) {
    70       return OptimizeTree(tree,
     71      return OptimizeTree(tree, (SymbolicDataAnalysisExpressionTreeVectorInterpreter)SymbolicDataAnalysisTreeInterpreterParameter.ActualValue,
    7172        problemData, rows,
    7273        ApplyLinearScalingParameter.ActualValue.Value, ConstantOptimizationIterations, UpdateVariableWeights,
     
    7677    public static ISymbolicExpressionTree OptimizeTree(
    7778      ISymbolicExpressionTree tree,
     79      SymbolicDataAnalysisExpressionTreeVectorInterpreter interpreter,
    7880      IRegressionProblemData problemData, IEnumerable<int> rows,
    7981      bool applyLinearScaling, int maxIterations, bool updateVariableWeights,
    8082      CancellationToken cancellationToken = default(CancellationToken), EvaluationsCounter counter = null, Action<double[], double, object> iterationCallback = null) {
     83
     84      var vectorLengths = problemData.Dataset.DoubleVectorVariables
     85        .SelectMany(var => problemData.Dataset.GetDoubleVectorValues(var, rows))
     86        .Select(v => v.Count);
     87      var vectorlength = vectorLengths.First();
     88      if (vectorLengths.Any(l => l != vectorlength))
     89        throw new InvalidOperationException("All vectors must be of same length.");
     90      var evaluationTraces = interpreter.GetIntermediateNodeValues(tree, problemData.Dataset, rows);
     91      var evaluationTrace = evaluationTraces.First(); // assume all vector lengths are the same
     92
    8193
    8294      // numeric constants in the tree become variables for constant opt
     
    8597      // variable name, variable value (for factor vars) and lag as a DataForVariable object.
    8698      // A dictionary is used to find parameters
    87       bool success = TreeToAutoDiffTermConverter.TryConvertToAutoDiff(
    88         tree, updateVariableWeights, applyLinearScaling,
     99      bool success = VectorUnrollingTreeToAutoDiffTermConverter.TryConvertToAutoDiff(
     100        tree, evaluationTrace,
     101        updateVariableWeights, applyLinearScaling,
    89102        out var parameters, out var initialConstants, out var func, out var func_grad);
    90103      if (!success)
     
    114127          } else if (ds.VariableHasType<string>(info.variableName)) {
    115128            x[row, col] = ds.GetStringValue(info.variableName, r) == info.variableValue ? 1 : 0;
     129          } else if (ds.VariableHasType<DoubleVector>(info.variableName)) {
     130            x[row, col] = ds.GetDoubleVectorValue(info.variableName, r)[info.index];
    116131          } else throw new InvalidProgramException("found a variable of unknown type");
    117132          col++;
     
    182197    }
    183198
    184     private static alglib.ndimensional_pfunc CreatePFunc(TreeToAutoDiffTermConverter.ParametricFunction func) {
     199    private static alglib.ndimensional_pfunc CreatePFunc(VectorUnrollingTreeToAutoDiffTermConverter.ParametricFunction func) {
    185200      return (double[] c, double[] x, ref double fx, object o) => {
    186201        fx = func(c, x);
     
    190205    }
    191206
    192     private static alglib.ndimensional_pgrad CreatePGrad(TreeToAutoDiffTermConverter.ParametricFunctionGradient func_grad) {
     207    private static alglib.ndimensional_pgrad CreatePGrad(VectorUnrollingTreeToAutoDiffTermConverter.ParametricFunctionGradient func_grad) {
    193208      return (double[] c, double[] x, ref double fx, double[] grad, object o) => {
    194209        var tuple = func_grad(c, x);
     
    201216
    202217    public static bool CanOptimizeConstants(ISymbolicExpressionTree tree) {
    203       return TreeToAutoDiffTermConverter.IsCompatible(tree);
     218      return VectorUnrollingTreeToAutoDiffTermConverter.IsCompatible(tree);
    204219    }
    205220  }
Note: See TracChangeset for help on using the changeset viewer.