[3294] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15583] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3294] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
[4068] | 23 | using System.Collections.Generic;
|
---|
[3294] | 24 | using System.Linq;
|
---|
| 25 |
|
---|
[5499] | 26 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
[8798] | 27 | public static class SymbolicExpressionTreeCompiler {
|
---|
[3294] | 28 |
|
---|
[8798] | 29 | public static Instruction[] Compile(ISymbolicExpressionTree tree, Func<ISymbolicExpressionTreeNode, byte> opCodeMapper) {
|
---|
| 30 | return Compile(tree, opCodeMapper, Enumerable.Empty<Func<Instruction, Instruction>>());
|
---|
| 31 | }
|
---|
| 32 | public static Instruction[] Compile(ISymbolicExpressionTree tree, Func<ISymbolicExpressionTreeNode, byte> opCodeMapper, IEnumerable<Func<Instruction, Instruction>> postInstructionCompiledHooks) {
|
---|
| 33 | Dictionary<string, ushort> entryPoint = new Dictionary<string, ushort>();
|
---|
[3294] | 34 | List<Instruction> code = new List<Instruction>();
|
---|
[4022] | 35 | // compile main body branches
|
---|
[5733] | 36 | foreach (var branch in tree.Root.GetSubtree(0).Subtrees) {
|
---|
[8798] | 37 | code.AddRange(Compile(branch, opCodeMapper, postInstructionCompiledHooks));
|
---|
[4068] | 38 | }
|
---|
[4022] | 39 | // compile function branches
|
---|
[3294] | 40 | var functionBranches = from node in tree.IterateNodesPrefix()
|
---|
| 41 | where node.Symbol is Defun
|
---|
| 42 | select node;
|
---|
| 43 | foreach (DefunTreeNode branch in functionBranches) {
|
---|
[3462] | 44 | if (code.Count > ushort.MaxValue) throw new ArgumentException("Code for the tree is too long (> ushort.MaxValue).");
|
---|
| 45 | entryPoint[branch.FunctionName] = (ushort)code.Count;
|
---|
[8798] | 46 | code.AddRange(Compile(branch.GetSubtree(0), opCodeMapper, postInstructionCompiledHooks));
|
---|
[3294] | 47 | }
|
---|
[3442] | 48 | // address of all functions is fixed now
|
---|
| 49 | // iterate through code again and fill in the jump locations
|
---|
| 50 | for (int i = 0; i < code.Count; i++) {
|
---|
| 51 | Instruction instr = code[i];
|
---|
[3462] | 52 | if (instr.dynamicNode.Symbol is InvokeFunction) {
|
---|
[3442] | 53 | var invokeNode = (InvokeFunctionTreeNode)instr.dynamicNode;
|
---|
[9828] | 54 | instr.data = entryPoint[invokeNode.Symbol.FunctionName];
|
---|
[3442] | 55 | }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[3294] | 58 | return code.ToArray();
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[8798] | 61 | private static IEnumerable<Instruction> Compile(ISymbolicExpressionTreeNode branch, Func<ISymbolicExpressionTreeNode, byte> opCodeMapper, IEnumerable<Func<Instruction, Instruction>> postInstructionCompiledHooks) {
|
---|
[3462] | 62 | foreach (var node in branch.IterateNodesPrefix()) {
|
---|
[3294] | 63 | Instruction instr = new Instruction();
|
---|
[6803] | 64 | int subtreesCount = node.SubtreeCount;
|
---|
[5733] | 65 | if (subtreesCount > 255) throw new ArgumentException("Number of subtrees is too big (>255)");
|
---|
| 66 | instr.nArguments = (byte)subtreesCount;
|
---|
[3462] | 67 | instr.opCode = opCodeMapper(node);
|
---|
[3747] | 68 | if (node.Symbol is Argument) {
|
---|
[3462] | 69 | var argNode = (ArgumentTreeNode)node;
|
---|
[9828] | 70 | instr.data = (ushort)argNode.Symbol.ArgumentIndex;
|
---|
[3294] | 71 | }
|
---|
[3462] | 72 | instr.dynamicNode = node;
|
---|
| 73 | foreach (var hook in postInstructionCompiledHooks) {
|
---|
| 74 | instr = hook(instr);
|
---|
| 75 | }
|
---|
[3294] | 76 | yield return instr;
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 | }
|
---|