Changeset 18071


Ignore:
Timestamp:
10/20/21 14:39:46 (8 months ago)
Author:
dpiringe
Message:

#3136

  • added linear scaling logic in Evaluate and (for UI reasons) Analyze
  • added logic forSubFunctionSymbol (modified OpCodes) -> the SubFunctionTreeNode is display in the tree but has no effect on evaluation (works like a flag)
    • works now with SymbolicDataAnalysisExpressionTreeInterpreter
  • default grammar for SubFunction is now ArithmeticExpressionGrammar instead of LinearScalingGrammar
Location:
branches/3136_Structural_GP
Files:
6 edited

Legend:

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

    r18068 r18071  
    4949    }
    5050
     51    public ISymbolicDataAnalysisExpressionTreeInterpreter Interpreter { get; } = new SymbolicDataAnalysisExpressionTreeInterpreter();
     52
    5153    IParameter IDataAnalysisProblem.ProblemDataParameter => ProblemDataParameter;
    5254    IDataAnalysisProblemData IDataAnalysisProblem.ProblemData => ProblemData;
     
    110112      }
    111113
    112       if (results.TryGetValue("Best Tree", out IResult result))
    113         result.Value = BuildTree(individuals[bestIdx]);
    114       else
    115         results.Add(new Result("Best Tree", BuildTree(individuals[bestIdx])));
     114      if (results.TryGetValue("Best Tree", out IResult result)) {
     115        var tree = BuildTree(individuals[bestIdx]);
     116        AdjustLinearScalingParams(tree, Interpreter);
     117        result.Value = tree;
     118      }
     119      else {
     120        var tree = BuildTree(individuals[bestIdx]);
     121        AdjustLinearScalingParams(tree, Interpreter);
     122        results.Add(new Result("Best Tree", tree));
     123      }
     124       
    116125    }
    117126
    118127    public override double Evaluate(Individual individual, IRandom random) {
    119128      var tree = BuildTree(individual);
    120       var interpreter = new SymbolicDataAnalysisExpressionTreeInterpreter();
     129
     130      AdjustLinearScalingParams(tree, Interpreter);
    121131      var estimationInterval = ProblemData.VariableRanges.GetInterval(ProblemData.TargetVariable);
    122132      var quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(
    123         interpreter, tree,
     133        Interpreter, tree,
    124134        estimationInterval.LowerBound, estimationInterval.UpperBound,
    125135        ProblemData, ProblemData.TrainingIndices, false);
    126136     
    127137      return quality;
     138    }
     139
     140    private void AdjustLinearScalingParams(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter) {
     141      var offsetNode = tree.Root.GetSubtree(0).GetSubtree(0);
     142      var scalingNode = offsetNode.Subtrees.Where(x => !(x is ConstantTreeNode)).First();
     143
     144      var offsetConstantNode = (ConstantTreeNode)offsetNode.Subtrees.Where(x => x is ConstantTreeNode).First();
     145      var scalingConstantNode = (ConstantTreeNode)scalingNode.Subtrees.Where(x => x is ConstantTreeNode).First();
     146
     147      var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, ProblemData.Dataset, ProblemData.TrainingIndices);
     148      var targetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices);
     149
     150      OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out double a, out double b, out OnlineCalculatorError error);
     151      if(error == OnlineCalculatorError.None) {
     152        offsetConstantNode.Value = a;
     153        scalingConstantNode.Value = b;
     154      }
    128155    }
    129156
     
    136163          var subFunctionTreeNode = n as SubFunctionTreeNode;
    137164          var subFunctionTree = individual.SymbolicExpressionTree(subFunctionTreeNode.Name);
    138           var parent = n.Parent;
     165          //var parent = n.Parent;
    139166
    140167          // remove SubFunctionTreeNode
    141           parent.RemoveSubtree(parent.IndexOfSubtree(subFunctionTreeNode));
     168          //parent.RemoveSubtree(parent.IndexOfSubtree(subFunctionTreeNode));
    142169
    143170          // add new tree
    144171          var subTree = subFunctionTree.Root.GetSubtree(0)  // Start
    145172                                            .GetSubtree(0); // Offset
    146           parent.AddSubtree(subTree);
     173          //parent.AddSubtree(subTree);
     174          subFunctionTreeNode.AddSubtree(subTree);
    147175        }
    148176      }
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/StructureTemplate/StructureTemplateView.cs

    r18069 r18071  
    3333      if(visualTreeNode != null) {
    3434        var subFunctionTreeNode = visualTreeNode.Content as SubFunctionTreeNode;
    35         if(Content.SubFunctions.TryGetValue(subFunctionTreeNode.Name, out SubFunction subFunction))
     35        if(subFunctionTreeNode != null && Content.SubFunctions.TryGetValue(subFunctionTreeNode.Name, out SubFunction subFunction))
    3636          viewHost.Content = subFunction;
    3737      }
     
    5555      infoLabel.ForeColor = Color.DarkOrange;
    5656    }
    57 
    5857
    5958    private void PaintTree() {
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/OpCodes.cs

    r17963 r18071  
    7979    CubeRoot = 51,
    8080    Tanh = 52,
     81    SubFunction = 53
    8182  };
    8283  public static class OpCodes {
     
    134135    public const byte CubeRoot = (byte)OpCode.CubeRoot;
    135136    public const byte Tanh = (byte)OpCode.Tanh;
    136 
     137    public const byte SubFunction = (byte)OpCode.SubFunction;
    137138
    138139    private static Dictionary<Type, byte> symbolToOpcode = new Dictionary<Type, byte>() {
    139        { typeof(Addition), OpCodes.Add },
     140      { typeof(Addition), OpCodes.Add },
    140141      { typeof(Subtraction), OpCodes.Sub },
    141142      { typeof(Multiplication), OpCodes.Mul },
     
    189190      { typeof(AnalyticQuotient), OpCodes.AnalyticQuotient },
    190191      { typeof(Cube), OpCodes.Cube },
    191       { typeof(CubeRoot), OpCodes.CubeRoot }
     192      { typeof(CubeRoot), OpCodes.CubeRoot },
     193      { typeof(SubFunctionSymbol), OpCodes.SubFunction }
    192194    };
    193195
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeInterpreter.cs

    r17180 r18071  
    528528            }
    529529          }
     530        case OpCodes.SubFunction: {
     531            return Evaluate(dataset, ref row, state);
     532          }
    530533        default:
    531534          throw new NotSupportedException();
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/StructureTemplate/StructureTemplate.cs

    r18069 r18071  
    4646      set {
    4747        applyLinearScaling = value;
    48         //subFunctions = GetSubFunctions();
    4948        OnChanged();
    5049      }
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/StructureTemplate/SubFunction.cs

    r18069 r18071  
    5252    #region Constructors
    5353    public SubFunction() {
    54       Parameters.Add(new ValueParameter<ISymbolicDataAnalysisGrammar>(GrammarParameterName, new LinearScalingGrammar()));
    55       Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, new IntValue(10)));
    56       Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, new IntValue(30)));
     54      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisGrammar>(GrammarParameterName, new ArithmeticExpressionGrammar()));
     55      Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, new IntValue(8)));
     56      Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, new IntValue(20)));
    5757
    58       // TODO: separate events for each parameter
    5958      GrammarParameter.ValueChanged += OnParameterValueChanged;
    6059      MaximumSymbolicExpressionTreeDepthParameter.Value.ValueChanged += OnParameterValueChanged;
Note: See TracChangeset for help on using the changeset viewer.