[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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Text;
|
---|
[3376] | 26 | using HeuristicLab.Common;
|
---|
[3338] | 27 | using HeuristicLab.Core;
|
---|
| 28 | using System.Xml;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[3462] | 30 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
|
---|
[3338] | 31 |
|
---|
| 32 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
| 33 | [StorableClass]
|
---|
| 34 | [Item("GlobalSymbolicExpressionGrammar", "Represents a grammar that defines the syntax of symbolic expression trees.")]
|
---|
| 35 | public class GlobalSymbolicExpressionGrammar : DefaultSymbolicExpressionGrammar {
|
---|
| 36 | [Storable]
|
---|
| 37 | private int minFunctionDefinitions;
|
---|
| 38 | public int MinFunctionDefinitions {
|
---|
| 39 | get { return minFunctionDefinitions; }
|
---|
| 40 | set {
|
---|
| 41 | minFunctionDefinitions = value;
|
---|
| 42 | Reset();
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 | [Storable]
|
---|
| 46 | private int maxFunctionDefinitions;
|
---|
| 47 | public int MaxFunctionDefinitions {
|
---|
| 48 | get { return maxFunctionDefinitions; }
|
---|
| 49 | set {
|
---|
| 50 | maxFunctionDefinitions = value;
|
---|
| 51 | Reset();
|
---|
| 52 | }
|
---|
| 53 | }
|
---|
| 54 | [Storable]
|
---|
| 55 | private int minFunctionArguments;
|
---|
| 56 | public int MinFunctionArguments {
|
---|
| 57 | get { return minFunctionArguments; }
|
---|
| 58 | set {
|
---|
| 59 | minFunctionArguments = value;
|
---|
| 60 | Reset();
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 | [Storable]
|
---|
| 64 | private int maxFunctionArguments;
|
---|
| 65 | public int MaxFunctionArguments {
|
---|
| 66 | get { return maxFunctionArguments; }
|
---|
| 67 | set {
|
---|
| 68 | maxFunctionArguments = value;
|
---|
| 69 | Reset();
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 | [Storable]
|
---|
| 73 | private ISymbolicExpressionGrammar mainBranchGrammar;
|
---|
| 74 |
|
---|
| 75 | public GlobalSymbolicExpressionGrammar() : base() { } // empty constructor for cloning
|
---|
| 76 |
|
---|
| 77 | public GlobalSymbolicExpressionGrammar(ISymbolicExpressionGrammar mainBranchGrammar )
|
---|
| 78 | : base() {
|
---|
| 79 | maxFunctionArguments = 3;
|
---|
| 80 | maxFunctionDefinitions = 3;
|
---|
| 81 | this.mainBranchGrammar = mainBranchGrammar;
|
---|
| 82 | Initialize();
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[3484] | 85 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 86 | private void AfterDeserializationHook() {
|
---|
| 87 | Reset();
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[3360] | 90 | private new void Reset() {
|
---|
[3338] | 91 | base.Reset();
|
---|
| 92 | Initialize();
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | private void Initialize() {
|
---|
| 96 | // remove the start symbol of the default grammar
|
---|
| 97 | RemoveSymbol(StartSymbol);
|
---|
| 98 |
|
---|
| 99 | StartSymbol = new ProgramRootSymbol();
|
---|
| 100 | var defunSymbol = new Defun();
|
---|
| 101 | AddSymbol(StartSymbol);
|
---|
| 102 | AddSymbol(defunSymbol);
|
---|
| 103 |
|
---|
| 104 | SetMinSubtreeCount(StartSymbol, minFunctionDefinitions + 1);
|
---|
| 105 | SetMaxSubtreeCount(StartSymbol, maxFunctionDefinitions + 1);
|
---|
| 106 | SetMinSubtreeCount(defunSymbol, 1);
|
---|
| 107 | SetMaxSubtreeCount(defunSymbol, 1);
|
---|
| 108 |
|
---|
| 109 | // copy symbols from mainBranchGrammar
|
---|
| 110 | foreach (var symb in mainBranchGrammar.Symbols) {
|
---|
| 111 | AddSymbol(symb);
|
---|
| 112 | SetMinSubtreeCount(symb, mainBranchGrammar.GetMinSubtreeCount(symb));
|
---|
| 113 | SetMaxSubtreeCount(symb, mainBranchGrammar.GetMaxSubtreeCount(symb));
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | // the start symbol of the mainBranchGrammar is allowed as the result producing branch
|
---|
| 117 | SetAllowedChild(StartSymbol, mainBranchGrammar.StartSymbol, 0);
|
---|
| 118 |
|
---|
| 119 | // ADF branches maxFunctionDefinitions
|
---|
| 120 | for (int argumentIndex = 1; argumentIndex < maxFunctionDefinitions + 1; argumentIndex++) {
|
---|
| 121 | SetAllowedChild(StartSymbol, defunSymbol, argumentIndex);
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | // copy syntax constraints from mainBranchGrammar
|
---|
| 125 | foreach (var parent in mainBranchGrammar.Symbols) {
|
---|
| 126 | for (int i = 0; i < mainBranchGrammar.GetMaxSubtreeCount(parent); i++) {
|
---|
| 127 | foreach (var child in mainBranchGrammar.Symbols) {
|
---|
| 128 | if (mainBranchGrammar.IsAllowedChild(parent, child, i)) {
|
---|
| 129 | SetAllowedChild(parent, child, i);
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | // every symbol of the mainBranchGrammar that is allowed as child of the start symbol is also allowed as direct child of defun
|
---|
| 136 | foreach (var symb in mainBranchGrammar.Symbols) {
|
---|
| 137 | if (mainBranchGrammar.IsAllowedChild(mainBranchGrammar.StartSymbol, symb, 0))
|
---|
| 138 | SetAllowedChild(defunSymbol, symb, 0);
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 142 | GlobalSymbolicExpressionGrammar clone = (GlobalSymbolicExpressionGrammar)base.Clone(cloner);
|
---|
| 143 | clone.maxFunctionArguments = maxFunctionArguments;
|
---|
| 144 | clone.maxFunctionDefinitions = maxFunctionDefinitions;
|
---|
| 145 | clone.minFunctionArguments = minFunctionArguments;
|
---|
| 146 | clone.minFunctionDefinitions = minFunctionDefinitions;
|
---|
| 147 | clone.mainBranchGrammar = mainBranchGrammar;
|
---|
| 148 | return clone;
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
| 151 | }
|
---|