Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/18/15 22:29:17 (9 years ago)
Author:
mkommend
Message:

#2472: Minor code improvements in HL.Problems.GP (typos, code organization, code unification).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.GeneticProgramming/3.3/BasicSymbolicRegression/Problem.cs

    r12937 r13267  
    102102      return InterpretRec(tree.Root.GetSubtree(0).GetSubtree(0), dataset, rows);
    103103    }
    104 
    105 
     104                                                                                                       
    106105    private IEnumerable<double> InterpretRec(ISymbolicExpressionTreeNode node, IDataset dataset, IEnumerable<int> rows) {
    107       var eval = CreateEvalClosure(dataset, rows);
     106      Func<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode, Func<double, double, double>, IEnumerable<double>> binaryEval =
     107        (left, right, f) => InterpretRec(left, dataset,rows).Zip(InterpretRec(right, dataset, rows), f);
    108108
    109109      switch (node.Symbol.Name) {
    110         case "+": return eval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x + y);
    111         case "*": return eval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x * y);
    112         case "-": return eval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x - y);
    113         case "%": return eval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => y.IsAlmost(0.0) ? 0.0 : x / y); // protected division
     110        case "+": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x + y);
     111        case "*": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x * y);
     112        case "-": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x - y);
     113        case "%": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => y.IsAlmost(0.0) ? 0.0 : x / y); // protected division
    114114        default: {
    115115            double erc;
     
    124124    }
    125125
    126     private Func<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode, Func<double, double, double>, IEnumerable<double>> CreateEvalClosure(IDataset dataset, IEnumerable<int> rows) {
    127       // capture dataset and rows in scope
    128       return (a, b, f) => {
    129         var leftResult = InterpretRec(a, dataset, rows);
    130         var rightResult = InterpretRec(b, dataset, rows);
    131         return leftResult.Zip(rightResult, f);
    132       };
    133     }
    134 
    135126    #region item cloning and persistence
    136127    // persistence
Note: See TracChangeset for help on using the changeset viewer.