Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3295 was 3294, checked in by gkronber, 14 years ago

Added first version of architecture altering operators for ADFs. #290 (Implement ADFs)

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