Free cookie consent management tool by TermsFeed Policy Generator

Changeset 9271


Ignore:
Timestamp:
03/04/13 14:43:44 (12 years ago)
Author:
bburlacu
Message:

#2021: Initial implementation of the SymbolicExpressionTreeLinearInterpreter.

Location:
branches/HeuristicLab.DataAnalysis.Symbolic.LinearInterpreter
Files:
5 added
6 edited
3 copied

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.DataAnalysis.Symbolic.LinearInterpreter/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Compiler/Instruction.cs

    r7259 r9271  
    3232    // an optional object value (addresses for calls, argument index for arguments)
    3333    public object iArg0;
     34    // hold the value of the instruction
     35    public double value;
    3436  }
    3537}
  • branches/HeuristicLab.DataAnalysis.Symbolic.LinearInterpreter/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Compiler/SymbolicExpressionTreeCompiler.cs

    r8798 r9271  
    2727  public static class SymbolicExpressionTreeCompiler {
    2828
    29     public static Instruction[] Compile(ISymbolicExpressionTree tree, Func<ISymbolicExpressionTreeNode, byte> opCodeMapper) {
    30       return Compile(tree, opCodeMapper, Enumerable.Empty<Func<Instruction, Instruction>>());
     29    public static Instruction[] CompilePrefix(ISymbolicExpressionTree tree, Func<ISymbolicExpressionTreeNode, byte> opCodeMapper) {
     30      return CompilePrefix(tree, opCodeMapper, Enumerable.Empty<Func<Instruction, Instruction>>());
    3131    }
    32     public static Instruction[] Compile(ISymbolicExpressionTree tree, Func<ISymbolicExpressionTreeNode, byte> opCodeMapper, IEnumerable<Func<Instruction, Instruction>> postInstructionCompiledHooks) {
     32    public static Instruction[] CompilePrefix(ISymbolicExpressionTree tree, Func<ISymbolicExpressionTreeNode, byte> opCodeMapper, IEnumerable<Func<Instruction, Instruction>> postInstructionCompiledHooks) {
    3333      Dictionary<string, ushort> entryPoint = new Dictionary<string, ushort>();
    3434      List<Instruction> code = new List<Instruction>();
    3535      // compile main body branches
    3636      foreach (var branch in tree.Root.GetSubtree(0).Subtrees) {
    37         code.AddRange(Compile(branch, opCodeMapper, postInstructionCompiledHooks));
     37        code.AddRange(CompilePrefix(branch, opCodeMapper, postInstructionCompiledHooks));
    3838      }
    3939      // compile function branches
     
    4444        if (code.Count > ushort.MaxValue) throw new ArgumentException("Code for the tree is too long (> ushort.MaxValue).");
    4545        entryPoint[branch.FunctionName] = (ushort)code.Count;
    46         code.AddRange(Compile(branch.GetSubtree(0), opCodeMapper, postInstructionCompiledHooks));
     46        code.AddRange(CompilePrefix(branch.GetSubtree(0), opCodeMapper, postInstructionCompiledHooks));
    4747      }
    4848      // address of all functions is fixed now
     
    5959    }
    6060
    61     private static IEnumerable<Instruction> Compile(ISymbolicExpressionTreeNode branch, Func<ISymbolicExpressionTreeNode, byte> opCodeMapper, IEnumerable<Func<Instruction, Instruction>> postInstructionCompiledHooks) {
     61    private static IEnumerable<Instruction> CompilePrefix(ISymbolicExpressionTreeNode branch, Func<ISymbolicExpressionTreeNode, byte> opCodeMapper, IEnumerable<Func<Instruction, Instruction>> postInstructionCompiledHooks) {
    6262      foreach (var node in branch.IterateNodesPrefix()) {
     63        Instruction instr = new Instruction();
     64        int subtreesCount = node.SubtreeCount;
     65        if (subtreesCount > 255) throw new ArgumentException("Number of subtrees is too big (>255)");
     66        instr.nArguments = (byte)subtreesCount;
     67        instr.opCode = opCodeMapper(node);
     68        if (node.Symbol is Argument) {
     69          var argNode = (ArgumentTreeNode)node;
     70          instr.iArg0 = (ushort)argNode.Symbol.ArgumentIndex;
     71        }
     72        instr.dynamicNode = node;
     73        foreach (var hook in postInstructionCompiledHooks) {
     74          instr = hook(instr);
     75        }
     76        yield return instr;
     77      }
     78    }
     79
     80    public static Instruction[] CompilePostfix(ISymbolicExpressionTree tree, Func<ISymbolicExpressionTreeNode, byte> opCodeMapper) {
     81      return CompilePostfix(tree, opCodeMapper, Enumerable.Empty<Func<Instruction, Instruction>>());
     82    }
     83    public static Instruction[] CompilePostfix(ISymbolicExpressionTree tree, Func<ISymbolicExpressionTreeNode, byte> opCodeMapper, IEnumerable<Func<Instruction, Instruction>> postInstructionCompiledHooks) {
     84      Dictionary<string, ushort> entryPoint = new Dictionary<string, ushort>();
     85      List<Instruction> code = new List<Instruction>();
     86      // compile main body branches
     87      foreach (var branch in tree.Root.GetSubtree(0).Subtrees) {
     88        code.AddRange(CompilePostfix(branch, opCodeMapper, postInstructionCompiledHooks));
     89      }
     90      // compile function branches
     91      var functionBranches = from node in tree.IterateNodesPostfix()
     92                             where node.Symbol is Defun
     93                             select node;
     94      foreach (DefunTreeNode branch in functionBranches) {
     95        if (code.Count > ushort.MaxValue) throw new ArgumentException("Code for the tree is too long (> ushort.MaxValue).");
     96        entryPoint[branch.FunctionName] = (ushort)code.Count;
     97        code.AddRange(CompilePostfix(branch.GetSubtree(0), opCodeMapper, postInstructionCompiledHooks));
     98      }
     99      // address of all functions is fixed now
     100      // iterate through code again and fill in the jump locations
     101      for (int i = 0; i < code.Count; i++) {
     102        Instruction instr = code[i];
     103        if (instr.dynamicNode.Symbol is InvokeFunction) {
     104          var invokeNode = (InvokeFunctionTreeNode)instr.dynamicNode;
     105          instr.iArg0 = entryPoint[invokeNode.Symbol.FunctionName];
     106        }
     107      }
     108
     109      return code.ToArray();
     110    }
     111
     112    private static IEnumerable<Instruction> CompilePostfix(ISymbolicExpressionTreeNode branch, Func<ISymbolicExpressionTreeNode, byte> opCodeMapper, IEnumerable<Func<Instruction, Instruction>> postInstructionCompiledHooks) {
     113      foreach (var node in branch.IterateNodesPostfix()) {
     114        Instruction instr = new Instruction();
     115        int subtreesCount = node.SubtreeCount;
     116        if (subtreesCount > 255) throw new ArgumentException("Number of subtrees is too big (>255)");
     117        instr.nArguments = (byte)subtreesCount;
     118        instr.opCode = opCodeMapper(node);
     119        if (node.Symbol is Argument) {
     120          var argNode = (ArgumentTreeNode)node;
     121          instr.iArg0 = (ushort)argNode.Symbol.ArgumentIndex;
     122        }
     123        instr.dynamicNode = node;
     124        foreach (var hook in postInstructionCompiledHooks) {
     125          instr = hook(instr);
     126        }
     127        yield return instr;
     128      }
     129    }
     130
     131    public static Instruction[] CompileBreadth(ISymbolicExpressionTree tree, Func<ISymbolicExpressionTreeNode, byte> opCodeMapper) {
     132      return CompileBreadth(tree, opCodeMapper, Enumerable.Empty<Func<Instruction, Instruction>>());
     133    }
     134    public static Instruction[] CompileBreadth(ISymbolicExpressionTree tree, Func<ISymbolicExpressionTreeNode, byte> opCodeMapper, IEnumerable<Func<Instruction, Instruction>> postInstructionCompiledHooks) {
     135      Dictionary<string, ushort> entryPoint = new Dictionary<string, ushort>();
     136      List<Instruction> code = new List<Instruction>();
     137      // compile main body branches
     138      foreach (var branch in tree.Root.GetSubtree(0).Subtrees) {
     139        code.AddRange(CompileBreadth(branch, opCodeMapper, postInstructionCompiledHooks));
     140      }
     141      // compile function branches
     142      var functionBranches = from node in tree.IterateNodesBreadth()
     143                             where node.Symbol is Defun
     144                             select node;
     145      foreach (DefunTreeNode branch in functionBranches) {
     146        if (code.Count > ushort.MaxValue) throw new ArgumentException("Code for the tree is too long (> ushort.MaxValue).");
     147        entryPoint[branch.FunctionName] = (ushort)code.Count;
     148        code.AddRange(CompileBreadth(branch.GetSubtree(0), opCodeMapper, postInstructionCompiledHooks));
     149      }
     150      // address of all functions is fixed now
     151      // iterate through code again and fill in the jump locations
     152      for (int i = 0; i < code.Count; i++) {
     153        Instruction instr = code[i];
     154        if (instr.dynamicNode.Symbol is InvokeFunction) {
     155          var invokeNode = (InvokeFunctionTreeNode)instr.dynamicNode;
     156          instr.iArg0 = entryPoint[invokeNode.Symbol.FunctionName];
     157        }
     158      }
     159
     160      return code.ToArray();
     161    }
     162
     163    private static IEnumerable<Instruction> CompileBreadth(ISymbolicExpressionTreeNode branch, Func<ISymbolicExpressionTreeNode, byte> opCodeMapper, IEnumerable<Func<Instruction, Instruction>> postInstructionCompiledHooks) {
     164      foreach (var node in branch.IterateNodesBreadth()) {
    63165        Instruction instr = new Instruction();
    64166        int subtreesCount = node.SubtreeCount;
  • branches/HeuristicLab.DataAnalysis.Symbolic.LinearInterpreter/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.csproj

    r8600 r9271  
    4141    <DebugType>full</DebugType>
    4242    <Optimize>false</Optimize>
    43     <OutputPath>$(SolutionDir)\bin\</OutputPath>
     43    <OutputPath>..\..\..\..\trunk\sources\bin\</OutputPath>
    4444    <DefineConstants>DEBUG;TRACE</DefineConstants>
    4545    <ErrorReport>prompt</ErrorReport>
     
    5151    <DebugType>pdbonly</DebugType>
    5252    <Optimize>true</Optimize>
    53     <OutputPath>$(SolutionDir)\bin\</OutputPath>
     53    <OutputPath>..\..\..\..\trunk\sources\bin\</OutputPath>
    5454    <DefineConstants>TRACE</DefineConstants>
    5555    <ErrorReport>prompt</ErrorReport>
     
    5959  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' ">
    6060    <DebugSymbols>true</DebugSymbols>
    61     <OutputPath>$(SolutionDir)\bin\</OutputPath>
     61    <OutputPath>..\..\..\..\trunk\sources\bin\</OutputPath>
    6262    <DefineConstants>DEBUG;TRACE</DefineConstants>
    6363    <DebugType>full</DebugType>
     
    6767  </PropertyGroup>
    6868  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x64' ">
    69     <OutputPath>$(SolutionDir)\bin\</OutputPath>
     69    <OutputPath>..\..\..\..\trunk\sources\bin\</OutputPath>
    7070    <DefineConstants>TRACE</DefineConstants>
    7171    <Optimize>true</Optimize>
     
    7777  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
    7878    <DebugSymbols>true</DebugSymbols>
    79     <OutputPath>$(SolutionDir)\bin\</OutputPath>
     79    <OutputPath>..\..\..\..\trunk\sources\bin\</OutputPath>
    8080    <DefineConstants>DEBUG;TRACE</DefineConstants>
    8181    <DebugType>full</DebugType>
     
    8585  </PropertyGroup>
    8686  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
    87     <OutputPath>$(SolutionDir)\bin\</OutputPath>
     87    <OutputPath>..\..\..\..\trunk\sources\bin\</OutputPath>
    8888    <DefineConstants>TRACE</DefineConstants>
    8989    <Optimize>true</Optimize>
     
    9494  </PropertyGroup>
    9595  <ItemGroup>
     96    <Reference Include="HeuristicLab.Analysis-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86">
     97      <SpecificVersion>False</SpecificVersion>
     98      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Analysis-3.3.dll</HintPath>
     99    </Reference>
     100    <Reference Include="HeuristicLab.Collections-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86">
     101      <SpecificVersion>False</SpecificVersion>
     102      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Collections-3.3.dll</HintPath>
     103    </Reference>
     104    <Reference Include="HeuristicLab.Common-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86">
     105      <SpecificVersion>False</SpecificVersion>
     106      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Common-3.3.dll</HintPath>
     107    </Reference>
     108    <Reference Include="HeuristicLab.Common.Resources-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86">
     109      <SpecificVersion>False</SpecificVersion>
     110      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Common.Resources-3.3.dll</HintPath>
     111    </Reference>
     112    <Reference Include="HeuristicLab.Core-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86">
     113      <SpecificVersion>False</SpecificVersion>
     114      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Core-3.3.dll</HintPath>
     115    </Reference>
     116    <Reference Include="HeuristicLab.Data-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86">
     117      <SpecificVersion>False</SpecificVersion>
     118      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Data-3.3.dll</HintPath>
     119    </Reference>
     120    <Reference Include="HeuristicLab.Operators-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86">
     121      <SpecificVersion>False</SpecificVersion>
     122      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Operators-3.3.dll</HintPath>
     123    </Reference>
     124    <Reference Include="HeuristicLab.Optimization-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86">
     125      <SpecificVersion>False</SpecificVersion>
     126      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Optimization-3.3.dll</HintPath>
     127    </Reference>
     128    <Reference Include="HeuristicLab.Optimization.Views-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86">
     129      <SpecificVersion>False</SpecificVersion>
     130      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Optimization.Views-3.3.dll</HintPath>
     131    </Reference>
     132    <Reference Include="HeuristicLab.Parameters-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86">
     133      <SpecificVersion>False</SpecificVersion>
     134      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Parameters-3.3.dll</HintPath>
     135    </Reference>
     136    <Reference Include="HeuristicLab.Persistence-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86">
     137      <SpecificVersion>False</SpecificVersion>
     138      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Persistence-3.3.dll</HintPath>
     139    </Reference>
     140    <Reference Include="HeuristicLab.PluginInfrastructure-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86">
     141      <SpecificVersion>False</SpecificVersion>
     142      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.PluginInfrastructure-3.3.dll</HintPath>
     143    </Reference>
     144    <Reference Include="HeuristicLab.Random-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86">
     145      <SpecificVersion>False</SpecificVersion>
     146      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Random-3.3.dll</HintPath>
     147    </Reference>
    96148    <Reference Include="System" />
    97149    <Reference Include="System.Core">
     
    191243  </ItemGroup>
    192244  <ItemGroup>
    193     <ProjectReference Include="..\..\HeuristicLab.Analysis\3.3\HeuristicLab.Analysis-3.3.csproj">
    194       <Project>{887425B4-4348-49ED-A457-B7D2C26DDBF9}</Project>
    195       <Name>HeuristicLab.Analysis-3.3</Name>
    196       <Private>False</Private>
    197     </ProjectReference>
    198     <ProjectReference Include="..\..\HeuristicLab.Collections\3.3\HeuristicLab.Collections-3.3.csproj">
    199       <Project>{958B43BC-CC5C-4FA2-8628-2B3B01D890B6}</Project>
    200       <Name>HeuristicLab.Collections-3.3</Name>
    201       <Private>False</Private>
    202     </ProjectReference>
    203     <ProjectReference Include="..\..\HeuristicLab.Common.Resources\3.3\HeuristicLab.Common.Resources-3.3.csproj">
    204       <Project>{0E27A536-1C4A-4624-A65E-DC4F4F23E3E1}</Project>
    205       <Name>HeuristicLab.Common.Resources-3.3</Name>
    206       <Private>False</Private>
    207     </ProjectReference>
    208     <ProjectReference Include="..\..\HeuristicLab.Common\3.3\HeuristicLab.Common-3.3.csproj">
    209       <Project>{A9AD58B9-3EF9-4CC1-97E5-8D909039FF5C}</Project>
    210       <Name>HeuristicLab.Common-3.3</Name>
    211       <Private>False</Private>
    212     </ProjectReference>
    213     <ProjectReference Include="..\..\HeuristicLab.Core\3.3\HeuristicLab.Core-3.3.csproj">
    214       <Project>{C36BD924-A541-4A00-AFA8-41701378DDC5}</Project>
    215       <Name>HeuristicLab.Core-3.3</Name>
    216       <Private>False</Private>
    217     </ProjectReference>
    218     <ProjectReference Include="..\..\HeuristicLab.Data\3.3\HeuristicLab.Data-3.3.csproj">
    219       <Project>{BBAB9DF5-5EF3-4BA8-ADE9-B36E82114937}</Project>
    220       <Name>HeuristicLab.Data-3.3</Name>
    221       <Private>False</Private>
    222     </ProjectReference>
    223     <ProjectReference Include="..\..\HeuristicLab.Operators\3.3\HeuristicLab.Operators-3.3.csproj">
    224       <Project>{23DA7FF4-D5B8-41B6-AA96-F0561D24F3EE}</Project>
    225       <Name>HeuristicLab.Operators-3.3</Name>
    226       <Private>False</Private>
    227     </ProjectReference>
    228     <ProjectReference Include="..\..\HeuristicLab.Optimization.Operators\3.3\HeuristicLab.Optimization.Operators-3.3.csproj">
    229       <Project>{25087811-F74C-4128-BC86-8324271DA13E}</Project>
    230       <Name>HeuristicLab.Optimization.Operators-3.3</Name>
    231       <Private>False</Private>
    232     </ProjectReference>
    233     <ProjectReference Include="..\..\HeuristicLab.Optimization\3.3\HeuristicLab.Optimization-3.3.csproj">
    234       <Project>{14AB8D24-25BC-400C-A846-4627AA945192}</Project>
    235       <Name>HeuristicLab.Optimization-3.3</Name>
    236       <Private>False</Private>
    237     </ProjectReference>
    238     <ProjectReference Include="..\..\HeuristicLab.Parameters\3.3\HeuristicLab.Parameters-3.3.csproj">
    239       <Project>{56F9106A-079F-4C61-92F6-86A84C2D84B7}</Project>
    240       <Name>HeuristicLab.Parameters-3.3</Name>
    241       <Private>False</Private>
    242     </ProjectReference>
    243     <ProjectReference Include="..\..\HeuristicLab.Persistence\3.3\HeuristicLab.Persistence-3.3.csproj">
    244       <Project>{102BC7D3-0EF9-439C-8F6D-96FF0FDB8E1B}</Project>
    245       <Name>HeuristicLab.Persistence-3.3</Name>
    246       <Private>False</Private>
    247     </ProjectReference>
    248     <ProjectReference Include="..\..\HeuristicLab.PluginInfrastructure\3.3\HeuristicLab.PluginInfrastructure-3.3.csproj">
    249       <Project>{94186A6A-5176-4402-AE83-886557B53CCA}</Project>
    250       <Name>HeuristicLab.PluginInfrastructure-3.3</Name>
    251       <Private>False</Private>
    252     </ProjectReference>
    253     <ProjectReference Include="..\..\HeuristicLab.Random\3.3\HeuristicLab.Random-3.3.csproj">
    254       <Project>{F4539FB6-4708-40C9-BE64-0A1390AEA197}</Project>
    255       <Name>HeuristicLab.Random-3.3</Name>
    256       <Private>False</Private>
    257     </ProjectReference>
    258   </ItemGroup>
    259   <ItemGroup>
    260245    <BootstrapperPackage Include="Microsoft.Net.Client.3.5">
    261246      <Visible>False</Visible>
     
    283268  -->
    284269  <PropertyGroup>
    285    <PreBuildEvent Condition=" '$(OS)' == 'Windows_NT' ">set Path=%25Path%25;$(ProjectDir);$(SolutionDir)
     270    <PreBuildEvent Condition=" '$(OS)' == 'Windows_NT' ">set Path=%25Path%25;$(ProjectDir);$(SolutionDir)
    286271set ProjectDir=$(ProjectDir)
    287272set SolutionDir=$(SolutionDir)
     
    290275call PreBuildEvent.cmd
    291276</PreBuildEvent>
    292 <PreBuildEvent Condition=" '$(OS)' != 'Windows_NT' ">
     277    <PreBuildEvent Condition=" '$(OS)' != 'Windows_NT' ">
    293278export ProjectDir=$(ProjectDir)
    294279export SolutionDir=$(SolutionDir)
  • branches/HeuristicLab.DataAnalysis.Symbolic.LinearInterpreter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj

    r8946 r9271  
    4141    <DebugType>full</DebugType>
    4242    <Optimize>false</Optimize>
    43     <OutputPath>$(SolutionDir)\bin\</OutputPath>
     43    <OutputPath>..\..\..\..\trunk\sources\bin\</OutputPath>
    4444    <DefineConstants>DEBUG;TRACE</DefineConstants>
    4545    <ErrorReport>prompt</ErrorReport>
     
    5050    <DebugType>pdbonly</DebugType>
    5151    <Optimize>true</Optimize>
    52     <OutputPath>$(SolutionDir)\bin\</OutputPath>
     52    <OutputPath>..\..\..\..\trunk\sources\bin\</OutputPath>
    5353    <DefineConstants>TRACE</DefineConstants>
    5454    <ErrorReport>prompt</ErrorReport>
     
    5858  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' ">
    5959    <DebugSymbols>true</DebugSymbols>
    60     <OutputPath>$(SolutionDir)\bin\</OutputPath>
     60    <OutputPath>..\..\..\..\trunk\sources\bin\</OutputPath>
    6161    <DefineConstants>DEBUG;TRACE</DefineConstants>
    6262    <DebugType>full</DebugType>
     
    6666  </PropertyGroup>
    6767  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x64' ">
    68     <OutputPath>$(SolutionDir)\bin\</OutputPath>
     68    <OutputPath>..\..\..\..\trunk\sources\bin\</OutputPath>
    6969    <DefineConstants>TRACE</DefineConstants>
    7070    <Optimize>true</Optimize>
     
    7676  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
    7777    <DebugSymbols>true</DebugSymbols>
    78     <OutputPath>$(SolutionDir)\bin\</OutputPath>
     78    <OutputPath>..\..\..\..\trunk\sources\bin\</OutputPath>
    7979    <DefineConstants>DEBUG;TRACE</DefineConstants>
    8080    <DebugType>full</DebugType>
     
    8484  </PropertyGroup>
    8585  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
    86     <OutputPath>$(SolutionDir)\bin\</OutputPath>
     86    <OutputPath>..\..\..\..\trunk\sources\bin\</OutputPath>
    8787    <DefineConstants>TRACE</DefineConstants>
    8888    <Optimize>true</Optimize>
     
    9696      <HintPath>..\..\bin\ALGLIB-3.6.0.dll</HintPath>
    9797      <Private>False</Private>
     98    </Reference>
     99    <Reference Include="HeuristicLab.Analysis-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86">
     100      <SpecificVersion>False</SpecificVersion>
     101      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Analysis-3.3.dll</HintPath>
     102    </Reference>
     103    <Reference Include="HeuristicLab.Collections-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86">
     104      <SpecificVersion>False</SpecificVersion>
     105      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Collections-3.3.dll</HintPath>
     106    </Reference>
     107    <Reference Include="HeuristicLab.Common-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86">
     108      <SpecificVersion>False</SpecificVersion>
     109      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Common-3.3.dll</HintPath>
     110    </Reference>
     111    <Reference Include="HeuristicLab.Common.Resources-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86">
     112      <SpecificVersion>False</SpecificVersion>
     113      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Common.Resources-3.3.dll</HintPath>
     114    </Reference>
     115    <Reference Include="HeuristicLab.Core-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86">
     116      <SpecificVersion>False</SpecificVersion>
     117      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Core-3.3.dll</HintPath>
     118    </Reference>
     119    <Reference Include="HeuristicLab.Data-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86">
     120      <SpecificVersion>False</SpecificVersion>
     121      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Data-3.3.dll</HintPath>
     122    </Reference>
     123    <Reference Include="HeuristicLab.Data.Views-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86">
     124      <SpecificVersion>False</SpecificVersion>
     125      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Data.Views-3.3.dll</HintPath>
     126    </Reference>
     127    <Reference Include="HeuristicLab.Operators-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86">
     128      <SpecificVersion>False</SpecificVersion>
     129      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Operators-3.3.dll</HintPath>
     130    </Reference>
     131    <Reference Include="HeuristicLab.Optimization-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86">
     132      <SpecificVersion>False</SpecificVersion>
     133      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Optimization-3.3.dll</HintPath>
     134    </Reference>
     135    <Reference Include="HeuristicLab.Parameters-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86">
     136      <SpecificVersion>False</SpecificVersion>
     137      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Parameters-3.3.dll</HintPath>
     138    </Reference>
     139    <Reference Include="HeuristicLab.Persistence-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86">
     140      <SpecificVersion>False</SpecificVersion>
     141      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Persistence-3.3.dll</HintPath>
     142    </Reference>
     143    <Reference Include="HeuristicLab.PluginInfrastructure-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86">
     144      <SpecificVersion>False</SpecificVersion>
     145      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.PluginInfrastructure-3.3.dll</HintPath>
     146    </Reference>
     147    <Reference Include="HeuristicLab.Problems.DataAnalysis-3.4, Version=3.4.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86">
     148      <SpecificVersion>False</SpecificVersion>
     149      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Problems.DataAnalysis-3.4.dll</HintPath>
     150    </Reference>
     151    <Reference Include="HeuristicLab.Problems.Instances-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86">
     152      <SpecificVersion>False</SpecificVersion>
     153      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Problems.Instances-3.3.dll</HintPath>
     154    </Reference>
     155    <Reference Include="HeuristicLab.Random-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86">
     156      <SpecificVersion>False</SpecificVersion>
     157      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Random-3.3.dll</HintPath>
    98158    </Reference>
    99159    <Reference Include="System" />
     
    141201    <Compile Include="Interfaces\ISymbolicDataAnalysisExpressionCrossover.cs" />
    142202    <Compile Include="Interfaces\ISymbolicDataAnalysisImpactValuesCalculator.cs" />
     203    <Compile Include="Interpreter\SymbolicDataAnalysisExpressionTreeLinearInterpreter.cs" />
    143204    <Compile Include="Interpreter\InterpreterState.cs" />
    144205    <Compile Include="Interpreter\OpCodes.cs" />
     
    250311  </ItemGroup>
    251312  <ItemGroup>
    252     <ProjectReference Include="..\..\HeuristicLab.Analysis\3.3\HeuristicLab.Analysis-3.3.csproj">
    253       <Project>{887425B4-4348-49ED-A457-B7D2C26DDBF9}</Project>
    254       <Name>HeuristicLab.Analysis-3.3</Name>
    255       <Private>False</Private>
    256     </ProjectReference>
    257     <ProjectReference Include="..\..\HeuristicLab.Collections\3.3\HeuristicLab.Collections-3.3.csproj">
    258       <Project>{958B43BC-CC5C-4FA2-8628-2B3B01D890B6}</Project>
    259       <Name>HeuristicLab.Collections-3.3</Name>
    260       <Private>False</Private>
    261     </ProjectReference>
    262     <ProjectReference Include="..\..\HeuristicLab.Common.Resources\3.3\HeuristicLab.Common.Resources-3.3.csproj">
    263       <Project>{0E27A536-1C4A-4624-A65E-DC4F4F23E3E1}</Project>
    264       <Name>HeuristicLab.Common.Resources-3.3</Name>
    265       <Private>False</Private>
    266     </ProjectReference>
    267     <ProjectReference Include="..\..\HeuristicLab.Common\3.3\HeuristicLab.Common-3.3.csproj">
    268       <Project>{A9AD58B9-3EF9-4CC1-97E5-8D909039FF5C}</Project>
    269       <Name>HeuristicLab.Common-3.3</Name>
    270       <Private>False</Private>
    271     </ProjectReference>
    272     <ProjectReference Include="..\..\HeuristicLab.Core\3.3\HeuristicLab.Core-3.3.csproj">
    273       <Project>{C36BD924-A541-4A00-AFA8-41701378DDC5}</Project>
    274       <Name>HeuristicLab.Core-3.3</Name>
    275       <Private>False</Private>
    276     </ProjectReference>
    277     <ProjectReference Include="..\..\HeuristicLab.Data\3.3\HeuristicLab.Data-3.3.csproj">
    278       <Project>{BBAB9DF5-5EF3-4BA8-ADE9-B36E82114937}</Project>
    279       <Name>HeuristicLab.Data-3.3</Name>
    280       <Private>False</Private>
    281     </ProjectReference>
    282313    <ProjectReference Include="..\..\HeuristicLab.Encodings.SymbolicExpressionTreeEncoding\3.4\HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.csproj">
    283314      <Project>{06D4A186-9319-48A0-BADE-A2058D462EEA}</Project>
    284315      <Name>HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4</Name>
    285       <Private>False</Private>
    286     </ProjectReference>
    287     <ProjectReference Include="..\..\HeuristicLab.Operators\3.3\HeuristicLab.Operators-3.3.csproj">
    288       <Project>{23DA7FF4-D5B8-41B6-AA96-F0561D24F3EE}</Project>
    289       <Name>HeuristicLab.Operators-3.3</Name>
    290       <Private>False</Private>
    291     </ProjectReference>
    292     <ProjectReference Include="..\..\HeuristicLab.Optimization.Operators\3.3\HeuristicLab.Optimization.Operators-3.3.csproj">
    293       <Project>{25087811-F74C-4128-BC86-8324271DA13E}</Project>
    294       <Name>HeuristicLab.Optimization.Operators-3.3</Name>
    295       <Private>False</Private>
    296     </ProjectReference>
    297     <ProjectReference Include="..\..\HeuristicLab.Optimization\3.3\HeuristicLab.Optimization-3.3.csproj">
    298       <Project>{14AB8D24-25BC-400C-A846-4627AA945192}</Project>
    299       <Name>HeuristicLab.Optimization-3.3</Name>
    300     </ProjectReference>
    301     <ProjectReference Include="..\..\HeuristicLab.Parameters\3.3\HeuristicLab.Parameters-3.3.csproj">
    302       <Project>{56F9106A-079F-4C61-92F6-86A84C2D84B7}</Project>
    303       <Name>HeuristicLab.Parameters-3.3</Name>
    304       <Private>False</Private>
    305     </ProjectReference>
    306     <ProjectReference Include="..\..\HeuristicLab.Persistence\3.3\HeuristicLab.Persistence-3.3.csproj">
    307       <Project>{102BC7D3-0EF9-439C-8F6D-96FF0FDB8E1B}</Project>
    308       <Name>HeuristicLab.Persistence-3.3</Name>
    309       <Private>False</Private>
    310     </ProjectReference>
    311     <ProjectReference Include="..\..\HeuristicLab.PluginInfrastructure\3.3\HeuristicLab.PluginInfrastructure-3.3.csproj">
    312       <Project>{94186A6A-5176-4402-AE83-886557B53CCA}</Project>
    313       <Name>HeuristicLab.PluginInfrastructure-3.3</Name>
    314       <Private>False</Private>
    315     </ProjectReference>
    316     <ProjectReference Include="..\..\HeuristicLab.Problems.DataAnalysis\3.4\HeuristicLab.Problems.DataAnalysis-3.4.csproj">
    317       <Project>{DF87C13E-A889-46FF-8153-66DCAA8C5674}</Project>
    318       <Name>HeuristicLab.Problems.DataAnalysis-3.4</Name>
    319       <Private>False</Private>
    320     </ProjectReference>
    321     <ProjectReference Include="..\..\HeuristicLab.Problems.Instances\3.3\HeuristicLab.Problems.Instances-3.3.csproj">
    322       <Project>{3540E29E-4793-49E7-8EE2-FEA7F61C3994}</Project>
    323       <Name>HeuristicLab.Problems.Instances-3.3</Name>
    324       <Private>False</Private>
    325     </ProjectReference>
    326     <ProjectReference Include="..\..\HeuristicLab.Random\3.3\HeuristicLab.Random-3.3.csproj">
    327       <Project>{F4539FB6-4708-40C9-BE64-0A1390AEA197}</Project>
    328       <Name>HeuristicLab.Random-3.3</Name>
    329316      <Private>False</Private>
    330317    </ProjectReference>
  • branches/HeuristicLab.DataAnalysis.Symbolic.LinearInterpreter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeILEmittingInterpreter.cs

    r8798 r9271  
    162162
    163163    private InterpreterState PrepareInterpreterState(ISymbolicExpressionTree tree, Dataset dataset) {
    164       Instruction[] code = SymbolicExpressionTreeCompiler.Compile(tree, OpCodes.MapSymbolToOpCode);
     164      Instruction[] code = SymbolicExpressionTreeCompiler.CompilePrefix(tree, OpCodes.MapSymbolToOpCode);
    165165      Dictionary<string, int> doubleVariableNames = dataset.DoubleVariables.Select((x, i) => new { x, i }).ToDictionary(e => e.x, e => e.i);
    166166      int necessaryArgStackSize = 0;
  • branches/HeuristicLab.DataAnalysis.Symbolic.LinearInterpreter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeInterpreter.cs

    r9004 r9271  
    112112
    113113    private static InterpreterState PrepareInterpreterState(ISymbolicExpressionTree tree, Dataset dataset) {
    114       Instruction[] code = SymbolicExpressionTreeCompiler.Compile(tree, OpCodes.MapSymbolToOpCode);
     114      Instruction[] code = SymbolicExpressionTreeCompiler.CompilePrefix(tree, OpCodes.MapSymbolToOpCode);
    115115      int necessaryArgStackSize = 0;
    116116      foreach (Instruction instr in code) {
  • branches/HeuristicLab.DataAnalysis.Symbolic.LinearInterpreter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeLinearInterpreter.cs

    r9257 r9271  
    3131namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    3232  [StorableClass]
    33   [Item("SymbolicDataAnalysisExpressionTreeInterpreter", "Interpreter for symbolic expression trees including automatically defined functions.")]
    34   public class SymbolicDataAnalysisExpressionTreeInterpreter : ParameterizedNamedItem, ISymbolicDataAnalysisExpressionTreeInterpreter {
     33  [Item("SymbolicDataAnalysisExpressionTreeLinearInterpreter", "Interpreter for symbolic expression trees including automatically defined functions.")]
     34  public class SymbolicDataAnalysisExpressionTreeLinearInterpreter : ParameterizedNamedItem, ISymbolicDataAnalysisExpressionTreeInterpreter {
    3535    private const string CheckExpressionsWithIntervalArithmeticParameterName = "CheckExpressionsWithIntervalArithmetic";
    3636    private const string EvaluatedSolutionsParameterName = "EvaluatedSolutions";
     
    6262
    6363    [StorableConstructor]
    64     protected SymbolicDataAnalysisExpressionTreeInterpreter(bool deserializing) : base(deserializing) { }
    65     protected SymbolicDataAnalysisExpressionTreeInterpreter(SymbolicDataAnalysisExpressionTreeInterpreter original, Cloner cloner) : base(original, cloner) { }
     64    protected SymbolicDataAnalysisExpressionTreeLinearInterpreter(bool deserializing) : base(deserializing) { }
     65    protected SymbolicDataAnalysisExpressionTreeLinearInterpreter(SymbolicDataAnalysisExpressionTreeLinearInterpreter original, Cloner cloner) : base(original, cloner) { }
    6666    public override IDeepCloneable Clone(Cloner cloner) {
    67       return new SymbolicDataAnalysisExpressionTreeInterpreter(this, cloner);
    68     }
    69 
    70     public SymbolicDataAnalysisExpressionTreeInterpreter()
    71       : base("SymbolicDataAnalysisExpressionTreeInterpreter", "Interpreter for symbolic expression trees including automatically defined functions.") {
     67      return new SymbolicDataAnalysisExpressionTreeLinearInterpreter(this, cloner);
     68    }
     69
     70    public SymbolicDataAnalysisExpressionTreeLinearInterpreter()
     71      : base("SymbolicDataAnalysisExpressionTreeLinearInterpreter", "Interpreter for symbolic expression trees including automatically defined functions.") {
    7272      Parameters.Add(new ValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false)));
    7373      Parameters.Add(new ValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
    7474    }
    7575
    76     protected SymbolicDataAnalysisExpressionTreeInterpreter(string name, string description)
     76    protected SymbolicDataAnalysisExpressionTreeLinearInterpreter(string name, string description)
    7777      : base(name, description) {
    7878      Parameters.Add(new ValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false)));
     
    102102        EvaluatedSolutions.Value++; // increment the evaluated solutions counter
    103103      }
    104       var state = PrepareInterpreterState(tree, dataset);
     104
     105      Instruction[] code = SymbolicExpressionTreeCompiler.CompilePostfix(tree, OpCodes.MapSymbolToOpCode);
     106      foreach (Instruction instr in code) {
     107        switch (instr.opCode) {
     108          case OpCodes.Variable: {
     109              var variableTreeNode = (VariableTreeNode)instr.dynamicNode;
     110              instr.iArg0 = dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName);
     111            }
     112            break;
     113          case OpCodes.LagVariable: {
     114              var laggedVariableTreeNode = (LaggedVariableTreeNode)instr.dynamicNode;
     115              instr.iArg0 = dataset.GetReadOnlyDoubleValues(laggedVariableTreeNode.VariableName);
     116            }
     117            break;
     118          case OpCodes.VariableCondition: {
     119              var variableConditionTreeNode = (VariableConditionTreeNode)instr.dynamicNode;
     120              instr.iArg0 = dataset.GetReadOnlyDoubleValues(variableConditionTreeNode.VariableName);
     121            }
     122            break;
     123        }
     124      }
    105125
    106126      foreach (var rowEnum in rows) {
    107127        int row = rowEnum;
    108         yield return Evaluate(dataset, ref row, state);
    109         state.Reset();
     128        yield return Evaluate(dataset, ref row, code);
    110129      }
    111130    }
    112131
    113     private static InterpreterState PrepareInterpreterState(ISymbolicExpressionTree tree, Dataset dataset) {
    114       Instruction[] code = SymbolicExpressionTreeCompiler.Compile(tree, OpCodes.MapSymbolToOpCode);
    115       int necessaryArgStackSize = 0;
    116       foreach (Instruction instr in code) {
    117         if (instr.opCode == OpCodes.Variable) {
    118           var variableTreeNode = (VariableTreeNode)instr.dynamicNode;
    119           instr.iArg0 = dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName);
    120         } else if (instr.opCode == OpCodes.LagVariable) {
    121           var laggedVariableTreeNode = (LaggedVariableTreeNode)instr.dynamicNode;
    122           instr.iArg0 = dataset.GetReadOnlyDoubleValues(laggedVariableTreeNode.VariableName);
    123         } else if (instr.opCode == OpCodes.VariableCondition) {
    124           var variableConditionTreeNode = (VariableConditionTreeNode)instr.dynamicNode;
    125           instr.iArg0 = dataset.GetReadOnlyDoubleValues(variableConditionTreeNode.VariableName);
    126         } else if (instr.opCode == OpCodes.Call) {
    127           necessaryArgStackSize += instr.nArguments + 1;
     132    protected virtual double Evaluate(Dataset dataset, ref int row, Instruction[] code) {
     133      int count = 0;
     134      for (int j = 0; j != code.Length; ++j) {
     135        Instruction currentInstr = code[j];
     136
     137        switch (currentInstr.opCode) {
     138          case OpCodes.Add: {
     139              double s = code[j - currentInstr.nArguments].value;
     140              for (int i = j - currentInstr.nArguments + 1; i < j; i++) {
     141                s += code[i].value;
     142              }
     143              currentInstr.value = s;
     144            }
     145            break;
     146          case OpCodes.Sub: {
     147              double s = code[j - currentInstr.nArguments].value;
     148              for (int i = j - currentInstr.nArguments + 1; i < j; i++) {
     149                s -= code[i].value;
     150              }
     151              if (currentInstr.nArguments == 1) s = -s;
     152              currentInstr.value = s;
     153            }
     154            break;
     155          case OpCodes.Mul: {
     156              double p = code[j - currentInstr.nArguments].value;
     157              for (int i = j - currentInstr.nArguments + 1; i < j; i++) {
     158                p *= code[i].value;
     159              }
     160              currentInstr.value = p;
     161            }
     162            break;
     163          case OpCodes.Div: {
     164              double p = code[j - currentInstr.nArguments].value;
     165              for (int i = j - currentInstr.nArguments + 1; i < j; i++) {
     166                p /= code[i].value;
     167              }
     168              if (currentInstr.nArguments == 1) p = 1.0 / p;
     169              currentInstr.value = p;
     170            }
     171            break;
     172          case OpCodes.Average: {
     173              double sum = code[j - currentInstr.nArguments].value;
     174              for (int i = j - currentInstr.nArguments + 1; i < j; i++) {
     175                sum += code[i].value;
     176              }
     177              currentInstr.value = sum / currentInstr.nArguments;
     178            }
     179            break;
     180          case OpCodes.Cos: {
     181              currentInstr.value = Math.Cos(code[j - 1].value);
     182            }
     183            break;
     184          case OpCodes.Sin: {
     185              currentInstr.value = Math.Sin(code[j - 1].value);
     186              break;
     187            }
     188          case OpCodes.Tan: {
     189              currentInstr.value = Math.Tan(code[j - 1].value);
     190            }
     191            break;
     192          case OpCodes.Square: {
     193              currentInstr.value = Math.Pow(code[j - 1].value, 2);
     194            }
     195            break;
     196          case OpCodes.Power: {
     197              double x = code[j - 2].value;
     198              double y = Math.Round(code[j - 1].value);
     199              currentInstr.value = Math.Pow(x, y);
     200            }
     201            break;
     202          case OpCodes.SquareRoot: {
     203              currentInstr.value = Math.Sqrt(code[j - 1].value);
     204            }
     205            break;
     206          case OpCodes.Root: {
     207              double x = code[j - 2].value;
     208              double y = Math.Round(code[j - 1].value);
     209              currentInstr.value = Math.Pow(x, 1 / y);
     210            }
     211            break;
     212          case OpCodes.Exp: {
     213              currentInstr.value = Math.Exp(code[j - 1].value);
     214            }
     215            break;
     216          case OpCodes.Log: {
     217              currentInstr.value = Math.Log(code[j - 1].value);
     218            }
     219            break;
     220          case OpCodes.Gamma: {
     221              currentInstr.value = double.IsNaN(code[j - 1].value) ? double.NaN : alglib.gammafunction(code[j - 1].value);
     222            }
     223            break;
     224          case OpCodes.Psi: {
     225              var x = code[j - 1].value;
     226              if (double.IsNaN(x)) currentInstr.value = double.NaN;
     227              else if (x <= 0 && (Math.Floor(x) - x).IsAlmost(0)) currentInstr.value = double.NaN;
     228              else currentInstr.value = alglib.psi(x);
     229            }
     230            break;
     231          case OpCodes.Dawson: {
     232              var x = code[j - 1].value;
     233              currentInstr.value = double.IsNaN(x) ? double.NaN : alglib.dawsonintegral(x);
     234            }
     235            break;
     236          case OpCodes.ExponentialIntegralEi: {
     237              var x = code[j - 1].value;
     238              currentInstr.value = double.IsNaN(x) ? double.NaN : alglib.exponentialintegralei(x);
     239            }
     240            break;
     241          case OpCodes.SineIntegral: {
     242              double si, ci;
     243              var x = code[j - 1].value;
     244              if (double.IsNaN(x)) currentInstr.value = double.NaN;
     245              else {
     246                alglib.sinecosineintegrals(x, out si, out ci);
     247                currentInstr.value = si;
     248              }
     249            }
     250            break;
     251          case OpCodes.CosineIntegral: {
     252              double si, ci;
     253              var x = code[j - 1].value;
     254              if (double.IsNaN(x)) currentInstr.value = double.NaN;
     255              else {
     256                alglib.sinecosineintegrals(x, out si, out ci);
     257                currentInstr.value = ci;
     258              }
     259            }
     260            break;
     261          case OpCodes.HyperbolicSineIntegral: {
     262              double shi, chi;
     263              var x = code[j - 1].value;
     264              if (double.IsNaN(x)) currentInstr.value = double.NaN;
     265              else {
     266                alglib.hyperbolicsinecosineintegrals(x, out shi, out chi);
     267                currentInstr.value = shi;
     268              }
     269            }
     270            break;
     271          case OpCodes.HyperbolicCosineIntegral: {
     272              double shi, chi;
     273              var x = code[j - 1].value;
     274              if (double.IsNaN(x)) currentInstr.value = double.NaN;
     275              else {
     276                alglib.hyperbolicsinecosineintegrals(x, out shi, out chi);
     277                currentInstr.value = chi;
     278              }
     279            }
     280            break;
     281          case OpCodes.FresnelCosineIntegral: {
     282              double c = 0, s = 0;
     283              var x = code[j - 1].value;
     284              if (double.IsNaN(x)) currentInstr.value = double.NaN;
     285              else {
     286                alglib.fresnelintegral(x, ref c, ref s);
     287                currentInstr.value = c;
     288              }
     289            }
     290            break;
     291          case OpCodes.FresnelSineIntegral: {
     292              double c = 0, s = 0;
     293              var x = code[j - 1].value;
     294              if (double.IsNaN(x)) currentInstr.value = double.NaN;
     295              else {
     296                alglib.fresnelintegral(x, ref c, ref s);
     297                currentInstr.value = s;
     298              }
     299            }
     300            break;
     301          case OpCodes.AiryA: {
     302              double ai, aip, bi, bip;
     303              var x = code[j - 1].value;
     304              if (double.IsNaN(x)) currentInstr.value = double.NaN;
     305              else {
     306                alglib.airy(x, out ai, out aip, out bi, out bip);
     307                currentInstr.value = ai;
     308              }
     309            }
     310            break;
     311          case OpCodes.AiryB: {
     312              double ai, aip, bi, bip;
     313              var x = code[j - 1].value;
     314              if (double.IsNaN(x)) currentInstr.value = double.NaN;
     315              else {
     316                alglib.airy(x, out ai, out aip, out bi, out bip);
     317                currentInstr.value = bi;
     318              }
     319            }
     320            break;
     321          case OpCodes.Norm: {
     322              var x = code[j - 1].value;
     323              currentInstr.value = double.IsNaN(x) ? double.NaN : alglib.normaldistribution(x);
     324            }
     325            break;
     326          case OpCodes.Erf: {
     327              var x = code[j - 1].value;
     328              currentInstr.value = double.IsNaN(x) ? double.NaN : alglib.errorfunction(x);
     329            }
     330            break;
     331          case OpCodes.Bessel: {
     332              var x = code[j - 1].value;
     333              currentInstr.value = double.IsNaN(x) ? double.NaN : alglib.besseli0(x);
     334            }
     335            break;
     336          case OpCodes.IfThenElse: {
     337              double condition = code[j - currentInstr.nArguments].value;
     338              double result = condition > 0.0 ? code[j - 2].value : code[j - 1].value;
     339              currentInstr.value = result;
     340            }
     341            break;
     342          case OpCodes.AND: {
     343              double result = code[j - currentInstr.nArguments].value;
     344              for (int i = j - currentInstr.nArguments + 1; i < j; i++) {
     345                if (result > 0.0) result = code[i].value;
     346              }
     347              currentInstr.value = result > 0.0 ? 1.0 : -1.0;
     348            }
     349            break;
     350          case OpCodes.OR: {
     351              double result = code[j - currentInstr.nArguments].value;
     352              for (int i = 1; i < currentInstr.nArguments; i++) {
     353                if (result <= 0.0) result = code[i].value; ;
     354              }
     355              currentInstr.value = result > 0.0 ? 1.0 : -1.0;
     356            }
     357            break;
     358          case OpCodes.NOT: {
     359              currentInstr.value = code[j - 1].value > 0.0 ? -1.0 : 1.0;
     360            }
     361            break;
     362          case OpCodes.GT: {
     363              double x = code[j - 2].value;
     364              double y = code[j - 1].value;
     365              currentInstr.value = x > y ? 1.0 : -1.0;
     366            }
     367            break;
     368          case OpCodes.LT: {
     369              double x = code[j - 2].value;
     370              double y = code[j - 1].value;
     371              currentInstr.value = x < y ? 1.0 : -1.0;
     372            }
     373            break;
     374          case OpCodes.TimeLag: {
     375              throw new NotSupportedException();
     376            }
     377          case OpCodes.Integral: {
     378              throw new NotSupportedException();
     379            }
     380
     381          //mkommend: derivate calculation taken from:
     382          //http://www.holoborodko.com/pavel/numerical-methods/numerical-derivative/smooth-low-noise-differentiators/
     383          //one sided smooth differentiatior, N = 4
     384          // y' = 1/8h (f_i + 2f_i-1, -2 f_i-3 - f_i-4)
     385          case OpCodes.Derivative: {
     386              throw new NotSupportedException();
     387            }
     388          case OpCodes.Call: {
     389              throw new NotSupportedException();
     390            }
     391          case OpCodes.Arg: {
     392              throw new NotSupportedException();
     393            }
     394          case OpCodes.Variable: {
     395              if (row < 0 || row >= dataset.Rows) currentInstr.value = double.NaN;
     396              else {
     397                var variableTreeNode = (VariableTreeNode)currentInstr.dynamicNode;
     398                currentInstr.value = ((IList<double>)currentInstr.iArg0)[row] * variableTreeNode.Weight;
     399              }
     400            }
     401            break;
     402          case OpCodes.LagVariable: {
     403              var laggedVariableTreeNode = (LaggedVariableTreeNode)currentInstr.dynamicNode;
     404              int actualRow = row + laggedVariableTreeNode.Lag;
     405              if (actualRow < 0 || actualRow >= dataset.Rows) currentInstr.value = double.NaN;
     406              else {
     407                currentInstr.value = ((IList<double>)currentInstr.iArg0)[actualRow] * laggedVariableTreeNode.Weight;
     408              }
     409            }
     410            break;
     411          case OpCodes.Constant: {
     412              var constTreeNode = (ConstantTreeNode)currentInstr.dynamicNode;
     413              currentInstr.value = constTreeNode.Value;
     414            }
     415            break;
     416
     417          //mkommend: this symbol uses the logistic function f(x) = 1 / (1 + e^(-alpha * x) )
     418          //to determine the relative amounts of the true and false branch see http://en.wikipedia.org/wiki/Logistic_function
     419          case OpCodes.VariableCondition: {
     420              if (row < 0 || row >= dataset.Rows) currentInstr.value = double.NaN;
     421              else {
     422                var variableConditionTreeNode = (VariableConditionTreeNode)currentInstr.dynamicNode;
     423                double variableValue = ((IList<double>)currentInstr.iArg0)[row];
     424                double x = variableValue - variableConditionTreeNode.Threshold;
     425                double p = 1 / (1 + Math.Exp(-variableConditionTreeNode.Slope * x));
     426
     427                double trueBranch = code[j - currentInstr.nArguments].value;
     428                double falseBranch = code[j - currentInstr.nArguments + 1].value;
     429
     430                currentInstr.value = trueBranch * p + falseBranch * (1 - p);
     431              }
     432            }
     433            break;
     434          default:
     435            throw new NotSupportedException();
    128436        }
     437
     438        // book-keeping in order to keep all the values correct
     439        int narg = currentInstr.nArguments;
     440
     441        // we count the values before the next function is encountered
     442        if (narg == 0) { ++count; continue; }
     443
     444        if (count > narg) {
     445          for (int i = 1; i <= count - narg; ++i)
     446            code[j - i].value = code[j - i - narg].value;
     447        }
     448
     449        count -= (narg - 1); // because after being evaluated the current instruction becomes a 'value'
    129450      }
    130       return new InterpreterState(code, necessaryArgStackSize);
    131     }
    132 
    133 
    134     protected virtual double Evaluate(Dataset dataset, ref int row, InterpreterState state) {
    135       Instruction currentInstr = state.NextInstruction();
    136       switch (currentInstr.opCode) {
    137         case OpCodes.Add: {
    138             double s = Evaluate(dataset, ref row, state);
    139             for (int i = 1; i < currentInstr.nArguments; i++) {
    140               s += Evaluate(dataset, ref row, state);
    141             }
    142             return s;
    143           }
    144         case OpCodes.Sub: {
    145             double s = Evaluate(dataset, ref row, state);
    146             for (int i = 1; i < currentInstr.nArguments; i++) {
    147               s -= Evaluate(dataset, ref row, state);
    148             }
    149             if (currentInstr.nArguments == 1) s = -s;
    150             return s;
    151           }
    152         case OpCodes.Mul: {
    153             double p = Evaluate(dataset, ref row, state);
    154             for (int i = 1; i < currentInstr.nArguments; i++) {
    155               p *= Evaluate(dataset, ref row, state);
    156             }
    157             return p;
    158           }
    159         case OpCodes.Div: {
    160             double p = Evaluate(dataset, ref row, state);
    161             for (int i = 1; i < currentInstr.nArguments; i++) {
    162               p /= Evaluate(dataset, ref row, state);
    163             }
    164             if (currentInstr.nArguments == 1) p = 1.0 / p;
    165             return p;
    166           }
    167         case OpCodes.Average: {
    168             double sum = Evaluate(dataset, ref row, state);
    169             for (int i = 1; i < currentInstr.nArguments; i++) {
    170               sum += Evaluate(dataset, ref row, state);
    171             }
    172             return sum / currentInstr.nArguments;
    173           }
    174         case OpCodes.Cos: {
    175             return Math.Cos(Evaluate(dataset, ref row, state));
    176           }
    177         case OpCodes.Sin: {
    178             return Math.Sin(Evaluate(dataset, ref row, state));
    179           }
    180         case OpCodes.Tan: {
    181             return Math.Tan(Evaluate(dataset, ref row, state));
    182           }
    183         case OpCodes.Square: {
    184             return Math.Pow(Evaluate(dataset, ref row, state), 2);
    185           }
    186         case OpCodes.Power: {
    187             double x = Evaluate(dataset, ref row, state);
    188             double y = Math.Round(Evaluate(dataset, ref row, state));
    189             return Math.Pow(x, y);
    190           }
    191         case OpCodes.SquareRoot: {
    192             return Math.Sqrt(Evaluate(dataset, ref row, state));
    193           }
    194         case OpCodes.Root: {
    195             double x = Evaluate(dataset, ref row, state);
    196             double y = Math.Round(Evaluate(dataset, ref row, state));
    197             return Math.Pow(x, 1 / y);
    198           }
    199         case OpCodes.Exp: {
    200             return Math.Exp(Evaluate(dataset, ref row, state));
    201           }
    202         case OpCodes.Log: {
    203             return Math.Log(Evaluate(dataset, ref row, state));
    204           }
    205         case OpCodes.Gamma: {
    206             var x = Evaluate(dataset, ref row, state);
    207             if (double.IsNaN(x)) return double.NaN;
    208             else return alglib.gammafunction(x);
    209           }
    210         case OpCodes.Psi: {
    211             var x = Evaluate(dataset, ref row, state);
    212             if (double.IsNaN(x)) return double.NaN;
    213             else if (x <= 0 && (Math.Floor(x) - x).IsAlmost(0)) return double.NaN;
    214             return alglib.psi(x);
    215           }
    216         case OpCodes.Dawson: {
    217             var x = Evaluate(dataset, ref row, state);
    218             if (double.IsNaN(x)) return double.NaN;
    219             return alglib.dawsonintegral(x);
    220           }
    221         case OpCodes.ExponentialIntegralEi: {
    222             var x = Evaluate(dataset, ref row, state);
    223             if (double.IsNaN(x)) return double.NaN;
    224             return alglib.exponentialintegralei(x);
    225           }
    226         case OpCodes.SineIntegral: {
    227             double si, ci;
    228             var x = Evaluate(dataset, ref row, state);
    229             if (double.IsNaN(x)) return double.NaN;
    230             else {
    231               alglib.sinecosineintegrals(x, out si, out ci);
    232               return si;
    233             }
    234           }
    235         case OpCodes.CosineIntegral: {
    236             double si, ci;
    237             var x = Evaluate(dataset, ref row, state);
    238             if (double.IsNaN(x)) return double.NaN;
    239             else {
    240               alglib.sinecosineintegrals(x, out si, out ci);
    241               return ci;
    242             }
    243           }
    244         case OpCodes.HyperbolicSineIntegral: {
    245             double shi, chi;
    246             var x = Evaluate(dataset, ref row, state);
    247             if (double.IsNaN(x)) return double.NaN;
    248             else {
    249               alglib.hyperbolicsinecosineintegrals(x, out shi, out chi);
    250               return shi;
    251             }
    252           }
    253         case OpCodes.HyperbolicCosineIntegral: {
    254             double shi, chi;
    255             var x = Evaluate(dataset, ref row, state);
    256             if (double.IsNaN(x)) return double.NaN;
    257             else {
    258               alglib.hyperbolicsinecosineintegrals(x, out shi, out chi);
    259               return chi;
    260             }
    261           }
    262         case OpCodes.FresnelCosineIntegral: {
    263             double c = 0, s = 0;
    264             var x = Evaluate(dataset, ref row, state);
    265             if (double.IsNaN(x)) return double.NaN;
    266             else {
    267               alglib.fresnelintegral(x, ref c, ref s);
    268               return c;
    269             }
    270           }
    271         case OpCodes.FresnelSineIntegral: {
    272             double c = 0, s = 0;
    273             var x = Evaluate(dataset, ref row, state);
    274             if (double.IsNaN(x)) return double.NaN;
    275             else {
    276               alglib.fresnelintegral(x, ref c, ref s);
    277               return s;
    278             }
    279           }
    280         case OpCodes.AiryA: {
    281             double ai, aip, bi, bip;
    282             var x = Evaluate(dataset, ref row, state);
    283             if (double.IsNaN(x)) return double.NaN;
    284             else {
    285               alglib.airy(x, out ai, out aip, out bi, out bip);
    286               return ai;
    287             }
    288           }
    289         case OpCodes.AiryB: {
    290             double ai, aip, bi, bip;
    291             var x = Evaluate(dataset, ref row, state);
    292             if (double.IsNaN(x)) return double.NaN;
    293             else {
    294               alglib.airy(x, out ai, out aip, out bi, out bip);
    295               return bi;
    296             }
    297           }
    298         case OpCodes.Norm: {
    299             var x = Evaluate(dataset, ref row, state);
    300             if (double.IsNaN(x)) return double.NaN;
    301             else return alglib.normaldistribution(x);
    302           }
    303         case OpCodes.Erf: {
    304             var x = Evaluate(dataset, ref row, state);
    305             if (double.IsNaN(x)) return double.NaN;
    306             else return alglib.errorfunction(x);
    307           }
    308         case OpCodes.Bessel: {
    309             var x = Evaluate(dataset, ref row, state);
    310             if (double.IsNaN(x)) return double.NaN;
    311             else return alglib.besseli0(x);
    312           }
    313         case OpCodes.IfThenElse: {
    314             double condition = Evaluate(dataset, ref row, state);
    315             double result;
    316             if (condition > 0.0) {
    317               result = Evaluate(dataset, ref row, state); state.SkipInstructions();
    318             } else {
    319               state.SkipInstructions(); result = Evaluate(dataset, ref row, state);
    320             }
    321             return result;
    322           }
    323         case OpCodes.AND: {
    324             double result = Evaluate(dataset, ref row, state);
    325             for (int i = 1; i < currentInstr.nArguments; i++) {
    326               if (result > 0.0) result = Evaluate(dataset, ref row, state);
    327               else {
    328                 state.SkipInstructions();
    329               }
    330             }
    331             return result > 0.0 ? 1.0 : -1.0;
    332           }
    333         case OpCodes.OR: {
    334             double result = Evaluate(dataset, ref row, state);
    335             for (int i = 1; i < currentInstr.nArguments; i++) {
    336               if (result <= 0.0) result = Evaluate(dataset, ref row, state);
    337               else {
    338                 state.SkipInstructions();
    339               }
    340             }
    341             return result > 0.0 ? 1.0 : -1.0;
    342           }
    343         case OpCodes.NOT: {
    344             return Evaluate(dataset, ref row, state) > 0.0 ? -1.0 : 1.0;
    345           }
    346         case OpCodes.GT: {
    347             double x = Evaluate(dataset, ref row, state);
    348             double y = Evaluate(dataset, ref row, state);
    349             if (x > y) return 1.0;
    350             else return -1.0;
    351           }
    352         case OpCodes.LT: {
    353             double x = Evaluate(dataset, ref row, state);
    354             double y = Evaluate(dataset, ref row, state);
    355             if (x < y) return 1.0;
    356             else return -1.0;
    357           }
    358         case OpCodes.TimeLag: {
    359             var timeLagTreeNode = (LaggedTreeNode)currentInstr.dynamicNode;
    360             row += timeLagTreeNode.Lag;
    361             double result = Evaluate(dataset, ref row, state);
    362             row -= timeLagTreeNode.Lag;
    363             return result;
    364           }
    365         case OpCodes.Integral: {
    366             int savedPc = state.ProgramCounter;
    367             var timeLagTreeNode = (LaggedTreeNode)currentInstr.dynamicNode;
    368             double sum = 0.0;
    369             for (int i = 0; i < Math.Abs(timeLagTreeNode.Lag); i++) {
    370               row += Math.Sign(timeLagTreeNode.Lag);
    371               sum += Evaluate(dataset, ref row, state);
    372               state.ProgramCounter = savedPc;
    373             }
    374             row -= timeLagTreeNode.Lag;
    375             sum += Evaluate(dataset, ref row, state);
    376             return sum;
    377           }
    378 
    379         //mkommend: derivate calculation taken from:
    380         //http://www.holoborodko.com/pavel/numerical-methods/numerical-derivative/smooth-low-noise-differentiators/
    381         //one sided smooth differentiatior, N = 4
    382         // y' = 1/8h (f_i + 2f_i-1, -2 f_i-3 - f_i-4)
    383         case OpCodes.Derivative: {
    384             int savedPc = state.ProgramCounter;
    385             double f_0 = Evaluate(dataset, ref row, state); row--;
    386             state.ProgramCounter = savedPc;
    387             double f_1 = Evaluate(dataset, ref row, state); row -= 2;
    388             state.ProgramCounter = savedPc;
    389             double f_3 = Evaluate(dataset, ref row, state); row--;
    390             state.ProgramCounter = savedPc;
    391             double f_4 = Evaluate(dataset, ref row, state);
    392             row += 4;
    393 
    394             return (f_0 + 2 * f_1 - 2 * f_3 - f_4) / 8; // h = 1
    395           }
    396         case OpCodes.Call: {
    397             // evaluate sub-trees
    398             double[] argValues = new double[currentInstr.nArguments];
    399             for (int i = 0; i < currentInstr.nArguments; i++) {
    400               argValues[i] = Evaluate(dataset, ref row, state);
    401             }
    402             // push on argument values on stack
    403             state.CreateStackFrame(argValues);
    404 
    405             // save the pc
    406             int savedPc = state.ProgramCounter;
    407             // set pc to start of function 
    408             state.ProgramCounter = (ushort)currentInstr.iArg0;
    409             // evaluate the function
    410             double v = Evaluate(dataset, ref row, state);
    411 
    412             // delete the stack frame
    413             state.RemoveStackFrame();
    414 
    415             // restore the pc => evaluation will continue at point after my subtrees 
    416             state.ProgramCounter = savedPc;
    417             return v;
    418           }
    419         case OpCodes.Arg: {
    420             return state.GetStackFrameValue((ushort)currentInstr.iArg0);
    421           }
    422         case OpCodes.Variable: {
    423             if (row < 0 || row >= dataset.Rows) return double.NaN;
    424             var variableTreeNode = (VariableTreeNode)currentInstr.dynamicNode;
    425             return ((IList<double>)currentInstr.iArg0)[row] * variableTreeNode.Weight;
    426           }
    427         case OpCodes.LagVariable: {
    428             var laggedVariableTreeNode = (LaggedVariableTreeNode)currentInstr.dynamicNode;
    429             int actualRow = row + laggedVariableTreeNode.Lag;
    430             if (actualRow < 0 || actualRow >= dataset.Rows) return double.NaN;
    431             return ((IList<double>)currentInstr.iArg0)[actualRow] * laggedVariableTreeNode.Weight;
    432           }
    433         case OpCodes.Constant: {
    434             var constTreeNode = (ConstantTreeNode)currentInstr.dynamicNode;
    435             return constTreeNode.Value;
    436           }
    437 
    438         //mkommend: this symbol uses the logistic function f(x) = 1 / (1 + e^(-alpha * x) )
    439         //to determine the relative amounts of the true and false branch see http://en.wikipedia.org/wiki/Logistic_function
    440         case OpCodes.VariableCondition: {
    441             if (row < 0 || row >= dataset.Rows) return double.NaN;
    442             var variableConditionTreeNode = (VariableConditionTreeNode)currentInstr.dynamicNode;
    443             double variableValue = ((IList<double>)currentInstr.iArg0)[row];
    444             double x = variableValue - variableConditionTreeNode.Threshold;
    445             double p = 1 / (1 + Math.Exp(-variableConditionTreeNode.Slope * x));
    446 
    447             double trueBranch = Evaluate(dataset, ref row, state);
    448             double falseBranch = Evaluate(dataset, ref row, state);
    449 
    450             return trueBranch * p + falseBranch * (1 - p);
    451           }
    452         default: throw new NotSupportedException();
    453       }
     451
     452      return code[code.Length - 1].value;
    454453    }
    455454  }
Note: See TracChangeset for help on using the changeset viewer.