Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5499 was 5499, checked in by gkronber, 13 years ago

#1418 changes in symbolic expression tree encoding.

File size: 3.1 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;
23
24namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
25  public static class GrammarModifier {
26    public static void AddDynamicSymbol(ISymbolicExpressionTreeGrammar grammar, ISymbol classRepresentative, string symbolName, int nArgs) {
27      var invokeSym = new InvokeFunction(symbolName);
28      grammar.AddSymbol(invokeSym);
29      grammar.SetMinSubtreeCount(invokeSym, nArgs);
30      grammar.SetMaxSubtreeCount(invokeSym, nArgs);
31      SetSyntaxConstraints(grammar, classRepresentative, invokeSym);
32    }
33
34
35    public static void AddDynamicArguments(ISymbolicExpressionTreeGrammar grammar, ISymbol classRepresentative, IEnumerable<int> argumentIndexes) {
36      foreach (int argIndex in argumentIndexes) {
37        var argSymbol = new Argument(argIndex);
38        grammar.AddSymbol(argSymbol);
39        grammar.SetMinSubtreeCount(argSymbol, 0);
40        grammar.SetMaxSubtreeCount(argSymbol, 0);
41        SetSyntaxConstraints(grammar, classRepresentative, argSymbol);
42      }
43    }
44
45    private static void SetSyntaxConstraints(ISymbolicExpressionTreeGrammar grammar, ISymbol classRepresentative, Symbol newSymbol) {
46      // allow symbol as child of the representative first to make sure that the symbol
47      // is in the list of parents and children
48      for (int i = 0; i < grammar.GetMaxSubtreeCount(classRepresentative); i++) {
49        grammar.SetAllowedChild(classRepresentative, newSymbol, i);
50      }
51      // for all available symbols add the new symbol as allowed child iff the available symbol is an allowed child of the class representative
52      foreach (var parent in grammar.Symbols) {
53        if (grammar.IsAllowedChild(classRepresentative, parent, 0))
54          for (int arg = 0; arg < grammar.GetMaxSubtreeCount(parent); arg++) {
55            grammar.SetAllowedChild(parent, newSymbol, arg);
56          }
57      }
58      // for all available symbols add the new symbol as allowed parent iff the available symbol is an allowed child of the class representative
59      foreach (var child in grammar.Symbols) {
60        if (grammar.IsAllowedChild(classRepresentative, child, 0))
61          for (int arg = 0; arg < grammar.GetMaxSubtreeCount(newSymbol); arg++) {
62            grammar.SetAllowedChild(newSymbol, child, arg);
63          }
64      }
65    }
66
67  }
68}
Note: See TracBrowser for help on using the repository browser.