1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
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;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 |
|
---|
26 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
27 | public static class SymbolicExpressionTreeCompiler {
|
---|
28 |
|
---|
29 | public static Instruction[] CompilePrefix(ISymbolicExpressionTree tree, Func<ISymbolicExpressionTreeNode, byte> opCodeMapper) {
|
---|
30 | return CompilePrefix(tree, opCodeMapper, Enumerable.Empty<Func<Instruction, Instruction>>());
|
---|
31 | }
|
---|
32 | public static Instruction[] CompilePrefix(ISymbolicExpressionTree tree, Func<ISymbolicExpressionTreeNode, byte> opCodeMapper, IEnumerable<Func<Instruction, Instruction>> postInstructionCompiledHooks) {
|
---|
33 | Dictionary<string, ushort> entryPoint = new Dictionary<string, ushort>();
|
---|
34 | List<Instruction> code = new List<Instruction>();
|
---|
35 | // compile main body branches
|
---|
36 | foreach (var branch in tree.Root.GetSubtree(0).Subtrees) {
|
---|
37 | code.AddRange(CompilePrefix(branch, opCodeMapper, postInstructionCompiledHooks));
|
---|
38 | }
|
---|
39 | // compile function branches
|
---|
40 | var functionBranches = from node in tree.IterateNodesPrefix()
|
---|
41 | where node.Symbol is Defun
|
---|
42 | select node;
|
---|
43 | foreach (DefunTreeNode branch in functionBranches) {
|
---|
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;
|
---|
46 | code.AddRange(CompilePrefix(branch.GetSubtree(0), opCodeMapper, postInstructionCompiledHooks));
|
---|
47 | }
|
---|
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];
|
---|
52 | if (instr.dynamicNode.Symbol is InvokeFunction) {
|
---|
53 | var invokeNode = (InvokeFunctionTreeNode)instr.dynamicNode;
|
---|
54 | instr.iArg0 = entryPoint[invokeNode.Symbol.FunctionName];
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | return code.ToArray();
|
---|
59 | }
|
---|
60 |
|
---|
61 | private static IEnumerable<Instruction> CompilePrefix(ISymbolicExpressionTreeNode branch, Func<ISymbolicExpressionTreeNode, byte> opCodeMapper, IEnumerable<Func<Instruction, Instruction>> postInstructionCompiledHooks) {
|
---|
62 | 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()) {
|
---|
165 | Instruction instr = new Instruction();
|
---|
166 | int subtreesCount = node.SubtreeCount;
|
---|
167 | if (subtreesCount > 255) throw new ArgumentException("Number of subtrees is too big (>255)");
|
---|
168 | instr.nArguments = (byte)subtreesCount;
|
---|
169 | instr.opCode = opCodeMapper(node);
|
---|
170 | if (node.Symbol is Argument) {
|
---|
171 | var argNode = (ArgumentTreeNode)node;
|
---|
172 | instr.iArg0 = (ushort)argNode.Symbol.ArgumentIndex;
|
---|
173 | }
|
---|
174 | instr.dynamicNode = node;
|
---|
175 | foreach (var hook in postInstructionCompiledHooks) {
|
---|
176 | instr = hook(instr);
|
---|
177 | }
|
---|
178 | yield return instr;
|
---|
179 | }
|
---|
180 | }
|
---|
181 | }
|
---|
182 | }
|
---|