Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/24/13 09:56:56 (11 years ago)
Author:
bburlacu
Message:

#2021: Added separate SymbolicExpressionTreeLinearCompiler. Updated the SymbolicDataAnalysisExpressionTreeLinearInterpreter:

  • moved constants evaluation outside of loop
  • simplified code
  • renamed EvaluateFast method to Evaluate and made it static.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.DataAnalysis.Symbolic.LinearInterpreter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeLinearInterpreter.cs

    r9738 r9739  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Linq;
    2425using HeuristicLab.Common;
    2526using HeuristicLab.Core;
     
    121122      }
    122123
    123       var root = tree.Root.GetSubtree(0).GetSubtree(0);
    124       var nodes = new List<ISymbolicExpressionTreeNode> { root };
    125       var code = new List<LinearInstruction>{
    126           new LinearInstruction { dynamicNode = root,
    127           nArguments = (byte) root.SubtreeCount,
    128           opCode = OpCodes.MapSymbolToOpCode(root)
    129         }
    130       };
    131 
    132       // iterate breadth-wise over tree nodes and produce an array of instructions
    133       int i = 0;
    134       while (i != nodes.Count) {
    135         if (nodes[i].SubtreeCount > 0) {
    136           // save index of the first child in the instructions array
    137           code[i].childIndex = code.Count;
    138           for (int j = 0; j != nodes[i].SubtreeCount; ++j) {
    139             var s = nodes[i].GetSubtree(j);
    140             nodes.Add(s);
    141             code.Add(new LinearInstruction {
    142               dynamicNode = s,
    143               nArguments = (byte)s.SubtreeCount,
    144               opCode = OpCodes.MapSymbolToOpCode(s)
    145             });
    146           }
    147         }
    148         ++i;
    149       }
    150       // fill in iArg0 value for terminal nodes
    151       foreach (var instr in code) {
     124      var code = SymbolicExpressionTreeLinearCompiler.Compile(tree, OpCodes.MapSymbolToOpCode);
     125      PrepareInterpreterState(code, dataset);
     126      return rows.Select(row => Evaluate(dataset, ref row, code));
     127    }
     128
     129    private static void PrepareInterpreterState(LinearInstruction[] code, Dataset dataset) {
     130      for (int i = code.Length - 1; i >= 0; --i) {
     131        var instr = code[i];
     132        #region opcode switch
    152133        switch (instr.opCode) {
     134          case OpCodes.Constant: {
     135              var constTreeNode = (ConstantTreeNode)instr.dynamicNode;
     136              instr.value = constTreeNode.Value;
     137            }
     138            break;
    153139          case OpCodes.Variable: {
    154140              var variableTreeNode = (VariableTreeNode)instr.dynamicNode;
     
    167153            break;
    168154        }
     155        #endregion
    169156      }
    170 
    171       var array = code.ToArray();
    172 
    173       foreach (var rowEnum in rows) {
    174         int row = rowEnum;
    175         EvaluateFast(dataset, ref row, array);
    176         yield return code[0].value;
    177       }
    178     }
    179 
    180     private void EvaluateFast(Dataset dataset, ref int row, LinearInstruction[] code) {
     157    }
     158
     159    private static double Evaluate(Dataset dataset, ref int row, LinearInstruction[] code) {
    181160      for (int i = code.Length - 1; i >= 0; --i) {
    182161        var instr = code[i];
    183 
     162        if (instr.opCode == OpCodes.Constant) continue;
     163        #region opcode switch
    184164        switch (instr.opCode) {
    185165          case OpCodes.Variable: {
     
    194174              if (actualRow < 0 || actualRow >= dataset.Rows) instr.value = double.NaN;
    195175              instr.value = ((IList<double>)instr.iArg0)[actualRow] * laggedVariableTreeNode.Weight;
    196             }
    197             break;
    198           case OpCodes.Constant: {
    199               var constTreeNode = (ConstantTreeNode)instr.dynamicNode;
    200               instr.value = constTreeNode.Value;
    201176            }
    202177            break;
     
    462437            throw new NotSupportedException();
    463438        }
     439        #endregion
    464440      }
     441      return code[0].value;
    465442    }
    466443  }
Note: See TracChangeset for help on using the changeset viewer.