source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeCompiler.cs @ 3409

Last change on this file since 3409 was 3409, checked in by gkronber, 11 years ago

Worked on ADF aware evaluation for symbolic regression problems #938 (Data types and operators for regression problems)

File size: 3.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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
22using System;
23using System.Linq;
24using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using System.Collections.Generic;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.GeneralSymbols;
30
31namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
32  public class SymbolicExpressionTreeCompiler {
33    private Dictionary<Type, CodeSymbol> codeSymbol = new Dictionary<Type, CodeSymbol>() {
34      {typeof(Addition), CodeSymbol.Add},
35      {typeof(Subtraction), CodeSymbol.Sub},
36      {typeof(Multiplication), CodeSymbol.Mul},
37      {typeof(Division), CodeSymbol.Div},
38      {typeof(InvokeFunction), CodeSymbol.Call},
39      {typeof(Argument), CodeSymbol.Arg},
40      //{typeof(Values), CodeSymbol.Values}
41    };
42    private Dictionary<string, short> entryPoint = new Dictionary<string, short>();
43
44    public Instruction[] Compile(SymbolicExpressionTree tree) {
45      List<Instruction> code = new List<Instruction>();
46      entryPoint.Clear();
47      // compile main body
48      code.AddRange(Compile(tree.Root.SubTrees[0]));
49      // compile branches
50      var functionBranches = from node in tree.IterateNodesPrefix()
51                             where node.Symbol is Defun
52                             select node;
53      foreach (DefunTreeNode branch in functionBranches) {
54        entryPoint[branch.FunctionName] = (short)code.Count;
55        code.AddRange(Compile(branch));
56      }
57      return code.ToArray();
58    }
59
60    private IEnumerable<Instruction> Compile(SymbolicExpressionTreeNode branch) {
61      foreach (var node in IteratePrefix(branch)) {
62        Instruction instr = new Instruction();
63        if (node.SubTrees.Count > 255) throw new ArgumentException();
64        instr.nArguments = (byte)node.SubTrees.Count;
65        if (codeSymbol.ContainsKey(node.Symbol.GetType())) {
66          instr.symbol = codeSymbol[node.Symbol.GetType()];
67          if (instr.symbol == CodeSymbol.Call) {
68            var invokeNode = (InvokeFunctionTreeNode)node;
69            instr.iArg0 = entryPoint[invokeNode.Symbol.FunctionName];
70          } else if (instr.symbol == CodeSymbol.Arg) {
71            var argNode = (ArgumentTreeNode)node;
72            instr.iArg0 = (short)argNode.Symbol.ArgumentIndex;
73          }
74        } else {
75          instr.symbol = CodeSymbol.Dynamic;
76          instr.dynamicNode = node;
77        }
78        yield return instr;
79      }
80    }
81
82    private IEnumerable<SymbolicExpressionTreeNode> IteratePrefix(SymbolicExpressionTreeNode branch) {
83      yield return branch;
84      foreach (var subtree in branch.SubTrees) {
85        foreach (var node in IteratePrefix(subtree))
86          yield return node;
87      }
88    }
89  }
90}
Note: See TracBrowser for help on using the repository browser.