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 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Operators;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
|
---|
32 | using System.Collections.Generic;
|
---|
33 | using System.Text;
|
---|
34 | using System.Diagnostics;
|
---|
35 |
|
---|
36 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators {
|
---|
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 | }
|
---|