Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/14/22 12:06:18 (2 years ago)
Author:
mkommend
Message:

#3136: Ommited parameter optimization of variable weights in the template part of the tree.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/ParameterOptimization.cs

    r18192 r18197  
    2727namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
    2828  public static class ParameterOptimization {
    29     public static double OptimizeTreeParameters(IRegressionProblemData problemData, ISymbolicExpressionTree tree,
    30       int maxIterations = 10, bool updateParametersInTree = true, bool updateVariableWeights = true,
     29    public static double OptimizeTreeParameters(IRegressionProblemData problemData, ISymbolicExpressionTree tree, int maxIterations = 10,
     30      bool updateParametersInTree = true, bool updateVariableWeights = true, IEnumerable<ISymbolicExpressionTreeNode> excludeNodes = null,
    3131      double lowerEstimationLimit = double.MinValue, double upperEstimationLimit = double.MaxValue,
    3232      IEnumerable<int> rows = null, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter = null,
     
    3535      if (rows == null) rows = problemData.TrainingIndices;
    3636      if (interpreter == null) interpreter = new SymbolicDataAnalysisExpressionTreeBatchInterpreter();
     37      if (excludeNodes == null) excludeNodes = Enumerable.Empty<ISymbolicExpressionTreeNode>();
    3738
    3839      // Numeric parameters in the tree become variables for parameter optimization.
     
    4647      TreeToAutoDiffTermConverter.ParametricFunction func;
    4748      TreeToAutoDiffTermConverter.ParametricFunctionGradient func_grad;
    48       if (!TreeToAutoDiffTermConverter.TryConvertToAutoDiff(tree, updateVariableWeights, addLinearScalingTerms: false, out parameters, out initialParameters, out func, out func_grad))
     49      if (!TreeToAutoDiffTermConverter.TryConvertToAutoDiff(tree,
     50        updateVariableWeights, addLinearScalingTerms: false, excludeNodes,
     51        out parameters, out initialParameters, out func, out func_grad))
    4952        throw new NotSupportedException("Could not optimize parameters of symbolic expression tree due to not supported symbols used in the tree.");
    5053      var parameterEntries = parameters.ToArray(); // order of entries must be the same for x
     
    112115      //             request was submitted.
    113116      if (rep.terminationtype > 0) {
    114         UpdateParameters(tree, c, updateVariableWeights);
     117        UpdateParameters(tree, c, updateVariableWeights, excludeNodes);
    115118      }
    116119      var quality = SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator.Calculate(
     
    119122        lowerEstimationLimit, upperEstimationLimit);
    120123
    121       if (!updateParametersInTree) UpdateParameters(tree, initialParameters, updateVariableWeights);
     124      if (!updateParametersInTree) UpdateParameters(tree, initialParameters, updateVariableWeights, excludeNodes);
    122125
    123126      if (originalQuality < quality || double.IsNaN(quality)) {
    124         UpdateParameters(tree, initialParameters, updateVariableWeights);
     127        UpdateParameters(tree, initialParameters, updateVariableWeights, excludeNodes);
    125128        return originalQuality;
    126129      }
     
    128131    }
    129132
    130     private static void UpdateParameters(ISymbolicExpressionTree tree, double[] parameters, bool updateVariableWeights) {
     133    private static void UpdateParameters(ISymbolicExpressionTree tree, double[] parameters,
     134      bool updateVariableWeights, IEnumerable<ISymbolicExpressionTreeNode> excludedNodes) {
    131135      int i = 0;
    132       foreach (var node in tree.Root.IterateNodesPrefix().OfType<SymbolicExpressionTreeTerminalNode>()) {
     136      foreach (var node in tree.Root.IterateNodesPrefix().OfType<SymbolicExpressionTreeTerminalNode>().Except(excludedNodes)) {
    133137        NumberTreeNode numberTreeNode = node as NumberTreeNode;
    134138        VariableTreeNodeBase variableTreeNodeBase = node as VariableTreeNodeBase;
     
    140144        } else if (updateVariableWeights && variableTreeNodeBase != null)
    141145          variableTreeNodeBase.Weight = parameters[i++];
    142         else if (factorVarTreeNode != null) {
     146        else if (updateVariableWeights && factorVarTreeNode != null) {
    143147          for (int j = 0; j < factorVarTreeNode.Weights.Length; j++)
    144148            factorVarTreeNode.Weights[j] = parameters[i++];
Note: See TracChangeset for help on using the changeset viewer.