Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/GrammarModifier.cs @ 5686

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

#1418: Finally added results from the grammar refactoring.

File size: 3.6 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      var invokeSym = new InvokeFunction(functionName);
29      grammar.AddSymbol(invokeSym);
30      grammar.SetSubtreeCount(invokeSym, nArgs, nArgs);
31
32      //allow invoke symbol everywhere, where the child of the startCutPoint was allowed
33      foreach (ISymbol parent in grammar.Symbols) {
34        if (grammar.IsAllowedChildSymbol(parent, startCutPoint.Child.Symbol))
35          grammar.AddAllowedChildSymbol(parent, invokeSym);
36        else {
37          for (int i = 0; i < grammar.GetMaximumSubtreeCount(parent); i++) {
38            if (grammar.IsAllowedChildSymbol(parent, startCutPoint.Child.Symbol, i))
39              grammar.AddAllowedChildSymbol(parent, invokeSym, i);
40          }
41        }
42      }
43
44      if (nArgs > 0) {
45        //set allowed child symbols of invoke symbol
46        foreach (ISymbol child in grammar.Symbols) {
47          if (argumentCutPoints.All(x => grammar.IsAllowedChildSymbol(x.Parent.Symbol, child)))
48            grammar.AddAllowedChildSymbol(invokeSym, child);
49          else {
50            int i = 0;
51            foreach (CutPoint argumentCutPoint in argumentCutPoints) {
52              if (grammar.IsAllowedChildSymbol(argumentCutPoint.Parent.Symbol, child, argumentCutPoint.ChildIndex))
53                grammar.AddAllowedChildSymbol(invokeSym, child, i);
54              i++;
55            }
56          }
57        }
58      }
59    }
60
61    internal static void AddArgumentSymbol(ISymbolicExpressionTreeGrammar originalGrammar, ISymbolicExpressionTreeGrammar grammar, IEnumerable<int> argumentIndexes, IEnumerable<CutPoint> argumentCutPoints) {
62      foreach (var pair in argumentIndexes.Zip(argumentCutPoints, (a, b) => new { Index = a, CutPoint = b })) {
63        var argSymbol = new Argument(pair.Index);
64        grammar.AddSymbol(argSymbol);
65        grammar.SetSubtreeCount(argSymbol, 0, 0);
66
67        foreach (ISymbol parent in originalGrammar.Symbols) {
68          if (parent is StartSymbol || parent is ProgramRootSymbol) continue;
69          if (originalGrammar.IsAllowedChildSymbol(parent, pair.CutPoint.Child.Symbol))
70            grammar.AddAllowedChildSymbol(parent, argSymbol);
71          else {
72            for (int i = 0; i < originalGrammar.GetMaximumSubtreeCount(parent); i++) {
73              if (originalGrammar.IsAllowedChildSymbol(parent, pair.CutPoint.Child.Symbol, i))
74                grammar.AddAllowedChildSymbol(parent, argSymbol, i);
75            }
76          }
77        }
78      }
79    }
80  }
81}
Note: See TracBrowser for help on using the repository browser.