Changeset 18191


Ignore:
Timestamp:
01/13/22 14:01:39 (8 days ago)
Author:
mkommend
Message:

#3136: Extracted linear scaling functionality in a dedicated helper class.

Location:
branches/3136_Structural_GP
Files:
1 added
3 edited

Legend:

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

    r18190 r18191  
    270270
    271271      // NMSEConstraintsEvaluator sets linear scaling terms itself
    272       if (StructureTemplate.ApplyLinearScaling && !(TreeEvaluator is NMSESingleObjectiveConstraintsEvaluator)) {
    273         AdjustLinearScalingParams(ProblemData, tree, Interpreter);
     272      if (ApplyLinearScaling && !(TreeEvaluator is NMSESingleObjectiveConstraintsEvaluator)) {
     273        LinearScaling.AdjustLinearScalingParams(ProblemData, tree, Interpreter);
    274274      }
    275275
     
    300300    }
    301301
    302     private static void AdjustLinearScalingParams(IRegressionProblemData problemData, ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter) {
    303       var offsetNode = tree.Root.GetSubtree(0).GetSubtree(0);
    304       var scalingNode = offsetNode.Subtrees.Where(x => !(x is NumberTreeNode)).First();
    305 
    306       var offsetNumberNode = (NumberTreeNode)offsetNode.Subtrees.Where(x => x is NumberTreeNode).First();
    307       var scalingNumberNode = (NumberTreeNode)scalingNode.Subtrees.Where(x => x is NumberTreeNode).First();
    308 
    309       var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, problemData.TrainingIndices);
    310       var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices);
    311 
    312       OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out double a, out double b, out OnlineCalculatorError error);
    313       if (error == OnlineCalculatorError.None) {
    314         offsetNumberNode.Value = a;
    315         scalingNumberNode.Value = b;
    316       }
    317     }
    318 
    319 
    320 
    321 
    322 
    323 
    324302    public void Load(IRegressionProblemData data) {
    325303      ProblemData = data;
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj

    r18149 r18191  
    188188    <Compile Include="Interpreter\SymbolicDataAnalysisExpressionTreeNativeInterpreter.cs" />
    189189    <Compile Include="IntervalUtil.cs" />
     190    <Compile Include="LinearScaling.cs" />
    190191    <Compile Include="Selectors\DiversitySelector.cs" />
    191192    <Compile Include="StructureTemplate\StructureTemplate.cs" />
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/StructureTemplate/StructureTemplate.cs

    r18190 r18191  
    4444        var parsedTree = Parser.Parse(template);
    4545        if (applyLinearScaling)
    46           parsedTree = AddLinearScalingTerms(parsedTree);
     46          parsedTree = LinearScaling.AddLinearScalingTerms(parsedTree);
    4747        Tree = parsedTree;
    4848        OnChanged();
     
    8383
    8484        applyLinearScaling = value;
    85         if (applyLinearScaling) Tree = AddLinearScalingTerms(Tree);
    86         else Tree = RemoveLinearScalingTerms(Tree);
     85        if (applyLinearScaling) LinearScaling.AddLinearScalingTerms(Tree);
     86        else LinearScaling.RemoveLinearScalingTerms(Tree);
    8787
    8888        OnChanged();
     
    120120    private void AfterDeserialization() {
    121121      if (Tree == null && _oldTree != null) {
    122         if (ApplyLinearScaling) _oldTree = AddLinearScalingTerms(_oldTree);
     122        if (ApplyLinearScaling) _oldTree = LinearScaling.AddLinearScalingTerms(_oldTree);
    123123        Tree = _oldTree;
    124124        _oldTree = null;
     
    172172      }
    173173    }
    174 
    175     private static ISymbolicExpressionTree AddLinearScalingTerms(ISymbolicExpressionTree tree) {
    176       var clonedTree = (ISymbolicExpressionTree)tree.Clone();
    177       var startNode = clonedTree.Root.Subtrees.First();
    178       var template = startNode.Subtrees.First();
    179 
    180       var addNode = new Addition().CreateTreeNode();
    181       var mulNode = new Multiplication().CreateTreeNode();
    182       var offsetNode = new NumberTreeNode(0.0);
    183       var scaleNode = new NumberTreeNode(1.0);
    184 
    185       addNode.AddSubtree(offsetNode);
    186       addNode.AddSubtree(mulNode);
    187       mulNode.AddSubtree(scaleNode);
    188 
    189       startNode.RemoveSubtree(0);
    190       startNode.AddSubtree(addNode);
    191       mulNode.AddSubtree(template);
    192       return clonedTree;
    193     }
    194 
    195     private static ISymbolicExpressionTree RemoveLinearScalingTerms(ISymbolicExpressionTree tree) {
    196       var clonedTree = (ISymbolicExpressionTree)tree.Clone();
    197       var startNode = clonedTree.Root.Subtrees.First();
    198 
    199       //check for scaling terms
    200       var addNode = startNode.GetSubtree(0);
    201       var offsetNode = addNode.GetSubtree(0);
    202       var mulNode = addNode.GetSubtree(1);
    203       var scaleNode = mulNode.GetSubtree(0);
    204       var templateNode = mulNode.GetSubtree(1);
    205 
    206       var error = false;
    207       if (addNode.Symbol is not Addition) error = true;
    208       if (mulNode.Symbol is not Multiplication) error = true;
    209       if (offsetNode is not NumberTreeNode offset || offset.Value != 0.0) error = true;
    210       if (scaleNode is not NumberTreeNode scale || scale.Value != 1.0) error = true;
    211       if (error) throw new ArgumentException("Scaling terms cannot be found.");
    212 
    213       startNode.RemoveSubtree(0);
    214       startNode.AddSubtree(templateNode);
    215 
    216       return clonedTree;
    217     }
    218 
    219174    private void OnSubFunctionChanged(object sender, EventArgs e) => OnChanged();
    220175  }
Note: See TracChangeset for help on using the changeset viewer.