Free cookie consent management tool by TermsFeed Policy Generator

Changeset 9739


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.
Location:
branches/HeuristicLab.DataAnalysis.Symbolic.LinearInterpreter
Files:
2 added
1 deleted
3 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.DataAnalysis.Symbolic.LinearInterpreter/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.csproj

    r9738 r9739  
    173173    <Compile Include="Compiler\Instruction.cs" />
    174174    <Compile Include="Compiler\SymbolicExpressionTreeCompiler.cs" />
     175    <Compile Include="Compiler\SymbolicExpressionTreeLinearCompiler.cs" />
    175176    <Compile Include="Creators\FullTreeCreator.cs">
    176177      <SubType>Code</SubType>
  • 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  }
  • branches/HeuristicLab.DataAnalysis.Symbolic.LinearInterpreter/HeuristicLab.Tests/HeuristicLab.Tests.csproj

    r9738 r9739  
    3131    <DebugType>pdbonly</DebugType>
    3232    <Optimize>true</Optimize>
    33     <OutputPath>..\..\..\..\trunk\sources\bin\</OutputPath>
     33    <OutputPath>bin\</OutputPath>
    3434    <DefineConstants>TRACE</DefineConstants>
    3535    <ErrorReport>prompt</ErrorReport>
     
    8282    <Reference Include="ALGLIB-3.7.0">
    8383      <HintPath>..\..\..\Trunk\sources\bin\ALGLIB-3.7.0.dll</HintPath>
    84       <Private>False</Private>
     84      <Private>True</Private>
    8585    </Reference>
    8686    <Reference Include="HeuristicLab.Common-3.3">
    8787      <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Common-3.3.dll</HintPath>
    88       <Private>False</Private>
     88      <Private>True</Private>
    8989    </Reference>
    9090    <Reference Include="HeuristicLab.Core-3.3">
    9191      <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Core-3.3.dll</HintPath>
    92       <Private>False</Private>
     92      <Private>True</Private>
    9393    </Reference>
    9494    <Reference Include="HeuristicLab.Data-3.3">
    9595      <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Data-3.3.dll</HintPath>
    96       <Private>False</Private>
     96      <Private>True</Private>
    9797    </Reference>
    9898    <Reference Include="HeuristicLab.Operators-3.3">
    9999      <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Operators-3.3.dll</HintPath>
    100       <Private>False</Private>
     100      <Private>True</Private>
    101101    </Reference>
    102102    <Reference Include="HeuristicLab.Optimization-3.3">
    103103      <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Optimization-3.3.dll</HintPath>
    104       <Private>False</Private>
     104      <Private>True</Private>
    105105    </Reference>
    106106    <Reference Include="HeuristicLab.Problems.DataAnalysis-3.4">
    107107      <HintPath>..\..\..\Trunk\sources\bin\HeuristicLab.Problems.DataAnalysis-3.4.dll</HintPath>
    108       <Private>False</Private>
     108      <Private>True</Private>
    109109    </Reference>
    110110    <Reference Include="HeuristicLab.Random-3.3">
    111111      <HintPath>..\..\..\Trunk\sources\bin\HeuristicLab.Random-3.3.dll</HintPath>
    112       <Private>False</Private>
     112      <Private>True</Private>
    113113    </Reference>
     114    <Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
    114115    <Reference Include="System" />
    115116  </ItemGroup>
     
    120121      </ItemGroup>
    121122    </When>
    122     <Otherwise>
    123       <ItemGroup>
    124         <Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework" />
    125       </ItemGroup>
    126     </Otherwise>
     123    <Otherwise />
    127124  </Choose>
    128125  <ItemGroup>
     
    137134      <Project>{06d4a186-9319-48a0-bade-a2058d462eea}</Project>
    138135      <Name>HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4</Name>
    139       <Private>False</Private>
     136      <Private>True</Private>
    140137    </ProjectReference>
    141138    <ProjectReference Include="..\HeuristicLab.Problems.DataAnalysis.Symbolic\3.4\HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj">
    142139      <Project>{3d28463f-ec96-4d82-afee-38be91a0ca00}</Project>
    143140      <Name>HeuristicLab.Problems.DataAnalysis.Symbolic-3.4</Name>
    144       <Private>False</Private>
     141      <Private>True</Private>
    145142    </ProjectReference>
    146143  </ItemGroup>
Note: See TracChangeset for help on using the changeset viewer.