Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2522_RefactorPluginInfrastructure/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Grammars/SimpleSymbolicExpressionGrammar.cs @ 18242

Last change on this file since 18242 was 15973, checked in by gkronber, 7 years ago

#2522: merged trunk changes from r13402:15972 to branch resolving conflicts where necessary

File size: 3.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Common;
24using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
25
26namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
27  public sealed class SimpleSymbolicExpressionGrammar : SymbolicExpressionGrammar {
28    [StorableConstructor]
29    private SimpleSymbolicExpressionGrammar(bool deserializing) : base(deserializing) { }
30    private SimpleSymbolicExpressionGrammar(SimpleSymbolicExpressionGrammar original, Cloner cloner) : base(original, cloner) { }
31    public override IDeepCloneable Clone(Cloner cloner) {
32      return new SimpleSymbolicExpressionGrammar(this, cloner);
33    }
34
35    public SimpleSymbolicExpressionGrammar()
36      : base("Simple Grammar", "A simple grammar containing symbols that differ only in their name.") {
37    }
38
39    public void AddSymbol(string symbolName, int minimumArity, int maximumArity) {
40      AddSymbol(symbolName, string.Empty, minimumArity, maximumArity);
41    }
42    public void AddSymbol(string symbolName, string description, int minimumArity, int maximumArity) {
43      var symbol = new SimpleSymbol(symbolName, description, minimumArity, maximumArity);
44      AddSymbol(symbol);
45      SetSubtreeCount(symbol, symbol.MinimumArity, symbol.MaximumArity);
46
47      foreach (var s in Symbols) {
48        if (s == ProgramRootSymbol) continue;
49        if (s.MaximumArity > 0) AddAllowedChildSymbol(s, symbol);
50
51        if (s == DefunSymbol) continue;
52        if (s == StartSymbol) continue;
53        if (symbol.MaximumArity > 0) AddAllowedChildSymbol(symbol, s);
54      }
55    }
56    public void AddSymbols(IEnumerable<string> symbolNames, int minimumArity, int maximiumArity) {
57      foreach (var symbolName in symbolNames) AddSymbol(symbolName, minimumArity, maximiumArity);
58    }
59
60    public void AddTerminalSymbol(string symbolName) {
61      AddTerminalSymbol(symbolName, string.Empty);
62    }
63    public void AddTerminalSymbol(string symbolName, string description) {
64      AddSymbol(symbolName, description, 0, 0);
65    }
66    public void AddTerminalSymbols(IEnumerable<string> symbolNames) {
67      foreach (var symbolName in symbolNames) AddTerminalSymbol(symbolName);
68    }
69
70  }
71}
Note: See TracBrowser for help on using the repository browser.