1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2019 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.Collections.Generic;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HEAL.Attic;
|
---|
25 |
|
---|
26 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
27 | [StorableType("70D190B2-22F4-41E5-9938-EFD1B14ECF43")]
|
---|
28 | public sealed class SimpleSymbolicExpressionGrammar : SymbolicExpressionGrammar {
|
---|
29 | [StorableConstructor]
|
---|
30 | private SimpleSymbolicExpressionGrammar(StorableConstructorFlag _) : base(_) { }
|
---|
31 | private SimpleSymbolicExpressionGrammar(SimpleSymbolicExpressionGrammar original, Cloner cloner) : base(original, cloner) { }
|
---|
32 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
33 | return new SimpleSymbolicExpressionGrammar(this, cloner);
|
---|
34 | }
|
---|
35 |
|
---|
36 | public SimpleSymbolicExpressionGrammar()
|
---|
37 | : base("Simple Grammar", "A simple grammar containing symbols that differ only in their name.") {
|
---|
38 | }
|
---|
39 |
|
---|
40 | public void AddSymbol(string symbolName, int minimumArity, int maximumArity) {
|
---|
41 | AddSymbol(symbolName, string.Empty, minimumArity, maximumArity);
|
---|
42 | }
|
---|
43 | public void AddSymbol(string symbolName, string description, int minimumArity, int maximumArity) {
|
---|
44 | var symbol = new SimpleSymbol(symbolName, description, minimumArity, maximumArity);
|
---|
45 | AddSymbol(symbol);
|
---|
46 | SetSubtreeCount(symbol, symbol.MinimumArity, symbol.MaximumArity);
|
---|
47 |
|
---|
48 | foreach (var s in Symbols) {
|
---|
49 | if (s == ProgramRootSymbol) continue;
|
---|
50 | if (s.MaximumArity > 0) AddAllowedChildSymbol(s, symbol);
|
---|
51 |
|
---|
52 | if (s == DefunSymbol) continue;
|
---|
53 | if (s == StartSymbol) continue;
|
---|
54 | if (symbol.MaximumArity > 0) AddAllowedChildSymbol(symbol, s);
|
---|
55 | }
|
---|
56 | }
|
---|
57 | public void AddSymbols(IEnumerable<string> symbolNames, int minimumArity, int maximiumArity) {
|
---|
58 | foreach (var symbolName in symbolNames) AddSymbol(symbolName, minimumArity, maximiumArity);
|
---|
59 | }
|
---|
60 |
|
---|
61 | public void AddTerminalSymbol(string symbolName) {
|
---|
62 | AddTerminalSymbol(symbolName, string.Empty);
|
---|
63 | }
|
---|
64 | public void AddTerminalSymbol(string symbolName, string description) {
|
---|
65 | AddSymbol(symbolName, description, 0, 0);
|
---|
66 | }
|
---|
67 | public void AddTerminalSymbols(IEnumerable<string> symbolNames) {
|
---|
68 | foreach (var symbolName in symbolNames) AddTerminalSymbol(symbolName);
|
---|
69 | }
|
---|
70 |
|
---|
71 | }
|
---|
72 | }
|
---|