Free cookie consent management tool by TermsFeed Policy Generator

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

#2021: Initial implementation of the SymbolicExpressionTreeLinearInterpreter.

Location:
branches/HeuristicLab.DataAnalysis.Symbolic.LinearInterpreter
Files:
1 added
2 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;
Note: See TracChangeset for help on using the changeset viewer.