Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/19/21 13:39:44 (3 years ago)
Author:
dpiringe
Message:

#3136

  • added linear scaling support for structure template parameter
File:
1 edited

Legend:

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

    r18068 r18069  
    1919      set {
    2020        template = value;
    21         tree = Parser.Parse(template);
    22         GetSubFunctions(Tree);
     21        Tree = Parser.Parse(template);
    2322        OnChanged();
    2423      }
     
    2625
    2726    [Storable]
    28     private ISymbolicExpressionTree tree;
    29     public ISymbolicExpressionTree Tree => tree;
     27    private ISymbolicExpressionTree treeWithoutLinearScaling;
     28    [Storable]
     29    private ISymbolicExpressionTree treeWithLinearScaling;
     30    public ISymbolicExpressionTree Tree {
     31      get => ApplyLinearScaling ? treeWithLinearScaling : treeWithoutLinearScaling;
     32      private set {
     33        treeWithLinearScaling = AddLinearScalingTerms(value);
     34        treeWithoutLinearScaling = value;
     35        SubFunctions = GetSubFunctions();
     36      }
     37    }
    3038
    3139    [Storable]
    32     public IReadOnlyDictionary<string, SubFunction> SubFunctions { get; private set; } = new Dictionary<string, SubFunction>();
     40    public IReadOnlyDictionary<string, SubFunction> SubFunctions { get; private set; }
     41
     42    [Storable]
     43    private bool applyLinearScaling = false;
     44    public bool ApplyLinearScaling {
     45      get => applyLinearScaling;
     46      set {
     47        applyLinearScaling = value;
     48        //subFunctions = GetSubFunctions();
     49        OnChanged();
     50      }
     51    }
    3352
    3453    protected InfixExpressionParser Parser { get; set; } = new InfixExpressionParser();
     
    5675    #endregion
    5776
    58     private void GetSubFunctions(ISymbolicExpressionTree tree) {
     77    private Dictionary<string, SubFunction> GetSubFunctions() {
    5978      var subFunctions = new Dictionary<string, SubFunction>();
    60       foreach (var node in tree.IterateNodesPrefix())
     79      foreach (var node in Tree.IterateNodesPrefix())
    6180        if (node is SubFunctionTreeNode subFunctionTreeNode) {
    6281          if (!subFunctionTreeNode.Arguments.Any())
     
    7796          }
    7897        }
    79       SubFunctions = subFunctions;
     98      return subFunctions;
     99    }
     100
     101    private ISymbolicExpressionTree AddLinearScalingTerms(ISymbolicExpressionTree tree) {
     102      var clonedTree = (ISymbolicExpressionTree)tree.Clone();
     103      var startNode = clonedTree.Root.Subtrees.First();
     104      var template = startNode.Subtrees.First();
     105
     106      var add = new Addition();
     107      var addNode = add.CreateTreeNode();
     108
     109      var mul = new Multiplication();
     110      var mulNode = mul.CreateTreeNode();
     111
     112      var c1 = new Constant();
     113      var c1Node = (ConstantTreeNode)c1.CreateTreeNode();
     114      c1Node.Value = 0.0;
     115      var c2 = new Constant();
     116      var c2Node = (ConstantTreeNode)c2.CreateTreeNode();
     117      c2Node.Value = 1.0;
     118     
     119      addNode.AddSubtree(c1Node);
     120      addNode.AddSubtree(mulNode);
     121      mulNode.AddSubtree(c2Node);
     122       
     123      startNode.RemoveSubtree(0);
     124      startNode.AddSubtree(addNode);
     125      mulNode.AddSubtree(template);
     126      return clonedTree;
    80127    }
    81128
Note: See TracChangeset for help on using the changeset viewer.