Changeset 14949


Ignore:
Timestamp:
05/09/17 19:54:31 (3 months ago)
Author:
gkronber
Message:

#2697: made TreeSimplifier static

Location:
trunk/sources
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/InteractiveSymbolicDataAnalysisSolutionSimplifierView.cs

    r14843 r14949  
    266266
    267267    private void btnSimplify_Click(object sender, EventArgs e) {
    268       var simplifier = new TreeSimplifier();
    269       var simplifiedExpressionTree = simplifier.Simplify(Content.Model.SymbolicExpressionTree);
     268      var simplifiedExpressionTree = TreeSimplifier.Simplify(Content.Model.SymbolicExpressionTree);
    270269      UpdateModel(simplifiedExpressionTree);
    271270    }
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/TreeSimplifier.cs

    r14938 r14949  
    3333  /// </summary>
    3434  public class TreeSimplifier {
    35     private readonly Addition addSymbol = new Addition();
    36     private readonly Multiplication mulSymbol = new Multiplication();
    37     private readonly Division divSymbol = new Division();
    38     private readonly Constant constSymbol = new Constant();
    39     private readonly Logarithm logSymbol = new Logarithm();
    40     private readonly Exponential expSymbol = new Exponential();
    41     private readonly Root rootSymbol = new Root();
    42     private readonly Square sqrSymbol = new Square();
    43     private readonly SquareRoot sqrtSymbol = new SquareRoot();
    44     private readonly Power powSymbol = new Power();
    45     private readonly Sine sineSymbol = new Sine();
    46     private readonly Cosine cosineSymbol = new Cosine();
    47     private readonly Tangent tanSymbol = new Tangent();
    48     private readonly IfThenElse ifThenElseSymbol = new IfThenElse();
    49     private readonly And andSymbol = new And();
    50     private readonly Or orSymbol = new Or();
    51     private readonly Not notSymbol = new Not();
    52     private readonly GreaterThan gtSymbol = new GreaterThan();
    53     private readonly LessThan ltSymbol = new LessThan();
    54     private readonly Integral integralSymbol = new Integral();
    55     private readonly LaggedVariable laggedVariableSymbol = new LaggedVariable();
    56     private readonly TimeLag timeLagSymbol = new TimeLag();
    57 
    58     public ISymbolicExpressionTree Simplify(ISymbolicExpressionTree originalTree) {
     35    private static readonly Addition addSymbol = new Addition();
     36    private static readonly Multiplication mulSymbol = new Multiplication();
     37    private static readonly Division divSymbol = new Division();
     38    private static readonly Constant constSymbol = new Constant();
     39    private static readonly Logarithm logSymbol = new Logarithm();
     40    private static readonly Exponential expSymbol = new Exponential();
     41    private static readonly Root rootSymbol = new Root();
     42    private static readonly Square sqrSymbol = new Square();
     43    private static readonly SquareRoot sqrtSymbol = new SquareRoot();
     44    private static readonly Power powSymbol = new Power();
     45    private static readonly Sine sineSymbol = new Sine();
     46    private static readonly Cosine cosineSymbol = new Cosine();
     47    private static readonly Tangent tanSymbol = new Tangent();
     48    private static readonly IfThenElse ifThenElseSymbol = new IfThenElse();
     49    private static readonly And andSymbol = new And();
     50    private static readonly Or orSymbol = new Or();
     51    private static readonly Not notSymbol = new Not();
     52    private static readonly GreaterThan gtSymbol = new GreaterThan();
     53    private static readonly LessThan ltSymbol = new LessThan();
     54    private static readonly Integral integralSymbol = new Integral();
     55    private static readonly LaggedVariable laggedVariableSymbol = new LaggedVariable();
     56    private static readonly TimeLag timeLagSymbol = new TimeLag();
     57
     58    [Obsolete("Use static method TreeSimplifier.Simplify instead")]
     59    public TreeSimplifier() { }
     60
     61    public static ISymbolicExpressionTree Simplify(ISymbolicExpressionTree originalTree) {
    5962      var clone = (ISymbolicExpressionTreeNode)originalTree.Root.Clone();
    6063      // macro expand (initially no argument trees)
     
    6669      // check that each node is only referenced once
    6770      var nodes = rootNode.IterateNodesPrefix().ToArray();
    68       foreach(var n in nodes) if(nodes.Count(ni => ni == n) > 1) throw new InvalidOperationException();
     71      foreach (var n in nodes) if (nodes.Count(ni => ni == n) > 1) throw new InvalidOperationException();
    6972#endif
    7073      return new SymbolicExpressionTree(rootNode);
     
    7275
    7376    // the argumentTrees list contains already expanded trees used as arguments for invocations
    74     private ISymbolicExpressionTreeNode MacroExpand(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode node,
     77    private static ISymbolicExpressionTreeNode MacroExpand(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode node,
    7578      IList<ISymbolicExpressionTreeNode> argumentTrees) {
    7679      List<ISymbolicExpressionTreeNode> subtrees = new List<ISymbolicExpressionTreeNode>(node.Subtrees);
     
    97100    }
    98101
    99     private ISymbolicExpressionTreeNode FindFunctionDefinition(ISymbolicExpressionTreeNode root, string functionName) {
     102    private static ISymbolicExpressionTreeNode FindFunctionDefinition(ISymbolicExpressionTreeNode root, string functionName) {
    100103      foreach (var subtree in root.Subtrees.OfType<DefunTreeNode>()) {
    101104        if (subtree.FunctionName == functionName) return subtree.GetSubtree(0);
     
    108111
    109112    // arithmetic
    110     private bool IsDivision(ISymbolicExpressionTreeNode node) {
     113    private static bool IsDivision(ISymbolicExpressionTreeNode node) {
    111114      return node.Symbol is Division;
    112115    }
    113116
    114     private bool IsMultiplication(ISymbolicExpressionTreeNode node) {
     117    private static bool IsMultiplication(ISymbolicExpressionTreeNode node) {
    115118      return node.Symbol is Multiplication;
    116119    }
    117120
    118     private bool IsSubtraction(ISymbolicExpressionTreeNode node) {
     121    private static bool IsSubtraction(ISymbolicExpressionTreeNode node) {
    119122      return node.Symbol is Subtraction;
    120123    }
    121124
    122     private bool IsAddition(ISymbolicExpressionTreeNode node) {
     125    private static bool IsAddition(ISymbolicExpressionTreeNode node) {
    123126      return node.Symbol is Addition;
    124127    }
    125128
    126     private bool IsAverage(ISymbolicExpressionTreeNode node) {
     129    private static bool IsAverage(ISymbolicExpressionTreeNode node) {
    127130      return node.Symbol is Average;
    128131    }
    129132
    130133    // exponential
    131     private bool IsLog(ISymbolicExpressionTreeNode node) {
     134    private static bool IsLog(ISymbolicExpressionTreeNode node) {
    132135      return node.Symbol is Logarithm;
    133136    }
    134137
    135     private bool IsExp(ISymbolicExpressionTreeNode node) {
     138    private static bool IsExp(ISymbolicExpressionTreeNode node) {
    136139      return node.Symbol is Exponential;
    137140    }
    138141
    139     private bool IsRoot(ISymbolicExpressionTreeNode node) {
     142    private static bool IsRoot(ISymbolicExpressionTreeNode node) {
    140143      return node.Symbol is Root;
    141144    }
    142145
    143     private bool IsSquare(ISymbolicExpressionTreeNode node) {
     146    private static bool IsSquare(ISymbolicExpressionTreeNode node) {
    144147      return node.Symbol is Square;
    145148    }
    146149
    147     private bool IsSquareRoot(ISymbolicExpressionTreeNode node) {
     150    private static bool IsSquareRoot(ISymbolicExpressionTreeNode node) {
    148151      return node.Symbol is SquareRoot;
    149152    }
    150153
    151     private bool IsPower(ISymbolicExpressionTreeNode node) {
     154    private static bool IsPower(ISymbolicExpressionTreeNode node) {
    152155      return node.Symbol is Power;
    153156    }
    154157
    155158    // trigonometric
    156     private bool IsSine(ISymbolicExpressionTreeNode node) {
     159    private static bool IsSine(ISymbolicExpressionTreeNode node) {
    157160      return node.Symbol is Sine;
    158161    }
    159162
    160     private bool IsCosine(ISymbolicExpressionTreeNode node) {
     163    private static bool IsCosine(ISymbolicExpressionTreeNode node) {
    161164      return node.Symbol is Cosine;
    162165    }
    163166
    164     private bool IsTangent(ISymbolicExpressionTreeNode node) {
     167    private static bool IsTangent(ISymbolicExpressionTreeNode node) {
    165168      return node.Symbol is Tangent;
    166169    }
    167170
    168171    // boolean
    169     private bool IsIfThenElse(ISymbolicExpressionTreeNode node) {
     172    private static bool IsIfThenElse(ISymbolicExpressionTreeNode node) {
    170173      return node.Symbol is IfThenElse;
    171174    }
    172175
    173     private bool IsAnd(ISymbolicExpressionTreeNode node) {
     176    private static bool IsAnd(ISymbolicExpressionTreeNode node) {
    174177      return node.Symbol is And;
    175178    }
    176179
    177     private bool IsOr(ISymbolicExpressionTreeNode node) {
     180    private static bool IsOr(ISymbolicExpressionTreeNode node) {
    178181      return node.Symbol is Or;
    179182    }
    180183
    181     private bool IsNot(ISymbolicExpressionTreeNode node) {
     184    private static bool IsNot(ISymbolicExpressionTreeNode node) {
    182185      return node.Symbol is Not;
    183186    }
    184187
    185188    // comparison
    186     private bool IsGreaterThan(ISymbolicExpressionTreeNode node) {
     189    private static bool IsGreaterThan(ISymbolicExpressionTreeNode node) {
    187190      return node.Symbol is GreaterThan;
    188191    }
    189192
    190     private bool IsLessThan(ISymbolicExpressionTreeNode node) {
     193    private static bool IsLessThan(ISymbolicExpressionTreeNode node) {
    191194      return node.Symbol is LessThan;
    192195    }
    193196
    194     private bool IsBoolean(ISymbolicExpressionTreeNode node) {
     197    private static bool IsBoolean(ISymbolicExpressionTreeNode node) {
    195198      return
    196199        node.Symbol is GreaterThan ||
     
    201204
    202205    // terminals
    203     private bool IsVariable(ISymbolicExpressionTreeNode node) {
     206    private static bool IsVariable(ISymbolicExpressionTreeNode node) {
    204207      return node.Symbol is Variable;
    205208    }
    206209
    207     private bool IsVariableBase(ISymbolicExpressionTreeNode node) {
     210    private static bool IsVariableBase(ISymbolicExpressionTreeNode node) {
    208211      return node is VariableTreeNodeBase;
    209212    }
    210213
    211     private bool IsFactor(ISymbolicExpressionTreeNode node) {
     214    private static bool IsFactor(ISymbolicExpressionTreeNode node) {
    212215      return node is FactorVariableTreeNode;
    213216    }
    214217
    215     private bool IsBinFactor(ISymbolicExpressionTreeNode node) {
     218    private static bool IsBinFactor(ISymbolicExpressionTreeNode node) {
    216219      return node is BinaryFactorVariableTreeNode;
    217220    }
    218221
    219     private bool IsConstant(ISymbolicExpressionTreeNode node) {
     222    private static bool IsConstant(ISymbolicExpressionTreeNode node) {
    220223      return node.Symbol is Constant;
    221224    }
    222225
    223226    // dynamic
    224     private bool IsTimeLag(ISymbolicExpressionTreeNode node) {
     227    private static bool IsTimeLag(ISymbolicExpressionTreeNode node) {
    225228      return node.Symbol is TimeLag;
    226229    }
    227230
    228     private bool IsIntegral(ISymbolicExpressionTreeNode node) {
     231    private static bool IsIntegral(ISymbolicExpressionTreeNode node) {
    229232      return node.Symbol is Integral;
    230233    }
     
    237240    /// <param name="original"></param>
    238241    /// <returns></returns>
    239     public ISymbolicExpressionTreeNode GetSimplifiedTree(ISymbolicExpressionTreeNode original) {
     242    public static ISymbolicExpressionTreeNode GetSimplifiedTree(ISymbolicExpressionTreeNode original) {
    240243      if (IsConstant(original) || IsVariableBase(original)) {
    241244        return (ISymbolicExpressionTreeNode)original.Clone();
     
    291294    #region specific simplification routines
    292295
    293     private ISymbolicExpressionTreeNode SimplifyAny(ISymbolicExpressionTreeNode original) {
     296    private static ISymbolicExpressionTreeNode SimplifyAny(ISymbolicExpressionTreeNode original) {
    294297      // can't simplify this function but simplify all subtrees
    295298      List<ISymbolicExpressionTreeNode> subtrees = new List<ISymbolicExpressionTreeNode>(original.Subtrees);
     
    310313    }
    311314
    312     private ISymbolicExpressionTreeNode SimplifyConstantExpression(ISymbolicExpressionTreeNode original) {
     315    private static ISymbolicExpressionTreeNode SimplifyConstantExpression(ISymbolicExpressionTreeNode original) {
    313316      // not yet implemented
    314317      return original;
    315318    }
    316319
    317     private ISymbolicExpressionTreeNode SimplifyAverage(ISymbolicExpressionTreeNode original) {
     320    private static ISymbolicExpressionTreeNode SimplifyAverage(ISymbolicExpressionTreeNode original) {
    318321      if (original.Subtrees.Count() == 1) {
    319322        return GetSimplifiedTree(original.GetSubtree(0));
     
    328331    }
    329332
    330     private ISymbolicExpressionTreeNode SimplifyDivision(ISymbolicExpressionTreeNode original) {
     333    private static ISymbolicExpressionTreeNode SimplifyDivision(ISymbolicExpressionTreeNode original) {
    331334      if (original.Subtrees.Count() == 1) {
    332335        return Invert(GetSimplifiedTree(original.GetSubtree(0)));
     
    343346    }
    344347
    345     private ISymbolicExpressionTreeNode SimplifyMultiplication(ISymbolicExpressionTreeNode original) {
     348    private static ISymbolicExpressionTreeNode SimplifyMultiplication(ISymbolicExpressionTreeNode original) {
    346349      if (original.Subtrees.Count() == 1) {
    347350        return GetSimplifiedTree(original.GetSubtree(0));
     
    353356    }
    354357
    355     private ISymbolicExpressionTreeNode SimplifySubtraction(ISymbolicExpressionTreeNode original) {
     358    private static ISymbolicExpressionTreeNode SimplifySubtraction(ISymbolicExpressionTreeNode original) {
    356359      if (original.Subtrees.Count() == 1) {
    357360        return Negate(GetSimplifiedTree(original.GetSubtree(0)));
     
    365368    }
    366369
    367     private ISymbolicExpressionTreeNode SimplifyAddition(ISymbolicExpressionTreeNode original) {
     370    private static ISymbolicExpressionTreeNode SimplifyAddition(ISymbolicExpressionTreeNode original) {
    368371      if (original.Subtrees.Count() == 1) {
    369372        return GetSimplifiedTree(original.GetSubtree(0));
     
    377380    }
    378381
    379     private ISymbolicExpressionTreeNode SimplifyNot(ISymbolicExpressionTreeNode original) {
     382    private static ISymbolicExpressionTreeNode SimplifyNot(ISymbolicExpressionTreeNode original) {
    380383      return MakeNot(GetSimplifiedTree(original.GetSubtree(0)));
    381384    }
    382385
    383     private ISymbolicExpressionTreeNode SimplifyOr(ISymbolicExpressionTreeNode original) {
     386    private static ISymbolicExpressionTreeNode SimplifyOr(ISymbolicExpressionTreeNode original) {
    384387      return original.Subtrees
    385388        .Select(GetSimplifiedTree)
     
    387390    }
    388391
    389     private ISymbolicExpressionTreeNode SimplifyAnd(ISymbolicExpressionTreeNode original) {
     392    private static ISymbolicExpressionTreeNode SimplifyAnd(ISymbolicExpressionTreeNode original) {
    390393      return original.Subtrees
    391394        .Select(GetSimplifiedTree)
     
    393396    }
    394397
    395     private ISymbolicExpressionTreeNode SimplifyLessThan(ISymbolicExpressionTreeNode original) {
     398    private static ISymbolicExpressionTreeNode SimplifyLessThan(ISymbolicExpressionTreeNode original) {
    396399      return MakeLessThan(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1)));
    397400    }
    398401
    399     private ISymbolicExpressionTreeNode SimplifyGreaterThan(ISymbolicExpressionTreeNode original) {
     402    private static ISymbolicExpressionTreeNode SimplifyGreaterThan(ISymbolicExpressionTreeNode original) {
    400403      return MakeGreaterThan(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1)));
    401404    }
    402405
    403     private ISymbolicExpressionTreeNode SimplifyIfThenElse(ISymbolicExpressionTreeNode original) {
     406    private static ISymbolicExpressionTreeNode SimplifyIfThenElse(ISymbolicExpressionTreeNode original) {
    404407      return MakeIfThenElse(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1)),
    405408        GetSimplifiedTree(original.GetSubtree(2)));
    406409    }
    407410
    408     private ISymbolicExpressionTreeNode SimplifyTangent(ISymbolicExpressionTreeNode original) {
     411    private static ISymbolicExpressionTreeNode SimplifyTangent(ISymbolicExpressionTreeNode original) {
    409412      return MakeTangent(GetSimplifiedTree(original.GetSubtree(0)));
    410413    }
    411414
    412     private ISymbolicExpressionTreeNode SimplifyCosine(ISymbolicExpressionTreeNode original) {
     415    private static ISymbolicExpressionTreeNode SimplifyCosine(ISymbolicExpressionTreeNode original) {
    413416      return MakeCosine(GetSimplifiedTree(original.GetSubtree(0)));
    414417    }
    415418
    416     private ISymbolicExpressionTreeNode SimplifySine(ISymbolicExpressionTreeNode original) {
     419    private static ISymbolicExpressionTreeNode SimplifySine(ISymbolicExpressionTreeNode original) {
    417420      return MakeSine(GetSimplifiedTree(original.GetSubtree(0)));
    418421    }
    419422
    420     private ISymbolicExpressionTreeNode SimplifyExp(ISymbolicExpressionTreeNode original) {
     423    private static ISymbolicExpressionTreeNode SimplifyExp(ISymbolicExpressionTreeNode original) {
    421424      return MakeExp(GetSimplifiedTree(original.GetSubtree(0)));
    422425    }
    423426
    424     private ISymbolicExpressionTreeNode SimplifySquare(ISymbolicExpressionTreeNode original) {
     427    private static ISymbolicExpressionTreeNode SimplifySquare(ISymbolicExpressionTreeNode original) {
    425428      return MakeSquare(GetSimplifiedTree(original.GetSubtree(0)));
    426429    }
    427430
    428     private ISymbolicExpressionTreeNode SimplifySquareRoot(ISymbolicExpressionTreeNode original) {
     431    private static ISymbolicExpressionTreeNode SimplifySquareRoot(ISymbolicExpressionTreeNode original) {
    429432      return MakeSquareRoot(GetSimplifiedTree(original.GetSubtree(0)));
    430433    }
    431434
    432     private ISymbolicExpressionTreeNode SimplifyLog(ISymbolicExpressionTreeNode original) {
     435    private static ISymbolicExpressionTreeNode SimplifyLog(ISymbolicExpressionTreeNode original) {
    433436      return MakeLog(GetSimplifiedTree(original.GetSubtree(0)));
    434437    }
    435438
    436     private ISymbolicExpressionTreeNode SimplifyRoot(ISymbolicExpressionTreeNode original) {
     439    private static ISymbolicExpressionTreeNode SimplifyRoot(ISymbolicExpressionTreeNode original) {
    437440      return MakeRoot(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1)));
    438441    }
    439442
    440     private ISymbolicExpressionTreeNode SimplifyPower(ISymbolicExpressionTreeNode original) {
     443    private static ISymbolicExpressionTreeNode SimplifyPower(ISymbolicExpressionTreeNode original) {
    441444      return MakePower(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1)));
    442445    }
    443446
    444     private ISymbolicExpressionTreeNode SimplifyTimeLag(ISymbolicExpressionTreeNode original) {
     447    private static ISymbolicExpressionTreeNode SimplifyTimeLag(ISymbolicExpressionTreeNode original) {
    445448      var laggedTreeNode = original as ILaggedTreeNode;
    446449      var simplifiedSubtree = GetSimplifiedTree(original.GetSubtree(0));
     
    452455    }
    453456
    454     private ISymbolicExpressionTreeNode SimplifyIntegral(ISymbolicExpressionTreeNode original) {
     457    private static ISymbolicExpressionTreeNode SimplifyIntegral(ISymbolicExpressionTreeNode original) {
    455458      var laggedTreeNode = original as ILaggedTreeNode;
    456459      var simplifiedSubtree = GetSimplifiedTree(original.GetSubtree(0));
     
    466469    #region low level tree restructuring
    467470
    468     private ISymbolicExpressionTreeNode MakeTimeLag(ISymbolicExpressionTreeNode subtree, int lag) {
     471    private static ISymbolicExpressionTreeNode MakeTimeLag(ISymbolicExpressionTreeNode subtree, int lag) {
    469472      if (lag == 0) return subtree;
    470473      if (IsConstant(subtree)) return subtree;
     
    475478    }
    476479
    477     private ISymbolicExpressionTreeNode MakeIntegral(ISymbolicExpressionTreeNode subtree, int lag) {
     480    private static ISymbolicExpressionTreeNode MakeIntegral(ISymbolicExpressionTreeNode subtree, int lag) {
    478481      if (lag == 0) return subtree;
    479482      else if (lag == -1 || lag == 1) {
     
    487490    }
    488491
    489     private ISymbolicExpressionTreeNode MakeNot(ISymbolicExpressionTreeNode t) {
     492    private static ISymbolicExpressionTreeNode MakeNot(ISymbolicExpressionTreeNode t) {
    490493      if (IsConstant(t)) {
    491494        var constNode = t as ConstantTreeNode;
     
    508511    }
    509512
    510     private ISymbolicExpressionTreeNode MakeOr(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
     513    private static ISymbolicExpressionTreeNode MakeOr(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
    511514      if (IsConstant(a) && IsConstant(b)) {
    512515        var constA = a as ConstantTreeNode;
     
    538541    }
    539542
    540     private ISymbolicExpressionTreeNode MakeAnd(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
     543    private static ISymbolicExpressionTreeNode MakeAnd(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
    541544      if (IsConstant(a) && IsConstant(b)) {
    542545        var constA = a as ConstantTreeNode;
     
    568571    }
    569572
    570     private ISymbolicExpressionTreeNode MakeLessThan(ISymbolicExpressionTreeNode leftSide,
     573    private static ISymbolicExpressionTreeNode MakeLessThan(ISymbolicExpressionTreeNode leftSide,
    571574      ISymbolicExpressionTreeNode rightSide) {
    572575      if (IsConstant(leftSide) && IsConstant(rightSide)) {
     
    583586    }
    584587
    585     private ISymbolicExpressionTreeNode MakeGreaterThan(ISymbolicExpressionTreeNode leftSide,
     588    private static ISymbolicExpressionTreeNode MakeGreaterThan(ISymbolicExpressionTreeNode leftSide,
    586589      ISymbolicExpressionTreeNode rightSide) {
    587590      if (IsConstant(leftSide) && IsConstant(rightSide)) {
     
    598601    }
    599602
    600     private ISymbolicExpressionTreeNode MakeIfThenElse(ISymbolicExpressionTreeNode condition,
     603    private static ISymbolicExpressionTreeNode MakeIfThenElse(ISymbolicExpressionTreeNode condition,
    601604      ISymbolicExpressionTreeNode trueBranch, ISymbolicExpressionTreeNode falseBranch) {
    602605      if (IsConstant(condition)) {
     
    620623    }
    621624
    622     private ISymbolicExpressionTreeNode MakeSine(ISymbolicExpressionTreeNode node) {
     625    private static ISymbolicExpressionTreeNode MakeSine(ISymbolicExpressionTreeNode node) {
    623626      if (IsConstant(node)) {
    624627        var constT = node as ConstantTreeNode;
     
    637640    }
    638641
    639     private ISymbolicExpressionTreeNode MakeTangent(ISymbolicExpressionTreeNode node) {
     642    private static ISymbolicExpressionTreeNode MakeTangent(ISymbolicExpressionTreeNode node) {
    640643      if (IsConstant(node)) {
    641644        var constT = node as ConstantTreeNode;
     
    654657    }
    655658
    656     private ISymbolicExpressionTreeNode MakeCosine(ISymbolicExpressionTreeNode node) {
     659    private static ISymbolicExpressionTreeNode MakeCosine(ISymbolicExpressionTreeNode node) {
    657660      if (IsConstant(node)) {
    658661        var constT = node as ConstantTreeNode;
     
    673676    }
    674677
    675     private ISymbolicExpressionTreeNode MakeExp(ISymbolicExpressionTreeNode node) {
     678    private static ISymbolicExpressionTreeNode MakeExp(ISymbolicExpressionTreeNode node) {
    676679      if (IsConstant(node)) {
    677680        var constT = node as ConstantTreeNode;
     
    697700      }
    698701    }
    699     private ISymbolicExpressionTreeNode MakeLog(ISymbolicExpressionTreeNode node) {
     702    private static ISymbolicExpressionTreeNode MakeLog(ISymbolicExpressionTreeNode node) {
    700703      if (IsConstant(node)) {
    701704        var constT = node as ConstantTreeNode;
     
    715718    }
    716719
    717     private ISymbolicExpressionTreeNode MakeSquare(ISymbolicExpressionTreeNode node) {
     720    private static ISymbolicExpressionTreeNode MakeSquare(ISymbolicExpressionTreeNode node) {
    718721      if (IsConstant(node)) {
    719722        var constT = node as ConstantTreeNode;
     
    734737    }
    735738
    736     private ISymbolicExpressionTreeNode MakeSquareRoot(ISymbolicExpressionTreeNode node) {
     739    private static ISymbolicExpressionTreeNode MakeSquareRoot(ISymbolicExpressionTreeNode node) {
    737740      if (IsConstant(node)) {
    738741        var constT = node as ConstantTreeNode;
     
    753756    }
    754757
    755     private ISymbolicExpressionTreeNode MakeRoot(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
     758    private static ISymbolicExpressionTreeNode MakeRoot(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
    756759      if (IsConstant(a) && IsConstant(b)) {
    757760        var constA = a as ConstantTreeNode;
     
    808811
    809812
    810     private ISymbolicExpressionTreeNode MakePower(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
     813    private static ISymbolicExpressionTreeNode MakePower(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
    811814      if (IsConstant(a) && IsConstant(b)) {
    812815        var constA = a as ConstantTreeNode;
     
    863866
    864867    // MakeFraction, MakeProduct and MakeSum take two already simplified trees and create a new simplified tree
    865     private ISymbolicExpressionTreeNode MakeFraction(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
     868    private static ISymbolicExpressionTreeNode MakeFraction(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
    866869      if (IsConstant(a) && IsConstant(b)) {
    867870        // fold constants
     
    932935    }
    933936
    934     private ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
     937    private static ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
    935938      if (IsConstant(a) && IsConstant(b)) {
    936939        // fold constants
     
    10271030
    10281031    // makes sure variable symbols in sums are combined
    1029     private void MergeVariablesInSum(ISymbolicExpressionTreeNode sum) {
     1032    private static void MergeVariablesInSum(ISymbolicExpressionTreeNode sum) {
    10301033      var subtrees = new List<ISymbolicExpressionTreeNode>(sum.Subtrees);
    10311034      while (sum.Subtrees.Any()) sum.RemoveSubtree(0);
     
    10661069
    10671070    // nodes referencing variables can be grouped if they have
    1068     private string GroupId(IVariableTreeNode node) {
     1071    private static string GroupId(IVariableTreeNode node) {
    10691072      var binaryFactorNode = node as BinaryFactorVariableTreeNode;
    10701073      var factorNode = node as FactorVariableTreeNode;
     
    10851088
    10861089
    1087     private ISymbolicExpressionTreeNode MakeProduct(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
     1090    private static ISymbolicExpressionTreeNode MakeProduct(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
    10881091      if (IsConstant(a) && IsConstant(b)) {
    10891092        // fold constants
     
    11661169    #region helper functions
    11671170
    1168     private bool ContainsVariableCondition(ISymbolicExpressionTreeNode node) {
     1171    private static bool ContainsVariableCondition(ISymbolicExpressionTreeNode node) {
    11691172      if (node.Symbol is VariableCondition) return true;
    11701173      foreach (var subtree in node.Subtrees)
     
    11731176    }
    11741177
    1175     private ISymbolicExpressionTreeNode AddLagToDynamicNodes(ISymbolicExpressionTreeNode node, int lag) {
     1178    private static ISymbolicExpressionTreeNode AddLagToDynamicNodes(ISymbolicExpressionTreeNode node, int lag) {
    11761179      var laggedTreeNode = node as ILaggedTreeNode;
    11771180      var variableNode = node as VariableTreeNode;
     
    11951198    }
    11961199
    1197     private bool AreSameTypeAndVariable(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
     1200    private static bool AreSameTypeAndVariable(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
    11981201      return GroupId((IVariableTreeNode)a) == GroupId((IVariableTreeNode)b);
    11991202    }
    12001203
    12011204    // helper to combine the constant factors in products and to combine variables (powers of 2, 3...)
    1202     private void MergeVariablesAndConstantsInProduct(ISymbolicExpressionTreeNode prod) {
     1205    private static void MergeVariablesAndConstantsInProduct(ISymbolicExpressionTreeNode prod) {
    12031206      var subtrees = new List<ISymbolicExpressionTreeNode>(prod.Subtrees);
    12041207      while (prod.Subtrees.Any()) prod.RemoveSubtree(0);
     
    12641267    /// <param name="x"></param>
    12651268    /// <returns>-x</returns>
    1266     private ISymbolicExpressionTreeNode Negate(ISymbolicExpressionTreeNode x) {
     1269    private static ISymbolicExpressionTreeNode Negate(ISymbolicExpressionTreeNode x) {
    12671270      if (IsConstant(x)) {
    12681271        ((ConstantTreeNode)x).Value *= -1;
     
    13011304    /// <param name="x"></param>
    13021305    /// <returns></returns>
    1303     private ISymbolicExpressionTreeNode Invert(ISymbolicExpressionTreeNode x) {
     1306    private static ISymbolicExpressionTreeNode Invert(ISymbolicExpressionTreeNode x) {
    13041307      if (IsConstant(x)) {
    13051308        return MakeConstant(1.0 / ((ConstantTreeNode)x).Value);
     
    13151318    }
    13161319
    1317     private ISymbolicExpressionTreeNode MakeConstant(double value) {
     1320    private static ISymbolicExpressionTreeNode MakeConstant(double value) {
    13181321      ConstantTreeNode constantTreeNode = (ConstantTreeNode)(constSymbol.CreateTreeNode());
    13191322      constantTreeNode.Value = value;
     
    13211324    }
    13221325
    1323     private ISymbolicExpressionTreeNode MakeFactor(FactorVariable sy, string variableName, IEnumerable<double> weights) {
     1326    private static ISymbolicExpressionTreeNode MakeFactor(FactorVariable sy, string variableName, IEnumerable<double> weights) {
    13241327      var tree = (FactorVariableTreeNode)sy.CreateTreeNode();
    13251328      tree.VariableName = variableName;
     
    13271330      return tree;
    13281331    }
    1329     private ISymbolicExpressionTreeNode MakeBinFactor(BinaryFactorVariable sy, string variableName, string variableValue, double weight) {
     1332    private static ISymbolicExpressionTreeNode MakeBinFactor(BinaryFactorVariable sy, string variableName, string variableValue, double weight) {
    13301333      var tree = (BinaryFactorVariableTreeNode)sy.CreateTreeNode();
    13311334      tree.VariableName = variableName;
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeSimplificationOperator.cs

    r14843 r14949  
    3737    }
    3838
    39     private readonly TreeSimplifier simplifier = new TreeSimplifier();
    40 
    4139    [StorableConstructor]
    4240    protected SymbolicDataAnalysisExpressionTreeSimplificationOperator(bool deserializing) : base(deserializing) { }
     
    5351    public override IOperation Apply() {
    5452      var tree = SymbolicExpressionTreeParameter.ActualValue;
    55       var simplifiedTree = simplifier.Simplify(tree);
    56       simplifiedTree = simplifier.Simplify(simplifiedTree);
     53      var simplifiedTree = TreeSimplifier.Simplify(tree);
     54      simplifiedTree = TreeSimplifier.Simplify(simplifiedTree);
    5755      SymbolicExpressionTreeParameter.ActualValue = simplifiedTree;
    5856
  • trunk/sources/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/SymbolicDataAnalysisExpressionTreeSimplifierTest.cs

    r14845 r14949  
    3434    public void SimplifierAxiomsTest() {
    3535      SymbolicExpressionImporter importer = new SymbolicExpressionImporter();
    36       TreeSimplifier simplifier = new TreeSimplifier();
    3736      SymbolicExpressionTreeStringFormatter formatter = new SymbolicExpressionTreeStringFormatter();
    3837      #region single argument arithmetics
     
    233232
    234233    private void AssertEqualAfterSimplification(string original, string expected) {
    235       var simplifier = new TreeSimplifier();
    236234      var formatter = new SymbolicExpressionTreeStringFormatter();
    237235      var importer = new SymbolicExpressionImporter();
    238       var actualTree = simplifier.Simplify(importer.Import(original));
     236      var actualTree = TreeSimplifier.Simplify(importer.Import(original));
    239237      var expectedTree = importer.Import(expected);
    240238      Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
Note: See TracChangeset for help on using the changeset viewer.