Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/GrammarModifier.cs @ 6406

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

#1418: Reintegrated branch into trunk.

File size: 3.7 KB
RevLine 
[3360]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3360]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
[4068]22using System.Collections.Generic;
[5686]23using System.Linq;
[3360]24
[5499]25namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
[3360]26  public static class GrammarModifier {
[5686]27    internal static void AddInvokeSymbol(ISymbolicExpressionTreeGrammar grammar, string functionName, int nArgs, CutPoint startCutPoint, IEnumerable<CutPoint> argumentCutPoints) {
[5792]28      if (!grammar.ContainsSymbol(startCutPoint.Child.Symbol)) return;
29
[5686]30      var invokeSym = new InvokeFunction(functionName);
[3360]31      grammar.AddSymbol(invokeSym);
[5686]32      grammar.SetSubtreeCount(invokeSym, nArgs, nArgs);
[3360]33
[5686]34      //allow invoke symbol everywhere, where the child of the startCutPoint was allowed
[5792]35      SetAllowedParentSymbols(grammar, startCutPoint.Child.Symbol, invokeSym);
[3360]36
[5686]37      if (nArgs > 0) {
38        //set allowed child symbols of invoke symbol
39        foreach (ISymbol child in grammar.Symbols) {
[5792]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++;
[5686]45          }
46        }
[3360]47      }
48    }
49
[5792]50
51
[5686]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
[5792]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);
[5686]62          else {
[5792]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);
[5686]66            }
[3360]67          }
[5686]68        }
[3360]69      }
70    }
[5792]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    }
[3360]86  }
87}
Note: See TracBrowser for help on using the repository browser.