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