Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureManipulators/GrammarModifier.cs @ 4047

Last change on this file since 4047 was 3539, checked in by gkronber, 15 years ago

Cosmetic name-space rename. #937 (Data types and operators for symbolic expression tree encoding)

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