Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureManipulators/GrammarModifier.cs @ 4745

Last change on this file since 4745 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

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