Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14192 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 3.8 KB
RevLine 
[3360]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 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) {
[6918]40          if (child.Name == invokeSym.Name) continue;
[5792]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++;
[5686]46          }
47        }
[3360]48      }
49    }
50
[5792]51
52
[5686]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
[5792]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);
[5686]63          else {
[5792]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);
[5686]67            }
[3360]68          }
[5686]69        }
[3360]70      }
71    }
[5792]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    }
[3360]87  }
88}
Note: See TracBrowser for help on using the repository browser.