Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/06/17 20:51:26 (7 years ago)
Author:
bburlacu
Message:

#1772: Merge trunk changes, add a couple of convenience parameters to the SchemaCreator.

Location:
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic
Files:
2 deleted
9 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic

  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/TreeSimplifier.cs

    r14843 r15351  
    3333  /// </summary>
    3434  public class TreeSimplifier {
    35     private Addition addSymbol = new Addition();
    36     private Multiplication mulSymbol = new Multiplication();
    37     private Division divSymbol = new Division();
    38     private Constant constSymbol = new Constant();
    39     private Variable varSymbol = new Variable();
    40     private Logarithm logSymbol = new Logarithm();
    41     private Exponential expSymbol = new Exponential();
    42     private Root rootSymbol = new Root();
    43     private Square sqrSymbol = new Square();
    44     private SquareRoot sqrtSymbol = new SquareRoot();
    45     private Power powSymbol = new Power();
    46     private Sine sineSymbol = new Sine();
    47     private Cosine cosineSymbol = new Cosine();
    48     private Tangent tanSymbol = new Tangent();
    49     private IfThenElse ifThenElseSymbol = new IfThenElse();
    50     private And andSymbol = new And();
    51     private Or orSymbol = new Or();
    52     private Not notSymbol = new Not();
    53     private GreaterThan gtSymbol = new GreaterThan();
    54     private LessThan ltSymbol = new LessThan();
    55     private Integral integralSymbol = new Integral();
    56     private LaggedVariable laggedVariableSymbol = new LaggedVariable();
    57     private TimeLag timeLagSymbol = new TimeLag();
    58 
    59     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) {
    6062      var clone = (ISymbolicExpressionTreeNode)originalTree.Root.Clone();
    6163      // macro expand (initially no argument trees)
     
    6769      // check that each node is only referenced once
    6870      var nodes = rootNode.IterateNodesPrefix().ToArray();
    69       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();
    7072#endif
    7173      return new SymbolicExpressionTree(rootNode);
     
    7375
    7476    // the argumentTrees list contains already expanded trees used as arguments for invocations
    75     private ISymbolicExpressionTreeNode MacroExpand(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode node,
     77    private static ISymbolicExpressionTreeNode MacroExpand(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode node,
    7678      IList<ISymbolicExpressionTreeNode> argumentTrees) {
    7779      List<ISymbolicExpressionTreeNode> subtrees = new List<ISymbolicExpressionTreeNode>(node.Subtrees);
     
    98100    }
    99101
    100     private ISymbolicExpressionTreeNode FindFunctionDefinition(ISymbolicExpressionTreeNode root, string functionName) {
     102    private static ISymbolicExpressionTreeNode FindFunctionDefinition(ISymbolicExpressionTreeNode root, string functionName) {
    101103      foreach (var subtree in root.Subtrees.OfType<DefunTreeNode>()) {
    102104        if (subtree.FunctionName == functionName) return subtree.GetSubtree(0);
     
    109111
    110112    // arithmetic
    111     private bool IsDivision(ISymbolicExpressionTreeNode node) {
     113    private static bool IsDivision(ISymbolicExpressionTreeNode node) {
    112114      return node.Symbol is Division;
    113115    }
    114116
    115     private bool IsMultiplication(ISymbolicExpressionTreeNode node) {
     117    private static bool IsMultiplication(ISymbolicExpressionTreeNode node) {
    116118      return node.Symbol is Multiplication;
    117119    }
    118120
    119     private bool IsSubtraction(ISymbolicExpressionTreeNode node) {
     121    private static bool IsSubtraction(ISymbolicExpressionTreeNode node) {
    120122      return node.Symbol is Subtraction;
    121123    }
    122124
    123     private bool IsAddition(ISymbolicExpressionTreeNode node) {
     125    private static bool IsAddition(ISymbolicExpressionTreeNode node) {
    124126      return node.Symbol is Addition;
    125127    }
    126128
    127     private bool IsAverage(ISymbolicExpressionTreeNode node) {
     129    private static bool IsAverage(ISymbolicExpressionTreeNode node) {
    128130      return node.Symbol is Average;
    129131    }
    130132
    131133    // exponential
    132     private bool IsLog(ISymbolicExpressionTreeNode node) {
     134    private static bool IsLog(ISymbolicExpressionTreeNode node) {
    133135      return node.Symbol is Logarithm;
    134136    }
    135137
    136     private bool IsExp(ISymbolicExpressionTreeNode node) {
     138    private static bool IsExp(ISymbolicExpressionTreeNode node) {
    137139      return node.Symbol is Exponential;
    138140    }
    139141
    140     private bool IsRoot(ISymbolicExpressionTreeNode node) {
     142    private static bool IsRoot(ISymbolicExpressionTreeNode node) {
    141143      return node.Symbol is Root;
    142144    }
    143145
    144     private bool IsSquare(ISymbolicExpressionTreeNode node) {
     146    private static bool IsSquare(ISymbolicExpressionTreeNode node) {
    145147      return node.Symbol is Square;
    146148    }
    147149
    148     private bool IsSquareRoot(ISymbolicExpressionTreeNode node) {
     150    private static bool IsSquareRoot(ISymbolicExpressionTreeNode node) {
    149151      return node.Symbol is SquareRoot;
    150152    }
    151153
    152     private bool IsPower(ISymbolicExpressionTreeNode node) {
     154    private static bool IsPower(ISymbolicExpressionTreeNode node) {
    153155      return node.Symbol is Power;
    154156    }
    155157
    156158    // trigonometric
    157     private bool IsSine(ISymbolicExpressionTreeNode node) {
     159    private static bool IsSine(ISymbolicExpressionTreeNode node) {
    158160      return node.Symbol is Sine;
    159161    }
    160162
    161     private bool IsCosine(ISymbolicExpressionTreeNode node) {
     163    private static bool IsCosine(ISymbolicExpressionTreeNode node) {
    162164      return node.Symbol is Cosine;
    163165    }
    164166
    165     private bool IsTangent(ISymbolicExpressionTreeNode node) {
     167    private static bool IsTangent(ISymbolicExpressionTreeNode node) {
    166168      return node.Symbol is Tangent;
    167169    }
    168170
    169171    // boolean
    170     private bool IsIfThenElse(ISymbolicExpressionTreeNode node) {
     172    private static bool IsIfThenElse(ISymbolicExpressionTreeNode node) {
    171173      return node.Symbol is IfThenElse;
    172174    }
    173175
    174     private bool IsAnd(ISymbolicExpressionTreeNode node) {
     176    private static bool IsAnd(ISymbolicExpressionTreeNode node) {
    175177      return node.Symbol is And;
    176178    }
    177179
    178     private bool IsOr(ISymbolicExpressionTreeNode node) {
     180    private static bool IsOr(ISymbolicExpressionTreeNode node) {
    179181      return node.Symbol is Or;
    180182    }
    181183
    182     private bool IsNot(ISymbolicExpressionTreeNode node) {
     184    private static bool IsNot(ISymbolicExpressionTreeNode node) {
    183185      return node.Symbol is Not;
    184186    }
    185187
    186188    // comparison
    187     private bool IsGreaterThan(ISymbolicExpressionTreeNode node) {
     189    private static bool IsGreaterThan(ISymbolicExpressionTreeNode node) {
    188190      return node.Symbol is GreaterThan;
    189191    }
    190192
    191     private bool IsLessThan(ISymbolicExpressionTreeNode node) {
     193    private static bool IsLessThan(ISymbolicExpressionTreeNode node) {
    192194      return node.Symbol is LessThan;
    193195    }
    194196
    195     private bool IsBoolean(ISymbolicExpressionTreeNode node) {
     197    private static bool IsBoolean(ISymbolicExpressionTreeNode node) {
    196198      return
    197199        node.Symbol is GreaterThan ||
     
    202204
    203205    // terminals
    204     private bool IsVariable(ISymbolicExpressionTreeNode node) {
     206    private static bool IsVariable(ISymbolicExpressionTreeNode node) {
    205207      return node.Symbol is Variable;
    206208    }
    207209
    208     private bool IsVariableBase(ISymbolicExpressionTreeNode node) {
     210    private static bool IsVariableBase(ISymbolicExpressionTreeNode node) {
    209211      return node is VariableTreeNodeBase;
    210212    }
    211213
    212     private bool IsFactor(ISymbolicExpressionTreeNode node) {
     214    private static bool IsFactor(ISymbolicExpressionTreeNode node) {
    213215      return node is FactorVariableTreeNode;
    214216    }
    215217
    216     private bool IsBinFactor(ISymbolicExpressionTreeNode node) {
     218    private static bool IsBinFactor(ISymbolicExpressionTreeNode node) {
    217219      return node is BinaryFactorVariableTreeNode;
    218220    }
    219221
    220     private bool IsConstant(ISymbolicExpressionTreeNode node) {
     222    private static bool IsConstant(ISymbolicExpressionTreeNode node) {
    221223      return node.Symbol is Constant;
    222224    }
    223225
    224226    // dynamic
    225     private bool IsTimeLag(ISymbolicExpressionTreeNode node) {
     227    private static bool IsTimeLag(ISymbolicExpressionTreeNode node) {
    226228      return node.Symbol is TimeLag;
    227229    }
    228230
    229     private bool IsIntegral(ISymbolicExpressionTreeNode node) {
     231    private static bool IsIntegral(ISymbolicExpressionTreeNode node) {
    230232      return node.Symbol is Integral;
    231233    }
     
    238240    /// <param name="original"></param>
    239241    /// <returns></returns>
    240     public ISymbolicExpressionTreeNode GetSimplifiedTree(ISymbolicExpressionTreeNode original) {
     242    public static ISymbolicExpressionTreeNode GetSimplifiedTree(ISymbolicExpressionTreeNode original) {
    241243      if (IsConstant(original) || IsVariableBase(original)) {
    242244        return (ISymbolicExpressionTreeNode)original.Clone();
     
    292294    #region specific simplification routines
    293295
    294     private ISymbolicExpressionTreeNode SimplifyAny(ISymbolicExpressionTreeNode original) {
     296    private static ISymbolicExpressionTreeNode SimplifyAny(ISymbolicExpressionTreeNode original) {
    295297      // can't simplify this function but simplify all subtrees
    296298      List<ISymbolicExpressionTreeNode> subtrees = new List<ISymbolicExpressionTreeNode>(original.Subtrees);
     
    311313    }
    312314
    313     private ISymbolicExpressionTreeNode SimplifyConstantExpression(ISymbolicExpressionTreeNode original) {
     315    private static ISymbolicExpressionTreeNode SimplifyConstantExpression(ISymbolicExpressionTreeNode original) {
    314316      // not yet implemented
    315317      return original;
    316318    }
    317319
    318     private ISymbolicExpressionTreeNode SimplifyAverage(ISymbolicExpressionTreeNode original) {
     320    private static ISymbolicExpressionTreeNode SimplifyAverage(ISymbolicExpressionTreeNode original) {
    319321      if (original.Subtrees.Count() == 1) {
    320322        return GetSimplifiedTree(original.GetSubtree(0));
     
    329331    }
    330332
    331     private ISymbolicExpressionTreeNode SimplifyDivision(ISymbolicExpressionTreeNode original) {
     333    private static ISymbolicExpressionTreeNode SimplifyDivision(ISymbolicExpressionTreeNode original) {
    332334      if (original.Subtrees.Count() == 1) {
    333335        return Invert(GetSimplifiedTree(original.GetSubtree(0)));
     
    344346    }
    345347
    346     private ISymbolicExpressionTreeNode SimplifyMultiplication(ISymbolicExpressionTreeNode original) {
     348    private static ISymbolicExpressionTreeNode SimplifyMultiplication(ISymbolicExpressionTreeNode original) {
    347349      if (original.Subtrees.Count() == 1) {
    348350        return GetSimplifiedTree(original.GetSubtree(0));
     
    354356    }
    355357
    356     private ISymbolicExpressionTreeNode SimplifySubtraction(ISymbolicExpressionTreeNode original) {
     358    private static ISymbolicExpressionTreeNode SimplifySubtraction(ISymbolicExpressionTreeNode original) {
    357359      if (original.Subtrees.Count() == 1) {
    358360        return Negate(GetSimplifiedTree(original.GetSubtree(0)));
     
    366368    }
    367369
    368     private ISymbolicExpressionTreeNode SimplifyAddition(ISymbolicExpressionTreeNode original) {
     370    private static ISymbolicExpressionTreeNode SimplifyAddition(ISymbolicExpressionTreeNode original) {
    369371      if (original.Subtrees.Count() == 1) {
    370372        return GetSimplifiedTree(original.GetSubtree(0));
     
    378380    }
    379381
    380     private ISymbolicExpressionTreeNode SimplifyNot(ISymbolicExpressionTreeNode original) {
     382    private static ISymbolicExpressionTreeNode SimplifyNot(ISymbolicExpressionTreeNode original) {
    381383      return MakeNot(GetSimplifiedTree(original.GetSubtree(0)));
    382384    }
    383385
    384     private ISymbolicExpressionTreeNode SimplifyOr(ISymbolicExpressionTreeNode original) {
     386    private static ISymbolicExpressionTreeNode SimplifyOr(ISymbolicExpressionTreeNode original) {
    385387      return original.Subtrees
    386388        .Select(GetSimplifiedTree)
     
    388390    }
    389391
    390     private ISymbolicExpressionTreeNode SimplifyAnd(ISymbolicExpressionTreeNode original) {
     392    private static ISymbolicExpressionTreeNode SimplifyAnd(ISymbolicExpressionTreeNode original) {
    391393      return original.Subtrees
    392394        .Select(GetSimplifiedTree)
     
    394396    }
    395397
    396     private ISymbolicExpressionTreeNode SimplifyLessThan(ISymbolicExpressionTreeNode original) {
     398    private static ISymbolicExpressionTreeNode SimplifyLessThan(ISymbolicExpressionTreeNode original) {
    397399      return MakeLessThan(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1)));
    398400    }
    399401
    400     private ISymbolicExpressionTreeNode SimplifyGreaterThan(ISymbolicExpressionTreeNode original) {
     402    private static ISymbolicExpressionTreeNode SimplifyGreaterThan(ISymbolicExpressionTreeNode original) {
    401403      return MakeGreaterThan(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1)));
    402404    }
    403405
    404     private ISymbolicExpressionTreeNode SimplifyIfThenElse(ISymbolicExpressionTreeNode original) {
     406    private static ISymbolicExpressionTreeNode SimplifyIfThenElse(ISymbolicExpressionTreeNode original) {
    405407      return MakeIfThenElse(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1)),
    406408        GetSimplifiedTree(original.GetSubtree(2)));
    407409    }
    408410
    409     private ISymbolicExpressionTreeNode SimplifyTangent(ISymbolicExpressionTreeNode original) {
     411    private static ISymbolicExpressionTreeNode SimplifyTangent(ISymbolicExpressionTreeNode original) {
    410412      return MakeTangent(GetSimplifiedTree(original.GetSubtree(0)));
    411413    }
    412414
    413     private ISymbolicExpressionTreeNode SimplifyCosine(ISymbolicExpressionTreeNode original) {
     415    private static ISymbolicExpressionTreeNode SimplifyCosine(ISymbolicExpressionTreeNode original) {
    414416      return MakeCosine(GetSimplifiedTree(original.GetSubtree(0)));
    415417    }
    416418
    417     private ISymbolicExpressionTreeNode SimplifySine(ISymbolicExpressionTreeNode original) {
     419    private static ISymbolicExpressionTreeNode SimplifySine(ISymbolicExpressionTreeNode original) {
    418420      return MakeSine(GetSimplifiedTree(original.GetSubtree(0)));
    419421    }
    420422
    421     private ISymbolicExpressionTreeNode SimplifyExp(ISymbolicExpressionTreeNode original) {
     423    private static ISymbolicExpressionTreeNode SimplifyExp(ISymbolicExpressionTreeNode original) {
    422424      return MakeExp(GetSimplifiedTree(original.GetSubtree(0)));
    423425    }
    424426
    425     private ISymbolicExpressionTreeNode SimplifySquare(ISymbolicExpressionTreeNode original) {
     427    private static ISymbolicExpressionTreeNode SimplifySquare(ISymbolicExpressionTreeNode original) {
    426428      return MakeSquare(GetSimplifiedTree(original.GetSubtree(0)));
    427429    }
    428430
    429     private ISymbolicExpressionTreeNode SimplifySquareRoot(ISymbolicExpressionTreeNode original) {
     431    private static ISymbolicExpressionTreeNode SimplifySquareRoot(ISymbolicExpressionTreeNode original) {
    430432      return MakeSquareRoot(GetSimplifiedTree(original.GetSubtree(0)));
    431433    }
    432434
    433     private ISymbolicExpressionTreeNode SimplifyLog(ISymbolicExpressionTreeNode original) {
     435    private static ISymbolicExpressionTreeNode SimplifyLog(ISymbolicExpressionTreeNode original) {
    434436      return MakeLog(GetSimplifiedTree(original.GetSubtree(0)));
    435437    }
    436438
    437     private ISymbolicExpressionTreeNode SimplifyRoot(ISymbolicExpressionTreeNode original) {
     439    private static ISymbolicExpressionTreeNode SimplifyRoot(ISymbolicExpressionTreeNode original) {
    438440      return MakeRoot(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1)));
    439441    }
    440442
    441     private ISymbolicExpressionTreeNode SimplifyPower(ISymbolicExpressionTreeNode original) {
     443    private static ISymbolicExpressionTreeNode SimplifyPower(ISymbolicExpressionTreeNode original) {
    442444      return MakePower(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1)));
    443445    }
    444446
    445     private ISymbolicExpressionTreeNode SimplifyTimeLag(ISymbolicExpressionTreeNode original) {
     447    private static ISymbolicExpressionTreeNode SimplifyTimeLag(ISymbolicExpressionTreeNode original) {
    446448      var laggedTreeNode = original as ILaggedTreeNode;
    447449      var simplifiedSubtree = GetSimplifiedTree(original.GetSubtree(0));
     
    453455    }
    454456
    455     private ISymbolicExpressionTreeNode SimplifyIntegral(ISymbolicExpressionTreeNode original) {
     457    private static ISymbolicExpressionTreeNode SimplifyIntegral(ISymbolicExpressionTreeNode original) {
    456458      var laggedTreeNode = original as ILaggedTreeNode;
    457459      var simplifiedSubtree = GetSimplifiedTree(original.GetSubtree(0));
     
    467469    #region low level tree restructuring
    468470
    469     private ISymbolicExpressionTreeNode MakeTimeLag(ISymbolicExpressionTreeNode subtree, int lag) {
     471    private static ISymbolicExpressionTreeNode MakeTimeLag(ISymbolicExpressionTreeNode subtree, int lag) {
    470472      if (lag == 0) return subtree;
    471473      if (IsConstant(subtree)) return subtree;
     
    476478    }
    477479
    478     private ISymbolicExpressionTreeNode MakeIntegral(ISymbolicExpressionTreeNode subtree, int lag) {
     480    private static ISymbolicExpressionTreeNode MakeIntegral(ISymbolicExpressionTreeNode subtree, int lag) {
    479481      if (lag == 0) return subtree;
    480482      else if (lag == -1 || lag == 1) {
     
    488490    }
    489491
    490     private ISymbolicExpressionTreeNode MakeNot(ISymbolicExpressionTreeNode t) {
     492    private static ISymbolicExpressionTreeNode MakeNot(ISymbolicExpressionTreeNode t) {
    491493      if (IsConstant(t)) {
    492494        var constNode = t as ConstantTreeNode;
     
    509511    }
    510512
    511     private ISymbolicExpressionTreeNode MakeOr(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
     513    private static ISymbolicExpressionTreeNode MakeOr(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
    512514      if (IsConstant(a) && IsConstant(b)) {
    513515        var constA = a as ConstantTreeNode;
     
    539541    }
    540542
    541     private ISymbolicExpressionTreeNode MakeAnd(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
     543    private static ISymbolicExpressionTreeNode MakeAnd(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
    542544      if (IsConstant(a) && IsConstant(b)) {
    543545        var constA = a as ConstantTreeNode;
     
    569571    }
    570572
    571     private ISymbolicExpressionTreeNode MakeLessThan(ISymbolicExpressionTreeNode leftSide,
     573    private static ISymbolicExpressionTreeNode MakeLessThan(ISymbolicExpressionTreeNode leftSide,
    572574      ISymbolicExpressionTreeNode rightSide) {
    573575      if (IsConstant(leftSide) && IsConstant(rightSide)) {
     
    584586    }
    585587
    586     private ISymbolicExpressionTreeNode MakeGreaterThan(ISymbolicExpressionTreeNode leftSide,
     588    private static ISymbolicExpressionTreeNode MakeGreaterThan(ISymbolicExpressionTreeNode leftSide,
    587589      ISymbolicExpressionTreeNode rightSide) {
    588590      if (IsConstant(leftSide) && IsConstant(rightSide)) {
     
    599601    }
    600602
    601     private ISymbolicExpressionTreeNode MakeIfThenElse(ISymbolicExpressionTreeNode condition,
     603    private static ISymbolicExpressionTreeNode MakeIfThenElse(ISymbolicExpressionTreeNode condition,
    602604      ISymbolicExpressionTreeNode trueBranch, ISymbolicExpressionTreeNode falseBranch) {
    603605      if (IsConstant(condition)) {
     
    621623    }
    622624
    623     private ISymbolicExpressionTreeNode MakeSine(ISymbolicExpressionTreeNode node) {
     625    private static ISymbolicExpressionTreeNode MakeSine(ISymbolicExpressionTreeNode node) {
    624626      if (IsConstant(node)) {
    625627        var constT = node as ConstantTreeNode;
     
    638640    }
    639641
    640     private ISymbolicExpressionTreeNode MakeTangent(ISymbolicExpressionTreeNode node) {
     642    private static ISymbolicExpressionTreeNode MakeTangent(ISymbolicExpressionTreeNode node) {
    641643      if (IsConstant(node)) {
    642644        var constT = node as ConstantTreeNode;
     
    655657    }
    656658
    657     private ISymbolicExpressionTreeNode MakeCosine(ISymbolicExpressionTreeNode node) {
     659    private static ISymbolicExpressionTreeNode MakeCosine(ISymbolicExpressionTreeNode node) {
    658660      if (IsConstant(node)) {
    659661        var constT = node as ConstantTreeNode;
     
    674676    }
    675677
    676     private ISymbolicExpressionTreeNode MakeExp(ISymbolicExpressionTreeNode node) {
     678    private static ISymbolicExpressionTreeNode MakeExp(ISymbolicExpressionTreeNode node) {
    677679      if (IsConstant(node)) {
    678680        var constT = node as ConstantTreeNode;
     
    698700      }
    699701    }
    700     private ISymbolicExpressionTreeNode MakeLog(ISymbolicExpressionTreeNode node) {
     702    private static ISymbolicExpressionTreeNode MakeLog(ISymbolicExpressionTreeNode node) {
    701703      if (IsConstant(node)) {
    702704        var constT = node as ConstantTreeNode;
     
    716718    }
    717719
    718     private ISymbolicExpressionTreeNode MakeSquare(ISymbolicExpressionTreeNode node) {
     720    private static ISymbolicExpressionTreeNode MakeSquare(ISymbolicExpressionTreeNode node) {
    719721      if (IsConstant(node)) {
    720722        var constT = node as ConstantTreeNode;
     
    735737    }
    736738
    737     private ISymbolicExpressionTreeNode MakeSquareRoot(ISymbolicExpressionTreeNode node) {
     739    private static ISymbolicExpressionTreeNode MakeSquareRoot(ISymbolicExpressionTreeNode node) {
    738740      if (IsConstant(node)) {
    739741        var constT = node as ConstantTreeNode;
     
    754756    }
    755757
    756     private ISymbolicExpressionTreeNode MakeRoot(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
     758    private static ISymbolicExpressionTreeNode MakeRoot(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
    757759      if (IsConstant(a) && IsConstant(b)) {
    758760        var constA = a as ConstantTreeNode;
     
    809811
    810812
    811     private ISymbolicExpressionTreeNode MakePower(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
     813    private static ISymbolicExpressionTreeNode MakePower(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
    812814      if (IsConstant(a) && IsConstant(b)) {
    813815        var constA = a as ConstantTreeNode;
     
    864866
    865867    // MakeFraction, MakeProduct and MakeSum take two already simplified trees and create a new simplified tree
    866     private ISymbolicExpressionTreeNode MakeFraction(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
     868    private static ISymbolicExpressionTreeNode MakeFraction(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
    867869      if (IsConstant(a) && IsConstant(b)) {
    868870        // fold constants
     
    933935    }
    934936
    935     private ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
     937    private static ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
    936938      if (IsConstant(a) && IsConstant(b)) {
    937939        // fold constants
     
    10281030
    10291031    // makes sure variable symbols in sums are combined
    1030     private void MergeVariablesInSum(ISymbolicExpressionTreeNode sum) {
     1032    private static void MergeVariablesInSum(ISymbolicExpressionTreeNode sum) {
    10311033      var subtrees = new List<ISymbolicExpressionTreeNode>(sum.Subtrees);
    10321034      while (sum.Subtrees.Any()) sum.RemoveSubtree(0);
     
    10531055            }
    10541056          }
    1055           for (int j = 0; j < representative.Weights.Length; j++) {
    1056             representative.Weights[j] += constant;
    1057           }
    10581057          sum.AddSubtree(representative);
    10591058        }
     
    10671066
    10681067    // nodes referencing variables can be grouped if they have
    1069     private string GroupId(IVariableTreeNode node) {
     1068    private static string GroupId(IVariableTreeNode node) {
    10701069      var binaryFactorNode = node as BinaryFactorVariableTreeNode;
    10711070      var factorNode = node as FactorVariableTreeNode;
     
    10861085
    10871086
    1088     private ISymbolicExpressionTreeNode MakeProduct(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
     1087    private static ISymbolicExpressionTreeNode MakeProduct(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
    10891088      if (IsConstant(a) && IsConstant(b)) {
    10901089        // fold constants
     
    11671166    #region helper functions
    11681167
    1169     private bool ContainsVariableCondition(ISymbolicExpressionTreeNode node) {
     1168    private static bool ContainsVariableCondition(ISymbolicExpressionTreeNode node) {
    11701169      if (node.Symbol is VariableCondition) return true;
    11711170      foreach (var subtree in node.Subtrees)
     
    11741173    }
    11751174
    1176     private ISymbolicExpressionTreeNode AddLagToDynamicNodes(ISymbolicExpressionTreeNode node, int lag) {
     1175    private static ISymbolicExpressionTreeNode AddLagToDynamicNodes(ISymbolicExpressionTreeNode node, int lag) {
    11771176      var laggedTreeNode = node as ILaggedTreeNode;
    11781177      var variableNode = node as VariableTreeNode;
     
    11961195    }
    11971196
    1198     private bool AreSameTypeAndVariable(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
     1197    private static bool AreSameTypeAndVariable(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
    11991198      return GroupId((IVariableTreeNode)a) == GroupId((IVariableTreeNode)b);
    12001199    }
    12011200
    12021201    // helper to combine the constant factors in products and to combine variables (powers of 2, 3...)
    1203     private void MergeVariablesAndConstantsInProduct(ISymbolicExpressionTreeNode prod) {
     1202    private static void MergeVariablesAndConstantsInProduct(ISymbolicExpressionTreeNode prod) {
    12041203      var subtrees = new List<ISymbolicExpressionTreeNode>(prod.Subtrees);
    12051204      while (prod.Subtrees.Any()) prod.RemoveSubtree(0);
     
    12651264    /// <param name="x"></param>
    12661265    /// <returns>-x</returns>
    1267     private ISymbolicExpressionTreeNode Negate(ISymbolicExpressionTreeNode x) {
     1266    private static ISymbolicExpressionTreeNode Negate(ISymbolicExpressionTreeNode x) {
    12681267      if (IsConstant(x)) {
    12691268        ((ConstantTreeNode)x).Value *= -1;
     
    13021301    /// <param name="x"></param>
    13031302    /// <returns></returns>
    1304     private ISymbolicExpressionTreeNode Invert(ISymbolicExpressionTreeNode x) {
     1303    private static ISymbolicExpressionTreeNode Invert(ISymbolicExpressionTreeNode x) {
    13051304      if (IsConstant(x)) {
    13061305        return MakeConstant(1.0 / ((ConstantTreeNode)x).Value);
     
    13161315    }
    13171316
    1318     private ISymbolicExpressionTreeNode MakeConstant(double value) {
     1317    private static ISymbolicExpressionTreeNode MakeConstant(double value) {
    13191318      ConstantTreeNode constantTreeNode = (ConstantTreeNode)(constSymbol.CreateTreeNode());
    13201319      constantTreeNode.Value = value;
     
    13221321    }
    13231322
    1324     private ISymbolicExpressionTreeNode MakeFactor(FactorVariable sy, string variableName, IEnumerable<double> weights) {
     1323    private static ISymbolicExpressionTreeNode MakeFactor(FactorVariable sy, string variableName, IEnumerable<double> weights) {
    13251324      var tree = (FactorVariableTreeNode)sy.CreateTreeNode();
    13261325      tree.VariableName = variableName;
     
    13281327      return tree;
    13291328    }
    1330     private ISymbolicExpressionTreeNode MakeBinFactor(BinaryFactorVariable sy, string variableName, string variableValue, double weight) {
     1329    private static ISymbolicExpressionTreeNode MakeBinFactor(BinaryFactorVariable sy, string variableName, string variableValue, double weight) {
    13311330      var tree = (BinaryFactorVariableTreeNode)sy.CreateTreeNode();
    13321331      tree.VariableName = variableName;
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/TreeToAutoDiffTermConverter.cs

    r14851 r15351  
    2323using System.Collections.Generic;
    2424using System.Linq;
     25using System.Runtime.Serialization;
    2526using AutoDiff;
    2627using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     
    2930  public class TreeToAutoDiffTermConverter {
    3031    public delegate double ParametricFunction(double[] vars, double[] @params);
     32
    3133    public delegate Tuple<double[], double> ParametricFunctionGradient(double[] vars, double[] @params);
    3234
     
    6264      eval: Math.Atan,
    6365      diff: x => 1 / (1 + x * x));
     66
    6467    private static readonly Func<Term, UnaryFunc> sin = UnaryFunc.Factory(
    6568      eval: Math.Sin,
    6669      diff: Math.Cos);
     70
    6771    private static readonly Func<Term, UnaryFunc> cos = UnaryFunc.Factory(
    68        eval: Math.Cos,
    69        diff: x => -Math.Sin(x));
     72      eval: Math.Cos,
     73      diff: x => -Math.Sin(x));
     74
    7075    private static readonly Func<Term, UnaryFunc> tan = UnaryFunc.Factory(
    7176      eval: Math.Tan,
    7277      diff: x => 1 + Math.Tan(x) * Math.Tan(x));
     78
    7379    private static readonly Func<Term, UnaryFunc> erf = UnaryFunc.Factory(
    7480      eval: alglib.errorfunction,
    7581      diff: x => 2.0 * Math.Exp(-(x * x)) / Math.Sqrt(Math.PI));
     82
    7683    private static readonly Func<Term, UnaryFunc> norm = UnaryFunc.Factory(
    7784      eval: alglib.normaldistribution,
     
    8895      var transformator = new TreeToAutoDiffTermConverter(makeVariableWeightsVariable);
    8996      AutoDiff.Term term;
    90       var success = transformator.TryConvertToAutoDiff(tree.Root.GetSubtree(0), out term);
    91       if (success) {
     97      try {
     98        term = transformator.ConvertToAutoDiff(tree.Root.GetSubtree(0));
    9299        var parameterEntries = transformator.parameters.ToArray(); // guarantee same order for keys and values
    93         var compiledTerm = term.Compile(transformator.variables.ToArray(), parameterEntries.Select(kvp => kvp.Value).ToArray());
     100        var compiledTerm = term.Compile(transformator.variables.ToArray(),
     101          parameterEntries.Select(kvp => kvp.Value).ToArray());
    94102        parameters = new List<DataForVariable>(parameterEntries.Select(kvp => kvp.Key));
    95103        initialConstants = transformator.initialConstants.ToArray();
    96104        func = (vars, @params) => compiledTerm.Evaluate(vars, @params);
    97105        func_grad = (vars, @params) => compiledTerm.Differentiate(vars, @params);
    98       } else {
     106        return true;
     107      } catch (ConversionException) {
    99108        func = null;
    100109        func_grad = null;
     
    102111        initialConstants = null;
    103112      }
    104       return success;
     113      return false;
    105114    }
    106115
    107116    // state for recursive transformation of trees
    108     private readonly List<double> initialConstants;
     117    private readonly
     118    List<double> initialConstants;
    109119    private readonly Dictionary<DataForVariable, AutoDiff.Variable> parameters;
    110120    private readonly List<AutoDiff.Variable> variables;
     
    118128    }
    119129
    120     private bool TryConvertToAutoDiff(ISymbolicExpressionTreeNode node, out AutoDiff.Term term) {
     130    private AutoDiff.Term ConvertToAutoDiff(ISymbolicExpressionTreeNode node) {
    121131      if (node.Symbol is Constant) {
    122132        initialConstants.Add(((ConstantTreeNode)node).Value);
    123133        var var = new AutoDiff.Variable();
    124134        variables.Add(var);
    125         term = var;
    126         return true;
     135        return var;
    127136      }
    128137      if (node.Symbol is Variable || node.Symbol is BinaryFactorVariable) {
     
    137146          var w = new AutoDiff.Variable();
    138147          variables.Add(w);
    139           term = AutoDiff.TermBuilder.Product(w, par);
     148          return AutoDiff.TermBuilder.Product(w, par);
    140149        } else {
    141           term = varNode.Weight * par;
    142         }
    143         return true;
     150          return varNode.Weight * par;
     151        }
    144152      }
    145153      if (node.Symbol is FactorVariable) {
     
    155163          products.Add(AutoDiff.TermBuilder.Product(wVar, par));
    156164        }
    157         term = AutoDiff.TermBuilder.Sum(products);
    158         return true;
     165        return AutoDiff.TermBuilder.Sum(products);
    159166      }
    160167      if (node.Symbol is LaggedVariable) {
     
    166173          var w = new AutoDiff.Variable();
    167174          variables.Add(w);
    168           term = AutoDiff.TermBuilder.Product(w, par);
     175          return AutoDiff.TermBuilder.Product(w, par);
    169176        } else {
    170           term = varNode.Weight * par;
    171         }
    172         return true;
     177          return varNode.Weight * par;
     178        }
    173179      }
    174180      if (node.Symbol is Addition) {
    175181        List<AutoDiff.Term> terms = new List<Term>();
    176182        foreach (var subTree in node.Subtrees) {
    177           AutoDiff.Term t;
    178           if (!TryConvertToAutoDiff(subTree, out t)) {
    179             term = null;
    180             return false;
    181           }
    182           terms.Add(t);
    183         }
    184         term = AutoDiff.TermBuilder.Sum(terms);
    185         return true;
     183          terms.Add(ConvertToAutoDiff(subTree));
     184        }
     185        return AutoDiff.TermBuilder.Sum(terms);
    186186      }
    187187      if (node.Symbol is Subtraction) {
    188188        List<AutoDiff.Term> terms = new List<Term>();
    189189        for (int i = 0; i < node.SubtreeCount; i++) {
    190           AutoDiff.Term t;
    191           if (!TryConvertToAutoDiff(node.GetSubtree(i), out t)) {
    192             term = null;
    193             return false;
    194           }
     190          AutoDiff.Term t = ConvertToAutoDiff(node.GetSubtree(i));
    195191          if (i > 0) t = -t;
    196192          terms.Add(t);
    197193        }
    198         if (terms.Count == 1) term = -terms[0];
    199         else term = AutoDiff.TermBuilder.Sum(terms);
    200         return true;
     194        if (terms.Count == 1) return -terms[0];
     195        else return AutoDiff.TermBuilder.Sum(terms);
    201196      }
    202197      if (node.Symbol is Multiplication) {
    203198        List<AutoDiff.Term> terms = new List<Term>();
    204199        foreach (var subTree in node.Subtrees) {
    205           AutoDiff.Term t;
    206           if (!TryConvertToAutoDiff(subTree, out t)) {
    207             term = null;
    208             return false;
    209           }
    210           terms.Add(t);
    211         }
    212         if (terms.Count == 1) term = terms[0];
    213         else term = terms.Aggregate((a, b) => new AutoDiff.Product(a, b));
    214         return true;
    215 
     200          terms.Add(ConvertToAutoDiff(subTree));
     201        }
     202        if (terms.Count == 1) return terms[0];
     203        else return terms.Aggregate((a, b) => new AutoDiff.Product(a, b));
    216204      }
    217205      if (node.Symbol is Division) {
    218206        List<AutoDiff.Term> terms = new List<Term>();
    219207        foreach (var subTree in node.Subtrees) {
    220           AutoDiff.Term t;
    221           if (!TryConvertToAutoDiff(subTree, out t)) {
    222             term = null;
    223             return false;
    224           }
    225           terms.Add(t);
    226         }
    227         if (terms.Count == 1) term = 1.0 / terms[0];
    228         else term = terms.Aggregate((a, b) => new AutoDiff.Product(a, 1.0 / b));
    229         return true;
     208          terms.Add(ConvertToAutoDiff(subTree));
     209        }
     210        if (terms.Count == 1) return 1.0 / terms[0];
     211        else return terms.Aggregate((a, b) => new AutoDiff.Product(a, 1.0 / b));
    230212      }
    231213      if (node.Symbol is Logarithm) {
    232         AutoDiff.Term t;
    233         if (!TryConvertToAutoDiff(node.GetSubtree(0), out t)) {
    234           term = null;
    235           return false;
    236         } else {
    237           term = AutoDiff.TermBuilder.Log(t);
    238           return true;
    239         }
     214        return AutoDiff.TermBuilder.Log(
     215          ConvertToAutoDiff(node.GetSubtree(0)));
    240216      }
    241217      if (node.Symbol is Exponential) {
    242         AutoDiff.Term t;
    243         if (!TryConvertToAutoDiff(node.GetSubtree(0), out t)) {
    244           term = null;
    245           return false;
    246         } else {
    247           term = AutoDiff.TermBuilder.Exp(t);
    248           return true;
    249         }
     218        return AutoDiff.TermBuilder.Exp(
     219          ConvertToAutoDiff(node.GetSubtree(0)));
    250220      }
    251221      if (node.Symbol is Square) {
    252         AutoDiff.Term t;
    253         if (!TryConvertToAutoDiff(node.GetSubtree(0), out t)) {
    254           term = null;
    255           return false;
    256         } else {
    257           term = AutoDiff.TermBuilder.Power(t, 2.0);
    258           return true;
    259         }
     222        return AutoDiff.TermBuilder.Power(
     223          ConvertToAutoDiff(node.GetSubtree(0)), 2.0);
    260224      }
    261225      if (node.Symbol is SquareRoot) {
    262         AutoDiff.Term t;
    263         if (!TryConvertToAutoDiff(node.GetSubtree(0), out t)) {
    264           term = null;
    265           return false;
    266         } else {
    267           term = AutoDiff.TermBuilder.Power(t, 0.5);
    268           return true;
    269         }
     226        return AutoDiff.TermBuilder.Power(
     227          ConvertToAutoDiff(node.GetSubtree(0)), 0.5);
    270228      }
    271229      if (node.Symbol is Sine) {
    272         AutoDiff.Term t;
    273         if (!TryConvertToAutoDiff(node.GetSubtree(0), out t)) {
    274           term = null;
    275           return false;
    276         } else {
    277           term = sin(t);
    278           return true;
    279         }
     230        return sin(
     231          ConvertToAutoDiff(node.GetSubtree(0)));
    280232      }
    281233      if (node.Symbol is Cosine) {
    282         AutoDiff.Term t;
    283         if (!TryConvertToAutoDiff(node.GetSubtree(0), out t)) {
    284           term = null;
    285           return false;
    286         } else {
    287           term = cos(t);
    288           return true;
    289         }
     234        return cos(
     235          ConvertToAutoDiff(node.GetSubtree(0)));
    290236      }
    291237      if (node.Symbol is Tangent) {
    292         AutoDiff.Term t;
    293         if (!TryConvertToAutoDiff(node.GetSubtree(0), out t)) {
    294           term = null;
    295           return false;
    296         } else {
    297           term = tan(t);
    298           return true;
    299         }
     238        return tan(
     239          ConvertToAutoDiff(node.GetSubtree(0)));
    300240      }
    301241      if (node.Symbol is Erf) {
    302         AutoDiff.Term t;
    303         if (!TryConvertToAutoDiff(node.GetSubtree(0), out t)) {
    304           term = null;
    305           return false;
    306         } else {
    307           term = erf(t);
    308           return true;
    309         }
     242        return erf(
     243          ConvertToAutoDiff(node.GetSubtree(0)));
    310244      }
    311245      if (node.Symbol is Norm) {
    312         AutoDiff.Term t;
    313         if (!TryConvertToAutoDiff(node.GetSubtree(0), out t)) {
    314           term = null;
    315           return false;
    316         } else {
    317           term = norm(t);
    318           return true;
    319         }
     246        return norm(
     247          ConvertToAutoDiff(node.GetSubtree(0)));
    320248      }
    321249      if (node.Symbol is StartSymbol) {
     
    324252        variables.Add(beta);
    325253        variables.Add(alpha);
    326         AutoDiff.Term branchTerm;
    327         if (TryConvertToAutoDiff(node.GetSubtree(0), out branchTerm)) {
    328           term = branchTerm * alpha + beta;
    329           return true;
    330         } else {
    331           term = null;
    332           return false;
    333         }
    334       }
    335       term = null;
    336       return false;
     254        return ConvertToAutoDiff(node.GetSubtree(0)) * alpha + beta;
     255      }
     256      throw new ConversionException();
    337257    }
    338258
     
    357277        from n in tree.Root.GetSubtree(0).IterateNodesPrefix()
    358278        where
    359         !(n.Symbol is Variable) &&
    360         !(n.Symbol is BinaryFactorVariable) &&
    361         !(n.Symbol is FactorVariable) &&
    362         !(n.Symbol is LaggedVariable) &&
    363         !(n.Symbol is Constant) &&
    364         !(n.Symbol is Addition) &&
    365         !(n.Symbol is Subtraction) &&
    366         !(n.Symbol is Multiplication) &&
    367         !(n.Symbol is Division) &&
    368         !(n.Symbol is Logarithm) &&
    369         !(n.Symbol is Exponential) &&
    370         !(n.Symbol is SquareRoot) &&
    371         !(n.Symbol is Square) &&
    372         !(n.Symbol is Sine) &&
    373         !(n.Symbol is Cosine) &&
    374         !(n.Symbol is Tangent) &&
    375         !(n.Symbol is Erf) &&
    376         !(n.Symbol is Norm) &&
    377         !(n.Symbol is StartSymbol)
     279          !(n.Symbol is Variable) &&
     280          !(n.Symbol is BinaryFactorVariable) &&
     281          !(n.Symbol is FactorVariable) &&
     282          !(n.Symbol is LaggedVariable) &&
     283          !(n.Symbol is Constant) &&
     284          !(n.Symbol is Addition) &&
     285          !(n.Symbol is Subtraction) &&
     286          !(n.Symbol is Multiplication) &&
     287          !(n.Symbol is Division) &&
     288          !(n.Symbol is Logarithm) &&
     289          !(n.Symbol is Exponential) &&
     290          !(n.Symbol is SquareRoot) &&
     291          !(n.Symbol is Square) &&
     292          !(n.Symbol is Sine) &&
     293          !(n.Symbol is Cosine) &&
     294          !(n.Symbol is Tangent) &&
     295          !(n.Symbol is Erf) &&
     296          !(n.Symbol is Norm) &&
     297          !(n.Symbol is StartSymbol)
    378298        select n).Any();
    379299      return !containsUnknownSymbol;
    380300    }
     301    #region exception class
     302    [Serializable]
     303    public class ConversionException : Exception {
     304
     305      public ConversionException() {
     306      }
     307
     308      public ConversionException(string message) : base(message) {
     309      }
     310
     311      public ConversionException(string message, Exception inner) : base(message, inner) {
     312      }
     313
     314      protected ConversionException(
     315        SerializationInfo info,
     316        StreamingContext context) : base(info, context) {
     317      }
     318    }
     319    #endregion
    381320  }
    382321}
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionExcelFormatter.cs

    r14878 r15351  
    7878    }
    7979
    80     public string FormatWithMapping(ISymbolicExpressionTree symbolicExpressionTree, Dictionary<string,string> variableNameMapping)
    81     {
    82       foreach(var kvp in variableNameMapping) this.variableNameMapping.Add(kvp.Key,kvp.Value);
     80    public string FormatWithMapping(ISymbolicExpressionTree symbolicExpressionTree, Dictionary<string, string> variableNameMapping) {
     81      foreach (var kvp in variableNameMapping) this.variableNameMapping.Add(kvp.Key, kvp.Value);
    8382      var stringBuilder = new StringBuilder();
    84      
     83
    8584      stringBuilder.Append("=");
    8685      stringBuilder.Append(FormatRecursively(symbolicExpressionTree.Root));
     
    9392    }
    9493
    95     private Dictionary<string,string> CalculateVariableMapping(ISymbolicExpressionTree tree, IDataset dataset) {
    96       var mapping = new Dictionary<string,string>();
     94    private Dictionary<string, string> CalculateVariableMapping(ISymbolicExpressionTree tree, IDataset dataset) {
     95      var mapping = new Dictionary<string, string>();
    9796      int inputIndex = 0;
    9897      var usedVariables = tree.IterateNodesPrefix().OfType<IVariableTreeNode>().Select(v => v.VariableName).Distinct().ToArray();
     
    241240      } else if (symbol is VariableCondition) {
    242241        VariableConditionTreeNode variableConditionTreeNode = node as VariableConditionTreeNode;
    243         double threshold = variableConditionTreeNode.Threshold;
    244         double slope = variableConditionTreeNode.Slope;
    245         string p = "(1 / (1 + EXP(-" + slope.ToString(CultureInfo.InvariantCulture) + " * (" + GetColumnToVariableName(variableConditionTreeNode.VariableName) + "-" + threshold.ToString(CultureInfo.InvariantCulture) + "))))";
    246         stringBuilder.Append("((");
    247         stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    248         stringBuilder.Append("*");
    249         stringBuilder.Append(p);
    250         stringBuilder.Append(") + (");
    251         stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
    252         stringBuilder.Append("*(");
    253         stringBuilder.Append("1 - " + p + ")");
    254         stringBuilder.Append("))");
     242        if (!variableConditionTreeNode.Symbol.IgnoreSlope) {
     243          double threshold = variableConditionTreeNode.Threshold;
     244          double slope = variableConditionTreeNode.Slope;
     245          string p = "(1 / (1 + EXP(-" + slope.ToString(CultureInfo.InvariantCulture) + " * (" +
     246                     GetColumnToVariableName(variableConditionTreeNode.VariableName) + "-" +
     247                     threshold.ToString(CultureInfo.InvariantCulture) + "))))";
     248          stringBuilder.Append("((");
     249          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
     250          stringBuilder.Append("*");
     251          stringBuilder.Append(p);
     252          stringBuilder.Append(") + (");
     253          stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
     254          stringBuilder.Append("*(");
     255          stringBuilder.Append("1 - " + p + ")");
     256          stringBuilder.Append("))");
     257        } else {
     258          stringBuilder.AppendFormat(CultureInfo.InvariantCulture, "(IF({0} <= {1}, {2}, {3}))",
     259            GetColumnToVariableName(variableConditionTreeNode.VariableName),
     260            variableConditionTreeNode.Threshold,
     261            FormatRecursively(node.GetSubtree(0)),
     262            FormatRecursively(node.GetSubtree(1))
     263            );
     264        }
    255265      } else if (symbol is Xor) {
    256266        stringBuilder.Append("IF(");
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj

    r14878 r15351  
    182182      <SubType>Code</SubType>
    183183    </Compile>
    184     <Compile Include="Converters\Convert.cs" />
    185184    <Compile Include="Converters\LinearModelToTreeConverter.cs" />
    186185    <Compile Include="Converters\TreeSimplifier.cs" />
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeSimplificationOperator.cs

    r14878 r15351  
    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
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisProblem.cs

    r14878 r15351  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using System.Drawing;
    2425using System.Linq;
     
    241242
    242243    private void InitializeOperators() {
    243       Operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>());
    244       Operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicDataAnalysisExpressionCrossover<T>>());
    245       Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
    246       Operators.Add(new SymbolicDataAnalysisVariableFrequencyAnalyzer());
    247       Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
    248       Operators.Add(new SymbolicExpressionTreeLengthAnalyzer());
    249       Operators.Add(new SymbolicExpressionTreeBottomUpSimilarityCalculator());
    250       Operators.Add(new SymbolicDataAnalysisBottomUpDiversityAnalyzer(Operators.OfType<SymbolicExpressionTreeBottomUpSimilarityCalculator>().First()));
     244      var operators = new HashSet<IItem>(new TypeEqualityComparer<IItem>());
     245      operators.Add(new SubtreeCrossover());
     246      operators.Add(new MultiSymbolicExpressionTreeManipulator());
     247
     248      foreach (var op in ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>())
     249        operators.Add(op);
     250      foreach (var op in ApplicationManager.Manager.GetInstances<ISymbolicDataAnalysisExpressionCrossover<T>>())
     251        operators.Add(op);
     252
     253      operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
     254      operators.Add(new SymbolicDataAnalysisVariableFrequencyAnalyzer());
     255      operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
     256      operators.Add(new SymbolicExpressionTreeLengthAnalyzer());
     257      operators.Add(new SymbolicExpressionTreeBottomUpSimilarityCalculator());
     258      operators.Add(new SymbolicDataAnalysisBottomUpDiversityAnalyzer(operators.OfType<SymbolicExpressionTreeBottomUpSimilarityCalculator>().First()));
     259
     260      Operators.AddRange(operators);
    251261      Operators.Add(new SymbolicDataAnalysisGenealogyAnalyzer());
    252262      ParameterizeOperators();
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/Analyzers/SymbolicDataAnalysisSchemaFrequencyAnalyzer.cs

    r14626 r15351  
    185185      }
    186186
    187       var schemas = SchemaCreator.GenerateSchemas(vertices, MinimumSchemaLength, 0, StrictSchemaMatching).ToList();
     187      var schemas = SchemaCreator.GenerateCombinedSchemas(vertices, MinimumSchemaLength, 0, StrictSchemaMatching).ToList();
    188188      var schemaStrings = schemas.Select(x => x.Root.GetSubtree(0).GetSubtree(0).FormatToString(StrictSchemaMatching)).ToList();
    189189      int[][] matchingIndices;
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SchemaDiversification/SchemaCreator.cs

    r14627 r15351  
    5252    private const string ReplacementRatioUpdateRuleParameterName = "ReplacementRatioUpdateRule";
    5353    private const string StrictSchemaMatchingParameterName = "StrictSchemaMatching";
     54    private const string SchemaDefinitionParameterName = "SchemaDefinition";
    5455    #endregion
    5556
    5657    #region parameters
    5758
     59    public IConstrainedValueParameter<StringValue> SchemaDefinitionParameter {
     60      get { return (IConstrainedValueParameter<StringValue>)Parameters[SchemaDefinitionParameterName]; }
     61    }
    5862    public IConstrainedValueParameter<StringValue> ReplacementRatioUpdateRuleParameter {
    5963      get { return (IConstrainedValueParameter<StringValue>)Parameters[ReplacementRatioUpdateRuleParameterName]; }
     
    147151        new StringValue("f(x) = 1-sqrt(1-x)")
    148152      });
    149       Parameters.Add(new ConstrainedValueParameter<StringValue>(ReplacementRatioUpdateRuleParameterName, replacementRatioUpdateRules));
     153      var replacementRatioUpdateRuleParameter = new ConstrainedValueParameter<StringValue>(ReplacementRatioUpdateRuleParameterName, replacementRatioUpdateRules);
     154      replacementRatioUpdateRuleParameter.Value = replacementRatioUpdateRules.First();
     155      Parameters.Add(replacementRatioUpdateRuleParameter);
     156
     157      var schemaDefinitions = new ItemSet<StringValue>(new[] {
     158         new StringValue("="),
     159         new StringValue("#"),
     160         new StringValue("=,#")
     161      });
     162      var schemaDefinitionParameter = new ConstrainedValueParameter<StringValue>(SchemaDefinitionParameterName, schemaDefinitions);
     163      schemaDefinitionParameter.Value = schemaDefinitions.First();
     164      Parameters.Add(schemaDefinitionParameter);
    150165      #endregion
     166
    151167      NumberOfChangedTreesParameter.Hidden = true;
    152168      NumberOfSchemasParameter.Hidden = true;
     
    201217                     select v;
    202218
    203       var schemas = GenerateSchemas(vertices, MinimumSchemaLength, 0, StrictSchemaMatching).ToList();
     219      List<ISymbolicExpressionTree> schemas;
     220      var def = SchemaDefinitionParameter.Value.Value;
     221      switch (def) {
     222        case "=":
     223          schemas = GenerateAnyNodeSchemas(vertices, MinimumSchemaLength, 0, StrictSchemaMatching).ToList();
     224          break;
     225        case "#":
     226          schemas = GenerateAnySubtreeSchemas(vertices, MinimumSchemaLength, 0, StrictSchemaMatching).ToList();
     227          break;
     228        case "=,#":
     229          schemas = GenerateCombinedSchemas(vertices, MinimumSchemaLength, 0, StrictSchemaMatching).ToList();
     230          break;
     231        default:
     232          return base.Apply();
     233      }
     234
     235      if (!schemas.Any())
     236        return base.Apply();
    204237
    205238      #region create schemas and add subscopes representing the individuals
     
    223256    }
    224257
    225     public static IEnumerable<ISymbolicExpressionTree> GenerateSchemas(IEnumerable<IGenealogyGraphNode<ISymbolicExpressionTree>> vertices, int minimumSchemaLength, int minOffspringCount = 1, bool strict = true) {
     258    #region schema generation
     259    public static IEnumerable<ISymbolicExpressionTree> GenerateAnyNodeSchemas(IEnumerable<IGenealogyGraphNode<ISymbolicExpressionTree>> vertices, int minimumSchemaLength, int minOffspringCount = 1, bool strict = true) {
     260      var anySubtreeSymbol = new AnySubtreeSymbol();
     261      var groups = vertices.GroupBy(x => x.Parents.First()).Where(g => g.Count() >= minOffspringCount).OrderByDescending(g => g.Count());
     262      var hash = new HashSet<string>();
     263      foreach (var g in groups) {
     264        var parent = g.Key;
     265        if (parent.Data.Length < minimumSchemaLength)
     266          continue;
     267        bool replaced = false;
     268        var schema = (ISymbolicExpressionTree)parent.Data.Clone();
     269        var nodes = schema.IterateNodesPrefix().ToList();
     270        var fragments = g.Select(x => x.InArcs.Last().Data).Where(x => x != null).Cast<IFragment<ISymbolicExpressionTreeNode>>();
     271        var indices = fragments.Select(x => x.Index1).Distinct().OrderByDescending(x => schema.Root.GetBranchLevel(nodes[x]));
     272        foreach (var i in indices) {
     273          var node = nodes[i];
     274          var anyNodeSymbol = new AnyNodeSymbol(node.Symbol.MinimumArity, node.Symbol.MaximumArity);
     275          var replacement = anyNodeSymbol.CreateTreeNode();
     276          SchemaUtil.ReplaceSubtree(node, replacement, true);
     277          replaced = true;
     278        }
     279        if (replaced) {
     280          var str = schema.Root.GetSubtree(0).GetSubtree(0).FormatToString(strict);
     281          if (hash.Add(str))
     282            yield return schema;
     283        }
     284      }
     285    }
     286
     287    public static IEnumerable<ISymbolicExpressionTree> GenerateAnySubtreeSchemas(IEnumerable<IGenealogyGraphNode<ISymbolicExpressionTree>> vertices, int minimumSchemaLength, int minOffspringCount = 1, bool strict = true) {
    226288      var anySubtreeSymbol = new AnySubtreeSymbol();
    227289      var groups = vertices.GroupBy(x => x.Parents.First()).Where(g => g.Count() >= minOffspringCount).OrderByDescending(g => g.Count());
     
    239301          var node = nodes[i];
    240302          // do not replace the node with a wildcard if it would result in a length < MinimumSchemaLength
    241           //          if (schema.Length - node.GetLength() + 1 < minimumSchemaLength)
    242           //            continue;
    243           ISymbolicExpressionTreeNode replacement;
    244           if (node.SubtreeCount > 0) {
    245             var anyNodeSymbol = new AnyNodeSymbol(node.Symbol.MinimumArity, node.Symbol.MaximumArity);
    246             replacement = anyNodeSymbol.CreateTreeNode();
    247           } else {
    248             replacement = anySubtreeSymbol.CreateTreeNode();
    249           }
    250           SchemaUtil.ReplaceSubtree(node, replacement, true);
    251           //          var replacement = anySubtreeSymbol.CreateTreeNode();
    252           //          var replacement = new AnyNodeSymbol(node.Symbol.MinimumArity, node.Symbol.MinimumArity).CreateTreeNode();
    253           //          ReplaceSubtree(node, replacement, true);
    254           //          SchemaUtil.ReplaceSubtree(node, replacement, false);
     303          if (schema.Length - node.GetLength() + 1 < minimumSchemaLength)
     304            continue;
     305          var replacement = anySubtreeSymbol.CreateTreeNode();
     306          SchemaUtil.ReplaceSubtree(node, replacement, false);
    255307          replaced = true;
    256308        }
     
    262314      }
    263315    }
     316
     317    public static IEnumerable<ISymbolicExpressionTree> GenerateCombinedSchemas(IEnumerable<IGenealogyGraphNode<ISymbolicExpressionTree>> vertices, int minimumSchemaLength, int minOffspringCount = 1, bool strict = true) {
     318      var anySubtreeSymbol = new AnySubtreeSymbol();
     319      var groups = vertices.GroupBy(x => x.Parents.First()).Where(g => g.Count() >= minOffspringCount).OrderByDescending(g => g.Count());
     320      var hash = new HashSet<string>();
     321      foreach (var g in groups) {
     322        var parent = g.Key;
     323        if (parent.Data.Length < minimumSchemaLength)
     324          continue;
     325        bool replaced = false;
     326        var schema = (ISymbolicExpressionTree)parent.Data.Clone();
     327        var nodes = schema.IterateNodesPrefix().ToList();
     328        var fragments = g.Select(x => x.InArcs.Last().Data).Where(x => x != null).Cast<IFragment<ISymbolicExpressionTreeNode>>();
     329        var indices = fragments.Select(x => x.Index1).Distinct().OrderByDescending(x => schema.Root.GetBranchLevel(nodes[x]));
     330        foreach (var i in indices) {
     331          var node = nodes[i];
     332          ISymbolicExpressionTreeNode replacement;
     333          if (node.SubtreeCount > 0) {
     334            replacement = new AnyNodeSymbol(node.Symbol.MinimumArity, node.Symbol.MaximumArity).CreateTreeNode();
     335            SchemaUtil.ReplaceSubtree(node, replacement, true);
     336          } else {
     337            replacement = anySubtreeSymbol.CreateTreeNode();
     338            SchemaUtil.ReplaceSubtree(node, replacement, false);
     339          }
     340          replaced = true;
     341        }
     342        if (replaced) {
     343          var str = schema.Root.GetSubtree(0).GetSubtree(0).FormatToString(strict);
     344          if (hash.Add(str))
     345            yield return schema;
     346        }
     347      }
     348    }
     349    #endregion
    264350  }
    265351}
Note: See TracChangeset for help on using the changeset viewer.