[3338] | 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 |
|
---|
[4249] | 22 | using System;
|
---|
| 23 | using System.Linq;
|
---|
[3376] | 24 | using HeuristicLab.Common;
|
---|
[3338] | 25 | using HeuristicLab.Core;
|
---|
[4068] | 26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
|
---|
[3338] | 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
| 30 | [StorableClass]
|
---|
| 31 | [Item("GlobalSymbolicExpressionGrammar", "Represents a grammar that defines the syntax of symbolic expression trees.")]
|
---|
[4262] | 32 | public sealed class GlobalSymbolicExpressionGrammar : DefaultSymbolicExpressionGrammar {
|
---|
[3338] | 33 | [Storable]
|
---|
| 34 | private int minFunctionDefinitions;
|
---|
| 35 | public int MinFunctionDefinitions {
|
---|
| 36 | get { return minFunctionDefinitions; }
|
---|
[4068] | 37 | set {
|
---|
[3338] | 38 | minFunctionDefinitions = value;
|
---|
[3990] | 39 | UpdateAdfConstraints();
|
---|
[3338] | 40 | }
|
---|
| 41 | }
|
---|
| 42 | [Storable]
|
---|
| 43 | private int maxFunctionDefinitions;
|
---|
| 44 | public int MaxFunctionDefinitions {
|
---|
| 45 | get { return maxFunctionDefinitions; }
|
---|
[4068] | 46 | set {
|
---|
[3338] | 47 | maxFunctionDefinitions = value;
|
---|
[3990] | 48 | UpdateAdfConstraints();
|
---|
[3338] | 49 | }
|
---|
| 50 | }
|
---|
| 51 | [Storable]
|
---|
| 52 | private int minFunctionArguments;
|
---|
| 53 | public int MinFunctionArguments {
|
---|
| 54 | get { return minFunctionArguments; }
|
---|
[4068] | 55 | set {
|
---|
[3338] | 56 | minFunctionArguments = value;
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 | [Storable]
|
---|
| 60 | private int maxFunctionArguments;
|
---|
| 61 | public int MaxFunctionArguments {
|
---|
| 62 | get { return maxFunctionArguments; }
|
---|
[4068] | 63 | set {
|
---|
[3338] | 64 | maxFunctionArguments = value;
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
[3990] | 67 |
|
---|
[3338] | 68 | [Storable]
|
---|
[3990] | 69 | private Defun defunSymbol;
|
---|
[3338] | 70 |
|
---|
[4068] | 71 | public GlobalSymbolicExpressionGrammar(ISymbolicExpressionGrammar mainBranchGrammar)
|
---|
[4249] | 72 | : base(mainBranchGrammar) {
|
---|
[3338] | 73 | maxFunctionArguments = 3;
|
---|
| 74 | maxFunctionDefinitions = 3;
|
---|
| 75 |
|
---|
[4249] | 76 | ProgramRootSymbol programRootSymbol = Symbols.OfType<ProgramRootSymbol>().FirstOrDefault();
|
---|
| 77 | if (programRootSymbol == null) {
|
---|
| 78 | programRootSymbol = new ProgramRootSymbol();
|
---|
| 79 | AddSymbol(programRootSymbol);
|
---|
| 80 | }
|
---|
| 81 | StartSymbol = programRootSymbol;
|
---|
| 82 |
|
---|
| 83 | defunSymbol = Symbols.OfType<Defun>().FirstOrDefault();
|
---|
| 84 | if (defunSymbol == null) {
|
---|
| 85 | defunSymbol = new Defun();
|
---|
| 86 | AddSymbol(defunSymbol);
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | SetMinSubtreeCount(StartSymbol, minFunctionDefinitions + 1);
|
---|
| 90 | SetMaxSubtreeCount(StartSymbol, maxFunctionDefinitions + 1);
|
---|
| 91 | SetMinSubtreeCount(defunSymbol, 1);
|
---|
| 92 | SetMaxSubtreeCount(defunSymbol, 1);
|
---|
| 93 |
|
---|
| 94 | // the start symbol of the mainBranchGrammar is allowed as the result producing branch
|
---|
| 95 | SetAllowedChild(StartSymbol, Symbols.Where(s => s.Name == mainBranchGrammar.StartSymbol.Name).First(), 0);
|
---|
| 96 |
|
---|
| 97 | // every symbol of the mainBranchGrammar that is allowed as child of the start symbol is also allowed as direct child of defun
|
---|
| 98 | foreach (var symb in mainBranchGrammar.Symbols) {
|
---|
| 99 | if (mainBranchGrammar.IsAllowedChild(mainBranchGrammar.StartSymbol, symb, 0))
|
---|
| 100 | SetAllowedChild(defunSymbol, Symbols.Where(s => s.Name == symb.Name).First(), 0);
|
---|
| 101 | }
|
---|
[3993] | 102 | }
|
---|
| 103 |
|
---|
[4249] | 104 | //ctor for cloning
|
---|
[4262] | 105 | private GlobalSymbolicExpressionGrammar() : base() { }
|
---|
[3993] | 106 | [StorableConstructor]
|
---|
[4262] | 107 | private GlobalSymbolicExpressionGrammar(bool deserializing)
|
---|
[3993] | 108 | : base(deserializing) {
|
---|
| 109 | }
|
---|
| 110 |
|
---|
[4249] | 111 | [Obsolete]
|
---|
[3990] | 112 | private void Initialize(ISymbolicExpressionGrammar mainBranchGrammar) {
|
---|
[3541] | 113 | base.Clear();
|
---|
[3484] | 114 |
|
---|
[3338] | 115 | // remove the start symbol of the default grammar
|
---|
| 116 | RemoveSymbol(StartSymbol);
|
---|
| 117 |
|
---|
| 118 | StartSymbol = new ProgramRootSymbol();
|
---|
[3990] | 119 | defunSymbol = new Defun();
|
---|
[3338] | 120 | AddSymbol(StartSymbol);
|
---|
| 121 | AddSymbol(defunSymbol);
|
---|
| 122 |
|
---|
| 123 | SetMinSubtreeCount(StartSymbol, minFunctionDefinitions + 1);
|
---|
| 124 | SetMaxSubtreeCount(StartSymbol, maxFunctionDefinitions + 1);
|
---|
| 125 | SetMinSubtreeCount(defunSymbol, 1);
|
---|
| 126 | SetMaxSubtreeCount(defunSymbol, 1);
|
---|
| 127 |
|
---|
[3990] | 128 | // ADF branches maxFunctionDefinitions
|
---|
| 129 | for (int argumentIndex = 1; argumentIndex < maxFunctionDefinitions + 1; argumentIndex++) {
|
---|
| 130 | SetAllowedChild(StartSymbol, defunSymbol, argumentIndex);
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[3541] | 133 | if (mainBranchGrammar != null) {
|
---|
| 134 | // copy symbols from mainBranchGrammar
|
---|
| 135 | foreach (var symb in mainBranchGrammar.Symbols) {
|
---|
| 136 | AddSymbol(symb);
|
---|
| 137 | SetMinSubtreeCount(symb, mainBranchGrammar.GetMinSubtreeCount(symb));
|
---|
| 138 | SetMaxSubtreeCount(symb, mainBranchGrammar.GetMaxSubtreeCount(symb));
|
---|
| 139 | }
|
---|
[3338] | 140 |
|
---|
[3541] | 141 | // the start symbol of the mainBranchGrammar is allowed as the result producing branch
|
---|
| 142 | SetAllowedChild(StartSymbol, mainBranchGrammar.StartSymbol, 0);
|
---|
[3338] | 143 |
|
---|
[3541] | 144 | // copy syntax constraints from mainBranchGrammar
|
---|
| 145 | foreach (var parent in mainBranchGrammar.Symbols) {
|
---|
| 146 | for (int i = 0; i < mainBranchGrammar.GetMaxSubtreeCount(parent); i++) {
|
---|
| 147 | foreach (var child in mainBranchGrammar.Symbols) {
|
---|
| 148 | if (mainBranchGrammar.IsAllowedChild(parent, child, i)) {
|
---|
| 149 | SetAllowedChild(parent, child, i);
|
---|
| 150 | }
|
---|
[3338] | 151 | }
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 |
|
---|
[3541] | 155 | // every symbol of the mainBranchGrammar that is allowed as child of the start symbol is also allowed as direct child of defun
|
---|
| 156 | foreach (var symb in mainBranchGrammar.Symbols) {
|
---|
| 157 | if (mainBranchGrammar.IsAllowedChild(mainBranchGrammar.StartSymbol, symb, 0))
|
---|
| 158 | SetAllowedChild(defunSymbol, symb, 0);
|
---|
| 159 | }
|
---|
[3338] | 160 | }
|
---|
| 161 | }
|
---|
[3990] | 162 | private void UpdateAdfConstraints() {
|
---|
| 163 | SetMinSubtreeCount(StartSymbol, minFunctionDefinitions + 1);
|
---|
| 164 | SetMaxSubtreeCount(StartSymbol, maxFunctionDefinitions + 1);
|
---|
[4068] | 165 |
|
---|
[3990] | 166 | // ADF branches maxFunctionDefinitions
|
---|
| 167 | for (int argumentIndex = 1; argumentIndex < maxFunctionDefinitions + 1; argumentIndex++) {
|
---|
| 168 | SetAllowedChild(StartSymbol, defunSymbol, argumentIndex);
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 |
|
---|
[3338] | 172 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[4249] | 173 | GlobalSymbolicExpressionGrammar clone = (GlobalSymbolicExpressionGrammar)base.Clone(cloner);
|
---|
| 174 | clone.defunSymbol = (Defun)cloner.Clone(this.defunSymbol);
|
---|
| 175 | clone.maxFunctionArguments = this.maxFunctionArguments;
|
---|
| 176 | clone.minFunctionArguments = this.minFunctionArguments;
|
---|
| 177 | clone.maxFunctionDefinitions = this.maxFunctionDefinitions;
|
---|
| 178 | clone.minFunctionDefinitions = this.minFunctionDefinitions;
|
---|
[3338] | 179 | return clone;
|
---|
| 180 | }
|
---|
| 181 | }
|
---|
| 182 | }
|
---|