Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/GrammarModifier.cs @ 15529

Last change on this file since 15529 was 5809, checked in by mkommend, 14 years ago

#1418: Reintegrated branch into trunk.

File size: 3.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Collections.Generic;
23using System.Linq;
24
25namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
26  public static class GrammarModifier {
27    internal static void AddInvokeSymbol(ISymbolicExpressionTreeGrammar grammar, string functionName, int nArgs, CutPoint startCutPoint, IEnumerable<CutPoint> argumentCutPoints) {
28      if (!grammar.ContainsSymbol(startCutPoint.Child.Symbol)) return;
29
30      var invokeSym = new InvokeFunction(functionName);
31      grammar.AddSymbol(invokeSym);
32      grammar.SetSubtreeCount(invokeSym, nArgs, nArgs);
33
34      //allow invoke symbol everywhere, where the child of the startCutPoint was allowed
35      SetAllowedParentSymbols(grammar, startCutPoint.Child.Symbol, invokeSym);
36
37      if (nArgs > 0) {
38        //set allowed child symbols of invoke symbol
39        foreach (ISymbol child in grammar.Symbols) {
40          int i = 0;
41          foreach (CutPoint argumentCutPoint in argumentCutPoints) {
42            if (grammar.IsAllowedChildSymbol(argumentCutPoint.Parent.Symbol, child, argumentCutPoint.ChildIndex))
43              grammar.AddAllowedChildSymbol(invokeSym, child, i);
44            i++;
45          }
46        }
47      }
48    }
49
50
51
52    internal static void AddArgumentSymbol(ISymbolicExpressionTreeGrammar originalGrammar, ISymbolicExpressionTreeGrammar grammar, IEnumerable<int> argumentIndexes, IEnumerable<CutPoint> argumentCutPoints) {
53      foreach (var pair in argumentIndexes.Zip(argumentCutPoints, (a, b) => new { Index = a, CutPoint = b })) {
54        var argSymbol = new Argument(pair.Index);
55        grammar.AddSymbol(argSymbol);
56        grammar.SetSubtreeCount(argSymbol, 0, 0);
57
58        foreach (var symb in grammar.Symbols) {
59          if (symb is ProgramRootSymbol || symb is StartSymbol) continue;
60          if (originalGrammar.IsAllowedChildSymbol(symb, pair.CutPoint.Child.Symbol))
61            grammar.AddAllowedChildSymbol(symb, argSymbol);
62          else {
63            for (int i = 0; i < grammar.GetMaximumSubtreeCount(symb); i++) {
64              if (originalGrammar.IsAllowedChildSymbol(symb, pair.CutPoint.Child.Symbol, i))
65                grammar.AddAllowedChildSymbol(symb, argSymbol, i);
66            }
67          }
68        }
69      }
70    }
71
72    internal static void SetAllowedParentSymbols(ISymbolicExpressionTreeGrammar grammar, ISymbol symbol, ISymbol newSymbol) {
73      foreach (var symb in grammar.Symbols) {
74        if (symb is ProgramRootSymbol) continue;
75        if (newSymbol is Argument && symb is StartSymbol) continue;
76        if (grammar.IsAllowedChildSymbol(symb, symbol))
77          grammar.AddAllowedChildSymbol(symb, newSymbol);
78        else {
79          for (int i = 0; i < grammar.GetMaximumSubtreeCount(symb); i++) {
80            if (grammar.IsAllowedChildSymbol(symb, symbol, i))
81              grammar.AddAllowedChildSymbol(symb, newSymbol, i);
82          }
83        }
84      }
85    }
86  }
87}
Note: See TracBrowser for help on using the repository browser.