[5686] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2011 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 HeuristicLab.Common;
|
---|
| 23 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 24 |
|
---|
| 25 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
| 26 | [StorableClass]
|
---|
| 27 | public abstract class SymbolicExpressionGrammar : SymbolicExpressionGrammarBase, ISymbolicExpressionGrammar {
|
---|
| 28 | #region fields & properties
|
---|
| 29 | [Storable]
|
---|
| 30 | private int minimumFunctionDefinitions;
|
---|
| 31 | public int MinimumFunctionDefinitions {
|
---|
| 32 | get { return minimumFunctionDefinitions; }
|
---|
| 33 | set {
|
---|
| 34 | minimumFunctionDefinitions = value;
|
---|
| 35 | UpdateAdfConstraints();
|
---|
| 36 | }
|
---|
| 37 | }
|
---|
| 38 | [Storable]
|
---|
| 39 | private int maximumFunctionDefinitions;
|
---|
| 40 | public int MaximumFunctionDefinitions {
|
---|
| 41 | get { return maximumFunctionDefinitions; }
|
---|
| 42 | set {
|
---|
| 43 | maximumFunctionDefinitions = value;
|
---|
| 44 | UpdateAdfConstraints();
|
---|
| 45 | }
|
---|
| 46 | }
|
---|
| 47 | [Storable]
|
---|
| 48 | private int minimumFunctionArguments;
|
---|
| 49 | public int MinimumFunctionArguments {
|
---|
| 50 | get { return minimumFunctionArguments; }
|
---|
| 51 | set {
|
---|
| 52 | minimumFunctionArguments = value;
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 | [Storable]
|
---|
| 56 | private int maximumFunctionArguments;
|
---|
| 57 | public int MaximumFunctionArguments {
|
---|
| 58 | get { return maximumFunctionArguments; }
|
---|
| 59 | set {
|
---|
| 60 | maximumFunctionArguments = value;
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | [Storable]
|
---|
| 65 | private ProgramRootSymbol programRootSymbol;
|
---|
| 66 | public ProgramRootSymbol ProgramRootSymbol {
|
---|
| 67 | get { return programRootSymbol; }
|
---|
| 68 | }
|
---|
| 69 | ISymbol ISymbolicExpressionGrammar.ProgramRootSymbol {
|
---|
| 70 | get { return ProgramRootSymbol; }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | [Storable]
|
---|
| 74 | private StartSymbol startSymbol;
|
---|
| 75 | public StartSymbol StartSymbol {
|
---|
| 76 | get { return startSymbol; }
|
---|
| 77 | }
|
---|
| 78 | ISymbol ISymbolicExpressionGrammar.StartSymbol {
|
---|
| 79 | get { return StartSymbol; }
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | [Storable]
|
---|
| 83 | private Defun defunSymbol;
|
---|
| 84 | protected Defun DefunSymbol {
|
---|
| 85 | get { return defunSymbol; }
|
---|
| 86 | }
|
---|
| 87 | #endregion
|
---|
| 88 |
|
---|
| 89 | [StorableConstructor]
|
---|
| 90 | protected SymbolicExpressionGrammar(bool deserializing) : base(deserializing) { }
|
---|
| 91 | protected SymbolicExpressionGrammar(SymbolicExpressionGrammar original, Cloner cloner)
|
---|
| 92 | : base(original, cloner) {
|
---|
| 93 | programRootSymbol = (ProgramRootSymbol)cloner.Clone(original.programRootSymbol);
|
---|
| 94 | startSymbol = (StartSymbol)cloner.Clone(original.StartSymbol);
|
---|
| 95 | defunSymbol = (Defun)cloner.Clone(original.defunSymbol);
|
---|
| 96 | maximumFunctionArguments = original.maximumFunctionArguments;
|
---|
| 97 | minimumFunctionArguments = original.minimumFunctionArguments;
|
---|
| 98 | maximumFunctionDefinitions = original.maximumFunctionDefinitions;
|
---|
| 99 | minimumFunctionDefinitions = original.minimumFunctionDefinitions;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[5688] | 102 | public SymbolicExpressionGrammar(string name, string description)
|
---|
| 103 | : base(name, description) {
|
---|
[5686] | 104 | programRootSymbol = new ProgramRootSymbol();
|
---|
| 105 | AddSymbol(programRootSymbol);
|
---|
| 106 | startSymbol = new StartSymbol();
|
---|
| 107 | AddSymbol(startSymbol);
|
---|
| 108 | defunSymbol = new Defun();
|
---|
| 109 | AddSymbol(defunSymbol);
|
---|
| 110 |
|
---|
| 111 | SetSubtreeCount(programRootSymbol, 1, 1);
|
---|
| 112 | AddAllowedChildSymbol(programRootSymbol, startSymbol, 0);
|
---|
| 113 | UpdateAdfConstraints();
|
---|
| 114 |
|
---|
| 115 | SetSubtreeCount(defunSymbol, 1, 1);
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | private void UpdateAdfConstraints() {
|
---|
| 119 | SetSubtreeCount(programRootSymbol, minimumFunctionDefinitions + 1, maximumFunctionDefinitions + 1);
|
---|
| 120 |
|
---|
| 121 | // ADF branches maxFunctionDefinitions
|
---|
| 122 | for (int argumentIndex = 1; argumentIndex < maximumFunctionDefinitions + 1; argumentIndex++) {
|
---|
| 123 | AddAllowedChildSymbol(programRootSymbol, defunSymbol, argumentIndex);
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 | }
|
---|