Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/10/20 11:31:32 (4 years ago)
Author:
pfleck
Message:

#3040 Added a new simplifier that can also simplify vector-specific operators.

  • Added simplification rules for sum-symbol and mean-symbol for addition and multiplication
Location:
branches/3040_VectorBasedGP
Files:
3 edited
1 copied

Legend:

Unmodified
Added
Removed
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/InteractiveSymbolicDataAnalysisSolutionSimplifierView.cs

    r17472 r17593  
    127127      if (valid) {
    128128        // check if all variables are contained in the dataset
    129         var variables = new HashSet<string>(Content.ProblemData.Dataset.DoubleVariables);
     129        var variables = new HashSet<string>(Content.ProblemData.Dataset.VariableNames);
    130130        valid = nodes.OfType<VariableTreeNode>().All(x => variables.Contains(x.VariableName));
    131131      }
     
    314314
    315315    private void btnSimplify_Click(object sender, EventArgs e) {
    316       var simplifiedExpressionTree = TreeSimplifier.Simplify(Content.Model.SymbolicExpressionTree);
    317       UpdateModel(simplifiedExpressionTree);
     316      if (Content.Model.Interpreter is SymbolicDataAnalysisExpressionTreeVectorInterpreter interpreter) { // TODO: Own interface for data-dependent node-types-interpreter?
     317        var simplifier = new VectorTreeSimplifier(interpreter, Content.ProblemData);
     318        var simplifiedExpressionTree = simplifier.Simplify(Content.Model.SymbolicExpressionTree);
     319        UpdateModel(simplifiedExpressionTree);
     320      } else {
     321        var simplifiedExpressionTree = TreeSimplifier.Simplify(Content.Model.SymbolicExpressionTree);
     322        UpdateModel(simplifiedExpressionTree);
     323      }
    318324    }
    319325
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/VectorTreeSimplifier.cs

    r17592 r17593  
    3232  /// Simplifier for symbolic expressions
    3333  /// </summary>
    34   public class TreeSimplifier {
     34  public class VectorTreeSimplifier {
    3535    private static readonly Addition addSymbol = new Addition();
    3636    private static readonly Multiplication mulSymbol = new Multiplication();
     
    5959    private static readonly LaggedVariable laggedVariableSymbol = new LaggedVariable();
    6060    private static readonly TimeLag timeLagSymbol = new TimeLag();
    61 
    62     [Obsolete("Use static method TreeSimplifier.Simplify instead")]
    63     public TreeSimplifier() { }
    64 
    65     public static ISymbolicExpressionTree Simplify(ISymbolicExpressionTree originalTree) {
     61    private static readonly Sum sumSymbol = new Sum();
     62    private static readonly Mean meanSymbol = new Mean();
     63    private static readonly Length lengthSymbol = new Length();
     64
     65    private readonly SymbolicDataAnalysisExpressionTreeVectorInterpreter interpreter;
     66    private readonly IDataAnalysisProblemData problemData;
     67
     68    public VectorTreeSimplifier(SymbolicDataAnalysisExpressionTreeVectorInterpreter interpreter, IDataAnalysisProblemData problemData) {
     69      this.interpreter = interpreter;
     70      this.problemData = problemData;
     71    }
     72
     73    public ISymbolicExpressionTree Simplify(ISymbolicExpressionTree originalTree) {
    6674      var clone = (ISymbolicExpressionTreeNode)originalTree.Root.Clone();
    6775      // macro expand (initially no argument trees)
     
    253261    }
    254262
     263    private static bool IsSum(ISymbolicExpressionTreeNode node) {
     264      return node.Symbol is Sum;
     265    }
     266
     267    private static bool IsMean(ISymbolicExpressionTreeNode node) {
     268      return node.Symbol is Mean;
     269    }
     270    #endregion
     271
     272    #region type predicates
     273    public bool IsScalarNode(ISymbolicExpressionTreeNode node) {
     274      var results = interpreter.EvaluateNode(node, problemData.Dataset, problemData.TrainingIndices);
     275      return results.All(r => r.IsScalar);
     276    }
     277    public bool IsVectorNode(ISymbolicExpressionTreeNode node) {
     278      var results = interpreter.EvaluateNode(node, problemData.Dataset, problemData.TrainingIndices);
     279      return results.All(r => r.IsVector);
     280    }
    255281    #endregion
    256282
     
    260286    /// <param name="original"></param>
    261287    /// <returns></returns>
    262     public static ISymbolicExpressionTreeNode GetSimplifiedTree(ISymbolicExpressionTreeNode original) {
     288    public ISymbolicExpressionTreeNode GetSimplifiedTree(ISymbolicExpressionTreeNode original) {
    263289      if (IsConstant(original) || IsVariableBase(original)) {
    264290        return (ISymbolicExpressionTreeNode)original.Clone();
     
    315341      } else if (IsIntegral(original)) {
    316342        return SimplifyIntegral(original);
     343      } else if (IsSum(original)) {
     344        return SimplifySumAggregation(original);
     345      } else if (IsMean(original)) {
     346        return SimplifyMeanAggregation(original);
    317347      } else {
    318348        return SimplifyAny(original);
     
    322352    #region specific simplification routines
    323353
    324     private static ISymbolicExpressionTreeNode SimplifyAny(ISymbolicExpressionTreeNode original) {
     354    private ISymbolicExpressionTreeNode SimplifyAny(ISymbolicExpressionTreeNode original) {
    325355      // can't simplify this function but simplify all subtrees
    326356      List<ISymbolicExpressionTreeNode> subtrees = new List<ISymbolicExpressionTreeNode>(original.Subtrees);
     
    341371    }
    342372
    343     private static ISymbolicExpressionTreeNode SimplifyConstantExpression(ISymbolicExpressionTreeNode original) {
     373    private ISymbolicExpressionTreeNode SimplifyConstantExpression(ISymbolicExpressionTreeNode original) {
    344374      // not yet implemented
    345375      return original;
    346376    }
    347377
    348     private static ISymbolicExpressionTreeNode SimplifyAverage(ISymbolicExpressionTreeNode original) {
     378    private ISymbolicExpressionTreeNode SimplifyAverage(ISymbolicExpressionTreeNode original) {
    349379      if (original.Subtrees.Count() == 1) {
    350380        return GetSimplifiedTree(original.GetSubtree(0));
     
    359389    }
    360390
    361     private static ISymbolicExpressionTreeNode SimplifyDivision(ISymbolicExpressionTreeNode original) {
     391    private ISymbolicExpressionTreeNode SimplifyDivision(ISymbolicExpressionTreeNode original) {
    362392      if (original.Subtrees.Count() == 1) {
    363393        return Invert(GetSimplifiedTree(original.GetSubtree(0)));
     
    374404    }
    375405
    376     private static ISymbolicExpressionTreeNode SimplifyMultiplication(ISymbolicExpressionTreeNode original) {
     406    private ISymbolicExpressionTreeNode SimplifyMultiplication(ISymbolicExpressionTreeNode original) {
    377407      if (original.Subtrees.Count() == 1) {
    378408        return GetSimplifiedTree(original.GetSubtree(0));
     
    384414    }
    385415
    386     private static ISymbolicExpressionTreeNode SimplifySubtraction(ISymbolicExpressionTreeNode original) {
     416    private ISymbolicExpressionTreeNode SimplifySubtraction(ISymbolicExpressionTreeNode original) {
    387417      if (original.Subtrees.Count() == 1) {
    388418        return Negate(GetSimplifiedTree(original.GetSubtree(0)));
     
    396426    }
    397427
    398     private static ISymbolicExpressionTreeNode SimplifyAddition(ISymbolicExpressionTreeNode original) {
     428    private ISymbolicExpressionTreeNode SimplifyAddition(ISymbolicExpressionTreeNode original) {
    399429      if (original.Subtrees.Count() == 1) {
    400430        return GetSimplifiedTree(original.GetSubtree(0));
     
    408438    }
    409439
    410     private static ISymbolicExpressionTreeNode SimplifyAbsolute(ISymbolicExpressionTreeNode original) {
     440    private ISymbolicExpressionTreeNode SimplifyAbsolute(ISymbolicExpressionTreeNode original) {
    411441      return MakeAbs(GetSimplifiedTree(original.GetSubtree(0)));
    412442    }
    413443
    414     private static ISymbolicExpressionTreeNode SimplifyNot(ISymbolicExpressionTreeNode original) {
     444    private ISymbolicExpressionTreeNode SimplifyNot(ISymbolicExpressionTreeNode original) {
    415445      return MakeNot(GetSimplifiedTree(original.GetSubtree(0)));
    416446    }
    417447
    418     private static ISymbolicExpressionTreeNode SimplifyOr(ISymbolicExpressionTreeNode original) {
     448    private ISymbolicExpressionTreeNode SimplifyOr(ISymbolicExpressionTreeNode original) {
    419449      return original.Subtrees
    420450        .Select(GetSimplifiedTree)
     
    422452    }
    423453
    424     private static ISymbolicExpressionTreeNode SimplifyAnd(ISymbolicExpressionTreeNode original) {
     454    private ISymbolicExpressionTreeNode SimplifyAnd(ISymbolicExpressionTreeNode original) {
    425455      return original.Subtrees
    426456        .Select(GetSimplifiedTree)
     
    428458    }
    429459
    430     private static ISymbolicExpressionTreeNode SimplifyLessThan(ISymbolicExpressionTreeNode original) {
     460    private ISymbolicExpressionTreeNode SimplifyLessThan(ISymbolicExpressionTreeNode original) {
    431461      return MakeLessThan(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1)));
    432462    }
    433463
    434     private static ISymbolicExpressionTreeNode SimplifyGreaterThan(ISymbolicExpressionTreeNode original) {
     464    private ISymbolicExpressionTreeNode SimplifyGreaterThan(ISymbolicExpressionTreeNode original) {
    435465      return MakeGreaterThan(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1)));
    436466    }
    437467
    438     private static ISymbolicExpressionTreeNode SimplifyIfThenElse(ISymbolicExpressionTreeNode original) {
     468    private ISymbolicExpressionTreeNode SimplifyIfThenElse(ISymbolicExpressionTreeNode original) {
    439469      return MakeIfThenElse(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1)),
    440470        GetSimplifiedTree(original.GetSubtree(2)));
    441471    }
    442472
    443     private static ISymbolicExpressionTreeNode SimplifyTangent(ISymbolicExpressionTreeNode original) {
     473    private ISymbolicExpressionTreeNode SimplifyTangent(ISymbolicExpressionTreeNode original) {
    444474      return MakeTangent(GetSimplifiedTree(original.GetSubtree(0)));
    445475    }
    446476
    447     private static ISymbolicExpressionTreeNode SimplifyCosine(ISymbolicExpressionTreeNode original) {
     477    private ISymbolicExpressionTreeNode SimplifyCosine(ISymbolicExpressionTreeNode original) {
    448478      return MakeCosine(GetSimplifiedTree(original.GetSubtree(0)));
    449479    }
    450480
    451     private static ISymbolicExpressionTreeNode SimplifySine(ISymbolicExpressionTreeNode original) {
     481    private ISymbolicExpressionTreeNode SimplifySine(ISymbolicExpressionTreeNode original) {
    452482      return MakeSine(GetSimplifiedTree(original.GetSubtree(0)));
    453483    }
    454484
    455     private static ISymbolicExpressionTreeNode SimplifyExp(ISymbolicExpressionTreeNode original) {
     485    private ISymbolicExpressionTreeNode SimplifyExp(ISymbolicExpressionTreeNode original) {
    456486      return MakeExp(GetSimplifiedTree(original.GetSubtree(0)));
    457487    }
    458488
    459     private static ISymbolicExpressionTreeNode SimplifySquare(ISymbolicExpressionTreeNode original) {
     489    private ISymbolicExpressionTreeNode SimplifySquare(ISymbolicExpressionTreeNode original) {
    460490      return MakeSquare(GetSimplifiedTree(original.GetSubtree(0)));
    461491    }
    462492
    463     private static ISymbolicExpressionTreeNode SimplifySquareRoot(ISymbolicExpressionTreeNode original) {
     493    private ISymbolicExpressionTreeNode SimplifySquareRoot(ISymbolicExpressionTreeNode original) {
    464494      return MakeSquareRoot(GetSimplifiedTree(original.GetSubtree(0)));
    465495    }
    466     private static ISymbolicExpressionTreeNode SimplifyCube(ISymbolicExpressionTreeNode original) {
     496    private ISymbolicExpressionTreeNode SimplifyCube(ISymbolicExpressionTreeNode original) {
    467497      return MakeCube(GetSimplifiedTree(original.GetSubtree(0)));
    468498    }
    469499
    470     private static ISymbolicExpressionTreeNode SimplifyCubeRoot(ISymbolicExpressionTreeNode original) {
     500    private ISymbolicExpressionTreeNode SimplifyCubeRoot(ISymbolicExpressionTreeNode original) {
    471501      return MakeCubeRoot(GetSimplifiedTree(original.GetSubtree(0)));
    472502    }
    473503
    474     private static ISymbolicExpressionTreeNode SimplifyLog(ISymbolicExpressionTreeNode original) {
     504    private ISymbolicExpressionTreeNode SimplifyLog(ISymbolicExpressionTreeNode original) {
    475505      return MakeLog(GetSimplifiedTree(original.GetSubtree(0)));
    476506    }
    477507
    478     private static ISymbolicExpressionTreeNode SimplifyRoot(ISymbolicExpressionTreeNode original) {
     508    private ISymbolicExpressionTreeNode SimplifyRoot(ISymbolicExpressionTreeNode original) {
    479509      return MakeRoot(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1)));
    480510    }
    481511
    482     private static ISymbolicExpressionTreeNode SimplifyPower(ISymbolicExpressionTreeNode original) {
     512    private ISymbolicExpressionTreeNode SimplifyPower(ISymbolicExpressionTreeNode original) {
    483513      return MakePower(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1)));
    484514    }
    485515
    486     private static ISymbolicExpressionTreeNode SimplifyAnalyticalQuotient(ISymbolicExpressionTreeNode original) {
     516    private ISymbolicExpressionTreeNode SimplifyAnalyticalQuotient(ISymbolicExpressionTreeNode original) {
    487517      return MakeAnalyticalQuotient(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1)));
    488518    }
    489519
    490     private static ISymbolicExpressionTreeNode SimplifyTimeLag(ISymbolicExpressionTreeNode original) {
     520    private ISymbolicExpressionTreeNode SimplifyTimeLag(ISymbolicExpressionTreeNode original) {
    491521      var laggedTreeNode = original as ILaggedTreeNode;
    492522      var simplifiedSubtree = GetSimplifiedTree(original.GetSubtree(0));
     
    498528    }
    499529
    500     private static ISymbolicExpressionTreeNode SimplifyIntegral(ISymbolicExpressionTreeNode original) {
     530    private ISymbolicExpressionTreeNode SimplifyIntegral(ISymbolicExpressionTreeNode original) {
    501531      var laggedTreeNode = original as ILaggedTreeNode;
    502532      var simplifiedSubtree = GetSimplifiedTree(original.GetSubtree(0));
     
    508538    }
    509539
     540    private ISymbolicExpressionTreeNode SimplifySumAggregation(ISymbolicExpressionTreeNode original) {
     541      return MakeSumAggregation(GetSimplifiedTree(original.GetSubtree(0)));
     542    }
     543
     544    private ISymbolicExpressionTreeNode SimplifyMeanAggregation(ISymbolicExpressionTreeNode original) {
     545      return MakeMeanAggregation(GetSimplifiedTree(original.GetSubtree(0)));
     546    }
     547
    510548    #endregion
    511549
    512550    #region low level tree restructuring
    513551
    514     private static ISymbolicExpressionTreeNode MakeTimeLag(ISymbolicExpressionTreeNode subtree, int lag) {
     552    private ISymbolicExpressionTreeNode MakeTimeLag(ISymbolicExpressionTreeNode subtree, int lag) {
    515553      if (lag == 0) return subtree;
    516554      if (IsConstant(subtree)) return subtree;
     
    521559    }
    522560
    523     private static ISymbolicExpressionTreeNode MakeIntegral(ISymbolicExpressionTreeNode subtree, int lag) {
     561    private ISymbolicExpressionTreeNode MakeIntegral(ISymbolicExpressionTreeNode subtree, int lag) {
    524562      if (lag == 0) return subtree;
    525563      else if (lag == -1 || lag == 1) {
     
    533571    }
    534572
    535     private static ISymbolicExpressionTreeNode MakeNot(ISymbolicExpressionTreeNode t) {
     573    private ISymbolicExpressionTreeNode MakeNot(ISymbolicExpressionTreeNode t) {
    536574      if (IsConstant(t)) {
    537575        var constNode = t as ConstantTreeNode;
     
    554592    }
    555593
    556     private static ISymbolicExpressionTreeNode MakeOr(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
     594    private ISymbolicExpressionTreeNode MakeOr(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
    557595      if (IsConstant(a) && IsConstant(b)) {
    558596        var constA = a as ConstantTreeNode;
     
    584622    }
    585623
    586     private static ISymbolicExpressionTreeNode MakeAnd(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
     624    private ISymbolicExpressionTreeNode MakeAnd(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
    587625      if (IsConstant(a) && IsConstant(b)) {
    588626        var constA = a as ConstantTreeNode;
     
    614652    }
    615653
    616     private static ISymbolicExpressionTreeNode MakeLessThan(ISymbolicExpressionTreeNode leftSide,
     654    private ISymbolicExpressionTreeNode MakeLessThan(ISymbolicExpressionTreeNode leftSide,
    617655      ISymbolicExpressionTreeNode rightSide) {
    618656      if (IsConstant(leftSide) && IsConstant(rightSide)) {
     
    629667    }
    630668
    631     private static ISymbolicExpressionTreeNode MakeGreaterThan(ISymbolicExpressionTreeNode leftSide,
     669    private ISymbolicExpressionTreeNode MakeGreaterThan(ISymbolicExpressionTreeNode leftSide,
    632670      ISymbolicExpressionTreeNode rightSide) {
    633671      if (IsConstant(leftSide) && IsConstant(rightSide)) {
     
    644682    }
    645683
    646     private static ISymbolicExpressionTreeNode MakeIfThenElse(ISymbolicExpressionTreeNode condition,
     684    private ISymbolicExpressionTreeNode MakeIfThenElse(ISymbolicExpressionTreeNode condition,
    647685      ISymbolicExpressionTreeNode trueBranch, ISymbolicExpressionTreeNode falseBranch) {
    648686      if (IsConstant(condition)) {
     
    666704    }
    667705
    668     private static ISymbolicExpressionTreeNode MakeSine(ISymbolicExpressionTreeNode node) {
     706    private ISymbolicExpressionTreeNode MakeSine(ISymbolicExpressionTreeNode node) {
    669707      if (IsConstant(node)) {
    670708        var constT = node as ConstantTreeNode;
     
    683721    }
    684722
    685     private static ISymbolicExpressionTreeNode MakeTangent(ISymbolicExpressionTreeNode node) {
     723    private ISymbolicExpressionTreeNode MakeTangent(ISymbolicExpressionTreeNode node) {
    686724      if (IsConstant(node)) {
    687725        var constT = node as ConstantTreeNode;
     
    700738    }
    701739
    702     private static ISymbolicExpressionTreeNode MakeCosine(ISymbolicExpressionTreeNode node) {
     740    private ISymbolicExpressionTreeNode MakeCosine(ISymbolicExpressionTreeNode node) {
    703741      if (IsConstant(node)) {
    704742        var constT = node as ConstantTreeNode;
     
    719757    }
    720758
    721     private static ISymbolicExpressionTreeNode MakeExp(ISymbolicExpressionTreeNode node) {
     759    private ISymbolicExpressionTreeNode MakeExp(ISymbolicExpressionTreeNode node) {
    722760      if (IsConstant(node)) {
    723761        var constT = node as ConstantTreeNode;
     
    743781      }
    744782    }
    745     private static ISymbolicExpressionTreeNode MakeLog(ISymbolicExpressionTreeNode node) {
     783    private ISymbolicExpressionTreeNode MakeLog(ISymbolicExpressionTreeNode node) {
    746784      if (IsConstant(node)) {
    747785        var constT = node as ConstantTreeNode;
     
    761799    }
    762800
    763     private static ISymbolicExpressionTreeNode MakeSquare(ISymbolicExpressionTreeNode node) {
     801    private ISymbolicExpressionTreeNode MakeSquare(ISymbolicExpressionTreeNode node) {
    764802      if (IsConstant(node)) {
    765803        var constT = node as ConstantTreeNode;
     
    793831    }
    794832
    795     private static ISymbolicExpressionTreeNode MakeCube(ISymbolicExpressionTreeNode node) {
     833    private ISymbolicExpressionTreeNode MakeCube(ISymbolicExpressionTreeNode node) {
    796834      if (IsConstant(node)) {
    797835        var constT = node as ConstantTreeNode;
     
    816854    }
    817855
    818     private static ISymbolicExpressionTreeNode MakeAbs(ISymbolicExpressionTreeNode node) {
     856    private ISymbolicExpressionTreeNode MakeAbs(ISymbolicExpressionTreeNode node) {
    819857      if (IsConstant(node)) {
    820858        var constT = node as ConstantTreeNode;
     
    848886
    849887    // constant folding only
    850     private static ISymbolicExpressionTreeNode MakeAnalyticalQuotient(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
     888    private ISymbolicExpressionTreeNode MakeAnalyticalQuotient(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
    851889      if (IsConstant(b)) {
    852890        var c = b as ConstantTreeNode;
     
    866904    }
    867905
    868     private static ISymbolicExpressionTreeNode MakeSquareRoot(ISymbolicExpressionTreeNode node) {
     906    private ISymbolicExpressionTreeNode MakeSquareRoot(ISymbolicExpressionTreeNode node) {
    869907      if (IsConstant(node)) {
    870908        var constT = node as ConstantTreeNode;
     
    885923    }
    886924
    887     private static ISymbolicExpressionTreeNode MakeCubeRoot(ISymbolicExpressionTreeNode node) {
     925    private ISymbolicExpressionTreeNode MakeCubeRoot(ISymbolicExpressionTreeNode node) {
    888926      if (IsConstant(node)) {
    889927        var constT = node as ConstantTreeNode;
     
    904942    }
    905943
    906     private static ISymbolicExpressionTreeNode MakeRoot(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
     944    private ISymbolicExpressionTreeNode MakeRoot(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
    907945      if (IsConstant(a) && IsConstant(b)) {
    908946        var constA = a as ConstantTreeNode;
     
    959997
    960998
    961     private static ISymbolicExpressionTreeNode MakePower(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
     999    private ISymbolicExpressionTreeNode MakePower(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
    9621000      if (IsConstant(a) && IsConstant(b)) {
    9631001        var constA = a as ConstantTreeNode;
     
    10141052
    10151053    // MakeFraction, MakeProduct and MakeSum take two already simplified trees and create a new simplified tree
    1016     private static ISymbolicExpressionTreeNode MakeFraction(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
     1054    private ISymbolicExpressionTreeNode MakeFraction(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
    10171055      if (IsConstant(a) && IsConstant(b)) {
    10181056        // fold constants
     
    10851123    }
    10861124
    1087     private static ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
     1125    private ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
    10881126      if (IsConstant(a) && IsConstant(b)) {
    10891127        // fold constants
     
    11801218
    11811219    // makes sure variable symbols in sums are combined
    1182     private static void MergeVariablesInSum(ISymbolicExpressionTreeNode sum) {
     1220    private void MergeVariablesInSum(ISymbolicExpressionTreeNode sum) {
    11831221      var subtrees = new List<ISymbolicExpressionTreeNode>(sum.Subtrees);
    11841222      while (sum.Subtrees.Any()) sum.RemoveSubtree(0);
     
    12161254
    12171255    // nodes referencing variables can be grouped if they have
    1218     private static string GroupId(IVariableTreeNode node) {
     1256    private string GroupId(IVariableTreeNode node) {
    12191257      var binaryFactorNode = node as BinaryFactorVariableTreeNode;
    12201258      var factorNode = node as FactorVariableTreeNode;
     
    12351273
    12361274
    1237     private static ISymbolicExpressionTreeNode MakeProduct(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
     1275    private ISymbolicExpressionTreeNode MakeProduct(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
    12381276      if (IsConstant(a) && IsConstant(b)) {
    12391277        // fold constants
     
    13291367    }
    13301368
     1369    ISymbolicExpressionTreeNode MakeSumAggregation(ISymbolicExpressionTreeNode node) {
     1370      if (IsConstant(node)) { // assumes scalar constant
     1371        return node;
     1372      } else if (IsAddition(node)) {
     1373        var terms = node.Subtrees;
     1374        var scalarTerms = terms.Where(IsScalarNode).ToList();
     1375        var remainingTerms = terms.Except(scalarTerms).ToList();
     1376
     1377        if (scalarTerms.Any() && remainingTerms.Any()) {
     1378          var scalarNode = scalarTerms.Aggregate(MakeSum);
     1379          var vectorNode = remainingTerms.Aggregate(MakeSum);
     1380
     1381          var lengthNode = lengthSymbol.CreateTreeNode();
     1382          lengthNode.AddSubtree((ISymbolicExpressionTreeNode)vectorNode.Clone());
     1383          var scalarMulNode = mulSymbol.CreateTreeNode();
     1384          scalarMulNode.AddSubtree(scalarNode);
     1385          scalarMulNode.AddSubtree(lengthNode);
     1386
     1387          var sumNode = sumSymbol.CreateTreeNode();
     1388          sumNode.AddSubtree(vectorNode);
     1389
     1390          return MakeSum(scalarMulNode, sumNode);
     1391        } else if (scalarTerms.Any()) {
     1392          var scalarNode = scalarTerms.Aggregate(MakeSum);
     1393          return scalarNode;
     1394        } else if (remainingTerms.Any()) {
     1395          var vectorNode = remainingTerms.Aggregate(MakeSum);
     1396          var sumNode = sumSymbol.CreateTreeNode();
     1397          sumNode.AddSubtree(vectorNode);
     1398          return sumNode;
     1399        } else
     1400          throw new InvalidOperationException("Addition does not contain any terms to simplify.");
     1401      } else if (IsMultiplication(node)) {
     1402        var factors = node.Subtrees;
     1403        var scalarFactors = factors.Where(IsScalarNode).ToList();
     1404        var remainingFactors = factors.Except(scalarFactors).ToList();
     1405
     1406        if (scalarFactors.Any() && remainingFactors.Any()) {
     1407          var scalarNode = scalarFactors.Aggregate(MakeProduct);
     1408          var vectorNode = remainingFactors.Aggregate(MakeProduct);
     1409
     1410          var sumNode = sumSymbol.CreateTreeNode();
     1411          sumNode.AddSubtree(vectorNode);
     1412
     1413          return MakeProduct(scalarNode, sumNode);
     1414        } else if (scalarFactors.Any()) {
     1415          var scalarNode = scalarFactors.Aggregate(MakeProduct);
     1416          return scalarNode;
     1417        } else if (remainingFactors.Any()) {
     1418          var vectorNode = remainingFactors.Aggregate(MakeProduct);
     1419          var sumNode = sumSymbol.CreateTreeNode();
     1420          sumNode.AddSubtree(vectorNode);
     1421          return sumNode;
     1422        } else
     1423          throw new InvalidOperationException("Multiplication does not contain any terms to simplify.");
     1424      } else {
     1425        var sumNode = sumSymbol.CreateTreeNode();
     1426        sumNode.AddSubtree(node);
     1427        return sumNode;
     1428      }
     1429    }
     1430
     1431    ISymbolicExpressionTreeNode MakeMeanAggregation(ISymbolicExpressionTreeNode node) {
     1432      if (IsConstant(node)) { // assumes scalar constant
     1433        return node;
     1434      } else if (IsAddition(node)) {
     1435        var terms = node.Subtrees;
     1436        var scalarTerms = terms.Where(IsScalarNode).ToList();
     1437        var remainingTerms = terms.Except(scalarTerms).ToList();
     1438
     1439        if (scalarTerms.Any() && remainingTerms.Any()) {
     1440          var scalarNode = scalarTerms.Aggregate(MakeSum);
     1441          var vectorNode = remainingTerms.Aggregate(MakeSum);
     1442
     1443          var meanNode = meanSymbol.CreateTreeNode();
     1444          meanNode.AddSubtree(vectorNode);
     1445
     1446          return MakeSum(scalarNode, meanNode);
     1447        } else if (scalarTerms.Any()) {
     1448          var scalarNode = scalarTerms.Aggregate(MakeSum);
     1449          return scalarNode;
     1450        } else if (remainingTerms.Any()) {
     1451          var vectorNode = remainingTerms.Aggregate(MakeSum);
     1452          var meanNode = meanSymbol.CreateTreeNode();
     1453          meanNode.AddSubtree(vectorNode);
     1454          return meanNode;
     1455        } else
     1456          throw new InvalidOperationException("Addition does not contain any terms to simplify.");
     1457      } else if (IsMultiplication(node)) {
     1458        var factors = node.Subtrees;
     1459        var scalarFactors = factors.Where(IsScalarNode).ToList();
     1460        var remainingFactors = factors.Except(scalarFactors).ToList();
     1461
     1462        if (scalarFactors.Any() && remainingFactors.Any()) {
     1463          var scalarNode = scalarFactors.Aggregate(MakeProduct);
     1464          var vectorNode = remainingFactors.Aggregate(MakeProduct);
     1465
     1466          var meanNode = meanSymbol.CreateTreeNode();
     1467          meanNode.AddSubtree(vectorNode);
     1468
     1469          return MakeProduct(scalarNode, meanNode);
     1470        } else if (scalarFactors.Any()) {
     1471          var scalarNode = scalarFactors.Aggregate(MakeProduct);
     1472          return scalarNode;
     1473        } else if (remainingFactors.Any()) {
     1474          var vectorNode = remainingFactors.Aggregate(MakeProduct);
     1475          var meanNode = meanSymbol.CreateTreeNode();
     1476          meanNode.AddSubtree(vectorNode);
     1477          return meanNode;
     1478        } else
     1479          throw new InvalidOperationException("Multiplication does not contain any terms to simplify.");
     1480      } else {
     1481        var meanNode = meanSymbol.CreateTreeNode();
     1482        meanNode.AddSubtree(node);
     1483        return meanNode;
     1484      }
     1485    }
     1486
    13311487    #endregion
    13321488
    13331489    #region helper functions
    13341490
    1335     private static bool ContainsVariableCondition(ISymbolicExpressionTreeNode node) {
     1491    private bool ContainsVariableCondition(ISymbolicExpressionTreeNode node) {
    13361492      if (node.Symbol is VariableCondition) return true;
    13371493      foreach (var subtree in node.Subtrees)
     
    13401496    }
    13411497
    1342     private static ISymbolicExpressionTreeNode AddLagToDynamicNodes(ISymbolicExpressionTreeNode node, int lag) {
     1498    private ISymbolicExpressionTreeNode AddLagToDynamicNodes(ISymbolicExpressionTreeNode node, int lag) {
    13431499      var laggedTreeNode = node as ILaggedTreeNode;
    13441500      var variableNode = node as VariableTreeNode;
     
    13621518    }
    13631519
    1364     private static bool AreSameTypeAndVariable(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
     1520    private bool AreSameTypeAndVariable(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
    13651521      return GroupId((IVariableTreeNode)a) == GroupId((IVariableTreeNode)b);
    13661522    }
    13671523
    13681524    // helper to combine the constant factors in products and to combine variables (powers of 2, 3...)
    1369     private static void MergeVariablesAndConstantsInProduct(ISymbolicExpressionTreeNode prod) {
     1525    private void MergeVariablesAndConstantsInProduct(ISymbolicExpressionTreeNode prod) {
    13701526      var subtrees = new List<ISymbolicExpressionTreeNode>(prod.Subtrees);
    13711527      while (prod.Subtrees.Any()) prod.RemoveSubtree(0);
     
    14311587    /// <param name="x"></param>
    14321588    /// <returns>-x</returns>
    1433     private static ISymbolicExpressionTreeNode Negate(ISymbolicExpressionTreeNode x) {
     1589    private ISymbolicExpressionTreeNode Negate(ISymbolicExpressionTreeNode x) {
    14341590      if (IsConstant(x)) {
    14351591        ((ConstantTreeNode)x).Value *= -1;
     
    14681624    /// <param name="x"></param>
    14691625    /// <returns></returns>
    1470     private static ISymbolicExpressionTreeNode Invert(ISymbolicExpressionTreeNode x) {
     1626    private ISymbolicExpressionTreeNode Invert(ISymbolicExpressionTreeNode x) {
    14711627      if (IsConstant(x)) {
    14721628        return MakeConstant(1.0 / ((ConstantTreeNode)x).Value);
     
    14821638    }
    14831639
    1484     private static ISymbolicExpressionTreeNode MakeConstant(double value) {
     1640    private ISymbolicExpressionTreeNode MakeConstant(double value) {
    14851641      ConstantTreeNode constantTreeNode = (ConstantTreeNode)(constSymbol.CreateTreeNode());
    14861642      constantTreeNode.Value = value;
     
    14881644    }
    14891645
    1490     private static ISymbolicExpressionTreeNode MakeFactor(FactorVariable sy, string variableName, IEnumerable<double> weights) {
     1646    private ISymbolicExpressionTreeNode MakeFactor(FactorVariable sy, string variableName, IEnumerable<double> weights) {
    14911647      var tree = (FactorVariableTreeNode)sy.CreateTreeNode();
    14921648      tree.VariableName = variableName;
     
    14941650      return tree;
    14951651    }
    1496     private static ISymbolicExpressionTreeNode MakeBinFactor(BinaryFactorVariable sy, string variableName, string variableValue, double weight) {
     1652    private ISymbolicExpressionTreeNode MakeBinFactor(BinaryFactorVariable sy, string variableName, string variableValue, double weight) {
    14971653      var tree = (BinaryFactorVariableTreeNode)sy.CreateTreeNode();
    14981654      tree.VariableName = variableName;
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj

    r17573 r17593  
    161161    </Compile>
    162162    <Compile Include="Converters\LinearModelToTreeConverter.cs" />
     163    <Compile Include="Converters\VectorTreeSimplifier.cs" />
    163164    <Compile Include="Converters\TreeSimplifier.cs" />
    164165    <Compile Include="Converters\DerivativeCalculator.cs" />
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeVectorInterpreter.cs

    r17573 r17593  
    254254    }
    255255
     256    public virtual IEnumerable<EvaluationResult> EvaluateNode(ISymbolicExpressionTreeNode node, IDataset dataset, IEnumerable<int> rows) {
     257      //lock (syncRoot) {
     258      //  EvaluatedSolutions++; // increment the evaluated solutions counter
     259      //}
     260
     261      var startNode = new StartSymbol().CreateTreeNode();
     262      startNode.AddSubtree(node);
     263      var programNode = new ProgramRootSymbol().CreateTreeNode();
     264      programNode.AddSubtree(startNode);
     265      var tree = new SymbolicExpressionTree(programNode);
     266
     267      var state = PrepareInterpreterState(tree, dataset);
     268
     269      foreach (var rowEnum in rows) {
     270        int row = rowEnum;
     271        var result = Evaluate(dataset, ref row, state);
     272        yield return result;
     273        state.Reset();
     274      }
     275    }
     276
    256277    public virtual EvaluationResult Evaluate(IDataset dataset, ref int row, InterpreterState state) {
    257278      Instruction currentInstr = state.NextInstruction();
     
    398419          }
    399420        case OpCodes.Length: {
    400           var cur = Evaluate(dataset, ref row, state);
    401           return AggregateApply(cur,
    402             s => 1,
    403             v => v.Count);
    404         }
     421            var cur = Evaluate(dataset, ref row, state);
     422            return AggregateApply(cur,
     423              s => 1,
     424              v => v.Count);
     425          }
    405426        case OpCodes.Min: {
    406           var cur = Evaluate(dataset, ref row, state);
    407           return AggregateApply(cur,
    408             s => s,
    409             v => Statistics.Minimum(v));
    410         }
     427            var cur = Evaluate(dataset, ref row, state);
     428            return AggregateApply(cur,
     429              s => s,
     430              v => Statistics.Minimum(v));
     431          }
    411432        case OpCodes.Max: {
    412           var cur = Evaluate(dataset, ref row, state);
    413           return AggregateApply(cur,
    414             s => s,
    415             v => Statistics.Maximum(v));
    416         }
     433            var cur = Evaluate(dataset, ref row, state);
     434            return AggregateApply(cur,
     435              s => s,
     436              v => Statistics.Maximum(v));
     437          }
    417438        case OpCodes.Variance: {
    418           var cur = Evaluate(dataset, ref row, state);
    419           return AggregateApply(cur,
    420             s => double.NaN,
    421             v => Statistics.Variance(v));
    422         }
     439            var cur = Evaluate(dataset, ref row, state);
     440            return AggregateApply(cur,
     441              s => double.NaN,
     442              v => Statistics.Variance(v));
     443          }
    423444        case OpCodes.Skewness: {
    424           var cur = Evaluate(dataset, ref row, state);
    425           return AggregateApply(cur,
    426             s => double.NaN,
    427             v => Statistics.Skewness(v));
    428         }
     445            var cur = Evaluate(dataset, ref row, state);
     446            return AggregateApply(cur,
     447              s => double.NaN,
     448              v => Statistics.Skewness(v));
     449          }
    429450        case OpCodes.Kurtosis: {
    430           var cur = Evaluate(dataset, ref row, state);
    431           return AggregateApply(cur,
    432             s => double.NaN,
    433             v => Statistics.Kurtosis(v));
    434         }
     451            var cur = Evaluate(dataset, ref row, state);
     452            return AggregateApply(cur,
     453              s => double.NaN,
     454              v => Statistics.Kurtosis(v));
     455          }
    435456        case OpCodes.EuclideanDistance: {
    436           var x1 = Evaluate(dataset, ref row, state);
    437           var x2 = Evaluate(dataset, ref row, state);
    438           return AggregateMultipleApply(x1, x2,
    439             //(s1, s2) => s1 - s2,
    440             //(s1, v2) => Math.Sqrt((s1 - v2).PointwisePower(2).Sum()),
    441             //(v1, s2) => Math.Sqrt((v1 - s2).PointwisePower(2).Sum()),
    442             vvFunc: (v1, v2) => v1.Count == v2.Count ? Math.Sqrt((v1 - v2).PointwisePower(2).Sum()) : double.NaN);
    443         }
     457            var x1 = Evaluate(dataset, ref row, state);
     458            var x2 = Evaluate(dataset, ref row, state);
     459            return AggregateMultipleApply(x1, x2,
     460              //(s1, s2) => s1 - s2,
     461              //(s1, v2) => Math.Sqrt((s1 - v2).PointwisePower(2).Sum()),
     462              //(v1, s2) => Math.Sqrt((v1 - s2).PointwisePower(2).Sum()),
     463              vvFunc: (v1, v2) => v1.Count == v2.Count ? Math.Sqrt((v1 - v2).PointwisePower(2).Sum()) : double.NaN);
     464          }
    444465        case OpCodes.Covariance: {
    445           var x1 = Evaluate(dataset, ref row, state);
    446           var x2 = Evaluate(dataset, ref row, state);
    447           return AggregateMultipleApply(x1, x2,
    448             //(s1, s2) => 0,
    449             //(s1, v2) => 0,
    450             //(v1, s2) => 0,
    451             vvFunc: (v1, v2) => v1.Count == v2.Count ? Statistics.Covariance(v1, v2) : double.NaN);
    452         }
     466            var x1 = Evaluate(dataset, ref row, state);
     467            var x2 = Evaluate(dataset, ref row, state);
     468            return AggregateMultipleApply(x1, x2,
     469              //(s1, s2) => 0,
     470              //(s1, v2) => 0,
     471              //(v1, s2) => 0,
     472              vvFunc: (v1, v2) => v1.Count == v2.Count ? Statistics.Covariance(v1, v2) : double.NaN);
     473          }
    453474        case OpCodes.Variable: {
    454475            if (row < 0 || row >= dataset.Rows) return EvaluationResult.NaN;
Note: See TracChangeset for help on using the changeset viewer.