Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HivePerformance/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/GrammarModifier.cs @ 9368

Last change on this file since 9368 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

File size: 3.8 KB
Line 
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
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          if (child.Name == invokeSym.Name) continue;
41          int i = 0;
42          foreach (CutPoint argumentCutPoint in argumentCutPoints) {
43            if (grammar.IsAllowedChildSymbol(argumentCutPoint.Parent.Symbol, child, argumentCutPoint.ChildIndex))
44              grammar.AddAllowedChildSymbol(invokeSym, child, i);
45            i++;
46          }
47        }
48      }
49    }
50
51
52
53    internal static void AddArgumentSymbol(ISymbolicExpressionTreeGrammar originalGrammar, ISymbolicExpressionTreeGrammar grammar, IEnumerable<int> argumentIndexes, IEnumerable<CutPoint> argumentCutPoints) {
54      foreach (var pair in argumentIndexes.Zip(argumentCutPoints, (a, b) => new { Index = a, CutPoint = b })) {
55        var argSymbol = new Argument(pair.Index);
56        grammar.AddSymbol(argSymbol);
57        grammar.SetSubtreeCount(argSymbol, 0, 0);
58
59        foreach (var symb in grammar.Symbols) {
60          if (symb is ProgramRootSymbol || symb is StartSymbol) continue;
61          if (originalGrammar.IsAllowedChildSymbol(symb, pair.CutPoint.Child.Symbol))
62            grammar.AddAllowedChildSymbol(symb, argSymbol);
63          else {
64            for (int i = 0; i < grammar.GetMaximumSubtreeCount(symb); i++) {
65              if (originalGrammar.IsAllowedChildSymbol(symb, pair.CutPoint.Child.Symbol, i))
66                grammar.AddAllowedChildSymbol(symb, argSymbol, i);
67            }
68          }
69        }
70      }
71    }
72
73    internal static void SetAllowedParentSymbols(ISymbolicExpressionTreeGrammar grammar, ISymbol symbol, ISymbol newSymbol) {
74      foreach (var symb in grammar.Symbols) {
75        if (symb is ProgramRootSymbol) continue;
76        if (newSymbol is Argument && symb is StartSymbol) continue;
77        if (grammar.IsAllowedChildSymbol(symb, symbol))
78          grammar.AddAllowedChildSymbol(symb, newSymbol);
79        else {
80          for (int i = 0; i < grammar.GetMaximumSubtreeCount(symb); i++) {
81            if (grammar.IsAllowedChildSymbol(symb, symbol, i))
82              grammar.AddAllowedChildSymbol(symb, newSymbol, i);
83          }
84        }
85      }
86    }
87  }
88}
Note: See TracBrowser for help on using the repository browser.