Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/GrammarModifier.cs @ 3360

Last change on this file since 3360 was 3360, checked in by gkronber, 14 years ago

Fixed bugs in ADF operators and added test classes for ADF operators. #290 (Implement ADFs)

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