Ignore:
Timestamp:
01/13/22 14:01:39 (4 months ago)
Author:
mkommend
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.