Free cookie consent management tool by TermsFeed Policy Generator

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:
1 added
3 edited
1 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)
Note: See TracChangeset for help on using the changeset viewer.